-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.cpp
117 lines (101 loc) · 3.2 KB
/
unit_test.cpp
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
#include "condition_language.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
unsigned hash (const char * key, unsigned len)
{
const unsigned m = 0x5bd1e995;
const int r = 24;
unsigned h = 0 ^ len;
const unsigned char * data = (const unsigned char *)key;
while(len >= 4) {
unsigned k = *(unsigned *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
struct Defines {
const char** defines;
unsigned n_defines;
};
namespace instrinsic {
bool defined(IdString32* params, unsigned n_params, void* ud) {
Defines& defs = *(Defines*)ud;
for (unsigned i = 0; i != defs.n_defines; ++i) {
if (params[0] == hash(defs.defines[i], strlen(defs.defines[i])))
return true;
}
return false;
}
bool equal(IdString32* params, unsigned n_params, void* ud) {
return params[0] == params[1];
}
}
void test_empty_string() {
unsigned char stack[1];
condition_language::Result r = condition_language::run("", stack, sizeof(stack), 0, 0, hash, 0);
assert(r.exit_status == condition_language::PARSE_FAILURE);
}
void test_single_intrinsic() {
const char* source = "equal(A, A) && !!(equal(B, B) || equal(B, C))";
condition_language::Intrinsic intrin[] = {
{ hash("equal", 5), instrinsic::equal },
};
unsigned char stack[64] = {0};
condition_language::Result r = condition_language::run(source, stack, sizeof(stack), intrin, 1, hash, 0);
assert(r.exit_status == condition_language::SUCCESS && r.value == true);
}
void test_several_intrinsics() {
const char* source = "equal(A, A) && !!(equal(B, B) || equal(B, C)) && (!defined(DEFINED) || defined(AVALUE))";
condition_language::Intrinsic intrin[] = {
{ hash("defined", 7), instrinsic::defined },
{ hash("equal", 5), instrinsic::equal },
};
const char* defines[] = { "DEFINED", "AVALUE" };
Defines macros = { defines, 2 };
unsigned char stack[64] = {0};
condition_language::Result r = condition_language::run(source, stack, sizeof(stack), intrin, 2, hash, ¯os);
assert(r.exit_status == condition_language::SUCCESS && r.value == true);
}
void benchmark(const char* source, const char* defines[], unsigned runs) {
unsigned char stack[64];
condition_language::Intrinsic intrin[] = {
{ hash("equal", 5), instrinsic::equal },
{ hash("defined", 7), instrinsic::defined }
};
Defines macros = { defines, sizeof(defines) / sizeof(char*) };
printf("Running '%s' %d times:\n", source, runs);
clock_t start = clock();
for (unsigned i = 0; i != runs; ++i)
condition_language::run(source, stack, sizeof(stack), intrin, 2, hash, ¯os);
clock_t end = clock();
double total = (end - start) * (1000.0 / CLOCKS_PER_SEC);
printf("Total %.4fms\nAvg: %.4fms/run\n\n", total, total / runs);
}
int main(int argc, char* argv[]) {
test_empty_string();
test_single_intrinsic();
test_several_intrinsics();
const char* macros[] = {"VAL1"};
benchmark("equal(A, A) && !!(equal(B, B) || equal(B, C))", macros, 100000);
benchmark("equal(A, A) && !!(equal(B, B) || equal(B, C)) && (!defined(DEFINED) || defined(VAL1))", macros, 100000);
getc(stdin);
return 0;
}