Added YAZ_CHECK_TERM which terminates test program. Using yaz/test.h
[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.3 2006-01-29 21:59:13 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_print1(int type, const char *file, int line, const char *expr)
122 {
123     const char *msg = "unknown";
124
125     test_total++;
126     switch(type)
127     {
128     case YAZ_TEST_TYPE_FAIL:
129         test_failed++;
130         msg = "failed";
131         if (test_verbose < 1)
132             return;
133         break;
134     case YAZ_TEST_TYPE_OK:
135         msg = "OK";
136         if (test_verbose < 3)
137             return;
138         break;
139     }
140     fprintf(get_file(), "%s:%d %s: %s\n", file, line, msg, expr);
141 }
142
143
144 /*
145  * Local variables:
146  * c-basic-offset: 4
147  * indent-tabs-mode: nil
148  * End:
149  * vim: shiftwidth=4 tabstop=8 expandtab
150  */
151