-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros_test.cpp
81 lines (62 loc) · 1.82 KB
/
macros_test.cpp
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
#include "macros.h"
#if defined(MACROS_TEST_MAIN)
struct testStruct {
int x;
int y;
};
CREATE_TEST_CASE("Macros test basic") {
{
int counter = 0;
for (int i = 0; i < 10; i++) {
BNS_DO_EXACTLY_ONCE() {
counter++;
}
}
ASSERT(counter == 1);
}
{
// Ideally, these would be static asserts, but clang trips up on them...:\...
ASSERT(BNS_OFFSET_OF(testStruct, x) == 0);
//possible packing issue?
ASSERT(BNS_OFFSET_OF(testStruct, y) == 4);
}
{
int arr[3] = { 1, 2, 3 };
static_assert(BNS_ARRAY_COUNT(arr) == 3, "check BNS_ARRAY_COUNT macro");
}
{
int arr_23 = 0;
BNS_GLUE_TOKS(arr, _23) = 5;
BNS_GLUE_TOKS(arr_, 23) = 6;
// No need to assert or anything. This will fail to compile if there's an issue
}
{
int arr1[3] = { 100, 1033, -23 };
int arr2[3] = {};
BNS_MEMCPY(arr2, arr1, sizeof(arr1));
for (int i = 0; i < 3; i++) {
ASSERT(arr1[i] == arr2[i]);
}
ASSERT(arr1[0] == 100);
ASSERT(arr1[1] == 1033);
ASSERT(arr1[2] == -23);
}
static_assert(BNS_MIN(2, 3) == 2, "check BNS_MIN macro");
static_assert(BNS_MAX(2, 3) == 3, "check BNS_MIN macro");
static_assert(BNS_ABS(3) == 3, "check BNS_ABS macro");
static_assert(BNS_ABS(-3) == 3, "check BNS_ABS macro");
static_assert(BNS_ABS(0) == 0, "check BNS_ABS macro");
static_assert(BNS_SQR(0) == 0, "check BNS_SQR macro");
static_assert(BNS_SQR(1) == 1, "check BNS_SQR macro");
static_assert(BNS_SQR(-1) == 1, "check BNS_SQR macro");
static_assert(BNS_SQR(2) == 4, "check BNS_SQR macro");
static_assert(BNS_SQR(-2) == 4, "check BNS_SQR macro");
static_assert(BNS_SQR(-6) == 36, "check BNS_SQR macro");
static_assert(BNS_SQR(6) == 36, "check BNS_SQR macro");
{
ASSERT(StrEqual(BNS_STRINGIFY(the), "the"));
static_assert(sizeof(BNS_STRINGIFY(the)) == 4, "check BNS_STRINGIFY macro");
}
return 0;
}
#endif