Difference between revisions of "LSL Language Test 2"

From Second Life Wiki
Jump to navigation Jump to search
Line 1: Line 1:
<lsl>integer gTestsPassed = 0;
<lsl>
integer gTestsPassed = 0;
integer gTestsFailed = 0;
integer gTestsFailed = 0;
 
key gNullKey = NULL_KEY;
key gNullKey = NULL_KEY;
key gStringInAKey = "stringInAKey";
key gStringInAKey = "foo";
   
   
testPassed(string description, string actual, string expected)
testPassed(string description, string actual, string expected)
Line 88: Line 89:
     }
     }
}
}
 
ensureStringEqual(string description, string actual, string expected)
ensureStringEqual(string description, string actual, string expected)
{
{
Line 155: Line 156:
   
   
vector gVector;
vector gVector;
 
float testReturnFloat()
float testReturnFloat()
{
{
     return 1.0;
     return 1.0;
}
}
 
testArgumentAccessor(vector v)
testArgumentAccessor(vector v)
{
{
Line 169: Line 170:
     ensureVectorEqual("testArgumentAccessor", v, <1,1,1>);
     ensureVectorEqual("testArgumentAccessor", v, <1,1,1>);
}
}
 
testLocalAccessor()
testLocalAccessor()
{
{
Line 178: Line 179:
     ensureVectorEqual("testLocalAccessor", v, <1,1,1>);
     ensureVectorEqual("testLocalAccessor", v, <1,1,1>);
}
}
 
testGlobalAccessor()
testGlobalAccessor()
{
{
Line 187: Line 188:
     ensureVectorEqual("testGlobalAccessor", gVector, <1,1,1>);
     ensureVectorEqual("testGlobalAccessor", gVector, <1,1,1>);
}
}
 
tests()
tests()
{
{
     ensureFloatExactEqual("1.4e-45 == (float)\"1.4e-45\"", 1.4e-45, (float)"1.4e-45");
     ensureFloatExactEqual("1.4e-45 == (float)\"1.4e-45\"", 1.4e-45, (float)"1.4e-45");
   
     // Managed pointer save/restore tests.
     // Managed pointer save/restore tests.
     testArgumentAccessor(<0,0,0>);
     testArgumentAccessor(<0,0,0>);
     testLocalAccessor();
     testLocalAccessor();
     testGlobalAccessor();
     testGlobalAccessor();
 
     // Multiple list element saving (SVC-1712 Regression)
     // Multiple list element saving (SVC-1712 Regression)
     ensureStringEqual("(string)[1, testReturnFloat(), testReturnFloat()] == \"11.01.0\"", (string)[1, testReturnFloat(), testReturnFloat()], "11.0000001.000000");
     ensureStringEqual("(string)[1, testReturnFloat(), testReturnFloat()] == \"11.01.0\"", (string)[1, testReturnFloat(), testReturnFloat()], "11.0000001.000000");
 
     // Key equality (SVC-1720 Regression)
     // Key equality (SVC-1720 Regression)
     key k = "foo";
     key k = "foo";
     key k2 = "foo";
     key k2 = "foo";
     ensureKeyEqual("k = \"foo\";k2 = \"foo\";k == k2", k, k2);
     ensureKeyEqual("k = \"foo\";k2 = \"foo\";k == k2", k, k2);
 
     // List comparison. Not actual comparison, just length.
     // List comparison. Not actual comparison, just length.
     ensureIntegerEqual("[] == []", [] == [], 1);
     ensureIntegerEqual("[] == []", [] == [], 1);
Line 211: Line 212:
     ensureIntegerEqual("[1,2] == [2]", [1,2] == [2], 0);
     ensureIntegerEqual("[1,2] == [2]", [1,2] == [2], 0);
     ensureIntegerEqual("[1] == [2,3]", [1] == [2,3], 0);
     ensureIntegerEqual("[1] == [2,3]", [1] == [2,3], 0);
   
     ensureIntegerEqual("[] != []", [] != [], 0);
     ensureIntegerEqual("[] != []", [] != [], 0);
     ensureIntegerEqual("[1] != [2]", [1] != [2], 0);
     ensureIntegerEqual("[1] != [2]", [1] != [2], 0);
Line 217: Line 218:
     ensureIntegerEqual("[1,2] != [2]", [1,2] != [2], 1);
     ensureIntegerEqual("[1,2] != [2]", [1,2] != [2], 1);
     ensureIntegerEqual("[1] != [2,3]", [1] != [2,3], -1);
     ensureIntegerEqual("[1] != [2,3]", [1] != [2,3], -1);
   
     // Parsing numbers with junk after
     // Parsing numbers with junk after
     ensureFloatEqual("(float)\"5.0a\" == 5.0", (float) "5a", 5.0);
     ensureFloatEqual("(float)\"5.0a\" == 5.0", (float) "5a", 5.0);
     ensureFloatEqual("(integer)\"12foo\" == 12", (integer) "12foo", 12);
     ensureFloatEqual("(integer)\"12foo\" == 12", (integer) "12foo", 12);
   
     // List value semantics across addition (SVC-1448 Regression)
     // List value semantics across addition (SVC-1448 Regression)
     list l;
     list l;
Line 239: Line 240:
     l + <1,2,3,4>;     
     l + <1,2,3,4>;     
     ensureListEqual("list l = [42]; l + <1,2,3>; l == [42]", l, [42]);
     ensureListEqual("list l = [42]; l + <1,2,3>; l == [42]", l, [42]);
   
   
     // Conditional test of key (SVC-1776/SVC-1811 Regression)
     // Conditional test of key (SVC-1776/SVC-1811 Regression)
     if(k)
     if(k)
Line 250: Line 251:
         testPassed("if(k)", "FALSE", "FALSE");
         testPassed("if(k)", "FALSE", "FALSE");
     }
     }
   
     // String/Key equality (SVC-1770 Regression)
     // String/Key equality (SVC-1770 Regression)
     ensureIntegerEqual("k == \"foo\"",k == "foo", TRUE);
     ensureIntegerEqual("k == \"foo\"",k == "foo", TRUE);
 
     // String/Key equality (SVC-1772 Regression)
     // String/Key equality (SVC-1772 Regression)
     ensureIntegerEqual("gStringInAKey == \"foo\"", gNtringInAKey == "foo", TRUE);
     ensureIntegerEqual("gStringInAKey == \"foo\"", gStringInAKey == "foo", TRUE);
     ensureIntegerEqual("gNullKey == NULL_KEY", gNullKey == NULL_KEY, TRUE);
     ensureIntegerEqual("gNullKey == NULL_KEY", gNullKey == NULL_KEY, TRUE);
 
}
}
 
runTests()
runTests()
{
{
Line 278: Line 279:
         runTests();
         runTests();
     }
     }
}  
}
</lsl>
</lsl>

Revision as of 02:17, 13 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)
   {
       runTests();
   }

} </lsl>