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