00001
00002
00003
00004
00005
00010 #if HAVE_CONFIG_H
00011 #include <config.h>
00012 #endif
00013
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <stdlib.h>
00017 #if HAVE_UNISTSD_H
00018 #include <unistd.h>
00019 #endif
00020
00021 #include <yaz/test.h>
00022 #include <yaz/log.h>
00023
00024 static FILE *test_fout = 0;
00025 static int test_total = 0;
00026 static int test_failed = 0;
00027 static int test_todo = 0;
00028 static int test_verbose = 1;
00029 static const char *test_prog = 0;
00030 static int log_tests = 0;
00031
00032 static FILE *get_file(void)
00033 {
00034 if (test_fout)
00035 return test_fout;
00036 return stdout;
00037 }
00038
00039 static const char *progname(const char *argv0)
00040 {
00041 const char *cp = strrchr(argv0, '/');
00042 if (cp)
00043 return cp+1;
00044 cp = strrchr(argv0, '\\');
00045 if (cp)
00046 return cp+1;
00047 return argv0;
00048 }
00049
00050 void yaz_check_init1(int *argc_p, char ***argv_p)
00051 {
00052 int i = 0;
00053 int argc = *argc_p;
00054 char **argv = *argv_p;
00055
00056 test_prog = progname(argv[0]);
00057
00058 for (i = 1; i<argc; i++)
00059 {
00060 if (strlen(argv[i]) >= 7 && !memcmp(argv[i], "--test-", 7))
00061 {
00062 const char *suf = argv[i]+7;
00063 if (i < argc-1 && !strcmp(suf, "file"))
00064 {
00065 i++;
00066 if (test_fout)
00067 fclose(test_fout);
00068 test_fout = fopen(argv[i], "w");
00069 continue;
00070 }
00071 else if (i < argc-1 && !strcmp(suf, "verbose"))
00072 {
00073 i++;
00074 test_verbose = atoi(argv[i]);
00075 continue;
00076 }
00077 else if (!strcmp(suf, "help"))
00078 {
00079 fprintf(stderr,
00080 "--test-help help\n"
00081 "--test-file fname output to fname\n"
00082 "--test-verbose level verbose level\n"
00083 " 0=Quiet. Only exit code tells what's wrong\n"
00084 " 1=Report+Summary only if tests fail.\n"
00085 " 2=Report failures. Print summary always\n"
00086 " 3=Report + summary always\n"
00087 " 4=Report + summary + extra prints from tests\n"
00088 );
00089 exit(0);
00090 }
00091 else
00092 {
00093 fprintf(stderr, "Unrecognized option for YAZ test: %s\n",
00094 argv[i]);
00095 fprintf(stderr, "Use --test-help for more info\n");
00096 exit(1);
00097 }
00098
00099 }
00100 break;
00101 }
00102
00103 (*argv_p)[i-1] = **argv_p;
00104 --i;
00105 *argc_p -= i;
00106 *argv_p += i;
00107 }
00108
00110 void yaz_check_init_log(const char *argv0)
00111 {
00112 char logfilename[2048];
00113 log_tests = 1;
00114 sprintf(logfilename,"%s.log", progname(argv0) );
00115 yaz_log_init_file(logfilename);
00116 yaz_log_trunc();
00117
00118 }
00119
00120 void yaz_check_inc_todo(void)
00121 {
00122 test_todo++;
00123 }
00124
00125 void yaz_check_term1(void)
00126 {
00127
00128 if (test_failed)
00129 {
00130 if (test_verbose >= 1) {
00131 if (test_todo)
00132 fprintf(get_file(), "%d out of %d tests failed for program %s"
00133 " (%d TODO's remaining)\n",
00134 test_failed, test_total, test_prog,test_todo);
00135 else
00136 fprintf(get_file(), "%d out of %d tests failed for program %s\n",
00137 test_failed, test_total, test_prog);
00138 }
00139 }
00140 else
00141 {
00142 if (test_verbose >= 2) {
00143 if (test_todo)
00144 fprintf(get_file(), "%d tests passed for program %s"
00145 " (%d TODO's remaining)\n",
00146 test_total, test_prog,test_todo);
00147 else
00148 fprintf(get_file(), "%d tests passed for program %s\n",
00149 test_total, test_prog);
00150 }
00151 }
00152 if (test_fout)
00153 fclose(test_fout);
00154 if (test_failed)
00155 exit(1);
00156 exit(0);
00157 }
00158
00159 void yaz_check_eq1(int type, const char *file, int line,
00160 const char *left, const char *right, int lval, int rval)
00161 {
00162 char formstr[2048];
00163
00164 if (type == YAZ_TEST_TYPE_OK)
00165 sprintf(formstr, "%.500s == %.500s ", left, right);
00166 else
00167 sprintf(formstr, "%.500s != %.500s\n %d != %d", left, right, lval,rval);
00168 yaz_check_print1(type, file, line, formstr);
00169 }
00170
00171 void yaz_check_print1(int type, const char *file, int line,
00172 const char *expr)
00173 {
00174 const char *msg = "unknown";
00175 int printit = 1;
00176
00177 test_total++;
00178 switch(type)
00179 {
00180 case YAZ_TEST_TYPE_FAIL:
00181 test_failed++;
00182 msg = "FAILED";
00183 if (test_verbose < 1)
00184 printit = 0;
00185 break;
00186 case YAZ_TEST_TYPE_OK:
00187 msg = "ok";
00188 if (test_verbose < 3)
00189 printit = 0;
00190 break;
00191 }
00192 if (printit)
00193 {
00194 fprintf(get_file(), "%s:%d %s: ", file, line, msg);
00195 fprintf(get_file(), "%s\n", expr);
00196 }
00197 if (log_tests)
00198 {
00199 yaz_log(YLOG_LOG, "%s:%d %s: ", file, line, msg);
00200 yaz_log(YLOG_LOG, "%s", expr);
00201 }
00202 }
00203
00204
00205 int yaz_test_get_verbosity()
00206 {
00207 return test_verbose;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217