Difference between revisions of "LSL Language Test 2"

From Second Life Wiki
Jump to navigation Jump to search
m (suggested addition to this test script to prevent accumulation of counts)
Line 277: Line 277:
     touch_start(integer total_number)
     touch_start(integer total_number)
     {
     {
        gTestsPassed = 0;
        gTestsFailed = 0;
         runTests();
         runTests();
     }
     }
}
}
</lsl>
</lsl>

Revision as of 07:59, 16 March 2008

<lsl> integer gTestsPassed = 0; integer gTestsFailed = 0;

key gNullKey = NULL_KEY; key gStringInAKey = "foo";

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]);


   // Conditional test of key (SVC-1776/SVC-1811 Regression)
   if(k)
   {
       testFailed("if(k)", "TRUE", "FALSE");
   }
   else
   {
       testPassed("if(k)", "FALSE", "FALSE");
   }

   // String/Key equality (SVC-1770 Regression)
   ensureIntegerEqual("k == \"foo\"",k == "foo", TRUE);

   // String/Key equality (SVC-1772 Regression)
   ensureIntegerEqual("gStringInAKey == \"foo\"", gStringInAKey == "foo", TRUE);
   ensureIntegerEqual("gNullKey == NULL_KEY", gNullKey == NULL_KEY, TRUE);

}

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)
   {
       gTestsPassed = 0;
       gTestsFailed = 0;
       runTests();
   }

} </lsl>