Annotate
[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.2 2006-01-27 19:01:56 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 = 0; /* can't init this to stderr on some systems */
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     for (i = 1; i<argc; i++)
27     {
28         if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
29         {
30             const char *suf = argv[i]+7;
31             if (i < argc-1 && !strcmp(suf, "file"))
32             {
33                 i++;
34                 if (test_fout)
35                     fclose(test_fout);
36                 test_fout = fopen(argv[i], "w");
37                 continue;
38             }
39             else if (i < argc-1 && !strcmp(suf, "verbose"))
40             {
41                 i++;
42                 test_verbose = atoi(argv[i]);
43                 continue;
44             }
45             else if (!strcmp(suf, "help"))
46             {
47                 fprintf(stderr, 
48                         "--test-help           help\n"
49                         "--test-verbose level  verbose; 0=quiet; 1=normal; 2=more\n"
50                         "--test-file fname     output to fname\n");
51                 exit(0);
52             }
53             else
54             {
55                 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
56                         argv[i]);
57                 fprintf(stderr, "Use --test-help for more info\n");
58                 exit(1);
59             }
60             
61         }
62         break;
63     }
64     /* remove --test- options from argc, argv so that they disappear */
65     (*argv_p)[i-1] = **argv_p;  /* program name */
66     --i;
67     *argc_p -= i;
68     *argv_p += i;
69     if (!test_fout)   
70         test_fout = stdout;  /* by default, set output to this */
71 }
72
73 void yaz_check_print1(int type, const char *file, int line, const char *expr)
74 {
75     const char *msg = "unknown";
76
77     test_number++;
78     switch(type)
79     {
80     case YAZ_TEST_TYPE_FAIL:
81         msg = "failed";
82         if (test_verbose < 1)
83             return;
84         break;
85     case YAZ_TEST_TYPE_OK:
86         msg = "OK";
87         if (test_verbose < 2)
88             return;
89         break;
90     }
91     fprintf(test_fout, "%s:%d %s: %s\n", file, line, msg, expr);
92 }
93
94
95 /*
96  * Local variables:
97  * c-basic-offset: 4
98  * indent-tabs-mode: nil
99  * End:
100  * vim: shiftwidth=4 tabstop=8 expandtab
101  */
102