1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 \brief Unit Test for YAZ
24 static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
25 static int test_total = 0;
26 static int test_failed = 0;
27 static int test_todo = 0;
28 static int test_verbose = 1;
29 static const char *test_prog = 0;
30 static int log_tests = 0;
31 static int test_stop = 0;
33 static FILE *get_file(void)
40 static const char *progname(const char *argv0)
42 const char *cp = strrchr(argv0, '/');
45 cp = strrchr(argv0, '\\');
51 void yaz_check_init1(int *argc_p, char ***argv_p)
55 char **argv = *argv_p;
57 test_prog = progname(argv[0]);
59 for (i = 1; i<argc; i++)
61 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
63 const char *suf = argv[i]+7;
64 if (i < argc-1 && !strcmp(suf, "file"))
69 test_fout = fopen(argv[i], "w");
72 else if (i < argc-1 && !strcmp(suf, "verbose"))
75 test_verbose = atoi(argv[i]);
78 else if (i < argc && !strcmp(suf, "stop"))
83 else if (!strcmp(suf, "help"))
87 "--test-file fname output to fname\n"
88 "--test-stop stop at first failing test\n"
89 "--test-verbose level verbose level\n"
90 " 0=Quiet. Only exit code tells what's wrong\n"
91 " 1=Report+Summary only if tests fail.\n"
92 " 2=Report failures. Print summary always\n"
93 " 3=Report + summary always\n"
94 " 4=Report + summary + extra prints from tests\n"
100 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
102 fprintf(stderr, "Use --test-help for more info\n");
109 /* remove --test- options from argc, argv so that they disappear */
110 (*argv_p)[i-1] = **argv_p; /* program name */
116 /** \brief Initialize the log system */
117 void yaz_check_init_log(const char *argv0)
119 char logfilename[2048];
121 sprintf(logfilename,"%s.log", progname(argv0) );
122 yaz_log_init_file(logfilename);
127 void yaz_check_inc_todo(void)
132 void yaz_check_term1(void)
137 if (test_verbose >= 1) {
139 fprintf(get_file(), "%d out of %d tests failed for program %s"
140 " (%d TODO's remaining)\n",
141 test_failed, test_total, test_prog,test_todo);
143 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
144 test_failed, test_total, test_prog);
149 if (test_verbose >= 2) {
151 fprintf(get_file(), "%d tests passed for program %s"
152 " (%d TODO's remaining)\n",
153 test_total, test_prog,test_todo);
155 fprintf(get_file(), "%d tests passed for program %s\n",
156 test_total, test_prog);
166 void yaz_check_eq1(int type, const char *file, int line,
167 const char *left, const char *right, int lval, int rval)
171 if (type == YAZ_TEST_TYPE_OK)
172 sprintf(formstr, "%.500s == %.500s ", left, right);
174 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
175 yaz_check_print1(type, file, line, formstr);
178 void yaz_check_print1(int type, const char *file, int line,
181 const char *msg = "unknown";
187 case YAZ_TEST_TYPE_FAIL:
190 if (test_verbose < 1)
193 case YAZ_TEST_TYPE_OK:
195 if (test_verbose < 3)
201 fprintf(get_file(), "%s:%d: %s: ", file, line, msg);
202 fprintf(get_file(), "%s\n", expr);
206 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
207 yaz_log(YLOG_LOG, "%s", expr);
209 if ( type == YAZ_TEST_TYPE_FAIL && test_stop )
216 int yaz_test_get_verbosity()
224 * c-file-style: "Stroustrup"
225 * indent-tabs-mode: nil
227 * vim: shiftwidth=4 tabstop=8 expandtab