Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created September 14, 2012 18:32
Show Gist options
  • Save IQAndreas/3723796 to your computer and use it in GitHub Desktop.
Save IQAndreas/3723796 to your computer and use it in GitHub Desktop.
Testbench for Flixel's "round" function
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main()
{
trace("AdamAtomic's `round()` function");
test(Math.round, adamatomic_round);
trace("IQAndreas's `round()` function");
test(Math.round, iqandreas_round);
trace("Moly's `round()` function");
test(Math.round, moly_round);
}
public function adamatomic_round(Value:Number):Number
{
var number:Number = int(Value+((Value>0)?0.5:-0.5));
return (Value>0)?(number):((number!=Value)?(number-1):(number));
}
public function iqandreas_round(Value:Number):Number
{
var number:Number = int(Value+0.5);
return (Value>0)?(number):((number!=Value)?(number-1):(number));
}
public function moly_round(Value:Number):Number
{
return int(Value+((Value>0)?0.5:-0.5));
}
private function test(func1:Function, func2:Function, numTests:uint = 256):void
{
var i:int = 0;
var passedTests:uint = 0;
var totalTests:uint = 0;
for (i = 0; i < numTests; i++)
{
totalTests++;
var wholeNumber:Number = int(256 * Math.random()) - 128;
if (func1.call(null, wholeNumber) == func2.call(null, wholeNumber))
{
passedTests++;
}
else
{
//trace("WRONG", wholeNumber);
}
}
for (i = 0; i < numTests; i++)
{
totalTests++;
var decimalNumber:Number = (Math.random() * 256) - 128;
if (func1.call(null, decimalNumber) == func2.call(null, decimalNumber))
{
passedTests++;
}
else
{
//trace("WRONG", decimalNumber);
}
}
for (i = 0; i < numTests; i++)
{
totalTests++;
var tinyNumber:Number = (Math.random() * 2) - 1;
if (func1.call(null, tinyNumber) == func2.call(null, tinyNumber))
{
passedTests++;
}
else
{
//trace("WRONG", tinyNumber);
}
}
// Test -1
totalTests++;
if (func1.call(null, -1) == func2.call(null, -1))
{
passedTests++;
}
else
{
//trace("WRONG", -1);
}
// Test 0
totalTests++;
if (func1.call(null, 0) == func2.call(null, 0))
{
passedTests++;
}
else
{
//trace("WRONG", 0);
}
// Test 1
totalTests++;
if (func1.call(null, 1) == func2.call(null, 1))
{
passedTests++;
}
else
{
//trace("WRONG", 1);
}
trace(" >", (passedTests == totalTests) ? "SUCCESS!" : "FAILURE!");
trace(" > Passed", passedTests, "of", totalTests, "tests.\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment