Gonna start unit testing to resolve issues

This commit is contained in:
2022-08-28 07:03:12 +01:00
parent 9c766ec61e
commit 5300e2ceec
13 changed files with 0 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
//test operators (integers)
assert 1 + 1 == 2, "1 + 1 == 2";
assert 1 - 1 == 0, "1 - 1 == 0";
assert 2 * 2 == 4, "2 * 2 == 4";
assert 1 / 2 == 0, "1 / 2 == 0"; //integer division
assert 5 % 2 == 1, "5 % 2 == 1";
//test operators (floats)
assert 1.0 + 1.0 == 2.0, "1.0 + 1.0 == 2.0";
assert 1.0 - 1.0 == 0.0, "1.0 - 1.0 == 0.0";
assert 2.0 * 2.0 == 4.0, "2.0 * 2.0 == 4.0";
assert 1.0 / 2.0 == 0.5, "1.0 / 2.0 == 0.5";
var a = 10;
a += 20;
a -= 25;
assert a == 5, "+= or -= failed";
a *= 5;
a /= 2;
assert a == 12, "*= or /= failed";
a %= 8;
assert a == 4, "%= failed";
print "All good";

33
scripts/test/casting.toy Normal file
View File

@@ -0,0 +1,33 @@
//boolean origin
var b: bool = true;
assert (int)b == 1, "bool -> int";
assert (float)b == 1, "bool -> float";
assert (string)b == "true", "bool -> string";
//integer origin
var i: int = 42;
assert (bool)i == true, "int -> bool";
assert (float)i == 42, "int -> float";
assert (string)i == "42", "int -> string";
//float origin
var f: float = 3.14;
assert (bool)f == true, "float -> bool";
assert (int)f == 3, "float -> int";
assert (string)f == "3.14", "float -> string";
//string origin
var s: string = "78.9";
assert (bool)s == true, "string -> bool";
assert (int)s == 78, "string -> int";
assert (float)s == 78.9, "string -> float";
print "All good";

View File

@@ -0,0 +1,27 @@
//test numbers
assert 1 < 2, "1 < 2";
assert 1 == 1, "1 == 1";
assert 2 > 1, "2 > 1";
assert 1 <= 2, "1 <= 2";
assert 2 >= 1, "2 >= 1";
//test variables
var a = 1;
var b = 2;
assert a < b, "a < b";
assert a == a, "a == a";
assert b > a, "b > a";
assert a <= b, "a <= b";
assert b >= a, "b >= a";
//test negation
assert !false, "!false";
var c = false;
assert !c, "!c";
print "All good";

View File

@@ -0,0 +1,55 @@
//test function return
fn testFourtyTwo() {
return 42;
}
assert testFourtyTwo() == 42, "function returns failed";
//test function parameters
fn identity(x) {
return x;
}
assert identity("hello world") == "hello world", "identity function failed";
//test closures
fn make() {
var counter = 0;
fn count() {
return ++counter;
}
return count;
}
var tally = make();
assert tally() == 1 && tally() == 2, "Closures failed";
//test expressions as arguments
fn argFn() {
return 42;
}
fn outerFn(val) {
assert val == 42, "expression as argument failed";
}
outerFn(argFn());
//test extra parameters
fn extra(one, two, ...rest) {
assert rest == ["three", "four", "five", "six", "seven"], "rest parameters failed";
}
extra("one", "two", "three", "four", "five", "six", "seven");
print "All good";

82
scripts/test/jumps.toy Normal file
View File

@@ -0,0 +1,82 @@
//test true jump
if (true) {
assert true, "if-then failed (1)";
}
else {
assert false, "if-then failed (2)";
}
//test false jump
if (false) {
assert false, "if-then failed (3)";
}
else {
assert true, "if-then failed (4)";
}
//test while loop
var whileCounter = 0;
while (whileCounter < 10) {
whileCounter = whileCounter + 1;
}
assert whileCounter == 10, "while-loop failed";
//test for loop
var forCache = 0;
for (var i = 0; i < 20; i++) {
forCache = i;
}
assert forCache == 19, "for-loop failed";
//test break - while
var breakWhileCache = 0;
while(true) {
breakWhileCache = breakWhileCache + 1;
if (breakWhileCache >= 7) {
break;
}
}
assert breakWhileCache == 7, "break-while failed";
//test continue - while
var continueWhileCache = 0;
while (continueWhileCache < 10) {
continueWhileCache = continueWhileCache + 1;
if (continueWhileCache >= 7) {
continue;
}
assert continueWhileCache < 7, "continue-while failed";
}
//test break - for
for (var i = 0; i < 10; i++) {
if (i >= 7) {
break;
}
assert i < 7, "break-for failed";
}
//test break - continue
for (var i = 0; i < 10; i++) {
if (i >= 7) {
continue;
}
assert i < 7, "continue-for failed";
}
print "All good";

View File

@@ -0,0 +1,6 @@
assert true && true, "boolean and failed";
assert true || true, "boolean or failed";
assert false || true && true, "boolen precedence failed";
print "All good";

339
scripts/test/long-array.toy Normal file
View File

@@ -0,0 +1,339 @@
var arr: [string] = [
"0",
"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"
];
print arr;

View File

@@ -0,0 +1,4 @@
//NOTE: dictionaries are NOT stored in order
var dict: [string, string] = ["0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"10","11":"11","12":"12","13":"13","14":"14","15":"15","16":"16","17":"17","18":"18","19":"19","20":"20","21":"21","22":"22","23":"23","24":"24","25":"25","26":"26","27":"27","28":"28","29":"29","30":"30","31":"31","32":"32","33":"33","34":"34","35":"35","36":"36","37":"37","38":"38","39":"39","40":"40","41":"41","42":"42","43":"43","44":"44","45":"45","46":"46","47":"47","48":"48","49":"49","50":"50","51":"51","52":"52","53":"53","54":"54","55":"55","56":"56","57":"57","58":"58","59":"59","60":"60","61":"61","62":"62","63":"63","64":"64","65":"65","66":"66","67":"67","68":"68","69":"69","70":"70","71":"71","72":"72","73":"73","74":"74","75":"75","76":"76","77":"77","78":"78","79":"79","80":"80","81":"81","82":"82","83":"83","84":"84","85":"85","86":"86","87":"87","88":"88","89":"89","90":"90","91":"91","92":"92","93":"93","94":"94","95":"95","96":"96","97":"97","98":"98","99":"99","100":"100","101":"101","102":"102","103":"103","104":"104","105":"105","106":"106","107":"107","108":"108","109":"109","110":"110","111":"111","112":"112","113":"113","114":"114","115":"115","116":"116","117":"117","118":"118","119":"119","120":"120","121":"121","122":"122","123":"123","124":"124","125":"125","126":"126","127":"127","128":"128","129":"129","130":"130","131":"131","132":"132","133":"133","134":"134","135":"135","136":"136","137":"137","138":"138","139":"139","140":"140","141":"141","142":"142","143":"143","144":"144","145":"145","146":"146","147":"147","148":"148","149":"149","150":"150","151":"151","152":"152","153":"153","154":"154","155":"155","156":"156","157":"157","158":"158","159":"159","160":"160","161":"161","162":"162","163":"163","164":"164","165":"165","166":"166","167":"167","168":"168","169":"169","170":"170","171":"171","172":"172","173":"173","174":"174","175":"175","176":"176","177":"177","178":"178","179":"179","180":"180","181":"181","182":"182","183":"183","184":"184","185":"185","186":"186","187":"187","188":"188","189":"189","190":"190","191":"191","192":"192","193":"193","194":"194","195":"195","196":"196","197":"197","198":"198","199":"199","200":"200","201":"201","202":"202","203":"203","204":"204","205":"205","206":"206","207":"207","208":"208","209":"209","210":"210","211":"211","212":"212","213":"213","214":"214","215":"215","216":"216","217":"217","218":"218","219":"219","220":"220","221":"221","222":"222","223":"223","224":"224","225":"225","226":"226","227":"227","228":"228","229":"229","230":"230","231":"231","232":"232","233":"233","234":"234","235":"235","236":"236","237":"237","238":"238","239":"239","240":"240","241":"241","242":"242","243":"243","244":"244","245":"245","246":"246","247":"247","248":"248","249":"249","250":"250","251":"251","252":"252","253":"253","254":"254","255":"255","256":"256","257":"257","258":"258","259":"259","260":"260","261":"261","262":"262","263":"263","264":"264","265":"265","266":"266","267":"267","268":"268","269":"269","270":"270","271":"271","272":"272","273":"273","274":"274","275":"275","276":"276","277":"277","278":"278","279":"279","280":"280","281":"281","282":"282","283":"283","284":"284","285":"285","286":"286","287":"287","288":"288","289":"289","290":"290","291":"291","292":"292","293":"293","294":"294","295":"295","296":"296","297":"297","298":"298","299":"299"];
print dict;

View File

@@ -0,0 +1,335 @@
print 0;
print 1;
print 2;
print 3;
print 4;
print 5;
print 6;
print 7;
print 8;
print 9;
print 10;
print 11;
print 12;
print 13;
print 14;
print 15;
print 16;
print 17;
print 18;
print 19;
print 20;
print 21;
print 22;
print 23;
print 24;
print 25;
print 26;
print 27;
print 28;
print 29;
print 30;
print 31;
print 32;
print 33;
print 34;
print 35;
print 36;
print 37;
print 38;
print 39;
print 40;
print 41;
print 42;
print 43;
print 44;
print 45;
print 46;
print 47;
print 48;
print 49;
print 50;
print 51;
print 52;
print 53;
print 54;
print 55;
print 56;
print 57;
print 58;
print 59;
print 60;
print 61;
print 62;
print 63;
print 64;
print 65;
print 66;
print 67;
print 68;
print 69;
print 70;
print 71;
print 72;
print 73;
print 74;
print 75;
print 76;
print 77;
print 78;
print 79;
print 80;
print 81;
print 82;
print 83;
print 84;
print 85;
print 86;
print 87;
print 88;
print 89;
print 90;
print 91;
print 92;
print 93;
print 94;
print 95;
print 96;
print 97;
print 98;
print 99;
//-------------------------
print 100;
print 101;
print 102;
print 103;
print 104;
print 105;
print 106;
print 107;
print 108;
print 109;
print 110;
print 111;
print 112;
print 113;
print 114;
print 115;
print 116;
print 117;
print 118;
print 119;
print 120;
print 121;
print 122;
print 123;
print 124;
print 125;
print 126;
print 127;
print 128;
print 129;
print 130;
print 131;
print 132;
print 133;
print 134;
print 135;
print 136;
print 137;
print 138;
print 139;
print 140;
print 141;
print 142;
print 143;
print 144;
print 145;
print 146;
print 147;
print 148;
print 149;
print 150;
print 151;
print 152;
print 153;
print 154;
print 155;
print 156;
print 157;
print 158;
print 159;
print 160;
print 161;
print 162;
print 163;
print 164;
print 165;
print 166;
print 167;
print 168;
print 169;
print 170;
print 171;
print 172;
print 173;
print 174;
print 175;
print 176;
print 177;
print 178;
print 179;
print 180;
print 181;
print 182;
print 183;
print 184;
print 185;
print 186;
print 187;
print 188;
print 189;
print 190;
print 191;
print 192;
print 193;
print 194;
print 195;
print 196;
print 197;
print 198;
print 199;
//-------------------------
print 200;
print 201;
print 202;
print 203;
print 204;
print 205;
print 206;
print 207;
print 208;
print 209;
print 210;
print 211;
print 212;
print 213;
print 214;
print 215;
print 216;
print 217;
print 218;
print 219;
print 220;
print 221;
print 222;
print 223;
print 224;
print 225;
print 226;
print 227;
print 228;
print 229;
print 230;
print 231;
print 232;
print 233;
print 234;
print 235;
print 236;
print 237;
print 238;
print 239;
print 240;
print 241;
print 242;
print 243;
print 244;
print 245;
print 246;
print 247;
print 248;
print 249;
print 250;
print 251;
print 252;
print 253;
print 254;
print 255;
print 256;
print 257;
print 258;
print 259;
print 260;
print 261;
print 262;
print 263;
print 264;
print 265;
print 266;
print 267;
print 268;
print 269;
print 270;
print 271;
print 272;
print 273;
print 274;
print 275;
print 276;
print 277;
print 278;
print 279;
print 280;
print 281;
print 282;
print 283;
print 284;
print 285;
print 286;
print 287;
print 288;
print 289;
print 290;
print 291;
print 292;
print 293;
print 294;
print 295;
print 296;
print 297;
print 298;
print 299;
//-------------------------

View File

@@ -0,0 +1,87 @@
{
//test arrays without types
var array = [];
_push(array, 1);
_push(array, 2);
_push(array, 3);
_push(array, 4);
_push(array, "foo");
assert _length(array) == 5, "_push or _length failed with array";
assert _pop(array) == "foo", "_pop failed with array";
_set(array, 2, "bar");
assert array == [1, 2, "bar", 4], "_set failed with array";
assert _get(array, 3) == 4, "_get failed with array";
//test dictionaries without types
var dict = [:];
_set(dict, "key", "value");
_set(dict, 1, 2);
assert dict == ["key":"value", 1:2], "_set failed with dictionaries";
assert _get(dict, "key") == "value", "_get failed with dictionaries";
//test _length
assert _length(array) == 4 && _length(dict) == 2, "_length failed with array or dictionaries";
//test clear
_clear(array);
_clear(dict);
assert _length(array) == 0 && _length(dict) == 0, "_clear failed with array or dictionaries";
}
{
//test arrays with types
var array: [int] = [];
_push(array, 1);
_push(array, 2);
_push(array, 3);
_push(array, 4);
_push(array, 10);
assert _length(array) == 5, "_push or _length failed with array (+ types)";
assert _pop(array) == 10, "_pop failed with array (+ types)";
_set(array, 2, 70);
assert array == [1, 2, 70, 4], "_set failed with array (+ types)";
assert _get(array, 3) == 4, "_get failed with array (+ types)";
//test dictionaries with types
var dict: [string, string] = [:];
_set(dict, "key", "value");
assert dict == ["key":"value"], "_set failed with dictionaries (+ types)";
assert _get(dict, "key") == "value", "_get failed with dictionaries (+ types)";
//test length with types
assert _length(array) == 4 && _length(dict) == 1, "_length failed with array or dictionaries (+ types)";
//test clear with types
_clear(array);
_clear(dict);
assert _length(array) == 0 && _length(dict) == 0, "_clear failed with array or dictionaries (+ types)";
}
{
var str = "hello world";
assert _length(str) == 11, "_length failed with string";
}
print "All good";

View File

@@ -0,0 +1,8 @@
fn panic() {
assert false, "This should only be seen once";
}
panic();
panic();

25
scripts/test/types.toy Normal file
View File

@@ -0,0 +1,25 @@
//basic types
var t: type = int;
var u: t = 42;
assert t == int, "types are not first class";
assert u == 42, "first-class types are screwing with values";
//differentiate by the "type" value
var v: type = type [int];
var w = [int];
var x = v;
assert w == [int], "defining an array of types failed";
assert x == type [int], "re-assigning a type value failed";
//complex type
var complex: type = type [string, [int]];
var dict: complex = [
"first array": [1, 2, 3],
"second array": [4, 5, 6],
"third array": [7, 8, 9]
];
print "All good";