2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: test.c,v 1.10 2006-07-07 13:39:04 heikki Exp $
9 \brief Unit Test for YAZ
26 static FILE *test_fout = 0; /* can't use '= stdout' on some systems */
27 static int test_total = 0;
28 static int test_failed = 0;
29 static int test_todo = 0;
30 static int test_verbose = 1;
31 static const char *test_prog = 0;
32 static int log_tests = 0;
34 static FILE *get_file()
41 static const char *progname(const char *argv0)
43 const char *cp = strrchr(argv0, '/');
46 cp = strrchr(argv0, '\\');
52 void yaz_check_init1(int *argc_p, char ***argv_p)
56 char **argv = *argv_p;
58 test_prog = progname(argv[0]);
60 for (i = 1; i<argc; i++)
62 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
64 const char *suf = argv[i]+7;
65 if (i < argc-1 && !strcmp(suf, "file"))
70 test_fout = fopen(argv[i], "w");
73 else if (i < argc-1 && !strcmp(suf, "verbose"))
76 test_verbose = atoi(argv[i]);
79 else if (!strcmp(suf, "help"))
83 "--test-file fname output to fname\n"
84 "--test-verbose level verbose level\n"
85 " 0=Quiet. Only exit code tells what's wrong\n"
86 " 1=Report+Summary only if tests fail.\n"
87 " 2=Report failures. Print summary always\n"
88 " 3=Report + summary always\n"
89 " 4=Report + summary + extra prints from tests\n"
95 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
97 fprintf(stderr, "Use --test-help for more info\n");
104 /* remove --test- options from argc, argv so that they disappear */
105 (*argv_p)[i-1] = **argv_p; /* program name */
111 /** \brief Initialize the log system */
112 void yaz_check_init_log(const char *argv0)
114 char logfilename[2048];
116 sprintf(logfilename,"%s.log", progname(argv0) );
117 yaz_log_init_file(logfilename);
122 void yaz_check_inc_todo(void)
127 void yaz_check_term1(void)
132 if (test_verbose >= 1) {
134 fprintf(get_file(), "%d out of %d tests failed for program %s"
135 " (%d TODO's remaining)\n",
136 test_failed, test_total, test_prog,test_todo);
138 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
139 test_failed, test_total, test_prog);
144 if (test_verbose >= 2) {
146 fprintf(get_file(), "%d tests passed for program %s"
147 " (%d TODO's remaining)\n",
148 test_total, test_prog,test_todo);
150 fprintf(get_file(), "%d tests passed for program %s\n",
151 test_total, test_prog);
161 void yaz_check_eq1(int type, const char *file, int line,
162 const char *left, const char *right, int lval, int rval)
166 if (type == YAZ_TEST_TYPE_OK)
167 sprintf(formstr, "%.500s == %.500s ", left, right);
169 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
170 yaz_check_print1(type, file, line, formstr);
173 void yaz_check_print1(int type, const char *file, int line,
176 const char *msg = "unknown";
182 case YAZ_TEST_TYPE_FAIL:
185 if (test_verbose < 1)
188 case YAZ_TEST_TYPE_OK:
190 if (test_verbose < 3)
196 fprintf(get_file(), "%s:%d %s: ", file, line, msg);
197 fprintf(get_file(), "%s\n", expr);
201 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
202 yaz_log(YLOG_LOG, "%s", expr);
207 int yaz_test_get_verbosity()
215 * indent-tabs-mode: nil
217 * vim: shiftwidth=4 tabstop=8 expandtab