Fix compilation on Windows. Reformat a bit
[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.9 2006-07-07 06:59:49 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 #if HAVE_UNISTSD_H
20 #include <unistd.h>
21 #endif
22
23 #include <yaz/test.h>
24 #include <yaz/log.h>
25
26 static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
27 static int test_total = 0;
28 static int test_failed = 0;
29 static int test_verbose = 1;
30 static const char *test_prog = 0;
31 static int log_tests = 0; 
32
33 static FILE *get_file()
34 {
35     if (test_fout)
36         return test_fout;
37     return stdout;
38 }
39
40 static const char *progname(const char *argv0)
41 {
42     const char *cp = strrchr(argv0, '/');
43     if (cp)
44         return cp+1;
45     cp = strrchr(argv0, '\\');
46     if (cp)
47         return cp+1;
48     return argv0;
49 }
50
51 void yaz_check_init1(int *argc_p, char ***argv_p)
52 {
53     int i = 0;
54     int argc = *argc_p;
55     char **argv = *argv_p;
56
57     test_prog = progname(argv[0]);
58
59     for (i = 1; i<argc; i++)
60     {
61         if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
62         {
63             const char *suf = argv[i]+7;
64             if (i < argc-1 && !strcmp(suf, "file"))
65             {
66                 i++;
67                 if (test_fout)
68                     fclose(test_fout);
69                 test_fout = fopen(argv[i], "w");
70                 continue;
71             }
72             else if (i < argc-1 && !strcmp(suf, "verbose"))
73             {
74                 i++;
75                 test_verbose = atoi(argv[i]);
76                 continue;
77             }
78             else if (!strcmp(suf, "help"))
79             {
80                 fprintf(stderr, 
81                         "--test-help           help\n"
82                         "--test-file fname     output to fname\n"
83                         "--test-verbose level  verbose level\n"
84                         "       0=Quiet. Only exit code tells what's wrong\n"
85                         "       1=Report+Summary only if tests fail.\n"
86                         "       2=Report failures. Print summary always\n"
87                         "       3=Report + summary always\n"
88                         "       4=Report + summary + extra prints from tests\n"
89                     );
90                 exit(0);
91             }
92             else
93             {
94                 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
95                         argv[i]);
96                 fprintf(stderr, "Use --test-help for more info\n");
97                 exit(1);
98             }
99             
100         }
101         break;
102     }
103     /* remove --test- options from argc, argv so that they disappear */
104     (*argv_p)[i-1] = **argv_p;  /* program name */
105     --i;
106     *argc_p -= i;
107     *argv_p += i;
108 }
109
110 /** \brief  Initialize the log system */
111 void yaz_check_init_log(const char *argv0)
112 {
113     char logfilename[2048];
114     log_tests = 1; 
115     sprintf(logfilename,"%s.log", progname(argv0) );
116     unlink(logfilename);
117     yaz_log_init_file(logfilename);
118     yaz_log_trunc();
119
120 }
121
122 void yaz_check_term1(void)
123 {
124     /* summary */
125     if (test_failed)
126     {
127         if (test_verbose >= 1)
128             fprintf(get_file(), "%d out of %d tests failed for program %s\n",
129                     test_failed, test_total, test_prog);
130     }
131     else
132     {
133         if (test_verbose >= 2)
134             fprintf(get_file(), "%d tests passed for program %s\n",
135                     test_total, test_prog);
136     }
137     if (test_fout)
138         fclose(test_fout);
139     if (test_failed)
140         exit(1);
141     exit(0);
142 }
143
144 void yaz_check_eq1(int type, const char *file, int line,
145                    const char *left, const char *right, int lval, int rval)
146 {
147     char formstr[2048];
148     
149     if (type == YAZ_TEST_TYPE_OK) 
150         sprintf(formstr, "%.500s == %.500s ", left, right);
151     else
152         sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
153     yaz_check_print1(type, file, line, formstr);
154 }
155
156 void yaz_check_print1(int type, const char *file, int line, 
157                       const char *expr)
158 {
159     const char *msg = "unknown";
160     int printit = 1;
161
162     test_total++;
163     switch(type)
164     {
165     case YAZ_TEST_TYPE_FAIL:
166         test_failed++;
167         msg = "FAILED";
168         if (test_verbose < 1)
169             printit = 0;
170         break;
171     case YAZ_TEST_TYPE_OK:
172         msg = "ok";
173         if (test_verbose < 3)
174             printit = 0;
175         break;
176     }
177     if (printit)
178     {
179         fprintf(get_file(), "%s:%d %s: ", file, line, msg);
180         fprintf(get_file(), "%s\n", expr);
181     }
182     if (log_tests)
183     {
184         yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
185         yaz_log(YLOG_LOG, "%s\n", expr);
186     }
187 }
188
189
190 int yaz_test_get_verbosity()
191 {
192     return test_verbose;
193 }
194
195 /*
196  * Local variables:
197  * c-basic-offset: 4
198  * indent-tabs-mode: nil
199  * End:
200  * vim: shiftwidth=4 tabstop=8 expandtab
201  */
202