-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_tests.cpp
389 lines (347 loc) · 10.4 KB
/
run_tests.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* TinyJS
*
* A single-file Javascript-alike engine
*
* Authored By Gordon Williams <[email protected]>
*
* Copyright (C) 2009 Pur3 Ltd
*
* 42TinyJS
*
* A fork of TinyJS with the goal to makes a more JavaScript/ECMA compliant engine
*
* Authored / Changed By Armin Diedering <[email protected]>
*
* Copyright (C) 2010-2015 ardisoft
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* This is a program to run all the tests in the tests folder...
*/
#ifdef _DEBUG
# ifdef _MSC_VER
# ifdef USE_DEBUG_NEW
//# include "targetver.h"
//# include <afx.h>
//# define new DEBUG_NEW
# endif
# else
# define DEBUG_MEMORY 1
# endif
#endif
#define _CRT_SECURE_NO_WARNINGS
#ifdef _WIN32
#include <process.h>
#ifndef _MSC_VER
# include <debugapi.h>
#endif
#endif
#include "TinyJS.h"
//#include "TinyJS_Functions.h"
//#include "TinyJS_MathFunctions.h"
//#include "TinyJS_StringFunctions.h"
#include <assert.h>
#include <sys/stat.h>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <cstring>
//#define WITH_TIME_LOGGER
//#define INSANE_MEMORY_DEBUG
#ifndef WITH_TIME_LOGGER
#define WITH_TIME_LOGGER
#endif
#include "time_logger.h"
#ifdef INSANE_MEMORY_DEBUG
// needs -rdynamic when compiling/linking
#include <execinfo.h>
#include <malloc.h>
#include <map>
#include <vector>
using namespace std;
void **get_stackframe() {
void **trace = (void**)malloc(sizeof(void*)*17);
int trace_size = 0;
for (int i=0;i<17;i++) trace[i]=(void*)0;
trace_size = backtrace(trace, 16);
return trace;
}
void print_stackframe(char *header, void **trace) {
char **messages = (char **)NULL;
int trace_size = 0;
trace_size = 0;
while (trace[trace_size]) trace_size++;
messages = backtrace_symbols(trace, trace_size);
printf("%s\n", header);
for (int i=0; i<trace_size; ++i) {
printf("%s\n", messages[i]);
}
//free(messages);
}
/* Prototypes for our hooks. */
static void *my_malloc_hook (size_t, const void *);
static void my_free_hook (void*, const void *);
static void *(*old_malloc_hook) (size_t, const void *);
static void (*old_free_hook) (void*, const void *);
map<void *, void **> malloced;
static void *my_malloc_hook(size_t size, const void *caller) {
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
void *result = malloc (size);
/* we call malloc here, so protect it too. */
//printf ("malloc (%u) returns %p\n", (unsigned int) size, result);
malloced[result] = get_stackframe();
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
return result;
}
static void my_free_hook(void *ptr, const void *caller) {
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
/* Call recursively */
free (ptr);
/* we call malloc here, so protect it too. */
//printf ("freed pointer %p\n", ptr);
if (malloced.find(ptr) == malloced.end()) {
/*fprintf(stderr, "INVALID FREE\n");
void *trace[16];
int trace_size = 0;
trace_size = backtrace(trace, 16);
backtrace_symbols_fd(trace, trace_size, STDERR_FILENO);*/
} else
malloced.erase(ptr);
/* Restore our own hooks */
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
}
void memtracing_init() {
old_malloc_hook = __malloc_hook;
old_free_hook = __free_hook;
__malloc_hook = my_malloc_hook;
__free_hook = my_free_hook;
}
long gethash(void **trace) {
unsigned long hash = 0;
while (*trace) {
hash = (hash<<1) ^ (hash>>63) ^ (unsigned long)*trace;
trace++;
}
return hash;
}
void memtracing_kill() {
/* Restore all old hooks */
__malloc_hook = old_malloc_hook;
__free_hook = old_free_hook;
map<long, void**> hashToReal;
map<long, int> counts;
map<void *, void **>::iterator it = malloced.begin();
while (it!=malloced.end()) {
long hash = gethash(it->second);
hashToReal[hash] = it->second;
if (counts.find(hash) == counts.end())
counts[hash] = 1;
else
counts[hash]++;
it++;
}
vector<pair<int, long> > sorting;
map<long, int>::iterator countit = counts.begin();
while (countit!=counts.end()) {
sorting.push_back(pair<int, long>(countit->second, countit->first));
countit++;
}
// sort
bool done = false;
while (!done) {
done = true;
for (int i=0;i<sorting.size()-1;i++) {
if (sorting[i].first < sorting[i+1].first) {
pair<int, long> t = sorting[i];
sorting[i] = sorting[i+1];
sorting[i+1] = t;
done = false;
}
}
}
for (int i=0;i<sorting.size();i++) {
long hash = sorting[i].second;
int count = sorting[i].first;
char header[256];
sprintf(header, "--------------------------- LEAKED %d", count);
print_stackframe(header, hashToReal[hash]);
}
}
#endif // INSANE_MEMORY_DEBUG
class end { // this is for VisualStudio debugging stuff. It's holds the console open up to ENTER is pressed
public:
end() : active(false) {}
~end()
{
if(active) {
#ifdef _WIN32
system("pause");
#else
printf("press Enter (end)");
getchar();
#endif
}
}
bool active;
} end;
void js_print(const CFunctionsScopePtr &v, void *) {
printf("> %s\n", v->getArgument("text")->toString().c_str());
}
bool run_test(const char *filename) {
printf("TEST %s ", filename);
#ifdef _MSC_VER
char out[256];
sprintf_s(out, "TEST %s \n", filename);
OutputDebugString(out);
#endif
struct stat results;
if (!stat(filename, &results) == 0) {
printf("Cannot stat file! '%s'\n", filename);
return false;
}
int size = results.st_size;
FILE *file = fopen( filename, "rb" );
/* if we open as text, the number of bytes read may be > the size we read */
if( !file ) {
printf("Unable to open file! '%s'\n", filename);
return false;
}
char *buffer = new char[size+1];
size_t actualRead = fread(buffer,1,size,file);
buffer[actualRead]=0;
buffer[size]=0;
fclose(file);
CTinyJS s;
s.addNative("function print(text)", &js_print, 0);
// registerFunctions(&s);
// registerMathFunctions(&s);
// registerStringFunctions(&s);
s.getRoot()->addChild("result", s.newScriptVar(0));
#ifdef WITH_TIME_LOGGER
TimeLoggerCreate(Test, true, filename);
#endif
try {
CScriptTokenizer tokens(0, filename);
// std::fstream out(std::string(filename)+'c', std::fstream::out | std::fstream::trunc | std::fstream::binary);
// tokens.serialize(out);
s.execute(tokens);
} catch (CScriptException &e) {
printf("%s\n", e.toString().c_str());
}
bool pass = s.getRoot()->findChild("result")->toBoolean();
#ifdef WITH_TIME_LOGGER
TimeLoggerLogprint(Test);
#endif
if (pass)
printf("PASS\n");
else {
char fn[64];
sprintf(fn, "%s.fail.txt", filename);
// logging is currently deactivated because stack-overflow by recursive vars
FILE *f = fopen(fn, "wt");
if (f) {
std::string symbols = s.getRoot()->CScriptVar::getParsableString();
fprintf(f, "%s", symbols.c_str());
fclose(f);
}
printf("FAIL - symbols written to %s\n", fn);
}
delete[] buffer;
return pass;
}
int main(int argc, char **argv)
{
#ifdef INSANE_MEMORY_DEBUG
memtracing_init();
#endif
printf("TinyJS test runner\n");
printf("USAGE:\n");
printf(" ./run_tests [-k] tests/test001.js [tests/42tests/test002.js] : run tests\n");
printf(" ./run_tests [-k] : run all tests\n");
printf(" -k needs press enter at the end of runs\n");
int arg_num = 1;
bool runs = false;
for(; arg_num<argc; arg_num++) {
if(argv[arg_num][0] == '-') {
if(strcmp(argv[arg_num], "-k")==0)
end.active = true;
} else {
run_test(argv[arg_num]);
runs=true;
}
}
if (runs) {
return 0;
}
int count = 0;
int passed = 0;
const char *mask[] = {"tests/test%03d%s.js", "tests/42tests/test%03d%s.js"};
#ifdef WITH_TIME_LOGGER
TimeLoggerCreate(Tests, true);
#endif
// activate writing of tokenized token-code
CScriptTokenizer::writeCompiledTokens = true;
for(int js42 = 0; js42<2; js42++) {
int test_num = 1;
while (test_num<1000) {
char fn[32];
sprintf(fn, mask[js42], test_num, ".42");
// check if the file exists - if not, assume we're at the end of our tests
FILE *f = fopen(fn,"r");
if (!f) {
sprintf(fn, mask[js42], test_num, "");
f = fopen(fn,"r");
if(!f) break;
}
fclose(f);
if (run_test(fn))
passed++;
count++;
test_num++;
}
}
printf("Done. %d tests, %d pass, %d fail\n", count, passed, count-passed);
#ifdef WITH_TIME_LOGGER
TimeLoggerLogprint(Tests);
#endif
#ifdef INSANE_MEMORY_DEBUG
memtracing_kill();
#endif
#ifdef _WIN32
#ifdef _DEBUG
// _CrtDumpMemoryLeaks();
/*
no dump momoryleaks here
_CrtSetDbgFlag(..) force dump memoryleake after call of all global deconstructors
*/
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
#endif
return 0;
}