A little more flexible test unit
[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.1 2006-01-27 18:58:58 adam Exp $
6  */
7 #if HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include <yaz/test.h>
15
16 static FILE *test_fout;
17 static int test_number = 0;
18 static int test_verbose = 0;
19
20 void yaz_check_init1(int *argc_p, char ***argv_p)
21 {
22     int i = 0;
23     int argc = *argc_p;
24     char **argv = *argv_p;
25
26     test_fout = 0;
27     for (i = 1; i<argc; i++)
28     {
29         if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
30         {
31             const char *suf = argv[i]+7;
32             if (i < argc-1 && !strcmp(suf, "file"))
33             {
34                 i++;
35                 if (test_fout)
36                     fclose(test_fout);
37                 test_fout = fopen(argv[i], "w");
38                 continue;
39             }
40             else if (i < argc-1 && !strcmp(suf, "verbose"))
41             {
42                 i++;
43                 test_verbose = atoi(argv[i]);
44                 continue;
45             }
46             else if (!strcmp(suf, "help"))
47             {
48                 fprintf(stderr, 
49                         "--test-help           help\n"
50                         "--test-verbose level  verbose; 0=quiet; 1=normal; 2=more\n"
51                         "--test-file fname     output to fname\n");
52                 exit(0);
53             }
54             else
55             {
56                 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
57                         argv[i]);
58                 fprintf(stderr, "Use --test-help for more info\n");
59                 exit(1);
60             }
61             
62         }
63         break;
64     }
65     /* remove --test- options from argc, argv so that they disappear */
66     (*argv_p)[i-1] = **argv_p;  /* program name */
67     --i;
68     *argc_p -= i;
69     *argv_p += i;
70     if (!test_fout)
71         test_fout = stdout;
72 }
73
74 void yaz_check_print1(int type, const char *file, int line, const char *expr)
75 {
76     const char *msg = "unknown";
77
78     test_number++;
79     switch(type)
80     {
81     case YAZ_TEST_TYPE_FAIL:
82         msg = "failed";
83         if (test_verbose < 1)
84             return;
85         break;
86     case YAZ_TEST_TYPE_OK:
87         msg = "OK";
88         if (test_verbose < 2)
89             return;
90         break;
91     }
92     fprintf(test_fout, "%s:%d %s: %s\n", file, line, msg, expr);
93 }
94
95
96 /*
97  * Local variables:
98  * c-basic-offset: 4
99  * indent-tabs-mode: nil
100  * End:
101  * vim: shiftwidth=4 tabstop=8 expandtab
102  */
103