-
Notifications
You must be signed in to change notification settings - Fork 1
/
unittest.c
128 lines (113 loc) · 3.46 KB
/
unittest.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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "utils.h"
#include "unittest.h"
void is_passing(bool pass, uint32_t i, uint32_t n) {
print("|", white);
printint(i);
print("/", white);
printint(n);
print("|", white);
if (pass == true) {
printlnc(" pass", green);
} else {
printlnc(" fail", red);
}
}
void unittest_finalize(unittest* module) {
enum PrintColors whites = bright_white;
enum PrintColors reds = bright_red;
enum PrintColors yellows = bright_yellow;
enum PrintColors greens = bright_green;
print(" |", bright_cyan);
print(" tests (", whites);
if (module -> fails == 0) {
printintc(module -> passes, greens);
print("/", whites);
printintc(module -> count, greens);
print(") ", whites);
printlnc("all passed", greens);
} else if ((module -> fails != 0) && (module -> passes != 0)) {
printintc(module -> passes, yellows);
print("/", whites);
printintc(module -> count, yellows);
print(") ", whites);
print("passed", yellows);
print(" and ", whites);
print("(", whites);
printintc(module -> fails, reds);
print("/", whites);
printintc(module -> count, reds);
print(") ", whites);
printlnc("failed", reds);
print(" | fails: ", whites);
for (uint64_t i = 0; i < module -> count; i++) {
if (module -> tests[i] == 1) {
printintc(i + 1, yellows);
print(" ", whites);
}
}
println("");
} else if (module -> passes == 0) {
printintc(module -> passes, reds);
print("/", whites);
printintc(module -> count, reds);
print(") ", whites);
printlnc("passes (all failed)", reds);
}
}
void unittest_reset(unittest* module) {
module -> count = 0;
module -> passes = 0;
module -> fails = 0;
for (uint64_t i = 0; i < module -> total_tests; i++) {
module -> tests[i] = 0;
}
}
void unittest_clock(unittest* module) {
print("|", bright_cyan);
print(" time: ", bright_white);
printf("%f", (float)(clock() - (module -> clock_start)) / CLOCKS_PER_SEC);
}
void unittest_index(unittest* module, char* index) {
module -> index_count += 1;
print("[", bright_white);
printintc(module -> index_count, bright_blue);
print("]", bright_white);
print(" ", white);
print(index, white);
print(" ", white);
unittest_clock(module);
unittest_finalize(module);
unittest_reset(module);
module -> clock_start = clock();
}
void unittest_update(unittest* module, bool pass_or_fail) {
module -> passes += (pass_or_fail == true) ? 1 : 0;
module -> fails += (pass_or_fail == false) ? 1 : 0;
module -> tests[module -> count] = (pass_or_fail == false) ? 1: 0;
module -> count += 1;
}
void unittest_free(unittest* module) {
free(module -> tests);
}
unittest malloc_UnitTest_Module(uint64_t total_tests) {
unittest module = {
.index_count = 0,
.count = 0,
.total_tests = total_tests,
.passes = 0,
.fails = 0,
.clock_start = clock()
};
module.tests = (uint8_t*)malloc(total_tests * sizeof(uint8_t));
module.reset = unittest_reset;
module.update = unittest_update;
module.finalize = unittest_finalize;
module.free = unittest_free;
module.index = unittest_index;
return module;
}