YAZ 5.37.0
test.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
5
9
10#if HAVE_CONFIG_H
11#include <config.h>
12#endif
13
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17#if HAVE_UNISTSD_H
18#include <unistd.h>
19#endif
20
21#include <yaz/test.h>
22#include <yaz/log.h>
23#include <yaz/snprintf.h>
24
25static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
26static int test_total = 0;
27static int test_failed = 0;
28static int test_todo = 0;
29static int test_verbose = 1;
30static const char *test_prog = 0;
31static int log_tests = 0;
32static int test_stop = 0;
33
34static FILE *get_file(void)
35{
36 if (test_fout)
37 return test_fout;
38 return stdout;
39}
40
41static const char *progname(const char *argv0)
42{
43 const char *cp = strrchr(argv0, '/');
44 if (cp)
45 return cp+1;
46 cp = strrchr(argv0, '\\');
47 if (cp)
48 return cp+1;
49 return argv0;
50}
51
52void yaz_check_init1(int *argc_p, char ***argv_p)
53{
54 int i = 0;
55 int argc = *argc_p;
56 char **argv = *argv_p;
57
58 test_prog = progname(argv[0]);
59
60 for (i = 1; i<argc; i++)
61 {
62 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
63 {
64 const char *suf = argv[i]+7;
65 if (i < argc-1 && !strcmp(suf, "file"))
66 {
67 i++;
68 if (test_fout)
69 fclose(test_fout);
70 test_fout = fopen(argv[i], "w");
71 continue;
72 }
73 else if (i < argc-1 && !strcmp(suf, "verbose"))
74 {
75 i++;
76 test_verbose = atoi(argv[i]);
77 continue;
78 }
79 else if (i < argc && !strcmp(suf, "stop"))
80 {
81 test_stop = 1;
82 continue;
83 }
84 else if (!strcmp(suf, "help"))
85 {
86 fprintf(stderr,
87 "--test-help help\n"
88 "--test-file fname output to fname\n"
89 "--test-stop stop at first failing test\n"
90 "--test-verbose level verbose level\n"
91 " 0=Quiet. Only exit code tells what's wrong\n"
92 " 1=Report+Summary only if tests fail.\n"
93 " 2=Report failures. Print summary always\n"
94 " 3=Report + summary always\n"
95 " 4=Report + summary + extra prints from tests\n"
96 );
97 exit(0);
98 }
99 else
100 {
101 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
102 argv[i]);
103 fprintf(stderr, "Use --test-help for more info\n");
104 exit(1);
105 }
106
107 }
108 break;
109 }
110 /* remove --test- options from argc, argv so that they disappear */
111 (*argv_p)[i-1] = **argv_p; /* program name */
112 --i;
113 *argc_p -= i;
114 *argv_p += i;
115}
116
118void yaz_check_init_log(const char *argv0)
119{
120 char logfilename[2048];
121 log_tests = 1;
122 yaz_snprintf(logfilename, sizeof(logfilename), "%s.log", progname(argv0));
123 yaz_log_init_file(logfilename);
125
126}
127
129{
130 test_todo++;
131}
132
134{
135 /* summary */
136 if (test_failed)
137 {
138 if (test_verbose >= 1) {
139 if (test_todo)
140 fprintf(get_file(), "%d out of %d tests failed for program %s"
141 " (%d TODO's remaining)\n",
143 else
144 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
146 }
147 }
148 else
149 {
150 if (test_verbose >= 2) {
151 if (test_todo)
152 fprintf(get_file(), "%d tests passed for program %s"
153 " (%d TODO's remaining)\n",
155 else
156 fprintf(get_file(), "%d tests passed for program %s\n",
158 }
159 }
160 if (test_fout)
161 fclose(test_fout);
163 if (test_failed)
164 exit(1);
165 exit(0);
166}
167
168void yaz_check_eq1(int type, const char *file, int line,
169 const char *left, const char *right, int lval, int rval)
170{
171 char formstr[2048];
172
173 if (type == YAZ_TEST_TYPE_OK)
174 yaz_snprintf(formstr, sizeof(formstr), "%s == %s ", left, right);
175 else
176 yaz_snprintf(formstr, sizeof(formstr), "%s != %s\n %d != %d", left, right, lval,rval);
177 yaz_check_print1(type, file, line, formstr);
178}
179
180void yaz_check_print1(int type, const char *file, int line,
181 const char *expr)
182{
183 const char *msg = "unknown";
184 int printit = 1;
185
186 test_total++;
187 switch(type)
188 {
190 test_failed++;
191 msg = "FAILED";
192 if (test_verbose < 1)
193 printit = 0;
194 break;
195 case YAZ_TEST_TYPE_OK:
196 msg = "ok";
197 if (test_verbose < 3)
198 printit = 0;
199 break;
200 }
201 if (printit)
202 {
203 fprintf(get_file(), "%s:%d: %s: ", file, line, msg);
204 fprintf(get_file(), "%s\n", expr);
205 }
206 if (log_tests)
207 {
208 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
209 yaz_log(YLOG_LOG, "%s", expr);
210 }
212 {
213 exit(1);
214 }
215}
216
217
219{
220 return test_verbose;
221}
222
223/*
224 * Local variables:
225 * c-basic-offset: 4
226 * c-file-style: "Stroustrup"
227 * indent-tabs-mode: nil
228 * End:
229 * vim: shiftwidth=4 tabstop=8 expandtab
230 */
231
void yaz_deinit_globals(void)
enum l_file_type type
Definition log.c:47
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition log.c:487
void yaz_log_init_file(const char *fname)
sets log file
Definition log.c:168
void yaz_log_trunc()
Truncate the log file.
Definition log.c:401
Logging utility.
#define YLOG_LOG
log level: log (regular)
Definition log.h:48
void yaz_snprintf(char *buf, size_t size, const char *fmt,...)
Definition snprintf.c:31
Header for config file reading utilities.
const char * progname
Definition stemwords.c:12
void yaz_check_init1(int *argc_p, char ***argv_p)
used by macro. Should not be called directly
Definition test.c:52
static int test_total
Definition test.c:26
static int log_tests
Definition test.c:31
int yaz_test_get_verbosity()
Get the verbosity level.
Definition test.c:218
static const char * test_prog
Definition test.c:30
void yaz_check_init_log(const char *argv0)
Initialize the log system.
Definition test.c:118
void yaz_check_term1(void)
used by macro. Should not be called directly
Definition test.c:133
static int test_verbose
Definition test.c:29
void yaz_check_inc_todo(void)
used by macro. Should not be called directly
Definition test.c:128
static int test_todo
Definition test.c:28
static int test_failed
Definition test.c:27
static int test_stop
Definition test.c:32
static FILE * get_file(void)
Definition test.c:34
void yaz_check_print1(int type, const char *file, int line, const char *expr)
used by macro. Should not be called directly
Definition test.c:180
void yaz_check_eq1(int type, const char *file, int line, const char *left, const char *right, int lval, int rval)
used by macro. Should not be called directly
Definition test.c:168
static FILE * test_fout
Definition test.c:25
Unit Test for YAZ.
#define YAZ_TEST_TYPE_FAIL
Test failed.
Definition test.h:44
#define YAZ_TEST_TYPE_OK
Test OK.
Definition test.h:42