-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcunit.h
125 lines (111 loc) · 3.21 KB
/
cunit.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
#pragma once
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#define NEWLINE "\r\n"
#elif defined macintosh // OS 9
#define NEWLINE "\r"
#else
#define NEWLINE "\n" // Mac OS X uses \n
#endif
typedef struct _test* Test;
struct _test {
/// <summary>
/// The next test in the test suite
/// </summary>
Test Next;
/// <summary>
/// The name of the test
/// </summary>
char* Name;
/// <summary>
/// The method that should be ran
/// </summary>
bool(*Method)();
};
typedef struct testSuite* TestSuite;
struct testSuite {
char* Name;
Test Head;
Test Tail;
size_t Count;
FILE* OutputStream;
void(*Append)(TestSuite suite, char* name, bool(*method)(FILE*));
void(*Dispose)(TestSuite suite);
bool(*Run)(TestSuite suite);
};
TestSuite CreateSuite(char* name);
static const char* PassFormat = "\t[PASS] ";
static const char* FailFormat = "\t[FAIL] ";
static const char* FailExpectedFormat = "\t\tExpected: ";
static const char* FailActualFormat = "Got: ";
static const char* BenchmarkStartFormat = "\t[%s] "; // MethodName
static const char* BenchmarkSpeedFormat = "[%lli ticks] "; // Speed(ticks)
static const char* BenchmarkEndFormat = " in %s at %s [%li]" NEWLINE; // __func__, __FILE__, __LINE__
static const char* SuiteStartFormat = "[%s] [STARTING] [COUNT = %lli]" NEWLINE; // SuiteName
static const char* SuiteEndFormat = "[%s] [FINISHED] [PASS = %lli] [FAIL = %lli]"; // SuiteName
static const char* TestFinishedFormat = "[%s]"; // TestName
#define Benchmark(expression,stream) do\
{\
size_t start = clock(); \
expression;\
size_t end = clock() - start; \
fprintf(stream, BenchmarkSpeedFormat, end); \
} while (false);
#define BenchmarkAssertion(expression,stream) do\
{\
size_t start = clock(); \
bool pass = (expression); \
size_t end = clock() - start; \
if (pass)\
{\
fprintf(stream, PassFormat); \
}\
else\
{\
fprintf(stream, FailFormat); \
}\
fprintf(stream, BenchmarkSpeedFormat, end); \
fprintf(stream, BenchmarkEndFormat,__func__,__FILE__,__LINE__); \
if(pass == false)\
{\
return false;\
}\
} while (false);
#define BenchmarkComparison(expected,actual,comparison,format,stream)do\
{\
size_t start = clock(); \
bool pass = (expected comparison actual); \
size_t end = clock() - start; \
if (pass)\
{\
fprintf(stream, PassFormat); \
}\
else\
{\
fprintf(stream, FailFormat); \
}\
fprintf(stream, BenchmarkSpeedFormat, end); \
fprintf(stream, BenchmarkEndFormat,__func__,__FILE__,__LINE__); \
if(pass == false)\
{\
fprintf(stream, FailExpectedFormat);\
fprintf(stream,format,expected);\
fputc(' ',stream);\
fprintf(stream, FailActualFormat);\
fprintf(stream,format,actual);\
fprintf(stream,NEWLINE);\
return false;\
}\
} while (false);
#define Assert(expr) BenchmarkAssertion(expr,stream);
#define StandardAssert(expr) Assert(expr);
#define IsNull(expr) StandardAssert(expr == NULL);
#define NotNull(expr) StandardAssert(expr != NULL);
#define IsZero(expr) StandardAssert(expr == 0);
#define NotZero(expr) StandardAssert(expr != 0);
#define IsFalse(expr) StandardAssert(expr != true);
#define IsTrue(expr) StandardAssert(expr);
#define Equals(left,right,format) BenchmarkComparison(left,right,==,format,stream);
#define NotEqual(left,right) StandardAssert(left != right);