Add truncation tests
[yaz-moved-to-github.git] / test / test_file_glob.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <errno.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14
15 #include <yaz/file_glob.h>
16 #include <yaz/test.h>
17 #include <yaz/log.h>
18 #include <yaz/wrbuf.h>
19
20 void tst_with_path(const char *tpath)
21 {
22     yaz_glob_res_t glob_res;
23     int ret = yaz_file_glob(tpath, &glob_res);
24     if (ret == 0)
25     {
26         size_t n = yaz_file_glob_get_num(glob_res);
27         size_t i;
28         for (i = 0; i < n; i++)
29         {
30             yaz_log(YLOG_LOG, "match %s", yaz_file_glob_get_file(glob_res, i));
31         }
32     }
33     yaz_file_globfree(&glob_res);
34 }
35
36 static int check_file(const char *got, const char *expect)
37 {
38     const char *f = got;
39     size_t l_match = strlen(expect);
40     YAZ_CHECK(f && strlen(f) >= l_match);
41     if (f && strlen(f) >= l_match && !strcmp(f + strlen(f) - l_match, expect))
42         return 1;
43     return 0;
44 }
45
46 void tst(void)
47 {
48     yaz_glob_res_t glob_res;
49     int ret;
50     WRBUF tpath = wrbuf_alloc();
51     const char *srcdir = getenv("srcdir");
52     
53     if (srcdir)
54     {
55         wrbuf_puts(tpath, srcdir);
56         wrbuf_puts(tpath, "/");
57     }
58     wrbuf_puts(tpath, "test_file*.c");
59     ret = yaz_file_glob(wrbuf_cstr(tpath), &glob_res);
60     YAZ_CHECK_EQ(ret, 0);
61
62     YAZ_CHECK_EQ(2, yaz_file_glob_get_num(glob_res));
63     if (yaz_file_glob_get_num(glob_res) == 2)
64     {
65         YAZ_CHECK(check_file(yaz_file_glob_get_file(glob_res, 0),
66                              "test_file_glob.c"));
67         YAZ_CHECK(check_file(yaz_file_glob_get_file(glob_res, 1),
68                              "test_filepath.c"));
69     }
70     wrbuf_destroy(tpath);
71     yaz_file_globfree(&glob_res);
72 }
73
74 int main (int argc, char **argv)
75 {
76     YAZ_CHECK_INIT(argc, argv);
77     if (argc >= 2)
78         tst_with_path(argv[1]);
79     else
80         tst();
81     YAZ_CHECK_TERM;
82 }
83 /*
84  * Local variables:
85  * c-basic-offset: 4
86  * c-file-style: "Stroustrup"
87  * indent-tabs-mode: nil
88  * End:
89  * vim: shiftwidth=4 tabstop=8 expandtab
90  */
91