Created
January 11, 2016 19:46
-
-
Save JCThePants/cd3430d105ca67959b5c to your computer and use it in GitHub Desktop.
Test for checking value falls in clamped range for Citizens 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hasMoved = true, | |
hasNotMoved = false; | |
// check ideal values | |
assert(0, 45, hasNotMoved); | |
assert(0, 91, hasMoved); | |
assert(0, -91, hasMoved); | |
assert(90, clamp(181), hasMoved); | |
assert(90, 180, hasNotMoved); | |
assert(90, -1, hasMoved); | |
assert(90, 0, hasNotMoved); | |
// check edge values | |
assert(-179, clamp(-185), hasNotMoved); | |
assert(-179, clamp(-195), hasNotMoved); | |
assert(-179, clamp(-205), hasNotMoved); | |
assert(-179, clamp(-215), hasNotMoved); | |
assert(-179, clamp(-185), hasNotMoved); | |
assert(-179, clamp(-195), hasNotMoved); | |
assert(-179, clamp(-205), hasNotMoved); | |
assert(-179, clamp(-215), hasNotMoved); | |
assert(179, clamp(185), hasNotMoved); | |
assert(179, clamp(195), hasNotMoved); | |
assert(179, clamp(205), hasNotMoved); | |
assert(179, clamp(215), hasNotMoved); | |
assert(179, clamp(185), hasNotMoved); | |
assert(179, clamp(195), hasNotMoved); | |
assert(179, clamp(205), hasNotMoved); | |
assert(179, clamp(215), hasNotMoved); | |
function assert(initYaw, currentYaw, expected) { | |
assertOld(initYaw, currentYaw, expected); | |
assertNew(initYaw, currentYaw, expected); | |
} | |
// assert OLD Algorithm correctness | |
function assertOld(initYaw, currentYaw, expected) { | |
var upperBound = initYaw + 90.0; | |
var lowerBound = initYaw - 90.0; | |
var hasMoved = currentYaw < lowerBound || currentYaw > upperBound; | |
// assert | |
if (hasMoved !== expected) | |
console.log('OLD ASSERTION FAIL: ' + expected + '!==' + hasMoved + '; yaw:' + currentYaw + ', lower:' + lowerBound + ', upper:' + upperBound); | |
} | |
// assert NEW Algorithm correctness | |
function assertNew(initYaw, currentYaw, expected) { | |
initYaw = clamp(initYaw); | |
var upperBound = clamp(initYaw + 90.0); | |
var lowerBound = clamp(initYaw - 90.0); | |
if (upperBound === -180.0 && initYaw > 0) { | |
upperBound = 180; | |
} | |
var hasMoved; | |
if (initYaw - 90.0 < -180.0 || initYaw + 90.0 > 180.0) { | |
hasMoved = currentYaw > lowerBound && currentYaw < upperBound; | |
} | |
else { | |
hasMoved = currentYaw < lowerBound || currentYaw > upperBound; | |
} | |
// assert | |
if (hasMoved !== expected) | |
console.log('NEW ASSERTION FAIL: ' + expected + '!==' + hasMoved + '; init: ' + initYaw + ', yaw:' + currentYaw); | |
} | |
function clamp(yaw) { | |
while (yaw < -180.0) { | |
yaw += 360.0; | |
} | |
while (yaw >= 180.0) { | |
yaw -= 360.0; | |
} | |
return yaw; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment