-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcunit.c
More file actions
157 lines (115 loc) · 2.44 KB
/
Copy pathcunit.c
File metadata and controls
157 lines (115 loc) · 2.44 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
#include "cunit.h"
#include <stdlib.h>
#include <intrin.h>
#define is ==
#define null NULL
static bool RunTest(Test test, FILE* stream);
static Test CreateTest(char* name, bool(*Method)(FILE*));
static bool RunSuite(TestSuite suite);
static void Append(TestSuite suite, char* name, bool(*method)(FILE*));
static void Dispose(TestSuite suite);
TestSuite CreateSuite(char* name)
{
if (name is null)
{
fprintf(stderr, "parameter: name, must be specified");
__debugbreak();
}
TestSuite suite = malloc(sizeof(struct testSuite));
if (suite is null)
{
__debugbreak();
}
suite->Name = name;
suite->Count = 0;
suite->OutputStream = stdout;
suite->Head = suite->Tail = null;
suite->Dispose = &Dispose;
suite->Append = &Append;
suite->Run = &RunSuite;
return suite;
}
static bool RunSuite(TestSuite suite)
{
if (suite is null)
{
__debugbreak();
}
if (suite->Name is null)
{
__debugbreak();
}
fprintf(suite->OutputStream, SuiteStartFormat, suite->Name, suite->Count);
Test head = suite->Head;
size_t passCount = 0;
Benchmark(
while (head != null)
{
fprintf(suite->OutputStream, "[%s]\n", head->Name);
if (RunTest(head, suite->OutputStream))
{
++passCount;
}
head = head->Next;
}
fprintf(suite->OutputStream, SuiteEndFormat, suite->Name, passCount, suite->Count - passCount);
, suite->OutputStream);
return passCount >= suite->Count;
}
static void Append(TestSuite suite, char* name, bool(*method)(FILE*))
{
if (suite is null)
{
__debugbreak();
}
Test newTest = CreateTest(name, method);
++(suite->Count);
if (suite->Head is null)
{
suite->Head = suite->Tail = newTest;
return;
}
suite->Tail->Next = newTest;
suite->Tail = newTest;
}
static void Dispose(TestSuite suite)
{
if (suite is null)
{
__debugbreak();
}
Test head = suite->Head;
while (head != null)
{
Test tmp = head;
#pragma warning(disable : 6001)
head = head->Next;
#pragma warning(default : 6001)
free(tmp);
}
free(suite);
}
static bool RunTest(Test test, FILE* stream)
{
bool pass = false;
Benchmark(
pass = test->Method(stream);
fprintf(stream, "[%s][%s] ", pass ? "PASS" : "FAIL", test->Name);
, stream);
fputc('\n', stream);
return pass;
}
static Test CreateTest(char* name, bool(*Method)(FILE*))
{
Test newTest = malloc(sizeof(struct _test));
if (newTest is null)
{
__debugbreak();
}
newTest->Name = name;
newTest->Method = Method;
newTest->Next = null;
return newTest;
}
#undef is
#undef null