forked from WheretIB/nullc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTests.cpp
324 lines (266 loc) · 11.2 KB
/
UnitTests.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
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
#if defined(_MSC_VER)
#include "stdafx.h"
#endif
#include "UnitTests.h"
#include "NULLC/nullc.h"
#include "NULLC/nullc_debug.h"
// Check that remote debug module compiles correctly
#if defined(_MSC_VER)
#include "NULLC/nullc_remote.h"
#endif
#include "NULLC/ParseClass.h"
#include "NULLC/includes/file.h"
#include "NULLC/includes/math.h"
#include "NULLC/includes/vector.h"
#include "NULLC/includes/random.h"
#include "NULLC/includes/dynamic.h"
#include "NULLC/includes/gc.h"
#include "NULLC/includes/time.h"
#include "NULLC/includes/string.h"
#include "NULLC/includes/canvas.h"
#include "NULLC/includes/window.h"
#include "NULLC/includes/io.h"
#include "NULLC/includes/pugi.h"
#include "tests/TestBase.h"
#include "tests/TestSpeed.h"
#include "tests/TestCompileFail.h"
#include "tests/TestParseFail.h"
#include "tests/TestInterface.h"
#pragma warning(disable: 4127)
//#define ALLOC_TOP_DOWN
//#define NO_CUSTOM_ALLOCATOR
void* testAlloc(int size)
{
#ifdef ALLOC_TOP_DOWN
return VirtualAlloc(NULL, size + 128, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
#else
return (char*)malloc(size + 128) + 128;
#endif
}
void testDealloc(void* ptr)
{
if(!ptr)
return;
#ifdef ALLOC_TOP_DOWN
VirtualFree((char*)ptr - 128, 0, MEM_RELEASE);
#else
free((char*)ptr - 128);
#endif
}
nullres CompileFile(const char* fileName)
{
static char content[64 * 1024];
FILE *euler = fopen(fileName, "rb");
fseek(euler, 0, SEEK_END);
unsigned int textSize = ftell(euler);
assert(textSize < 64 * 1024);
fseek(euler, 0, SEEK_SET);
fread(content, 1, textSize, euler);
content[textSize] = 0;
fclose(euler);
return nullcCompile(content);
}
void RunTests(bool verbose, const void* (NCDECL *fileLoadFunc)(const char*, unsigned int*, int*))
{
Tests::messageVerbose = verbose;
Tests::fileLoadFunc = fileLoadFunc;
// Extra tests
// Safe sprintf test
{
testsCount[3]++;
char buf[8];
char *pos = buf + NULLC::SafeSprintf(buf, 8, "this ");
pos += NULLC::SafeSprintf(pos, 8 - int(pos - buf), "string is too long");
if(memcmp(buf, "this st", 8) != 0)
printf("Safe sprintf test failed: string is incorrect\n");
else if(pos != buf + 8)
printf("Safe sprintf test failed: iterator is incorrect\n");
else
testsPassed[3]++;
}
/*
unsigned int tStart = clock();
for(unsigned int i = 0; i < 10000; i++)
{
nullcInit("Modules/");
nullcTerminate();
}
printf("Finished in %d\r\n", clock() - tStart);
*/
// Init NULLC
#ifdef NO_CUSTOM_ALLOCATOR
nullcInit(MODULE_PATH);
#else
nullcInitCustomAlloc(testAlloc, testDealloc, MODULE_PATH);
#endif
nullcSetFileReadHandler(Tests::fileLoadFunc);
nullcInitTypeinfoModule();
nullcInitDynamicModule();
RunInterfaceTests();
#ifdef NO_CUSTOM_ALLOCATOR
nullcInit(MODULE_PATH);
#else
nullcInitCustomAlloc(testAlloc, testDealloc, MODULE_PATH);
#endif
nullcSetFileReadHandler(Tests::fileLoadFunc);
nullcInitTypeinfoModule();
nullcInitFileModule();
nullcInitMathModule();
nullcInitVectorModule();
nullcInitRandomModule();
nullcInitDynamicModule();
nullcInitGCModule();
nullcInitStringModule();
nullcInitIOModule();
nullcInitCanvasModule();
#if defined(_MSC_VER)
nullcInitWindowModule();
#endif
/*
//SpeedTestFile("test_document.nc");
//SpeedTestFile("shapes.nc");
//SpeedTestFile("raytrace.nc");
//SpeedTestFile("blob.nc");
return;*/
#ifdef NULLC_ENABLE_C_TRANSLATION
if(!CompileFile("Modules/std/math.nc"))
printf("ERROR: failed to compile std.math for translation\n");
nullcTranslateToC("NULLC/translation/std_math.cpp", "__init_std_math_nc");
if(!CompileFile("Modules/std/typeinfo.nc"))
printf("ERROR: failed to compile std.typeinfo for translation\n");
nullcTranslateToC("NULLC/translation/std_typeinfo.cpp", "__init_std_typeinfo_nc");
if(!CompileFile("Modules/std/file.nc"))
printf("ERROR: failed to compile std.file for translation\n");
nullcTranslateToC("NULLC/translation/std_file.cpp", "__init_std_file_nc");
if(!CompileFile("Modules/std/vector.nc"))
printf("ERROR: failed to compile std.vector for translation\n");
nullcTranslateToC("NULLC/translation/std_vector.cpp", "__init_std_vector_nc");
if(!nullcCompile("import std.math; float4 a; a.x = 2;"))
printf("ERROR: failed to compile test_a for translation\n");
nullcTranslateToC("tests/translation/test_a.cpp", "__init_test_a_nc");
if(!nullcCompile("char[] arr2 = \" world\";{ int r = 5; }"))
printf("ERROR: failed to compile test_importhide for translation\n");
nullcTranslateToC("tests/translation/test_importhide.cpp", "__init_test_importhide_nc");
if(!nullcCompile("int func(int a, b = 6){ return a * b; }"))
printf("ERROR: failed to compile test_defargs for translation\n");
nullcTranslateToC("tests/translation/test_defargs.cpp", "__init_test_defargs_nc");
if(!nullcCompile("int func(int a, b = 6){ return a * b; } int func(int d, c, a, b = 4){ return d * c + a + b; }"))
printf("ERROR: failed to compile test_defargs2 for translation\n");
nullcTranslateToC("tests/translation/test_defargs2.cpp", "__init_test_defargs2_nc");
if(!nullcCompile("class Test{ int func(int a, b = 6){ return a * b; } }"))
printf("ERROR: failed to compile test_defargs3 for translation\n");
nullcTranslateToC("tests/translation/test_defargs3.cpp", "__init_test_defargs3_nc");
if(!nullcCompile("class Test{ int func(int a, b = 6); }"))
printf("ERROR: failed to compile test_defargs4 for translation\n");
nullcTranslateToC("tests/translation/test_defargs4.cpp", "__init_test_defargs4_nc");
if(!nullcCompile("int foo(int x, char[] a = \"xx\", int y = 0){return x + a[0] + a[1];}"))
printf("ERROR: failed to compile test_defargs5 for translation\n");
nullcTranslateToC("tests/translation/test_defargs5.cpp", "__init_test_defargs5_nc");
if(!nullcCompile("int x = 5; int foo(auto ref a = &x){return int(a);}"))
printf("ERROR: failed to compile test_defargs6 for translation\n");
nullcTranslateToC("tests/translation/test_defargs6.cpp", "__init_test_defargs6_nc");
if(!nullcCompile("int CheckAlignment(auto ref ptr, int alignment);"))
printf("ERROR: failed to compile test_alignment for translation\n");
nullcTranslateToC("tests/translation/test_alignment.cpp", "__init_test_alignment_nc");
if(!CompileFile("Modules/std/list.nc"))
printf("ERROR: failed to compile std.list for translation\n");
nullcTranslateToC("NULLC/translation/std_list.cpp", "__init_std_list_nc");
if(!CompileFile("Modules/std/range.nc"))
printf("ERROR: failed to compile std.range for translation\n");
nullcTranslateToC("NULLC/translation/std_range.cpp", "__init_std_range_nc");
if(!CompileFile("Modules/std/gc.nc"))
printf("ERROR: failed to compile std.gc for translation\n");
nullcTranslateToC("NULLC/translation/std_gc.cpp", "__init_std_gc_nc");
if(!CompileFile("Modules/std/dynamic.nc"))
printf("ERROR: failed to compile std.dynamic for translation\n");
nullcTranslateToC("NULLC/translation/std_dynamic.cpp", "__init_std_dynamic_nc");
if(!CompileFile("Modules/std/io.nc"))
printf("ERROR: failed to compile std.io for translation\n");
nullcTranslateToC("NULLC/translation/std_io.cpp", "__init_std_io_nc");
if(!nullcCompile("coroutine int foo(){ int i = 10; while(i) yield i++; }"))
printf("ERROR: failed to compile test_coroutine1 for translation\n");
nullcTranslateToC("tests/translation/test_coroutine1.cpp", "__init_test_coroutine1_nc");
if(!nullcCompile("import std.range; auto a = { for(i in range(4, 10)) yield i; };"))
printf("ERROR: failed to compile test_list_comp1 for translation\n");
nullcTranslateToC("tests/translation/test_list_comp1.cpp", "__init_test_list_comp1_nc");
if(!nullcCompile("import std.range; auto b = { for(i in range(2, 4)) yield i; };"))
printf("ERROR: failed to compile test_list_comp2 for translation\n");
nullcTranslateToC("tests/translation/test_list_comp2.cpp", "__init_test_list_comp2_nc");
if(!CompileFile("Modules/std/time.nc"))
printf("ERROR: failed to compile std.time for translation\n");
nullcTranslateToC("NULLC/translation/std_time.cpp", "__init_std_time_nc");
if(!CompileFile("Modules/img/canvas.nc"))
printf("ERROR: failed to compile img.canvas for translation\n");
nullcTranslateToC("NULLC/translation/img_canvas.cpp", "__init_img_canvas_nc");
if(!CompileFile("Modules/win/window_ex.nc"))
printf("ERROR: failed to compile win.window_ex for translation\n");
nullcTranslateToC("NULLC/translation/win_window_ex.cpp", "__init_win_window_ex_nc");
if(!CompileFile("Modules/win/window.nc"))
printf("ERROR: failed to compile win.window for translation\n");
nullcTranslateToC("NULLC/translation/win_window.cpp", "__init_win_window_nc");
if(!nullcCompile("class Proxy{ int foo(int x){ return x; } } int rc(auto ref z){ return z.foo(5); }"))
printf("ERROR: failed to compile test_autorefcall for translation\n");
nullcTranslateToC("tests/translation/test_autorefcall.cpp", "__init_test_autorefcall_nc");
if(!nullcCompile("class Foo { typedef int T; }"))
printf("ERROR: failed to compile test_class_typedef for translation\n");
nullcTranslateToC("tests/translation/test_class_typedef.cpp", "__init_test_class_typedef_nc");
#endif
RunCompileFailTests();
RunParseFailTests();
#if defined(NULLC_ENABLE_C_TRANSLATION)
#ifdef __linux
system("cp NULLC/translation/runtime.h runtime.h");
system("cp NULLC/translation/runtime.h tests/translation/runtime.h");
#else
_popen("copy \"NULLC\\translation\\runtime.h\" \"runtime.h\"", "r");
_popen("copy \"NULLC\\translation\\runtime.h\" \"tests\\translation\\runtime.h\"", "r");
#endif
#endif
TestQueue queue;
queue.RunTests();
// Conclusion
printf("VM passed %d of %d tests\r\n", testsPassed[0], testsCount[0]);
#ifdef NULLC_BUILD_X86_JIT
printf("X86 passed %d of %d tests\r\n", testsPassed[1], testsCount[1]);
#else
testsPassed[1] = 0;
testsCount[1] = 0;
#endif
#ifdef NULLC_LLVM_SUPPORT
printf("LLVM passed %d of %d tests\r\n", testsPassed[2], testsCount[2]);
#else
testsPassed[2] = 0;
testsCount[2] = 0;
#endif
printf("Failure tests: passed %d of %d tests\r\n", testsPassed[TEST_FAILURE_INDEX], testsCount[TEST_FAILURE_INDEX]);
printf("Extra tests: passed %d of %d tests\r\n", testsPassed[TEST_EXTRA_INDEX], testsCount[TEST_EXTRA_INDEX]);
#ifdef NULLC_ENABLE_C_TRANSLATION
printf("Translation tests: passed %d of %d tests\r\n", testsPassed[TEST_TRANSLATION_INDEX], testsCount[TEST_TRANSLATION_INDEX]);
#endif
unsigned allTests = 0;
unsigned allPassed = 0;
for(unsigned i = 0; i < 6; i++)
{
allTests += testsCount[i];
allPassed += testsPassed[i];
}
printf("Passed %d of %d tests\r\n", allPassed, allTests);
printf("Compilation time: %f\r\n", Tests::timeCompile);
printf("Get listing time: %f\r\n", Tests::timeGetListing);
printf("Get bytecode time: %f\r\n", Tests::timeGetBytecode);
printf("Clean time: %f\r\n", Tests::timeClean);
printf("Link time: %f\r\n", Tests::timeLinkCode);
printf("Run time: %f\r\n", Tests::timeRun);
RunSpeedTests();
#if defined(NULLC_ENABLE_C_TRANSLATION)
#ifdef __linux
system("rm runtime.h");
system("rm tests/translation/runtime.h");
#else
_popen("del \"runtime.h\"", "r");
_popen("del \"tests\\translation\\runtime.h\"", "r");
#endif
#endif
// Terminate NULLC
nullcTerminate();
}