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