2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: test.c,v 1.9 2006-07-07 06:59:49 adam 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_verbose = 1;
30 static const char *test_prog = 0;
31 static int log_tests = 0;
33 static FILE *get_file()
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 (!strcmp(suf, "help"))
82 "--test-file fname output to fname\n"
83 "--test-verbose level verbose level\n"
84 " 0=Quiet. Only exit code tells what's wrong\n"
85 " 1=Report+Summary only if tests fail.\n"
86 " 2=Report failures. Print summary always\n"
87 " 3=Report + summary always\n"
88 " 4=Report + summary + extra prints from tests\n"
94 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
96 fprintf(stderr, "Use --test-help for more info\n");
103 /* remove --test- options from argc, argv so that they disappear */
104 (*argv_p)[i-1] = **argv_p; /* program name */
110 /** \brief Initialize the log system */
111 void yaz_check_init_log(const char *argv0)
113 char logfilename[2048];
115 sprintf(logfilename,"%s.log", progname(argv0) );
117 yaz_log_init_file(logfilename);
122 void yaz_check_term1(void)
127 if (test_verbose >= 1)
128 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
129 test_failed, test_total, test_prog);
133 if (test_verbose >= 2)
134 fprintf(get_file(), "%d tests passed for program %s\n",
135 test_total, test_prog);
144 void yaz_check_eq1(int type, const char *file, int line,
145 const char *left, const char *right, int lval, int rval)
149 if (type == YAZ_TEST_TYPE_OK)
150 sprintf(formstr, "%.500s == %.500s ", left, right);
152 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
153 yaz_check_print1(type, file, line, formstr);
156 void yaz_check_print1(int type, const char *file, int line,
159 const char *msg = "unknown";
165 case YAZ_TEST_TYPE_FAIL:
168 if (test_verbose < 1)
171 case YAZ_TEST_TYPE_OK:
173 if (test_verbose < 3)
179 fprintf(get_file(), "%s:%d %s: ", file, line, msg);
180 fprintf(get_file(), "%s\n", expr);
184 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
185 yaz_log(YLOG_LOG, "%s\n", expr);
190 int yaz_test_get_verbosity()
198 * indent-tabs-mode: nil
200 * vim: shiftwidth=4 tabstop=8 expandtab