-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf_unit_test.c
More file actions
296 lines (221 loc) · 7.55 KB
/
cf_unit_test.c
File metadata and controls
296 lines (221 loc) · 7.55 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** cf_unit_test.c
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cf.h"
#include "cf_unit_test.h"
/* STATIC FUNCTIONS */
/* EXTERN FUNCTIONS */
void init_unit_test_rand_data(cf00_unit_test_rand_data *rd,
const uint32 rand_seed)
{
if (NULL != rd)
{
rd->m_w = rand_seed;
rd->m_z = 0xFFFFFFFF ^ rand_seed;
}
}
uint32 advance_unit_test_rand_data(cf00_unit_test_rand_data *rd)
{
uint32 result = 0;
if (NULL != rd)
{
/* bassed on multiply-with-carry formula, by George Marsaglia,
http://en.wikipedia.org/wiki/Random_number_generation
https://groups.google.com/forum/#!topic/sci.crypt/yoaCpGWKEk0 */
if (0 == rd->m_w || 0x464FFFFF == rd->m_w)
{
++(rd->m_w);
}
if (0 == rd->m_z || 0x9068FFFF == rd->m_z)
{
++(rd->m_z);
}
rd->m_z = 36969 * ((rd->m_z) & 65535) + ((rd->m_z) >> 16);
rd->m_w = 18000 * ((rd->m_w) & 65535) + ((rd->m_w) >> 16);
result = ((rd->m_z) << 16) + rd->m_w;
}
return result;
}
#if 0
const struct cf00_unit_test_suite *m_unit_test_suite;
cf00_indiv_unit_test_data *m_indiv_test_data; /* array */
uint32 m_random_seed; /* test suite random seed */
uint32 m_current_random_seed; /* current iteration start random seed */
cf00_unit_test_rand_data m_rng; /* pseudo-random number generator */
// temp variables type *, name
uint32 m_total_assertion_count;
uint32 m_total_error_count;
uint32 m_current_unit_test_idx;
uint32 m_current_iteration_idx;
uint32 m_total_iteration_count;
// callback function for logging
} cf00_unit_test_data;
void cf00_init_unit_test_data(cf00_unit_test_data *d,
const cf00_unit_test_suite *test_suite, const uint32 rand_seed);
void cf00_free_unit_test_data(cf00_unit_test_data *d);
#endif
void cf00_init_unit_test_data(cf00_unit_test_data *d,
const cf00_unit_test_suite *test_suite, const uint32 rand_seed,
const uint32 iteration_count)
{
size_t i;
cf00_indiv_unit_test_data *indiv_unit_test_data;
if (NULL != d)
{
memset(d, 0, sizeof(*d));
d->m_unit_test_suite = test_suite;
d->m_indiv_test_data = (cf00_indiv_unit_test_data *)malloc(
(test_suite->m_test_count) * sizeof(cf00_indiv_unit_test_data));
indiv_unit_test_data = d->m_indiv_test_data;
for (i = 0; i < test_suite->m_test_count; ++i, ++indiv_unit_test_data)
{
indiv_unit_test_data->m_unit_test_idx = i;
indiv_unit_test_data->m_assertion_count = 0;
indiv_unit_test_data->m_error_count = 0;
indiv_unit_test_data->m_elapsed_time = 0;
}
d->m_random_seed = rand_seed;
d->m_current_random_seed = rand_seed;
init_unit_test_rand_data(&(d->m_rng), rand_seed);
d->m_total_assertion_count = 0;
d->m_total_error_count = 0;
d->m_total_elapsed_time = 0;
d->m_current_unit_test_idx;
d->m_current_iteration_idx;
d->m_total_iteration_count = iteration_count;
}
}
void cf00_free_unit_test_data(cf00_unit_test_data *d)
{
if (NULL != d)
{
free(d->m_indiv_test_data);
}
}
uint32 *cf00_test_allocate_temp_uint32(const char *var_name)
{
return 0;
}
void cf00_test_assert(cf00_unit_test_data *d, const boolean condition,
const char *condition_str, const char *file_name, const int line_number)
{
/* increment d assertion count */
if (NULL != d)
{
++(d->m_total_assertion_count);
++((d->m_indiv_test_data)[
d->m_current_unit_test_idx].m_assertion_count);
}
if (!condition)
{
/* increment d assertion failure count */
if (NULL != d)
{
++(d->m_total_error_count);
++((d->m_indiv_test_data)[
d->m_current_unit_test_idx].m_error_count);
}
/* log error */
printf("ASSERTION FAILED %s [%i]: %s\n", file_name, line_number,
condition_str);
}
}
uint32 cf00_test_rand_uint32(cf00_unit_test_data *d)
{
return 0;
}
boolean cf00_test_rand_boolean(cf00_unit_test_data *d)
{
return (boolean)0;
}
float64 cf00_test_rand_float64(cf00_unit_test_data *d)
{
return 0.0;
}
void cf00_test_rand_str_buf(cf00_unit_test_data *d, char *str_buf,
const size_t len)
{
}
void cf00_test_rand_str_buf_abc123(cf00_unit_test_data *d, char *str_buf,
const size_t len)
{
}
void cf00_test_rand_str_buf_subset(cf00_unit_test_data *d, const char *superset,
const size_t superset_len, char *str_buf, const size_t len)
{
}
int cf00_run_unit_test_suite_main(int argc, char *argv[],
const cf00_unit_test_suite *unit_test_suite)
{
int result = EXIT_SUCCESS;
uint32 random_seed = 0;
uint32 iteration_count = 1;
/* parse random seed and iteration count from argc and argv */
result = cf00_run_unit_test_suite(unit_test_suite, random_seed,
iteration_count);
return result;
}
int cf00_run_unit_test_suite(const cf00_unit_test_suite *unit_test_suite,
const uint32 random_seed, const uint32 iteration_count)
{
/*
global random seed = random_seed
iteration random seed = updated at start of each iteration
current random number = updated every request for random number
need the ability to quickly repeat any unit test given a random seed
*/
time_t start_time;
time_t stop_time;
time_t elapsed_time;
cf00_unit_test_data d;
const cf00_unit_test *current_unit_test = NULL;
cf00_indiv_unit_test_data *indiv_unit_test_data = NULL;
printf("RUN TEST SUITE START\n");
if (NULL != unit_test_suite->m_suite_name)
{
printf("%s\n", unit_test_suite->m_suite_name);
}
printf("TEST_SUITE_RANDOM_SEED=%i\n", (int)random_seed);
printf("ITERATION_COUNT=%i\n", (int)iteration_count);
cf00_init_unit_test_data(&d, unit_test_suite, random_seed, iteration_count);
indiv_unit_test_data = d.m_indiv_test_data;
for (d.m_current_unit_test_idx = 0;
d.m_current_unit_test_idx < unit_test_suite->m_test_count;
++d.m_current_unit_test_idx, ++indiv_unit_test_data)
{
current_unit_test =
&((unit_test_suite->m_tests)[d.m_current_unit_test_idx]);
/* record start time */
start_time = time(NULL);
for (d.m_current_iteration_idx = 0;
d.m_current_iteration_idx < iteration_count;
++d.m_current_iteration_idx)
{
/* clear temp variables */
/* cf00_reset_unit_test_rand_gen(), but do not reset
global random seed or any other parameters */
/* run one iteration */
(*(current_unit_test->m_func))(&d);
}
/* record stop time */
stop_time = time(NULL);
/* record time for current unit test */
indiv_unit_test_data->m_elapsed_time = stop_time - start_time;
d.m_total_elapsed_time += indiv_unit_test_data->m_elapsed_time;
/* report unit test results */
printf("TEST:%s ASSERTIONS:%i ERRORS:%i TIME:%i\n",
current_unit_test->m_func_name,
(int)(indiv_unit_test_data->m_assertion_count),
(int)(indiv_unit_test_data->m_error_count),
(int)(indiv_unit_test_data->m_elapsed_time));
}
printf("RUN TEST SUITE END\n");
/* print summary */
// deconstruct unit test data
cf00_free_unit_test_data(&d);
return EXIT_SUCCESS; /* return total error count */
}