1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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;
32 static FILE *get_file(void)
39 static const char *progname(const char *argv0)
41 const char *cp = strrchr(argv0, '/');
44 cp = strrchr(argv0, '\\');
50 void yaz_check_init1(int *argc_p, char ***argv_p)
54 char **argv = *argv_p;
56 test_prog = progname(argv[0]);
58 for (i = 1; i<argc; i++)
60 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
62 const char *suf = argv[i]+7;
63 if (i < argc-1 && !strcmp(suf, "file"))
68 test_fout = fopen(argv[i], "w");
71 else if (i < argc-1 && !strcmp(suf, "verbose"))
74 test_verbose = atoi(argv[i]);
77 else if (!strcmp(suf, "help"))
81 "--test-file fname output to fname\n"
82 "--test-verbose level verbose level\n"
83 " 0=Quiet. Only exit code tells what's wrong\n"
84 " 1=Report+Summary only if tests fail.\n"
85 " 2=Report failures. Print summary always\n"
86 " 3=Report + summary always\n"
87 " 4=Report + summary + extra prints from tests\n"
93 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
95 fprintf(stderr, "Use --test-help for more info\n");
102 /* remove --test- options from argc, argv so that they disappear */
103 (*argv_p)[i-1] = **argv_p; /* program name */
109 /** \brief Initialize the log system */
110 void yaz_check_init_log(const char *argv0)
112 char logfilename[2048];
114 sprintf(logfilename,"%s.log", progname(argv0) );
115 yaz_log_init_file(logfilename);
120 void yaz_check_inc_todo(void)
125 void yaz_check_term1(void)
130 if (test_verbose >= 1) {
132 fprintf(get_file(), "%d out of %d tests failed for program %s"
133 " (%d TODO's remaining)\n",
134 test_failed, test_total, test_prog,test_todo);
136 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
137 test_failed, test_total, test_prog);
142 if (test_verbose >= 2) {
144 fprintf(get_file(), "%d tests passed for program %s"
145 " (%d TODO's remaining)\n",
146 test_total, test_prog,test_todo);
148 fprintf(get_file(), "%d tests passed for program %s\n",
149 test_total, test_prog);
159 void yaz_check_eq1(int type, const char *file, int line,
160 const char *left, const char *right, int lval, int rval)
164 if (type == YAZ_TEST_TYPE_OK)
165 sprintf(formstr, "%.500s == %.500s ", left, right);
167 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
168 yaz_check_print1(type, file, line, formstr);
171 void yaz_check_print1(int type, const char *file, int line,
174 const char *msg = "unknown";
180 case YAZ_TEST_TYPE_FAIL:
183 if (test_verbose < 1)
186 case YAZ_TEST_TYPE_OK:
188 if (test_verbose < 3)
194 fprintf(get_file(), "%s:%d: %s: ", file, line, msg);
195 fprintf(get_file(), "%s\n", expr);
199 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
200 yaz_log(YLOG_LOG, "%s", expr);
205 int yaz_test_get_verbosity()
213 * c-file-style: "Stroustrup"
214 * indent-tabs-mode: nil
216 * vim: shiftwidth=4 tabstop=8 expandtab