htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1#ifndef HTILS_TEST_H
2#define HTILS_TEST_H
3
4/***********************************/
5
6#include <htils/arena.h>
7#include <htils/basictypes.h>
8
9/***********************************/
10
12#define COLOR_GREEN "\x1b[32m"
13
15#define COLOR_CYAN "\x1b[36m"
16
18#define COLOR_RED "\x1b[31m"
19
21#define COLOR_RESET "\x1b[0m"
22
24#define HTILS_TEST_ASSERT(cond, msg) \
25 do { \
26 if (!(cond)) { \
27 fprintf(stderr, " %sFAIL%s: %s:%d: %s\n", COLOR_RED, COLOR_RESET, \
28 __FILE__, __LINE__, msg); \
29 return (msg); \
30 } \
31 } while (0)
32
33//
34//
35//
36
44#define HTILS_TEST(name) static const cstr *htils_test_##name(arena_t *arena)
45
46//
47//
48//
49
58#define HTILS_TEST_RUN(name) \
59 do { \
60 test_count++; \
61 if (arena == null) { \
62 fprintf(stderr, "No arena found, did you forget to initialize one?\n"); \
63 exit(1); \
64 } \
65 \
66 temp_arena_t temp_arena = temp_arena_new(arena); \
67 fprintf(stderr, "Running test: %s...\n", #name); \
68 const cstr *result = htils_test_##name(temp_arena.arena); \
69 temp_arena_free(temp_arena); \
70 \
71 if (!result) \
72 fprintf(stderr, " %sPASS%s: %s\n", COLOR_GREEN, COLOR_RESET, #name); \
73 else if (result[0] == '@') { \
74 fprintf(stderr, " %sSKIPPED%s: %s\n", COLOR_CYAN, COLOR_RESET, #name); \
75 skips++; \
76 } else \
77 (failures)++; \
78 } while (0)
79
80//
81//
82//
83
86#define HTILS_TEST_RESULT() \
87 do { \
88 if (failures > 0) { \
89 fprintf(stderr, "\n%u Tests %sFAILED%s.\n", failures, COLOR_RED, \
90 COLOR_RESET); \
91 fprintf(stderr, "%sSKIPPED%s %u tests.\n", COLOR_CYAN, COLOR_RESET, \
92 skips); \
93 return 1; \
94 } \
95 \
96 fprintf(stderr, "\nAll %u tests %sPASSED%s.\n", test_count, COLOR_GREEN, \
97 COLOR_RESET); \
98 fprintf(stderr, "%sSKIPPED%s %u tests.\n", COLOR_CYAN, COLOR_RESET, \
99 skips); \
100 return 0; \
101 } while (0)
102
103//
104//
105//
106
108#define HTILS_TEST_PASS null
109
110//
111//
112//
113
115#define HTILS_TEST_SKIP "@"
116
117#endif // !HTILS_TEST_H