2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: test.c,v 1.8 2006-07-06 13:10:31 heikki Exp $
9 \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_verbose = 1;
28 static char *test_prog = 0;
29 static int log_tests = 0;
31 static FILE *get_file()
38 static char *progname(char *argv0)
40 char *cp = strrchr(argv0, '/');
43 cp = strrchr(argv0, '\\');
49 void yaz_check_init1(int *argc_p, char ***argv_p)
53 char **argv = *argv_p;
55 test_prog = progname(argv[0]);
57 for (i = 1; i<argc; i++)
59 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
61 const char *suf = argv[i]+7;
62 if (i < argc-1 && !strcmp(suf, "file"))
67 test_fout = fopen(argv[i], "w");
70 else if (i < argc-1 && !strcmp(suf, "verbose"))
73 test_verbose = atoi(argv[i]);
76 else if (!strcmp(suf, "help"))
80 "--test-file fname output to fname\n"
81 "--test-verbose level verbose level\n"
82 " 0=Quiet. Only exit code tells what's wrong\n"
83 " 1=Report+Summary only if tests fail.\n"
84 " 2=Report failures. Print summary always\n"
85 " 3=Report + summary always\n"
86 " 4=Report + summary + extra prints from tests\n"
92 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
94 fprintf(stderr, "Use --test-help for more info\n");
101 /* remove --test- options from argc, argv so that they disappear */
102 (*argv_p)[i-1] = **argv_p; /* program name */
108 /** \brief Initialize the log system */
109 void yaz_check_init_log(char *argv0)
111 char logfilename[2048];
113 sprintf(logfilename,"%s.log", progname(argv0) );
115 yaz_log_init_file(logfilename);
120 void yaz_check_term1(void)
125 if (test_verbose >= 1)
126 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
127 test_failed, test_total, test_prog);
131 if (test_verbose >= 2)
132 fprintf(get_file(), "%d tests passed for program %s\n",
133 test_total, test_prog);
142 void yaz_check_eq1(int type, const char *file, int line,
143 const char *left, const char *right, int lval, int rval)
147 if (type==YAZ_TEST_TYPE_OK)
148 sprintf(formstr, "%.500s == %.500s ", left, right);
150 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
151 yaz_check_print1(type, file, line, formstr);
154 void yaz_check_print1(int type, const char *file, int line,
157 const char *msg = "unknown";
163 case YAZ_TEST_TYPE_FAIL:
166 if (test_verbose < 1)
169 case YAZ_TEST_TYPE_OK:
171 if (test_verbose < 3)
176 fprintf(get_file(), "%s:%d %s: ", file, line, msg);
177 fprintf(get_file(), "%s\n", expr);
180 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
181 yaz_log(YLOG_LOG, "%s\n", expr);
186 int yaz_test_get_verbosity(){
193 * indent-tabs-mode: nil
195 * vim: shiftwidth=4 tabstop=8 expandtab