-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
458 lines (455 loc) · 18 KB
/
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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "src/_header.hpp"
#include <chrono>
namespace TestResult {
inline void printIfError(const string& msg) {
if(msg.length()) {
std::cout << "Error: " << msg << std::endl;
}
}
inline static string fail(const string& message, const string& name) {
return "failed " + name + ", " + message;
}
inline static string thrown(const string& message, const string& name, const string& identifier) {
return identifier + " threw \"" + message + "\" (" + name + ")";
}
inline static string success() {
return "";
}
};
#pragma region Random generation
namespace Generate {
int fastrand_seed;
inline int fastrand() {
fastrand_seed = (214013 * fastrand_seed + 2531011);
return (fastrand_seed >> 16) & 0x7FFF;
}
string variable_name() {
const static string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ____0123456789";
const static int chars = str.length();
int count = fastrand() % 5 + 1;
string out(size_t(count), ' ');
out[0] = str[fastrand() % (chars - 14)];
for(int i = 1;i < count;i++)
out[i] = str[fastrand() % chars];
return out;
}
string valid_variable(ParseCtx& ctx, bool isFunction, int* argCount, bool allowUnit) {
int type = fastrand() % 4;
if(type == 1 && (isFunction || !allowUnit)) type = 0;
//Variable
if(type == 2) {
std::map<string, int>& vars = ctx.getVariableList();
if(vars.size() == 0) type = 0;
else {
std::map<string, int>::iterator it = vars.begin();
std::advance(it, (fastrand() % vars.size()));
return it->first;
}
}
//Argument
if(type == 3) {
if(ctx.argCount() == 0) type = 0;
else return ctx.getArgName(fastrand() % ctx.argCount());
}
//Unit
if(type == 1) {
std::unordered_map<string, Unit::Builtin>::const_iterator it = Unit::listOfUnits.begin();
std::advance(it, fastrand() % Unit::listOfUnits.size());
string prefix;
if(std::get<2>(it->second)) {
auto it = Unit::powers.begin();
std::advance(it, fastrand() % Unit::powers.size());
prefix = it->first;
}
return prefix + it->first;
}
//Global function
if(type == 0 && isFunction) {
std::map<string, int>::iterator it = Program::globalFunctionMap.begin();
std::advance(it, fastrand() % Program::globalFunctionMap.size());
if(argCount)
while(!Program::globalFunctions[it->second].assertArgCount(*argCount)) (*argCount)++;
if(it->first == "min") return "max";
return it->first;
}
return "pi";
}
string expression(int nestLeft, ParseCtx& ctx) {
int type = fastrand();
if(nestLeft == 0) type %= 3;
#ifdef USE_ARB
else type %= 10;
#else
else type %= 9;
#endif
//Number
if(type == 0) {
int num = fastrand();
string str = std::to_string(num);
if(num & 1) str[fastrand() % str.length()] = '.';
if(num & 2 && str.length() > 2) str[1 + fastrand() % (str.length() - 2)] = 'e';
if(num & 4 && str.length() > 2) {
static const string bases = "btodx";
str[0] = '0';
str[1] = bases[fastrand() % bases.length()];
}
return str;
}
//Variable
else if(type == 1)
return valid_variable(ctx, false, nullptr, ctx.useUnits());
//Parenthesis
else if(type == 2)
return "(" + expression(nestLeft, ctx) + ")";
//Square brackets
else if(type == 3) {
ctx.push(0, true);
string out = "[" + expression(nestLeft - 1, ctx) + "]";
ctx.pop();
return out;
}
//Function
else if(type == 4) {
int argCount = 0;
string name = valid_variable(ctx, true, &argCount, false);
if(argCount == 0) return name;
std::vector<string> args;
for(int i = 0;i < argCount;i++) args.push_back(expression(nestLeft - 1, ctx));
string out = name + "(";
for(int i = 0;i < argCount;i++) out += (i != 0 ? "," : " ") + args[i];
return out + ")";
}
//Vector
else if(type == 5) {
int count = fastrand() % 5;
string out = "(<";
for(int i = 0;i < count;i++) {
out += (i != 0 ? "," : " ") + expression(nestLeft - 1, ctx);
}
return out + ">)";
}
//Lambda
else if(type == 6) {
int argCount = fastrand() % 4;
string out;
std::vector<string> args;
if(argCount == 0) out = "_";
else if(argCount == 1) { args.push_back(variable_name());out = args.back(); }
else {
for(int i = 0;i < argCount;i++) {
args.push_back(variable_name());
out += (i == 0 ? "(" : ",") + args.back();
}
out += ")";
}
ctx.push(args);
out += "=>" + expression(nestLeft - 1, ctx);
ctx.pop();
return out;
}
//Operator
else if(type == 7) {
auto it = Expression::operatorList.begin();
std::advance(it, fastrand() % Expression::operatorList.size());
return "(" + expression(nestLeft - 1, ctx) + it->first + expression(nestLeft - 1, ctx) + ")";
}
//String
else if(type == 8) {
return "\"stringy\"";
}
//Arb numbers
#ifdef USE_ARB
else if(type == 9) {
string out = "1.";
for(int i = 0;i < 4;i++) {
int num = fastrand();
out += std::to_string(num);
}
out += "e" + std::to_string(fastrand() % 20);
out += "p" + std::to_string(fastrand() % 20 + 3);
return out;
}
#endif
return "0";
}
string valid_str() {
static const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(())[[]]{{}}<<>>\"\" ___== +-*/%^$.....,,,,,";
int count = fastrand() % 50 + 20;
string out(count, ' ');
for(int i = 0;i < count;i++) {
out[i] = validChars[fastrand() % validChars.size()];
}
return out;
}
string argList() {
int count = fastrand() % 3 + 1;
if(count == 1) return "(" + variable_name() + ")";
else if(count == 2) return "(" + variable_name() + "," + variable_name() + ")";
else return "(" + variable_name() + "," + variable_name() + "," + variable_name() + ")";
}
}
#pragma endregion
#pragma region Tests
#define THROW_IF_FAIL_BASE(ident,n) catch(string message) {return TestResult::thrown(message,ident,n);} catch(const char* c) {return TestResult::thrown(string(c),ident,n);} catch(...) {return TestResult::thrown("unknown",ident,n);}
#define THROW_IF_FAIL THROW_IF_FAIL_BASE(identifier,name)
#define THROW_IF_FAIL_RANDOM THROW_IF_FAIL_BASE(identifier,name+ "["+std::to_string(index)+"]")
namespace MatchingXpr {
const string name = "matching xpr";
std::map<string, string> tests = {
{"3+2","5"},
{"1/2","0.5"},
{"<1,2>*3","<3,6>"},
{"sqrt(-1)","-i"},
{"(1-4)+4","1"},
{"3*-4","-12"},
{"2**4","4**2"},
{"3^4","81"},
{"10^-1","0.1"},
{"24.5","24.5"},
{"si n(0)","0"},
{"tan(0)","0"},
{"[m ]","getu([ft])"},
{"[m*kg]","getu([ft*lb])"},
{"run (n=>n+1,4)","5"},
{"run(run(n=>(j=>(n+j)),4),3)","7"},
{"sum(n=> n+1,0, 5,1)","21"},
{"[0A1 ]_16","161"},
{"[0A1 ]_([10000]_2)","161"},
{"run((x, y)=>( x + y) , 5, 3 . 4)","8.4"},
{"0x1A","26"},
{"0b101.1","5.5"},
{"0d5 4.3","54.3"},
{"0o10.1","8+1/8"},
{"magnitude(<3,4>)","5"},
{"10(4)","40"},
{"run(n=>(5)n,4)","20"},
//"abs(<10,,5>)",},
{"1+2i","1+2i"},
{"10.5e2","1050"},
{"1e1","10"},
{"[10e3]_4","4^4"},
{"0e5","0"},
{"factors(30)","<2,3,5>"},
{"gcd(10,4)","2"},
{"lcm(10,4)","20"},
{"max(10,40)","40"},
{"getr(15-2i)","15"},
{"geti(15-2i)","-2"},
{"getu(15[kg*kg])","[kg]^2"},
{"min(10,40)","10"},
{"lerp(10,40,0)","10"},
{"lerp(10,40,0.5)","25"},
{"dist(0,3+4i)","5"},
{"sgn(1+i)","(sqrt(2)+i*sqrt(2))/2"},
{"10==10","1"},
{"10==2","0"},
{"10!=10","0"},
{"10!=2","1"},
{"ls(5,2)","20"},
{"pi","3.14159265358979323p15"},
{"inf","0-(-inf)"},
{"round(dint(x=>x^2,0,30))","9000"},
{"dint(x=>sin(x),0,pi/2)","1"},
{"normalize(<1,1>)","sqrt(<2,2>)/2"},
{"fill(x=>x,5)","<0,1,2,3,4>"},
{"{\"a\":10,5:20,<1,2>:5}[\"a\"]","10"},
{"{\"a\":10,5:20,<1,2>:5}[5]","20"},
{"{\"a\":10,5:20,<1,2>:5}[<1,2>]","5"},
{"every(<10,2,0,4>,x=>(x>=0))","1"},
{"every(<10,2,0,4>,x=>(x>0))","0"},
{"concat(<1,2>,<3,4>)","<1,2,3,4>"},
{"sort(<5,1,6.6,2,1>)","<1,1,2,5,6.6>"},
{"sort(<5,1,6.6,2,1>,(a,b)=>(a>b))","<6.6,5,2,1,1>"},
{"simplify(x=>x*x)","x=>x^2"},
{"substr(\"hello world\",2,3)","\"llo\""},
{"lowercase(\"AghsaSDGJA\")","\"aghsasdgja\""},
{"lowercase(\"AghsaSDGJA\")","\"AGHSASDGJA\""},
{"replace(\"hello world\",\"o\",\"n\")","\"helln wnrld\""},
{"indexof(\"lsadkjg\",\"ad\")","2"},
{"magnitude(normalize(<5,1,3,124,5,151>))","1"},
{"length({1:2,4:3,5:4,6:2})","4"},
{"simplify(x=>y=>x^y)","x=>y=>x^y"},
{"simplify(x=>x-x)","x=>0"},
{"simplify(x=>x^2*x*x)","x=>x^4"},
{"simplify(derivative(x=>x^2))","x=>2x"},
{"simplify(derivative(x=>sin(x)^cos(x)))","simplify(x=>sin(x)^cos(x)*(cos(x)^2/sin(x)+(-1)*sin(x)*ln(sin(x))))"},
{"simplify(derivative(x=>x/x))","x=>0"},
{"infinite_sum(n=>1/n!)","e"},
{"derivative(x=>run((y,z)=>y^2+z^2,x,3x))","x=>20x"},
{"derivative(x=>apply((y,z)=>x*y*z,<2x,4x>))","x=>24x^2"}
};
string validate(std::map<string, string>::iterator it) {
const string& identifier = it->first;
try {
Value first = Expression::evaluate(it->first);
Value second = Expression::evaluate(it->second);
if(first != second) {
if(first->toString() != second->toString())
return TestResult::fail(it->first + " does not match " + it->second + " (computed as " + first->toString() + " and " + second->toString() + ")", name);
}
return TestResult::success();
} THROW_IF_FAIL
}
};
namespace Zeroes {
const string name = "zero";
std::vector<string> tests = {
//Arithmetic
"i-i", "neg(1)+1", "-1+1", "pow(10,0)-1", "2^0-1", "mod(10,5)", "10%5", "mul(0,10)", "0*10", "10/10-1", "add(neg(1),1)",
//Trigonometric
"sin(0)", "cos(0)-1", "floor(tan(1.56))-92", "csc(pi/2)-1", "sec(pi)+1", "floor(1/cot(1.56))-92", "floor(sinh(10.3))-14866", "cosh(0)-1", "tanh(0)", "asin(1)-pi/2", "acos(1)", "atan(1e50)-pi/2", "asinh(sinh(1))-1", "acosh(cosh(1))-1", "round(atanh(tanh(1)))-1",
//Exponential
"sqrt(4)-2", "exp(ln(2))-2", "ln(exp(2))-2", "round(log(1000))-3", "round(logb(1000,10))-3", "round(factorial(3))-6", "sgn(i)-i", "abs(-i)-1", "arg(i)-pi/2",
//Rounding
"round(0.5)-1", "floor(0.5)", "ceil(0.3)-1", "getr(i)", "geti(10.5[m])", "getu([km])/[m]-1",
//Comparison
"(10>4)-1","(-10>4)", "10!=10","(10=10)-1", "equal(10.3,10.3)-1", "min(4,5)-4", "min(5,4)-4", "max(5,4)-5", "max(4,5)-5", "lerp(-1,1,0.5)", "dist(0,3+4i)-5",
//Binary Operators
"and(0,5)", "or(0,5)-5", "xor(5,3)-6", "ls(5,1)-10", "rs(5,1)-2",
//Variables
"floor(1/(pi-3.14))-627", "floor(1/(e-2.71))-120", "floor(rand)", "ans",
//Lambda
"run(x=>x+1,-1)", "sum(x=>x,0,10,1)-55", "product(x=>x,1,10,1)-3628800",
//Vectors
"length(<>)", "length(<1,1,1,1,>)-5", "get(<0,1>,0)", "get(<0,1,0>,2)", "magnitude(abs(fill(x=>0,4)))", "magnitude(map_vector(<1,1,sqrt(0.5)*2>,x=>x/2))-1",
//Numerical parsing
"1e-2-0.01","1e2-100","1e34-10000000000000000000000000000000000"
};
string validate(std::vector<string>::iterator it) {
const string& identifier = *it;
try {
if(!Value::isZero(Expression::evaluate(*it)))
return TestResult::fail(*it + " did not return zero", name);
return TestResult::success();
} THROW_IF_FAIL
}
};
namespace Highlight {
const string name = "highlight";
std::map<string, string> tests = {
{"1+2","non"},
{"1p2-4**2","nnnonoon"},
{"add(5,[kg])","fffbndbuubb"},
{"x=>x+1","aooaon"},
{"0x12A3","nnnnnn"},
{"<3,4,5>","bndndnb"},
{"(4<3)+(3>2)","bnonbobnonb"},
{"( 3 + (4-5)","e nno bnonb"},
{"<5,x=>x+1,x>","bndaooaondeb"},
{"[<0A1,1F,3G>]_16","bbnnndnndnebbonn"},
{"run(x=>[0A1]_x,16)","fffbaoobneeboednnb"},
{"x=><x,2x,0.5>","aoobadnadnnnb"},
{"\"abc\\\"123\"","ssssssssss"},
{"\"abc\"+4.5","sssssonnn"},
{"^%#$%@","ooeeoe"},
};
string validate(std::map<string, string>::iterator it) {
const string& identifier = it->first;
try {
string colored(identifier.length(), Expression::ColorType::hl_error);
Expression::color(identifier, colored.begin(), Program::parseCtx);
if(colored != it->second)
return TestResult::fail(identifier + " highlighted as " + colored + " not as " + it->second, name);
return TestResult::success();
} THROW_IF_FAIL
}
};
namespace RandomHighlight {
const string name = "random highlight";
string validate(int index) {
string identifier = Generate::expression(4, Program::parseCtx);
try {
string colored(identifier.length(), Expression::ColorType::hl_error);
Expression::color(identifier, colored.begin(), Program::parseCtx);
if(colored.find(Expression::ColorType::hl_error) != string::npos) {
if(identifier[colored.find(Expression::ColorType::hl_error)] != ',')
return TestResult::fail(identifier + " highlighted as " + colored + " with error", name + "[" + std::to_string(index) + "]");
else return TestResult::success();
}
else return TestResult::success();
} THROW_IF_FAIL
}
}
namespace LibTest {
const string name = "library";
string validate(std::map<string, Library::LibFunc>::iterator it) {
const string& identifier = it->second.fullName;
try {
for(int i = 0;i < it->second.dependencies.size();i++) {
if(Library::functions.find(it->second.dependencies[i]) == Library::functions.end())
return TestResult::fail("Could not resolve dependency " + it->second.dependencies[i], name);
Library::functions[it->second.dependencies[i]].include();
}
Tree::parseTree(it->second.inputs + "=>" + it->second.xpr, Program::parseCtx);
return TestResult::success();
} THROW_IF_FAIL
return "";
}
}
namespace ParsingRandXpr {
const string name = "rand_parse";
string validate(int index) {
string identifier = Generate::expression(3, Program::parseCtx);
try {
//Parse random expression
Value tr = Tree::parseTree(identifier, Program::parseCtx);
//Test computation for segfault, other errors are ignored
try {
tr->compute(Program::computeCtx);
}
catch(...) {}
//Return success
return TestResult::success();
} THROW_IF_FAIL_RANDOM
}
}
namespace ParsingRandChar {
const string name = "rand_str_parse";
string validate(int index) {
string identifier = Generate::valid_str();
try {
Tree::parseTree(identifier, Program::parseCtx);
}
catch(...) {}
return TestResult::success();
}
}
//Highlighting tests
//Help page tests
//Random highlight
//Random compute
#pragma endregion
template<typename T>
void doTestList(T begin, T end, string(*validate)(T)) {
auto startTime = std::chrono::steady_clock::now();
for(T it = begin;it != end;it++) {
TestResult::printIfError(validate(it));
}
auto endTime = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = endTime - startTime;
std::cout << "Tests took " << diff.count() << std::endl;
}
void doTestRandom(string(*validate)(int index), float timeSeconds = 3.0) {
auto startTime = std::chrono::steady_clock::now();
int thousands = 0;
while(std::chrono::duration<float>(std::chrono::steady_clock::now() - startTime) < std::chrono::duration<float>(timeSeconds)) {
for(int i = 0;i < 1000;i++) {
TestResult::printIfError(validate(thousands + i));
}
thousands += 1000;
}
std::cout << "Did random tests" << std::endl;
}
#define RunTestList(name) doTestList(name::tests.begin(),name::tests.end(),&name::validate)
int main() {
Program::smallCompute = true;
Program::startup();
Generate::fastrand_seed = std::chrono::steady_clock::now().time_since_epoch().count();
doTestList(MatchingXpr::tests.begin(), MatchingXpr::tests.end(), &MatchingXpr::validate);
doTestList(Zeroes::tests.begin(), Zeroes::tests.end(), &Zeroes::validate);
doTestList(Library::functions.begin(), Library::functions.end(), &LibTest::validate);
doTestList(Highlight::tests.begin(), Highlight::tests.end(), &Highlight::validate);
doTestRandom(&RandomHighlight::validate);
doTestRandom(&ParsingRandXpr::validate);
doTestRandom(&ParsingRandChar::validate);
}