-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvktest.h
157 lines (141 loc) · 4.06 KB
/
vktest.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* VK-Test is a tiny header-only test framework for C
* Please note that this file should only be included in one file,
* and this test should not be used for multi-thread case
*/
#ifndef VKTEST_H
#define VKTEST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
static unsigned __g_passing_tests, __g_all_tests;
static unsigned __g_passing_tests_section, __g_all_tests_section;
void __vk_maybe_terminate() {
#ifdef VK_TERMINATE_ON_FAIL
fprintf(stderr, "VK_TERMINATE_ON_FAIL was set, terminating test...\n");
fprintf(stderr, "Test terminated, %u of %u passing.\n",
__g_passing_tests_section, __g_all_tests_section);
abort();
#endif
}
#define VK_TEST_BEGIN fprintf(stderr, "VKTest starting...\n");
#define VK_TEST_END \
fprintf(stderr, "All test finished, %u of %u passing\n",\
__g_passing_tests, __g_all_tests);
#define VK_TEST_SECTION_BEGIN(section_name) \
{ \
fprintf(stderr, "Running test: %s...\n", section_name); \
__g_passing_tests_section = 0; \
__g_all_tests_section = 0; \
}
#define VK_TEST_SECTION_END(section_name) \
{ \
fprintf(stderr, "Test %s finished, %u of %u passing.\n", \
section_name, \
__g_passing_tests_section, __g_all_tests_section); \
}
#define VK_ASSERT(condition) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing Assert(%s)... ", __g_all_tests_section, \
#condition); \
if (condition) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#define VK_ASSERT_NOT(condition) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing AssertNot(%s)... ", __g_all_tests_section, \
#condition); \
if (!condition) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#define VK_ASSERT_EQUALS(standard, expr) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing AssertEquals(%s, %s)... ", \
__g_all_tests_section, \
#standard, #expr); \
if ((standard) == (expr)) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#define VK_ASSERT_EQUALS_S(standard, expr) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing AssertEquals(\"%s\", \"%s\")... ",\
__g_all_tests_section, \
standard, expr); \
if (!strcmp(standard, expr)) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#define VK_EPSILON 1e-5
#define VK_ASSERT_EQUALS_F(standard, expr) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing AssertEquals(%s, %s)... ", \
__g_all_tests_section, \
#standard, #expr); \
if (fabs((standard) - (expr)) <= VK_EPSILON) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#define VK_ASSERT_NOT_EQUALS(standard, expr) \
{ \
__g_all_tests++; \
__g_all_tests_section++; \
fprintf(stderr, "[%u] Testing AssertNotEquals(%s, %s)... ", \
__g_all_tests_section, \
#standard, #expr); \
if ((standard) != (expr)) { \
fprintf(stderr, "Passing\n"); \
__g_passing_tests++; \
__g_passing_tests_section++; \
} \
else { \
fprintf(stderr, "Failing\n"); \
__vk_maybe_terminate(); \
} \
}
#else
#error Multiple inclusion for vktest?
#endif