Files
Toy/test/scripts/lib/standard.toy
2023-07-31 13:56:06 -04:00

490 lines
11 KiB
Plaintext

import standard;
//test clock
{
//this depends on external factors, so only check the length
assert clock().length() == 24, "clock().length() failed";
}
//test hash
{
assert typeof "Hello world".hash() == int, "typeof \"Hello world\".hash() failed";
assert "Hello world".hash() == 994097935, "\"Hello world\".hash() failed"; //NOTE: specific value based on algorithm
}
//test abs
{
assert abs(-5) == 5, "abs(-integer) failed";
assert abs(-5.5) == 5.5, "abs(-float) failed";
assert abs(5) == 5, "abs(+integer) failed";
assert abs(5.5) == 5.5, "abs(+float) failed";
var x = -5;
assert x.abs() == 5, "var.abs() failed";
}
//test ceil
{
assert ceil(4) == 4, "ceil(int) failed";
assert ceil(4.0) == 4, "ceil(float) failed";
assert ceil(4.1) == 5, "ceil() failed";
var x = 4.1;
assert x.ceil() == 5, "var.ceil() failed";
}
//test floor
{
assert floor(4) == 4, "floor(int) failed";
assert floor(4.0) == 4, "floor(float) failed";
assert floor(4.1) == 4, "floor() failed";
var x = 4.1;
assert x.floor() == 4, "var.floor() failed";
}
//test max
{
assert max(1, 2, 3) == 3, "max() failed";
var a = 1;
var b = 2;
var c = 3;
assert max(a, b, c) == 3, "var.max() failed";
assert max(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) == 9, "max() with many args failed";
assert typeof max(1, 2, 3) == int, "typeof max() == int failed";
assert typeof max(1, 2, 3.4) == float, "typeof max() == float failed";
}
//test min
{
assert min(1, 2, 3) == 1, "min() failed";
var a = 1;
var b = 2;
var c = 3;
assert min(a, b, c) == 1, "var.min() failed";
assert min(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) == 0, "min() with many args failed";
assert typeof min(1, 2, 3) == int, "typeof min() == int failed";
assert typeof min(1, 2, 3.4) == float, "typeof min() == float failed";
}
//test round
{
assert round(4) == 4, "round(int) failed";
assert round(4.0) == 4, "round(float) failed";
assert round(4.1) == 4, "round(less than half) failed";
assert round(4.9) == 5, "round(greater than half) failed";
assert round(4.5) == 5, "round(exactly half) failed";
var x = 4.1;
assert x.round() == 4, "var.round() failed";
assert typeof round(1.0) == int, "typeof round() == int failed";
}
//test sign
{
assert sign(4) == 1, "sign(int) failed";
assert sign(-4) == -1, "sign(-int) failed";
assert sign(4.1) == 1, "sign(float) failed";
assert sign(-4.1) == -1, "sign(-float) failed";
assert sign(0) == 1, "sign(0) failed";
var x = 4.1;
assert x.sign() == 1, "var.sign() failed";
assert typeof sign(1.0) == int, "typeof sign() == int failed";
}
//test normalize
{
assert normalize(4) == 1, "normalize(int) failed";
assert normalize(-4) == -1, "normalize(-int) failed";
assert normalize(4.1) == 1, "normalize(float) failed";
assert normalize(-4.1) == -1, "normalize(-float) failed";
assert normalize(0) == 0, "normalize(0) failed";
var x = 4.1;
assert x.normalize() == 1, "var.normalize() failed";
assert typeof normalize(1.0) == int, "typeof normalize() == int failed";
}
//test clamp
{
assert clamp(1, 0, 5) == 1, "clamp(1, 0, 5) failed";
assert clamp(0, 1, 5) == 1, "clamp(0, 1, 5) failed";
assert clamp(10, 1, 5) == 5, "clamp(10, 1, 5) failed";
assert clamp(1.0, 0.0, 5.0) == 1, "clamp(1.0, 0.0, 5.0) failed";
assert clamp(0.0, 1.0, 5.0) == 1, "clamp(0.0, 1.0, 5.0) failed";
assert clamp(10.0, 1.0, 5.0) == 5, "clamp(10.0, 1.0, 5.0) failed";
assert typeof clamp(10, 1, 5) == int, "typeof clamp(10, 1, 5) == int failed";
assert typeof clamp(10.0, 1, 5) == int, "typeof clamp(10.0, 1, 5) == int failed";
assert typeof clamp(10, 1, 5.0) == float, "typeof clamp(10, 1, 5.0) == float failed";
}
//test lerp
{
assert lerp(0, 10, 0.5) == 5, "lerp 50% failed";
assert lerp(0, 10, 1.5) == 15, "lerp 150% failed";
assert typeof lerp(0, 10, 0) == float, "typeof lerp result failed";
}
// test toRad
{
assert toRad(0) == 0, "toRad 0° failed";
assert toRad(180) == PI, "toRad 180° failed";
assert toRad(360) == 2 * PI, "toRad 360° failed";
}
// test toDeg
{
assert toDeg(0) == 0, "toDeg 0 failed";
assert toDeg(PI) == 180, "toDeg π failed";
assert toDeg(2 * PI) == 360, "toDeg 2π failed";
}
//test concat
{
//test array concat
{
var a = [1, 2, 3];
var b = [4, 5, 6];
var c = a.concat(b).concat(b);
assert c == [1, 2, 3, 4, 5, 6, 4, 5, 6], "array.concat() failed";
}
//test dictionary concat
{
var a = ["one" : 1, "two": 2, "three": 3];
var b = ["four" : 4, "five": 5, "six": 6];
var c = a.concat(b);
assert c.length() == 6, "dictionary.concat().length() failed";
assert c == ["one" : 1, "two": 2, "three": 3, "four" : 4, "five": 5, "six": 6], "dictionary.concat() comparison failed";
}
//test dictionary concat with clashing keys
{
var a = ["one" : 1, "two": 2, "three": 3, "random": 1];
var b = ["four" : 4, "five": 5, "six": 6, "random": 2];
var c = a.concat(b);
assert c["random"] == 1, "dictionary.concat() clashing keys failed";
}
//test string concat
{
var a = "foo";
var b = "bar";
var c = a.concat(b);
assert c == "foobar", "string.concat() failed";
}
}
//test containsKey
{
var d = ["one": 1, "two": 2];
assert d.containsKey("one") == true, "dictionary.containsKey() == true failed";
assert d.containsKey("three") == false, "dictionary.containsKey() == false failed";
}
//test containsValue
{
var a = [1, 2, 3];
var d = ["one": 1, "two": 2];
assert a.containsValue(1) == true, "array.containsValue() == true failed";
assert a.containsValue(5) == false, "array.containsValue() == false failed";
assert d.containsValue(1) == true, "dictionary.containsValue() == true failed";
assert d.containsValue(3) == false, "dictionary.containsValue() == false failed";
}
//test every
{
var a = [1, 2, 3];
var d = ["one": 1, "two": 2];
var counter = 0;
fn f(k, v) {
counter++;
return v;
}
assert a.every(f) == true, "array.every() == true failed";
assert d.every(f) == true, "dictionary.every() == true failed";
assert counter == 5, "Unexpected number of calls for _every() == true";
counter = 0;
a[1] = false;
d["two"] = false;
assert a.every(f) == false, "array.every() == false failed";
assert d.every(f) == false, "dictionary.every() == false failed";
assert counter == 4, "Unexpected number of calls for _every() == false";
}
//test filter
{
var a = [1, 2, 3, 4];
var d = ["one": 1, "two": 2, "three": 3, "four": 4];
fn f(k, v) {
return v % 2 == 0;
}
assert a.filter(f) == [2, 4], "array.filter() failed";
assert d.filter(f) == ["two": 2, "four": 4], "dictionary.filter() failed";
}
//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];
var a = d.getKeys();
assert a.length() == 2, "_getKeys() length failed";
//NOTE: dependant on hash algorithm
assert a == ["bar", "foo"], "_getKeys() result failed";
}
//test getValues
{
var d = ["foo": 1, "bar": 2];
var a = d.getValues();
assert a.length() == 2, "_getValues() length failed";
//NOTE: dependant on hash algorithm
assert a == [2, 1], "_getValues() result failed";
}
//test indexOf
{
var a = [1, 2, 42, 3];
//results are zero-indexed
assert a.indexOf(42) == 2, "_indexOf() failed";
assert a.indexOf(4) == null, "_indexOf() == null failed";
}
//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 reduce
{
var a = [1, 2, 3, 4];
var d = ["one": 1, "two": 2, "three": 3, "four": 4];
fn f(acc, k, v) {
return acc + v;
}
assert a.reduce(0, f) == 10, "array.reduce() failed";
assert d.reduce(0, f) == 10, "dictionary.reduce() failed";
}
//test some
{
var a = [false, false, false];
var d = ["one": false, "two": false];
var counter = 0;
fn f(k, v) {
counter++;
return v;
}
assert a.some(f) == false, "array.some() == false failed";
assert d.some(f) == false, "dictionary.some() == false failed";
assert counter == 5, "Unexpected number of calls for _some() == false";
counter = 0;
a[1] = true;
d["two"] = true;
assert a.some(f) == true, "array.some() == true failed";
assert d.some(f) == true, "dictionary.some() == true failed";
assert counter == 4, "Unexpected number of calls for _some() == true";
}
//test sort
{
fn less(a, b) {
return a < b;
}
fn greater(a, b) {
return a > b;
}
var a = [7, 2, 1, 8, 6, 3, 5, 4];
var b = [7, 2, 1, 4, 6, 3, 5, 8];
var c = [1, 2, 3, 4, 5, 6, 7, 8];
var d = [7, 2, 1, 8, 6, 3, 5, 4];
a = a.sort(less);
b = b.sort(less);
c = c.sort(less);
d = d.sort(greater);
assert a == [1, 2, 3, 4, 5, 6, 7, 8], "array.sort(less) failed";
assert b == [1, 2, 3, 4, 5, 6, 7, 8], "array.sort(less) with pivot high failed";
assert c == [1, 2, 3, 4, 5, 6, 7, 8], "array.sort(less) pre-sorted array failed";
assert d == [8, 7, 6, 5, 4, 3, 2, 1], "array.sort(greater) failed";
}
//test toLower
{
assert "Hello World".toLower() == "hello world", "_toLower() failed";
}
//test toString
{
var a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var s = a.toString();
assert s == "[[1,2,3],[4,5,6],[7,8,9]]", "array._toString() failed";
}
//test toUpper
{
assert "Hello World".toUpper() == "HELLO WORLD", "_toUpper() failed";
}
//test trim defaults
{
{
//test a bunch
fn test(s, pass) {
var result = s.trim();
assert result == pass, "_trim(" + result + ") failed";
}
test("hello world", "hello world");
test(" hello world", "hello world");
test("hello world ", "hello world");
test(" hello world ", "hello world");
test(" hello world", "hello world");
test("hello world ", "hello world");
test(" hello world ", "hello world");
test(" hello world", "hello world");
test("hello world ", "hello world");
test(" hello world ", "hello world");
//one for goot luck
assert " hello world ".trim() == "hello world", "hello world.trim() failed";
}
//test trim custom values
{
var chars = "heliod";
assert "hello world".trim(chars) == " wor", "custom _trim() failed";
}
//test trimBegin() & trimEnd()
assert " foo ".trimBegin() == "foo ", "string.trimBegin() failed";
assert " foo ".trimEnd() == " foo", "string.trimBegin() failed";
}
print "All good";