LSL Language Test 2
<lsl> integer gTestsPassed = 0; integer gTestsFailed = 0;
testPassed(string description, string actual, string expected) {
++gTestsPassed; //llSay(0, description);
}
testFailed(string description, string actual, string expected) {
++gTestsFailed; llSay(0, "FAILED!: " + description + " (" + actual + " expected " + expected + ")");
}
ensureTrue(string description, integer actual) {
if(actual) { testPassed(description, (string) actual, (string) TRUE); } else { testFailed(description, (string) actual, (string) TRUE); }
}
ensureFalse(string description, integer actual) {
if(actual) { testFailed(description, (string) actual, (string) FALSE); } else { testPassed(description, (string) actual, (string) FALSE); }
}
ensureIntegerEqual(string description, integer actual, integer expected) {
if(actual == expected) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
integer floatEqual(float actual, float expected) {
float error = llFabs(expected - actual); float epsilon = 0.001; if(error > epsilon) { llSay(0,"Float equality delta " + (string)error); return FALSE; } return TRUE;
}
ensureFloatEqual(string description, float actual, float expected) {
if(floatEqual(actual, expected)) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureFloatExactEqual(string description, float actual, float expected) {
if(actual == expected) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureStringEqual(string description, string actual, string expected) {
if(actual == expected) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureKeyEqual(string description, key actual, key expected) {
if(actual == expected) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureVectorEqual(string description, vector actual, vector expected) {
if(floatEqual(actual.x, expected.x) && floatEqual(actual.y, expected.y) && floatEqual(actual.z, expected.z)) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureRotationEqual(string description, rotation actual, rotation expected) {
if(floatEqual(actual.x, expected.x) && floatEqual(actual.y, expected.y) && floatEqual(actual.z, expected.z) && floatEqual(actual.s, expected.s)) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
ensureListEqual(string description, list actual, list expected) {
if(actual == expected) { testPassed(description, (string) actual, (string) expected); } else { testFailed(description, (string) actual, (string) expected); }
}
vector gVector;
float testReturnFloat() {
return 1.0;
}
testArgumentAccessor(vector v) {
v = <0,0,0>; v.x = testReturnFloat(); v.y = testReturnFloat(); v.z = testReturnFloat(); ensureVectorEqual("testArgumentAccessor", v, <1,1,1>);
}
testLocalAccessor() {
vector v = <0,0,0>; v.x = testReturnFloat(); v.y = testReturnFloat(); v.z = testReturnFloat(); ensureVectorEqual("testLocalAccessor", v, <1,1,1>);
}
testGlobalAccessor() {
gVector = <0,0,0>; gVector.x = testReturnFloat(); gVector.y = testReturnFloat(); gVector.z = testReturnFloat(); ensureVectorEqual("testGlobalAccessor", gVector, <1,1,1>);
}
tests() {
ensureFloatExactEqual("1.4e-45 == (float)\"1.4e-45\"", 1.4e-45, (float)"1.4e-45"); // Managed pointer save/restore tests. testArgumentAccessor(<0,0,0>); testLocalAccessor(); testGlobalAccessor();
// Multiple list element saving (SVC-1712 Regression) ensureStringEqual("(string)[1, testReturnFloat(), testReturnFloat()] == \"11.01.0\"", (string)[1, testReturnFloat(), testReturnFloat()], "11.0000001.000000");
// Key equality (SVC-1720 Regression) key k = "foo"; key k2 = "foo"; ensureKeyEqual("k = \"foo\";k2 = \"foo\";k == k2", k, k2);
// List comparison. Not actual comparison, just length. ensureIntegerEqual("[] == []", [] == [], 1); ensureIntegerEqual("[1] == [2]", [1] == [2], 1); ensureIntegerEqual("[1,2] == [1,2]", [1,2] == [1,2], 1); ensureIntegerEqual("[1,2] == [2]", [1,2] == [2], 0); ensureIntegerEqual("[1] == [2,3]", [1] == [2,3], 0); ensureIntegerEqual("[] != []", [] != [], 0); ensureIntegerEqual("[1] != [2]", [1] != [2], 0); ensureIntegerEqual("[1,2] != [1,2]", [1,2] != [1,2], 0); ensureIntegerEqual("[1,2] != [2]", [1,2] != [2], 1); ensureIntegerEqual("[1] != [2,3]", [1] != [2,3], -1); // Parsing numbers with junk after ensureFloatEqual("(float)\"5.0a\" == 5.0", (float) "5a", 5.0); ensureFloatEqual("(integer)\"12foo\" == 12", (integer) "12foo", 12); // List value semantics across addition (SVC-1448 Regression) list l; l = [42]; l + 1; ensureListEqual("list l = [42]; l + 1; l == [42]", l, [42]); l = [42]; l + 1.5; ensureListEqual("list l = [42]; l + 1.5; l == [42]", l, [42]); l = [42]; l + NULL_KEY; ensureListEqual("list l = [42]; l + NULL_KEY; l == [42]", l, [42]); l = [42]; l + <1,2,3>; ensureListEqual("list l = [42]; l + <1,2,3>; l == [42]", l, [42]); l = [42]; l + <1,2,3,4>; ensureListEqual("list l = [42]; l + <1,2,3>; l == [42]", l, [42]);
}
runTests() {
llResetTime(); tests(); llSay(0, "Ran " + (string)(gTestsPassed + gTestsFailed) + " tests in " + (string) llGetTime() + " seconds with " + (string)gTestsFailed + " failures");
}
default {
state_entry() { runTests(); } touch_start(integer total_number) { runTests(); }
} </lsl>