Created
March 12, 2013 19:04
-
-
Save NigelKibodeaux/76fdfac4d37dc89e8b06 to your computer and use it in GitHub Desktop.
Robot logic unit tests
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
<script> | |
console = {}; | |
console.log = function(stuff) { | |
if (typeof stuff == 'object') | |
stuff = JSON.stringify(stuff); | |
document.write(stuff + '<br>'); | |
}; | |
tests = [ | |
{ | |
me: {x:5, y:5, angle:0}, | |
them: {x:5, y:10}, | |
bearing: 180, | |
turn: 180 | |
}, | |
{ | |
me: {x:5, y:5, angle:90}, | |
them: {x:5, y:10}, | |
bearing: 180, | |
turn: 90 | |
}, | |
{ | |
me: {x:5, y:5, angle:315}, | |
them: {x:5, y:10}, | |
bearing: 180, | |
turn: -135 | |
}, | |
{ | |
me: {x:5, y:5, angle:270}, | |
them: {x:5, y:10}, | |
bearing: 180, | |
turn: -90 | |
}, | |
{ | |
me: {x:5, y:5, angle:45}, | |
them: {x:5, y:10}, | |
bearing: 180, | |
turn: 135 | |
}, | |
{ | |
me: {x:5, y:5, angle:0}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: 90 | |
}, | |
{ | |
me: {x:5, y:5, angle:90}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: 0 | |
}, | |
{ | |
me: {x:5, y:5, angle:-90}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: 180 | |
}, | |
{ | |
me: {x:5, y:5, angle:45}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: 45 | |
}, | |
{ | |
me: {x:5, y:5, angle:-45}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: 135 | |
}, | |
{ | |
me: {x:5, y:5, angle:180}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: -90 | |
}, | |
{ | |
me: {x:5, y:5, angle:225}, | |
them: {x:10, y:5}, | |
bearing: 90, | |
turn: -135 | |
}, | |
{ | |
me: {x:5, y:5}, | |
them: {x:0, y:5}, | |
bearing: 270 | |
}, | |
{ | |
me: {x:5, y:5}, | |
them: {x:5, y:0}, | |
bearing: 0 | |
}, | |
{ | |
me: {x:5, y:5, angle:0}, | |
them: {x:10, y:10}, | |
bearing: 135, | |
turn: 45 | |
}, | |
{ | |
me: {x:5, y:5, angle:0}, | |
them: {x:0, y:10}, | |
bearing: 315, | |
turn: -45 | |
}, | |
{ | |
me: {x:5, y:5}, | |
them: {x:0, y:0}, | |
bearing: 315 | |
}, | |
{ | |
me: {x:5, y:5}, | |
them: {x:10, y:0}, | |
bearing: 45 | |
} | |
]; | |
Robot = function(position, angle) { | |
this.position = position; | |
this.angle = angle; | |
}; | |
function calculateDistance (robot, theirLocation) { | |
var dX = theirLocation.x - robot.position.x; | |
var dY = theirLocation.y - robot.position.y; | |
return Math.sqrt(dX*dX + dY*dY); | |
} | |
function calculateBearing (robot, theirLocation) { | |
var dX = theirLocation.x - robot.position.x; | |
var dY = theirLocation.y - robot.position.y; | |
var distance = calculateDistance(robot, theirLocation); | |
var absoluteAngle = Math.floor(Math.asin(Math.abs(dX)/distance) * 180/Math.PI); | |
if (dX < 0 && dY >= 0) absoluteAngle = 180 + absoluteAngle; | |
if (dX < 0 && dY < 0) absoluteAngle = 360 - absoluteAngle; | |
if (dX >= 0 && dY > 0) absoluteAngle = 180 - absoluteAngle; | |
return absoluteAngle >= 360 ? 360 - absoluteAngle : absoluteAngle; | |
} | |
function calculateTurn (robot, bearingAngle) { | |
var turn = bearingAngle - robot.angle; | |
if (Math.abs(turn) > 180) | |
turn = turn - 360 | |
return turn; | |
} | |
// in the real one, make calculateBearing a function in the prototype | |
console.log("Failed tests to follow...") | |
tests.forEach(function(item) { | |
var calculatedBearing = calculateBearing({position:item.me}, item.them); | |
//console.log(item.them); | |
if (Math.abs(item.angle - calculatedBearing) > 1) { | |
console.log(item.them); | |
console.log('expected bearing: ' + item.bearing); | |
console.log('returned bearing: ' + calculatedBearing); | |
} | |
if (item.me.angle) { | |
var calculatedTurn = calculateTurn(item.me, calculatedBearing); | |
if (Math.abs(item.turn - calculatedTurn) > 1) { | |
console.log('bearing: ' + calculatedBearing + ' angle: ' + item.me.angle); | |
console.log('expected turn: ' + item.turn); | |
console.log('returned turn: ' + calculatedTurn); | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment