-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
183 lines (156 loc) · 3.95 KB
/
test.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright 2012-2018 Dustin M. DeWeese
This file is part of the Startle library.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include "startle/types.h"
#include "startle/macros.h"
#include "startle/support.h"
#include "startle/error.h"
#include "startle/test.h"
#include "startle/log.h"
/** @file
* @brief Unit testing
*/
typedef struct test_entry {
char *name;
int (*run)();
} test_entry_t;
#define TEST__ITEM(name) extern int test_##name();
#include "test_list.h"
#undef TEST__ITEM
#define TEST__ITEM(_name) \
{ \
.name = #_name, \
.run = &test_##_name \
},
static test_entry_t tests[] = {
#include "test_list.h"
};
#undef TEST__ITEM
/** Run all tests matching the name. */
int run_test(seg_t name) {
int fail = 0;
FOREACH(i, tests) {
test_entry_t *entry = &tests[i];
if(strncmp(entry->name, name.s, name.n) == 0) {
printf("@ %s\n", entry->name);
int result = entry->run();
printf("%s => %d\n", entry->name, result);
if(result && !fail) fail = result;
}
}
return fail;
}
// Macro tests
TEST(loops) {
/** [loops] */
COUNTUP(i, 3) {
printf("up: %d\n", (unsigned int)i);
}
COUNTDOWN(i, 3) {
printf("down: %d\n", (unsigned int)i);
}
COUNTUP(i, 0) {
printf("down: shouldn't print this\n");
}
COUNTDOWN(i, 0) {
printf("down: shouldn't print this\n");
}
unsigned int arr[] = {1, 4, 9};
FOREACH(i, arr) {
printf("arr[%d] = %d\n", (unsigned int)i, arr[i]);
}
LOOP(3) {
LOOP(3) {
putchar('x');
}
putchar('\n');
}
RANGEUP(i, 3, 7) {
printf("range up: i = %d, REVI(i) = %d\n", (int)i, (int)REVI(i));
}
RANGEDOWN(i, 3, 7) {
printf("range down: i = %d, REVI(i) = %d\n", (int)i, (int)REVI(i));
}
/** [loops] */
return 0;
}
TEST(formask) {
unsigned int mask = 0x11af;
unsigned int prev_mask = (mask << 1) + 1;
FORMASK(i, j, 0x11af) {
printf("%d, %d\n", (int)i, (int)j);
if((prev_mask - (__mask << (__z + 1))) != 1) return -1;
prev_mask = __mask;
}
return 0;
}
TEST(next_bit) {
uintptr_t mask = 0x11af, m = mask;
while(m) {
int x = next_bit(&m);
if(x < 0) return -1;
mask &= ~(1 << x);
printf("bit = %d\n", x);
}
return mask ? -2 : 0;
}
/** [macro_dispatch] */
#define TEST_0() printf("TEST_0()\n")
#define TEST_1(x0) printf("TEST_1(" x0 ")\n")
#define TEST_2(x0, x1) printf("TEST_2(" x0 ", " x1 ")\n")
TEST(macro_dispatch) {
DISPATCH(TEST);
DISPATCH(TEST, "1");
DISPATCH(TEST, "1", "2");
return 0;
}
/** [macro_dispatch] */
TEST(inrange) {
if(!INRANGE(1, -3, 3)) return -1;
if(!INRANGE(12, -3, 3, 10, 20)) return -2;
if(!INRANGE(42, -3, 3, 10, 20, 40, 100)) return -3;
return 0;
}
TEST(oneof) {
if(!ONEOF(1, 1)) return -1;
if(!ONEOF(2, 1, 2)) return -2;
if(!ONEOF(3, 1, 2, 3)) return -3;
return 0;
}
static int shadow_x = 1;
TEST(shadow) {
if(shadow_x != 1) return -1;
SHADOW(shadow_x, 2) {
if(shadow_x != 2) return -2;
SHADOW(shadow_x, 3) {
if(shadow_x != 3) return -3;
}
if(shadow_x != 2) return -4;
}
if(shadow_x != 1) return -5;
return 0;
}
TEST(macro_math) {
if(min(3, 4) != 3) return -1;
if(max(3, 4) != 4) return -2;
if(DIV_UP(4, 3) != 2) return -3;
if(DIV_UP(3, 3) != 1) return -4;
if(csub(3, 4) != 0) return -5;
if(csub(4, 3) != 1) return -6;
if(SNAP_UP(1, 3) != 3) return -7;
if(SNAP_UP(3, 3) != 3) return -8;
return 0;
}