X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ftest.c;h=258517e76a19b13818d9b6eb0f79260197b78e4f;hp=f6dd704d311e8c76e89e8e54f370afc2eaf011f2;hb=b111819a3d4c8dea01efe2c12ef21689ea0c21ed;hpb=228ac8e67af8286708bf4ef65622e3d742272d61 diff --git a/src/test.c b/src/test.c index f6dd704..258517e 100644 --- a/src/test.c +++ b/src/test.c @@ -1,8 +1,6 @@ -/* - * Copyright (C) 1995-2005, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data * See the file LICENSE for details. - * - * $Id: test.c,v 1.6 2006-04-26 16:58:27 heikki Exp $ */ /** \file test.c @@ -16,25 +14,32 @@ #include #include #include +#if HAVE_UNISTSD_H +#include +#endif #include +#include static FILE *test_fout = 0; /* can't use '= stdout' on some systems */ static int test_total = 0; static int test_failed = 0; +static int test_todo = 0; static int test_verbose = 1; -static char *test_prog = 0; +static const char *test_prog = 0; +static int log_tests = 0; +static int test_stop = 0; -static FILE *get_file() +static FILE *get_file(void) { if (test_fout) return test_fout; return stdout; } -static char *progname(char *argv0) +static const char *progname(const char *argv0) { - char *cp = strrchr(argv0, '/'); + const char *cp = strrchr(argv0, '/'); if (cp) return cp+1; cp = strrchr(argv0, '\\'); @@ -70,16 +75,23 @@ void yaz_check_init1(int *argc_p, char ***argv_p) test_verbose = atoi(argv[i]); continue; } + else if (i < argc && !strcmp(suf, "stop")) + { + test_stop = 1; + continue; + } else if (!strcmp(suf, "help")) { - fprintf(stderr, + fprintf(stderr, "--test-help help\n" "--test-file fname output to fname\n" + "--test-stop stop at first failing test\n" "--test-verbose level verbose level\n" " 0=Quiet. Only exit code tells what's wrong\n" " 1=Report+Summary only if tests fail.\n" " 2=Report failures. Print summary always\n" " 3=Report + summary always\n" + " 4=Report + summary + extra prints from tests\n" ); exit(0); } @@ -90,7 +102,7 @@ void yaz_check_init1(int *argc_p, char ***argv_p) fprintf(stderr, "Use --test-help for more info\n"); exit(1); } - + } break; } @@ -101,20 +113,48 @@ void yaz_check_init1(int *argc_p, char ***argv_p) *argv_p += i; } +/** \brief Initialize the log system */ +void yaz_check_init_log(const char *argv0) +{ + char logfilename[2048]; + log_tests = 1; + sprintf(logfilename,"%s.log", progname(argv0) ); + yaz_log_init_file(logfilename); + yaz_log_trunc(); + +} + +void yaz_check_inc_todo(void) +{ + test_todo++; +} + void yaz_check_term1(void) { /* summary */ if (test_failed) { - if (test_verbose >= 1) - fprintf(get_file(), "%d out of %d tests failed for program %s\n", + if (test_verbose >= 1) { + if (test_todo) + fprintf(get_file(), "%d out of %d tests failed for program %s" + " (%d TODO's remaining)\n", + test_failed, test_total, test_prog,test_todo); + else + fprintf(get_file(), "%d out of %d tests failed for program %s\n", test_failed, test_total, test_prog); + } } else { - if (test_verbose >= 2) - fprintf(get_file(), "%d tests passed for program %s\n", + if (test_verbose >= 2) { + if (test_todo) + fprintf(get_file(), "%d tests passed for program %s" + " (%d TODO's remaining)\n", + test_total, test_prog,test_todo); + else + fprintf(get_file(), "%d tests passed for program %s\n", test_total, test_prog); + } } if (test_fout) fclose(test_fout); @@ -127,18 +167,19 @@ void yaz_check_eq1(int type, const char *file, int line, const char *left, const char *right, int lval, int rval) { char formstr[2048]; - - if (type==YAZ_TEST_TYPE_OK) + + if (type == YAZ_TEST_TYPE_OK) sprintf(formstr, "%.500s == %.500s ", left, right); else sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval); yaz_check_print1(type, file, line, formstr); } -void yaz_check_print1(int type, const char *file, int line, +void yaz_check_print1(int type, const char *file, int line, const char *expr) { const char *msg = "unknown"; + int printit = 1; test_total++; switch(type) @@ -147,22 +188,40 @@ void yaz_check_print1(int type, const char *file, int line, test_failed++; msg = "FAILED"; if (test_verbose < 1) - return; + printit = 0; break; case YAZ_TEST_TYPE_OK: msg = "ok"; if (test_verbose < 3) - return; + printit = 0; break; } - fprintf(get_file(), "%s:%d %s: ", file, line, msg); - fprintf(get_file(), "%s\n", expr); + if (printit) + { + fprintf(get_file(), "%s:%d: %s: ", file, line, msg); + fprintf(get_file(), "%s\n", expr); + } + if (log_tests) + { + yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg); + yaz_log(YLOG_LOG, "%s", expr); + } + if ( type == YAZ_TEST_TYPE_FAIL && test_stop ) + { + exit(1); + } } +int yaz_test_get_verbosity() +{ + return test_verbose; +} + /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab