Revert
[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.7 2006-05-10 12:52:28 heikki 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                         "       4=Report + summary + extra prints from tests\n"
84                     );
85                 exit(0);
86             }
87             else
88             {
89                 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
90                         argv[i]);
91                 fprintf(stderr, "Use --test-help for more info\n");
92                 exit(1);
93             }
94             
95         }
96         break;
97     }
98     /* remove --test- options from argc, argv so that they disappear */
99     (*argv_p)[i-1] = **argv_p;  /* program name */
100     --i;
101     *argc_p -= i;
102     *argv_p += i;
103 }
104
105 void yaz_check_term1(void)
106 {
107     /* summary */
108     if (test_failed)
109     {
110         if (test_verbose >= 1)
111             fprintf(get_file(), "%d out of %d tests failed for program %s\n",
112                     test_failed, test_total, test_prog);
113     }
114     else
115     {
116         if (test_verbose >= 2)
117             fprintf(get_file(), "%d tests passed for program %s\n",
118                     test_total, test_prog);
119     }
120     if (test_fout)
121         fclose(test_fout);
122     if (test_failed)
123         exit(1);
124     exit(0);
125 }
126
127 void yaz_check_eq1(int type, const char *file, int line,
128                    const char *left, const char *right, int lval, int rval)
129 {
130     char formstr[2048];
131     
132     if (type==YAZ_TEST_TYPE_OK) 
133         sprintf(formstr, "%.500s == %.500s ", left, right);
134     else
135         sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
136     yaz_check_print1(type, file, line, formstr);
137 }
138
139 void yaz_check_print1(int type, const char *file, int line, 
140                       const char *expr)
141 {
142     const char *msg = "unknown";
143
144     test_total++;
145     switch(type)
146     {
147     case YAZ_TEST_TYPE_FAIL:
148         test_failed++;
149         msg = "FAILED";
150         if (test_verbose < 1)
151             return;
152         break;
153     case YAZ_TEST_TYPE_OK:
154         msg = "ok";
155         if (test_verbose < 3)
156             return;
157         break;
158     }
159     fprintf(get_file(), "%s:%d %s: ", file, line, msg);
160     fprintf(get_file(), "%s\n", expr);
161 }
162
163
164 int yaz_test_get_verbosity(){
165     return test_verbose;
166 }
167
168 /*
169  * Local variables:
170  * c-basic-offset: 4
171  * indent-tabs-mode: nil
172  * End:
173  * vim: shiftwidth=4 tabstop=8 expandtab
174  */
175