1fc47c2d84ec28fee8bf98ef0ab6bc6c357581f0
[yaz-moved-to-github.git] / src / test.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: test.c,v 1.5 2006-04-20 20:50:51 adam Exp $
6  */
7
8 /** \file test.c
9     \brief Unit Test for YAZ
10 */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 #include <yaz/test.h>
21
22 static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
23 static int test_total = 0;
24 static int test_failed = 0;
25 static int test_verbose = 1;
26 static char *test_prog = 0;
27
28 static FILE *get_file()
29 {
30     if (test_fout)
31         return test_fout;
32     return stdout;
33 }
34
35 static char *progname(char *argv0)
36 {
37     char *cp = strrchr(argv0, '/');
38     if (cp)
39         return cp+1;
40     cp = strrchr(argv0, '\\');
41     if (cp)
42         return cp+1;
43     return argv0;
44 }
45
46 void yaz_check_init1(int *argc_p, char ***argv_p)
47 {
48     int i = 0;
49     int argc = *argc_p;
50     char **argv = *argv_p;
51
52     test_prog = progname(argv[0]);
53
54     for (i = 1; i<argc; i++)
55     {
56         if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
57         {
58             const char *suf = argv[i]+7;
59             if (i < argc-1 && !strcmp(suf, "file"))
60             {
61                 i++;
62                 if (test_fout)
63                     fclose(test_fout);
64                 test_fout = fopen(argv[i], "w");
65                 continue;
66             }
67             else if (i < argc-1 && !strcmp(suf, "verbose"))
68             {
69                 i++;
70                 test_verbose = atoi(argv[i]);
71                 continue;
72             }
73             else if (!strcmp(suf, "help"))
74             {
75                 fprintf(stderr, 
76                         "--test-help           help\n"
77                         "--test-file fname     output to fname\n"
78                         "--test-verbose level  verbose level\n"
79                         "       0=Quiet. Only exit code tells what's wrong\n"
80                         "       1=Report+Summary only if tests fail.\n"
81                         "       2=Report failures. Print summary always\n"
82                         "       3=Report + summary always\n"
83                     );
84                 exit(0);
85             }
86             else
87             {
88                 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
89                         argv[i]);
90                 fprintf(stderr, "Use --test-help for more info\n");
91                 exit(1);
92             }
93             
94         }
95         break;
96     }
97     /* remove --test- options from argc, argv so that they disappear */
98     (*argv_p)[i-1] = **argv_p;  /* program name */
99     --i;
100     *argc_p -= i;
101     *argv_p += i;
102 }
103
104 void yaz_check_term1(void)
105 {
106     /* summary */
107     if (test_failed)
108     {
109         if (test_verbose >= 1)
110             fprintf(get_file(), "%d out of %d tests failed for program %s\n",
111                     test_failed, test_total, test_prog);
112     }
113     else
114     {
115         if (test_verbose >= 2)
116             fprintf(get_file(), "%d tests passed for program %s\n",
117                     test_total, test_prog);
118     }
119     if (test_fout)
120         fclose(test_fout);
121     if (test_failed)
122         exit(1);
123     exit(0);
124 }
125
126 void yaz_check_eq1(int type, const char *file, int line,
127                    const char *left, const char *right, int lval, int rval)
128 {
129     char formstr[2048];
130     
131     sprintf(formstr, "%.500s != %.500s\n%d != %d", left, right, lval, rval);
132     yaz_check_print1(type, file, line, formstr);
133 }
134
135 void yaz_check_print1(int type, const char *file, int line, 
136                       const char *expr)
137 {
138     const char *msg = "unknown";
139
140     test_total++;
141     switch(type)
142     {
143     case YAZ_TEST_TYPE_FAIL:
144         test_failed++;
145         msg = "failed";
146         if (test_verbose < 1)
147             return;
148         break;
149     case YAZ_TEST_TYPE_OK:
150         msg = "OK";
151         if (test_verbose < 3)
152             return;
153         break;
154     }
155     fprintf(get_file(), "%s:%d %s: ", file, line, msg);
156     fprintf(get_file(), "%s\n", expr);
157 }
158
159
160 /*
161  * Local variables:
162  * c-basic-offset: 4
163  * indent-tabs-mode: nil
164  * End:
165  * vim: shiftwidth=4 tabstop=8 expandtab
166  */
167