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//
7
8#include <htils/arena.h>
9#include <htils/basictypes.h>
10
11#define COLOR_GREEN "\x1b[32m"
12#define COLOR_CYAN "\x1b[36m"
13#define COLOR_RED "\x1b[31m"
14#define COLOR_RESET "\x1b[0m"
15
16//
17//
18//
19
22#define HTILS_TEST_ASSERT(cond, msg) \
23 do { \
24 if (!(cond)) { \
25 fprintf(stderr, " %sFAIL%s: %s:%d: %s\n", COLOR_RED, COLOR_RESET, \
26 __FILE__, __LINE__, msg); \
27 return (msg); \
28 } \
29 } while (0)
30
32#define HTILS_TEST(name) static const cstr *htils_test_##name(arena_t *arena)
33
35#define HTILS_TEST_RUN(name) \
36 do { \
37 test_count++; \
38 if (arena == null) { \
39 fprintf(stderr, "No arena found, did you forget to initialize one?\n"); \
40 exit(1); \
41 } \
42 \
43 temp_arena_t temp_arena = temp_arena_new(arena); \
44 fprintf(stderr, "Running test: %s...\n", #name); \
45 const cstr *result = htils_test_##name(temp_arena.arena); \
46 temp_arena_free(temp_arena); \
47 \
48 if (!result) \
49 fprintf(stderr, " %sPASS%s: %s\n", COLOR_GREEN, COLOR_RESET, #name); \
50 else if (result[0] == '@') { \
51 fprintf(stderr, " %sSKIPPED%s: %s\n", COLOR_CYAN, COLOR_RESET, #name); \
52 skips++; \
53 } else \
54 (failures)++; \
55 } while (0)
56
59#define HTILS_TEST_RESULT() \
60 do { \
61 if (failures > 0) { \
62 fprintf(stderr, "\n%u Tests %sFAILED%s.\n", failures, COLOR_RED, \
63 COLOR_RESET); \
64 fprintf(stderr, "%sSKIPPED%s %u tests.\n", COLOR_CYAN, COLOR_RESET, \
65 skips); \
66 return 1; \
67 } \
68 \
69 fprintf(stderr, "\nAll %u tests %sPASSED%s.\n", test_count, COLOR_GREEN, \
70 COLOR_RESET); \
71 fprintf(stderr, "%sSKIPPED%s %u tests.\n", COLOR_CYAN, COLOR_RESET, \
72 skips); \
73 return 0; \
74 } while (0)
75
77#define HTILS_TEST_PASS null
78
80#define HTILS_TEST_SKIP "@"
81
82#endif // !HTILS_TEST_H