-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunic.h
443 lines (368 loc) · 11.6 KB
/
tunic.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
Copyright (C) 2018 RW Bunney, N Pritchard
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
/*Code initially developed from http://www.jera.com/techinfo/jtns/jtn002.html*/
#ifndef TUNIC_H
#define TUNIC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
Define TIME_TESTS in your test file if you want
to use the timing functions
*/
#ifdef TIME_TESTS
#include <time.h>
#endif
/*
PUBLIC VARIABLES FOR USER
Defining TRUE and FALSE makes things easier, and
means we do not need to use booleans
*/
#define TRUE 1
#define FALSE 0
enum tunic_output_level{
STD_OUTPUT,
TIME
};
/*
TESTING API
All public API functions have tunic in lower case followed by capitalised
functionality (ASSERT, SET etc.) Functions with lower case tunic_
are helper functions within the header file.
*/
/*
INTEGER
*/
void tunic_ASSERT_int(int assert, int a, int b);
void tunic_ALMOST_int(int assert, int a, int b, int tolerance);
void tunic_ASSERT_int_array(int assert, const int *a, const int *b, unsigned long n);
void tunic_ALMOST_int_array(int assert, const int *a, const int *b, unsigned long n, int tolerance);
/*
FLOAT
*/
void tunic_ASSERT_float(int assert, float a, float b);
void tunic_ALMOST_float(int assert, float a, float b, float tolerance);
void tunic_ASSERT_float_array(int assert, const float *a, const float *b, unsigned long n);
void tunic_ALMOST_float_array(int assert, const float *a, const float *b, unsigned long n, float tolerance);
/*
DOUBLE
*/
void tunic_ASSERT_double(int assert, double a, double b);
void tunic_ALMOST_double(int assert, double a, double b, double tolerance);
void tunic_ASSERT_double_array(int assert, const double *a, const double *b, unsigned long n);
/*
BOOLEAN
*/
void tunic_ASSERT_bool(int assert, int a, int b);
void tunic_ASSERT_bool_array(int assert, const int *a, const int *b, unsigned int n);
/*
The end of the header file!
*/
#endif //tunic_H
/*
IMPLEMENTATION
*/
#ifdef TUNIC_LIBRARY
int test_status;
int tests_run;
int tests_passed;
float tunic_fAccuracy = 1e-7;
double tunic_dAccuracy = 1e-15;
#ifdef TIME_TESTS
clock_t start_t;
#endif
/*
Private helper functions
*/
void tunic_update_test_status(int test_status);
void tunic_init(void);
void tunic_close(void);
int tunic_abs_int(int a);
float tunic_abs_float(float a);
double tunic_abs_double(double a);
long double tunic_abs_ldouble(long double a);
/*
===============================================================================
TEST-RUNNER MACROS
===============================================================================
*/
/*
tunic_run_test_suite:
Runs a test suite, which is simply a function with no return
value that separates different 'suites' of tests (based on the
users preferred logical/functional separation). Accepts optional
output assertition that affects how test results are displayed
to the user [TODO].
*/
#define tunic_run_test_suite(test_suite_name, tunic_output_level) do {\
printf("Running tests for: '%s'\n", #test_suite_name); \
tunic_init(); \
if(tunic_output_level == TIME){ \
test_suite_name(); \
} else{\
test_suite_name(); \
} \
tunic_close();\
printf("Finished test suite\n\n"); \
} while (0)
/*
===============================================================================
INTEGER
===============================================================================
*/
void tunic_ASSERT_int(int assert, int a, int b) {
if (((a == b) && (assert == TRUE)) || ((a != b) && (assert == FALSE))) {
tests_passed++;
test_status = 1;
}
else{
test_status = 0;
}
tunic_update_test_status(test_status);
}
void tunic_ALMOST_int(int assert, int a, int b, int tolerance){
test_status = 1;
if(assert == TRUE){
if(tunic_abs_int(a-b) > tunic_abs_int(tolerance)){
test_status = 0;
} else {
tests_passed++;
}
} else {
if(tunic_abs_int(a-b) >= tunic_abs_int(tolerance)){
test_status = 0;
tests_passed++;
}
}
tunic_update_test_status(test_status);
}
/*
Element wise check due to pointer depreciation of arrays
when passed as a parameter to a function.
*/
void tunic_ASSERT_int_array(int assert, const int *a, const int *b, unsigned long n) {
int i, result = 1;
for (i = 0; i < n; ++i) {
if (a[i] != b[i]) {
result = 0;
break;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
//TODO: handle negative tolerance case
void tunic_ALMOST_int_array(int assert, const int *a, const int *b, unsigned long n, int tolerance){
int i, result = 1;
for (i = 0; i < n; ++i) {
if (tunic_abs_int(a[i])-tunic_abs_int(b[i]) > tolerance) {
result = 0;
break;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
/*
===============================================================================
FLOAT
===============================================================================
*/
void tunic_ASSERT_float(int assert, float a, float b) {
if (((tunic_abs_float(a - b) <= tunic_fAccuracy) && (assert == TRUE)) ||
((tunic_abs_float(a - b) > tunic_fAccuracy) && (assert == FALSE))) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
void tunic_ALMOST_float(int assert, float a, float b, float tolerance){
test_status = 1;
if(tunic_abs_float(a-b) > tunic_abs_float(tolerance)){
test_status = 0;
}
if((test_status == 1 && assert == TRUE) || (test_status == 0 && assert == FALSE)){
tests_passed++;
}
tunic_update_test_status(test_status);
}
void tunic_ASSERT_float_array(int assert, const float *a, const float *b, unsigned long n) {
//Moving away from memcmp test for floating point values
int i, result = 1;
for (i = 0; i < n; ++i) {
if (tunic_abs_float(a[i] - b[i]) > tunic_fAccuracy) {
result = 0;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
void tunic_ALMOST_float_array(int assert, const float *a, const float *b, unsigned long n, float tolerance) {
//Moving away from memcmp test for floating point values
int i, result = 1;
for (i = 0; i < n; ++i) {
if (tunic_abs_float(a[i] - b[i]) > tunic_abs_float(tolerance)) {
result = 0;
break;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
tunic_update_test_status(test_status);
}
}
/*
===============================================================================
DOUBLE
===============================================================================
*/
void tunic_ASSERT_double(int assert, double a, double b){
if (((tunic_abs_double(a - b) <= tunic_dAccuracy) && (assert == TRUE)) ||
((tunic_abs_double(a - b) > tunic_dAccuracy) && (assert == FALSE))) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
void tunic_ALMOST_double(int assert, double a, double b, double tolerance){
test_status = 1;
if(assert == TRUE){ //Looking for difference > tolerance to fail
if(tunic_abs_double(a-b) > tunic_abs_double(tolerance)){
test_status = 0;
}
} else { // Looking for the opposite
if(tunic_abs_double(a-b) < tunic_abs_double(tolerance)){
test_status = 0;
}
}
if((test_status == 1 && assert == TRUE) || (test_status == 0 && assert == FALSE)){
tests_passed++;
}
tunic_update_test_status(test_status);
}
void tunic_ASSERT_double_array(int assert, const double *a, const double *b, unsigned long n) {
int i, result = 1;
for (i = 0; i < n; ++i) {
if (tunic_abs_double(a[i] - b[i]) > tunic_dAccuracy) {
result = 0;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
void tunic_ALMOST_double_array(int assert, const double *a, const double *b, unsigned long n, double tolerance){
int i, result = 1;
for (i = 0; i < n; ++i) {
if (tunic_abs_double(a[i] - b[i]) > tolerance) {
result = 0;
}
}
if ((result == 1 && assert == TRUE) || (result == 0 && assert == FALSE)) {
tests_passed++;
test_status = 1;
} else {
test_status = 0;
}
tunic_update_test_status(test_status);
}
/*
===============================================================================
BOOLEANS
===============================================================================
*/
void tunic_ASSERT_bool(int assert, int a, int b){
tunic_ASSERT_int(assert, a, b);
}
void tunic_ASSERT_bool_array(int assert, const int *a, const int *b, unsigned int n){
tunic_ASSERT_int_array(assert, a, b, n);
}
/*
===============================================================================
TIME
===============================================================================
*/
#ifdef TIME_TESTS
void tunic_start_timer(void){
start_t = clock();
}
void tunic_stop_timer(){
clock_t end_t;
double seconds;
end_t = clock();
seconds = (double)(end_t - start_t)/1000000.f;
printf("CPU time is %f seconds\n", seconds);
}
#endif //TIME_TESTS
/*
===============================================================================
PRIVATE FUNCTIONS
===============================================================================
*/
void tunic_init(void){
tests_run = 0;
tests_passed = 0;
}
void tunic_close(void){
printf("Passed %d out of %d tests\n", tests_passed, tests_run);
}
void tunic_update_test_status(int status){
tests_run++;
if(test_status > 0){
printf("Test %d passed\n", tests_run);
}
else{
printf("Test %d failed\n", tests_run);
}
}
int tunic_abs_int(int a){ //Sneaky bit-hax method
int t = a >> ((sizeof(int)*8)-1);
return t ^ (a+t);
}
float tunic_abs_float(float a){ //Wasteful, heathen but simple implementation
return a<0?-a:a;
}
double tunic_abs_double(double a){
return a<0?-a:a;
}
long double tunic_abs_ldouble(long double a){
return a<0?-a:a;
}
#endif //TUNIC_LIBRARY