esc

Externally Scriptable Editor

git clone git://mccd.space/esc

test_harness.h (1645B)

      1 #ifndef TEST_HARNESS_H
      2 #define TEST_HARNESS_H
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 static int tests_run    = 0;
      7 static int tests_failed = 0;
      8 
      9 #define ASSERT(cond) do { \
     10 	tests_run++; \
     11 	if (!(cond)) { \
     12 		fprintf(stderr, "  FAIL  %s:%d: %s\n", __FILE__, __LINE__, #cond); \
     13 		tests_failed++; \
     14 	} \
     15 } while (0)
     16 
     17 #define ASSERT_EQ_INT(a, b) do { \
     18 	tests_run++; \
     19 	int _a = (a); int _b = (b); \
     20 	if (_a != _b) { \
     21 		fprintf(stderr, "  FAIL  %s:%d: %s==%s (%d!=%d)\n", \
     22 		        __FILE__, __LINE__, #a, #b, _a, _b); \
     23 		tests_failed++; \
     24 	} \
     25 } while (0)
     26 
     27 #define ASSERT_EQ_STR(a, b) do { \
     28 	tests_run++; \
     29 	const char *_a = (a); const char *_b = (b); \
     30 	if (!_a || !_b || strcmp(_a, _b) != 0) { \
     31 		fprintf(stderr, "  FAIL  %s:%d: %s==%s (\"%s\"!=\"%s\")\n", \
     32 		        __FILE__, __LINE__, #a, #b, \
     33 		        _a ? _a : "(null)", _b ? _b : "(null)"); \
     34 		tests_failed++; \
     35 	} \
     36 } while (0)
     37 
     38 #define ASSERT_NULL(p) do { \
     39 	tests_run++; \
     40 	if ((p) != NULL) { \
     41 		fprintf(stderr, "  FAIL  %s:%d: expected NULL: %s\n", \
     42 		        __FILE__, __LINE__, #p); \
     43 		tests_failed++; \
     44 	} \
     45 } while (0)
     46 
     47 #define ASSERT_NOT_NULL(p) do { \
     48 	tests_run++; \
     49 	if ((p) == NULL) { \
     50 		fprintf(stderr, "  FAIL  %s:%d: expected non-NULL: %s\n", \
     51 		        __FILE__, __LINE__, #p); \
     52 		tests_failed++; \
     53 	} \
     54 } while (0)
     55 
     56 #define RUN_SUITE(name) fprintf(stderr, "suite: %s\n", #name)
     57 
     58 #define REPORT_AND_EXIT() do { \
     59 	if (tests_failed == 0) { \
     60 		fprintf(stderr, "OK  %d/%d tests passed\n", tests_run, tests_run); \
     61 		return 0; \
     62 	} else { \
     63 		fprintf(stderr, "FAILED  %d/%d tests failed\n", tests_failed, tests_run); \
     64 		return 1; \
     65 	} \
     66 } while (0)
     67 
     68 #endif