5 int seatest_is_string_equal_i(
const char* s1,
const char* s2)
7 #pragma warning(disable: 4996)
8 return stricmp(s1, s2) == 0;
14 long int GetTickCount()
23 gettimeofday(&tv, NULL);
24 return tv.tv_sec*1000000 + tv.tv_usec;
27 void _getch(
void ) { }
28 int seatest_is_string_equal_i(
const char* s1,
const char* s2)
30 return strcasecmp(s1, s2) == 0;
34 #ifdef SEATEST_INTERNAL_TESTS
35 static int sea_test_last_passed = 0;
40 SEATEST_DISPLAY_TESTS,
50 seatest_action_t action;
52 static int seatest_screen_width = 70;
53 static int sea_tests_run = 0;
54 static int sea_tests_passed = 0;
55 static int sea_tests_failed = 0;
56 static int seatest_display_only = 0;
57 static int seatest_verbose = 0;
58 static int seatest_machine_readable = 0;
59 static char* seatest_current_fixture;
60 static char* seatest_current_fixture_path;
61 static char seatest_magic_marker[20] =
"";
63 static seatest_void_void seatest_suite_setup_func = 0;
64 static seatest_void_void seatest_suite_teardown_func = 0;
65 static seatest_void_void seatest_fixture_setup = 0;
66 static seatest_void_void seatest_fixture_teardown = 0;
68 void (*seatest_simple_test_result)(
int passed,
char* reason,
const char*
function,
unsigned int line) = seatest_simple_test_result_log;
70 void suite_setup(seatest_void_void setup)
72 seatest_suite_setup_func = setup;
74 void suite_teardown(seatest_void_void teardown)
76 seatest_suite_teardown_func = teardown;
79 int seatest_is_display_only()
81 return seatest_display_only;
84 void seatest_suite_setup(
void )
86 if(seatest_suite_setup_func != 0) seatest_suite_setup_func();
89 void seatest_suite_teardown(
void )
91 if(seatest_suite_teardown_func != 0) seatest_suite_teardown_func();
94 void fixture_setup(
void (*setup)(
void ))
96 seatest_fixture_setup = setup;
98 void fixture_teardown(
void (*teardown)(
void ))
100 seatest_fixture_teardown = teardown;
103 void seatest_setup(
void )
105 if(seatest_fixture_setup != 0) seatest_fixture_setup();
108 void seatest_teardown(
void )
110 if(seatest_fixture_teardown != 0) seatest_fixture_teardown();
113 char* test_file_name(
char* path)
115 char* file = path + strlen(path);
116 while(file != path && *file!=
'\\' ) file--;
117 if(*file ==
'\\') file++;
121 static int seatest_fixture_tests_run;
122 static int seatest_fixture_tests_failed;
124 void seatest_simple_test_result_log(
int passed,
char* reason,
const char*
function,
unsigned int line)
129 if(seatest_machine_readable)
131 printf(
"%s%s,%s,%u,%s\r\n", seatest_magic_marker, seatest_current_fixture_path,
function, line, reason );
135 printf(
"%-30s Line %-5d %s\r\n",
function, line, reason );
143 if(seatest_machine_readable)
145 printf(
"%s%s,%s,%u,Passed\r\n", seatest_magic_marker, seatest_current_fixture_path,
function, line );
149 printf(
"%-30s Line %-5d Passed\r\n",
function, line);
156 void seatest_assert_true(
int test,
const char*
function,
unsigned int line)
158 seatest_simple_test_result(test,
"Should have been true",
function, line);
162 void seatest_assert_false(
int test,
const char*
function,
unsigned int line)
164 seatest_simple_test_result(!test,
"Should have been false",
function, line);
168 void seatest_assert_int_equal(
int expected,
int actual,
const char*
function,
unsigned int line)
170 char s[SEATEST_PRINT_BUFFER_SIZE];
171 sprintf(s,
"Expected %d but was %d", expected, actual);
172 seatest_simple_test_result(expected==actual, s,
function, line);
175 void seatest_assert_ulong_equal(
unsigned long expected,
unsigned long actual,
const char*
function,
unsigned int line)
177 char s[SEATEST_PRINT_BUFFER_SIZE];
178 sprintf(s,
"Expected %lu but was %lu", expected, actual);
179 seatest_simple_test_result(expected==actual, s,
function, line);
182 void seatest_assert_float_vec_equal(
float expected,
float actual,
unsigned int delta,
unsigned int seatest_vec,
const char*
function,
unsigned int line )
184 char s[SEATEST_PRINT_BUFFER_SIZE];
185 if (!EQUALS_FLOAT(expected, actual, delta))
187 sprintf(s,
"Expected %e (0x%04X) but was %e (0x%04X) at vector->%d ",
188 expected, *(
unsigned int*)&expected, actual, *(
unsigned int*)&actual, seatest_vec);
189 seatest_simple_test_result( 0, s,
function, line);
193 void seatest_assert_float_equal(
float expected,
float actual,
unsigned int delta,
unsigned int loop_round,
const char*
function,
unsigned int line )
195 char s[SEATEST_PRINT_BUFFER_SIZE];
196 if (!EQUALS_FLOAT(expected, actual, delta))
198 sprintf(s,
"Expected %e (0x%04X) but was %e (0x%04X) in loop round %d",
199 expected, *(
unsigned int*)&expected, actual, *(
unsigned int*)&actual, loop_round);
200 seatest_simple_test_result( 0, s,
function, line);
204 void seatest_assert_double_equal(
double expected,
double actual,
double delta,
const char*
function,
unsigned int line )
206 char s[SEATEST_PRINT_BUFFER_SIZE];
207 double result = expected-actual;
208 sprintf(s,
"Expected %f but was %f", expected, actual);
209 if(result < 0.0) result = 0.0 - result;
210 seatest_simple_test_result( result <= delta, s,
function, line);
213 void seatest_assert_string_equal(
char* expected,
char* actual,
const char*
function,
unsigned int line)
216 char s[SEATEST_PRINT_BUFFER_SIZE];
218 if ((expected == (
char *)0) && (actual == (
char *)0))
220 sprintf(s,
"Expected <NULL> but was <NULL>");
223 else if ((expected == (
char *)0))
225 sprintf(s,
"Expected <NULL> but was %s", actual);
228 else if ((actual == (
char *)0))
230 sprintf(s,
"Expected %s but was <NULL>", expected);
235 comparison = strcmp(expected, actual) == 0;
236 sprintf(s,
"Expected %s but was %s", expected, actual);
239 seatest_simple_test_result(comparison, s,
function, line);
242 void seatest_assert_string_ends_with(
char* expected,
char* actual,
const char*
function,
unsigned int line)
244 char s[SEATEST_PRINT_BUFFER_SIZE];
245 sprintf(s,
"Expected %s to end with %s", actual, expected);
246 seatest_simple_test_result(strcmp(expected, actual+(strlen(actual)-strlen(expected)))==0, s,
function, line);
249 void seatest_assert_string_starts_with(
char* expected,
char* actual,
const char*
function,
unsigned int line)
251 char s[SEATEST_PRINT_BUFFER_SIZE];
252 sprintf(s,
"Expected %s to start with %s", actual, expected);
253 seatest_simple_test_result(strncmp(expected, actual, strlen(expected))==0, s,
function, line);
256 void seatest_assert_string_contains(
char* expected,
char* actual,
const char*
function,
unsigned int line)
258 char s[SEATEST_PRINT_BUFFER_SIZE];
259 sprintf(s,
"Expected %s to be in %s", expected, actual);
260 seatest_simple_test_result(strstr(actual, expected)!=0, s,
function, line);
263 void seatest_assert_string_doesnt_contain(
char* expected,
char* actual,
const char*
function,
unsigned int line)
265 char s[SEATEST_PRINT_BUFFER_SIZE];
266 sprintf(s,
"Expected %s not to have %s in it", actual, expected);
267 seatest_simple_test_result(strstr(actual, expected)==0, s,
function, line);
270 void seatest_run_test(
char* fixture,
char* test)
275 void seatest_header_printer(
char* s,
int length,
char f)
278 int d = (length- (l + 2)) / 2;
280 if(seatest_is_display_only() || seatest_machine_readable)
return;
281 for(i = 0; i<d; i++) printf(
"%c",f);
282 if(l==0) printf(
"%c%c", f, f);
283 else printf(
" %s ", s);
284 for(i = (d+l+2); i<length; i++) printf(
"%c",f);
289 void seatest_test_fixture_start(
char* filepath)
291 seatest_current_fixture_path = filepath;
292 seatest_current_fixture = test_file_name(filepath);
293 seatest_header_printer(seatest_current_fixture, seatest_screen_width,
'-');
294 seatest_fixture_tests_failed = sea_tests_failed;
295 seatest_fixture_tests_run = sea_tests_run;
296 seatest_fixture_teardown = 0;
297 seatest_fixture_setup = 0;
300 void seatest_test_fixture_end()
302 char s[SEATEST_PRINT_BUFFER_SIZE];
303 sprintf(s,
"%d run %d failed", sea_tests_run-seatest_fixture_tests_run, sea_tests_failed-seatest_fixture_tests_failed);
304 seatest_header_printer(s, seatest_screen_width,
' ');
308 static char* seatest_fixture_filter = 0;
309 static char* seatest_test_filter = 0;
311 void fixture_filter(
char* filter)
313 seatest_fixture_filter = filter;
317 void test_filter(
char* filter)
319 seatest_test_filter = filter;
322 void set_magic_marker(
char* marker)
324 if(marker == NULL)
return;
325 strcpy(seatest_magic_marker, marker);
328 void seatest_display_test(
char* fixture_name,
char* test_name)
330 if(test_name == NULL)
return;
331 printf(
"%s,%s\r\n", fixture_name, test_name);
334 int seatest_should_run(
char* fixture,
char* test)
337 if(seatest_fixture_filter)
339 if(strncmp(seatest_fixture_filter, fixture, strlen(seatest_fixture_filter)) != 0) run = 0;
341 if(seatest_test_filter && test != NULL)
343 if(strncmp(seatest_test_filter, test, strlen(seatest_test_filter)) != 0) run = 0;
346 if(run && seatest_display_only)
348 seatest_display_test(fixture, test);
354 int run_tests(seatest_void_void tests)
356 unsigned long long end;
357 unsigned long long start = GetTickCount();
361 end = GetTickCount();
363 if(seatest_is_display_only() || seatest_machine_readable)
return 1;
364 sprintf(version,
"SEATEST v%s", SEATEST_VERSION);
366 seatest_header_printer(version, seatest_screen_width,
'=');
368 if (sea_tests_failed > 0) {
369 seatest_header_printer(
"Failed", seatest_screen_width,
' ');
372 seatest_header_printer(
"ALL TESTS PASSED", seatest_screen_width,
' ');
374 sprintf(s,
"%d tests run", sea_tests_run);
375 seatest_header_printer(s, seatest_screen_width,
' ');
376 sprintf(s,
"in %llu micro-s",end - start);
377 seatest_header_printer(s, seatest_screen_width,
' ');
379 seatest_header_printer(
"", seatest_screen_width,
'=');
381 return sea_tests_failed == 0;
385 void seatest_show_help(
void )
387 printf(
"Usage: [-t <testname>] [-f <fixturename>] [-d] [help] [-v] [-m] [-k <marker>\r\n");
388 printf(
"Flags:\r\n");
389 printf(
"\thelp:\twill display this help\r\n");
390 printf(
"\t-t:\twill only run tests that match <testname>\r\n");
391 printf(
"\t-f:\twill only run fixtures that match <fixturename>\r\n");
392 printf(
"\t-d:\twill just display test names and fixtures without\r\n");
393 printf(
"\t-d:\trunning the test\r\n");
394 printf(
"\t-v:\twill print a more verbose version of the test run\r\n");
395 printf(
"\t-m:\twill print a machine readable format of the test run, ie :- \r\n");
396 printf(
"\t \t<textfixture>,<testname>,<linenumber>,<testresult><EOL>\r\n");
397 printf(
"\t-k:\twill prepend <marker> before machine readable output \r\n");
398 printf(
"\t \t<marker> cannot start with a '-'\r\n");
404 if(!((arg+1) < runner->argc))
return 0;
405 if(runner->argv[arg+1][0]==
'-')
return 0;
409 int seatest_parse_commandline_option_with_value(
seatest_testrunner_t* runner,
int arg,
char* option, seatest_void_string setter)
411 if(seatest_is_string_equal_i(runner->argv[arg], option))
413 if(!seatest_commandline_has_value_after(runner, arg))
415 printf(
"Error: The %s option expects to be followed by a value\r\n", option);
416 runner->action = SEATEST_DO_ABORT;
419 setter(runner->argv[arg+1]);
428 for(arg=0; (arg < runner->argc) && (runner->action != SEATEST_DO_ABORT); arg++)
430 if(seatest_is_string_equal_i(runner->argv[arg],
"help"))
433 runner->action = SEATEST_DO_NOTHING;
436 if(seatest_is_string_equal_i(runner->argv[arg],
"-d")) runner->action = SEATEST_DISPLAY_TESTS;
437 if(seatest_is_string_equal_i(runner->argv[arg],
"-v")) seatest_verbose = 1;
438 if(seatest_is_string_equal_i(runner->argv[arg],
"-m")) seatest_machine_readable = 1;
439 if(seatest_parse_commandline_option_with_value(runner,arg,
"-t", test_filter)) arg++;
440 if(seatest_parse_commandline_option_with_value(runner,arg,
"-f", fixture_filter)) arg++;
441 if(seatest_parse_commandline_option_with_value(runner,arg,
"-k", set_magic_marker)) arg++;
447 runner->action = SEATEST_RUN_TESTS;
450 seatest_interpret_commandline(runner);
453 int seatest_testrunner(
int argc,
char** argv, seatest_void_void tests, seatest_void_void setup, seatest_void_void teardown)
456 seatest_testrunner_create(&runner, argc, argv);
457 switch(runner.action)
459 case SEATEST_DISPLAY_TESTS:
461 seatest_display_only = 1;
465 case SEATEST_RUN_TESTS:
468 suite_teardown(teardown);
469 return run_tests(tests);
471 case SEATEST_DO_NOTHING:
472 case SEATEST_DO_ABORT:
481 #ifdef SEATEST_INTERNAL_TESTS
482 void seatest_simple_test_result_nolog(
int passed,
char* reason,
const char*
function,
unsigned int line)
484 sea_test_last_passed = passed;
487 void seatest_assert_last_passed()
489 assert_int_equal(1, sea_test_last_passed);
492 void seatest_assert_last_failed()
494 assert_int_equal(0, sea_test_last_passed);
497 void seatest_disable_logging()
499 seatest_simple_test_result = seatest_simple_test_result_nolog;
502 void seatest_enable_logging()
504 seatest_simple_test_result = seatest_simple_test_result_log;