LSL Language Test 2

From Second Life Wiki
Revision as of 08:51, 29 February 2008 by Leffard Lassard (talk | contribs) (Fixed the vector name in testGlobalAccessor)
Jump to navigation Jump to search

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

}

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

}

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>