-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.h
106 lines (85 loc) · 4.29 KB
/
test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// https://github.com/ccgargantua/arena-allocator/blob/main/test.h
//
// Tests are contained in testing suite functions, the forware declarations
// of which are bundled together. A testing suite function should exist for
// each function within `arena.h`, and should be placed relative to where
// the function they are testing is placed in regard to the functions around
// it for both forward declarations, implementations, and `SUITE`s.
//
// After implementing a testing suite, you should use the `SUITE` in
// `main()`.
//
// Within the testing suite, the following macros should be used:
//
//
// TEST_EQUAL(a, b) | TEST_EQUAL will fail when a != b
//
// TEST_NULL(a) | TEST_NULL will fail when a != NULL
//
// TEST_NOT_NULL(a) | TEST_NOT_NULL will fail when a == NULL
//
// TEST_ARRAY_EQUAL(a, b, s) | TEST_ARRAY_EQUAL will fail if any elements differ
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
int passed_tests = 0;
int total_tests = 0;
int temp_passed;
int temp_total;
constexpr int BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE];
int buffer_index = 0;
#define REPORT(msg) \
buffer_index += snprintf(buffer + buffer_index, \
(unsigned int)(BUFFER_SIZE - buffer_index), \
" FAILURE: '%s' at %s:%d\n", \
msg, __FILE__, __LINE__)
#define TEST(exp, msg) \
do \
{ \
if (!(exp)) \
{ \
REPORT(msg); \
} \
else \
{ \
passed_tests++; \
} \
total_tests++; \
}while (0)
#define TEST_NULL(a) TEST(a == NULL, #a " is not NULL")
#define TEST_NOT_NULL(a) TEST(a != NULL, #a " is NULL")
#define TEST_EQUAL(a, b) TEST(a == b, #a " does not equal " #b)
#define TEST_TRUE(a) TEST(a, #a " is not true")
#define TEST_FALSE(a) TEST(!a, #a " is not true")
#define TEST_ARRAY_EQUAL(a, b, s) \
do \
{ \
int i; \
total_tests++; \
for (i = 0; i < s; i++) \
{ \
if (a[i] != b[i]) \
{ \
REPORT(#a " does not equal " #b); \
break; \
} \
} \
passed_tests++; \
}while (0)
#define SUITE(suite) \
do \
{ \
temp_passed = passed_tests; \
temp_total = total_tests; \
suite(); \
fprintf(stderr, "Passed %d/%d tests in '%s'\n", passed_tests-temp_passed, total_tests-temp_total, #suite); \
fprintf(stderr, "%s", buffer); \
buffer_index = 0; \
buffer[0] = '\0'; \
} while(0)
#define WRAP_UP() fprintf(stderr, "\nFinished. Passed %d/%d tests.\n", passed_tests, total_tests);
#endif