Files
Toy/test/scripts/lib/timer.toy
2022-11-12 11:30:24 +00:00

93 lines
2.4 KiB
Plaintext

//test the timer library
{
//create a timer, run it for a short period
import timer;
var timerA: opaque = startTimer();
for (var i: int = 0; i < 1000 * 1; i++);
var diffA: opaque = timerA.stopTimer();
//create another timer, run it for a longer period
var timerB: opaque = startTimer();
for (var i: int = 0; i < 1000 * 10; i++);
var diffB: opaque = timerB.stopTimer();
//check to ensure that the second timer took longer than the first
//WARNING: this has the small possibility of failing due to external factors
var difference: opaque = diffA.compareTimer(diffB);
assert difference.getTimerSeconds() >= 0, "compareTimer() (seconds) failed";
if (difference.getTimerSeconds() == 0) {
assert difference.getTimerMicroseconds() >= 0, "compareTimer() (microseconds) failed";
}
//all timers must be destroyed after use
timerA.destroyTimer();
timerB.destroyTimer();
diffA.destroyTimer();
diffB.destroyTimer();
difference.destroyTimer();
}
{
//create a timer, manipulate it's values
import timer;
//set the timer values manually
var timer: opaque = createTimer(42, 8891);
//check the timer values
assert timer.getTimerSeconds() == 42, "getTimerSeconds() failed";
assert timer.getTimerMicroseconds() == 8891, "getTimerMicroseconds() failed";
//all timers must be destroyed after use
timer.destroyTimer();
}
{
//set a timer to check string representation
import timer;
var timer: opaque = createTimer(42, 999);
assert timer.timerToString() == "42.000999", "timerToString() failed";
//all timers must be destroyed after use
timer.destroyTimer();
}
{
//test positive and negative values of timers
import timer;
var a = createTimer(1, 0);
var b = createTimer(2, 0);
var acmp = a.compareTimer(b);
var bcmp = b.compareTimer(a);
var c = createTimer(0, 1);
var d = createTimer(0, 2);
var ccmp = c.compareTimer(d);
var dcmp = d.compareTimer(c);
assert acmp.timerToString() == "1.000000", "positive and negative tests failed (acmp)";
assert bcmp.timerToString() == "-1.000000", "positive and negative tests failed (bcmp)";
assert ccmp.timerToString() == "0.000001", "positive and negative tests failed (ccmp)";
assert dcmp.timerToString() == "-0.000001", "positive and negative tests failed (dcmp)";
a.destroyTimer();
b.destroyTimer();
c.destroyTimer();
d.destroyTimer();
acmp.destroyTimer();
bcmp.destroyTimer();
ccmp.destroyTimer();
dcmp.destroyTimer();
}
print "All good";