mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Added _forEach and _map, added tests for them
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import compound;
|
||||
|
||||
//test concat
|
||||
{
|
||||
//test array concat
|
||||
{
|
||||
@@ -44,6 +45,26 @@ import compound;
|
||||
}
|
||||
}
|
||||
|
||||
//test forEach
|
||||
{
|
||||
var counter = 0;
|
||||
|
||||
fn p(k, v) {
|
||||
counter++;
|
||||
print string k + ": " + string v;
|
||||
}
|
||||
|
||||
var a = ["a", "b"];
|
||||
var d = ["foo": 1, "bar": 2, "bazz": 3, "fizz": 4];
|
||||
|
||||
a.forEach(p);
|
||||
assert counter == 2, "forEach ran an unusual number of times";
|
||||
|
||||
counter = 0;
|
||||
d.forEach(p);
|
||||
assert counter == 4, "forEach ran an unusual number of times";
|
||||
}
|
||||
|
||||
//test getKeys
|
||||
{
|
||||
var d = ["foo": 1, "bar": 2];
|
||||
@@ -70,6 +91,28 @@ import compound;
|
||||
}
|
||||
|
||||
|
||||
//test map
|
||||
{
|
||||
//test map with toy functions
|
||||
{
|
||||
fn increment(k, v) {
|
||||
return v + 1;
|
||||
}
|
||||
|
||||
var a = [1, 2, 3];
|
||||
var d = ["four": 4, "five": 5, "six": 6];
|
||||
|
||||
assert a.map(increment).map(increment).map(increment) == [4,5,6], "array.map() failed";
|
||||
assert d.map(increment).map(increment).map(increment) == [8,9,7], "dictionary.map() failed";
|
||||
}
|
||||
|
||||
//test map with native functions
|
||||
{
|
||||
//TODO: write some native functions for use with map
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//test toLower
|
||||
{
|
||||
assert "Hello World".toLower() == "hello world", "_toLower() failed";
|
||||
|
||||
40
test/scripts/lib/random-stuff.toy
Normal file
40
test/scripts/lib/random-stuff.toy
Normal file
@@ -0,0 +1,40 @@
|
||||
//test this logic for memory leaks
|
||||
{
|
||||
import compound;
|
||||
import timer;
|
||||
|
||||
fn start(k, v) {
|
||||
return startTimer();
|
||||
}
|
||||
|
||||
fn check(k, v) {
|
||||
var l = v.stopTimer();
|
||||
print l.timerToString();
|
||||
l.destroyTimer();
|
||||
return v;
|
||||
}
|
||||
|
||||
fn destroy(k, v) {
|
||||
v.destroyTimer();
|
||||
}
|
||||
|
||||
var arr = [1];
|
||||
|
||||
arr
|
||||
.map(start)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(check)
|
||||
.map(destroy)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
print "All good";
|
||||
@@ -53,6 +53,26 @@ void runBinaryWithLibrary(unsigned char* tb, size_t size, char* library, Toy_Hoo
|
||||
Toy_freeInterpreter(&interpreter);
|
||||
}
|
||||
|
||||
void runBinaryQuietly(unsigned char* tb, size_t size) {
|
||||
Toy_Interpreter interpreter;
|
||||
Toy_initInterpreter(&interpreter);
|
||||
|
||||
//NOTE: supress print output for testing
|
||||
Toy_setInterpreterPrint(&interpreter, noPrintFn);
|
||||
Toy_setInterpreterAssert(&interpreter, assertWrapper);
|
||||
Toy_setInterpreterError(&interpreter, errorWrapper);
|
||||
|
||||
//inject the libs
|
||||
Toy_injectNativeHook(&interpreter, "about", Toy_hookAbout);
|
||||
Toy_injectNativeHook(&interpreter, "compound", Toy_hookCompound);
|
||||
Toy_injectNativeHook(&interpreter, "standard", Toy_hookStandard);
|
||||
Toy_injectNativeHook(&interpreter, "timer", Toy_hookTimer);
|
||||
Toy_injectNativeHook(&interpreter, "runner", Toy_hookRunner);
|
||||
|
||||
Toy_runInterpreter(&interpreter, tb, size);
|
||||
Toy_freeInterpreter(&interpreter);
|
||||
}
|
||||
|
||||
typedef struct Payload {
|
||||
char* fname;
|
||||
char* libname;
|
||||
@@ -111,6 +131,41 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
//run whatever, testing stuff together to check for memory leaks
|
||||
char* whatever[] = {
|
||||
"random-stuff.toy",
|
||||
NULL
|
||||
};
|
||||
|
||||
for (int i = 0; whatever[i]; i++) {
|
||||
printf("Running %s\n", whatever[i]);
|
||||
|
||||
char fname[128];
|
||||
snprintf(fname, 128, "scripts/lib/%s", whatever[i]);
|
||||
|
||||
//compile the source
|
||||
size_t size = 0;
|
||||
char* source = Toy_readFile(fname, &size);
|
||||
if (!source) {
|
||||
printf(TOY_CC_ERROR "Failed to load file: %s\n" TOY_CC_RESET, fname);
|
||||
failedAsserts++;
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned char* tb = Toy_compileString(source, &size);
|
||||
free((void*)source);
|
||||
|
||||
if (!tb) {
|
||||
printf(TOY_CC_ERROR "Failed to compile file: %s\n" TOY_CC_RESET, fname);
|
||||
failedAsserts++;
|
||||
continue;
|
||||
}
|
||||
|
||||
runBinaryQuietly(tb, size);
|
||||
}
|
||||
}
|
||||
|
||||
//lib cleanup
|
||||
Toy_freeDriveDictionary();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user