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