Last active
July 23, 2018 03:22
-
-
Save pablit07/0db3021fe99928dd62a46ed4446d906d to your computer and use it in GitHub Desktop.
gameSense Real Time NodeJS Test
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
// Gears problem | |
// Connect an input force to a simulated system of gears | |
// | |
// there are two sets of gears in this system, cogs and sprockets | |
// cogs are connected in series and so are sprockets, but the two are not connected (they are parallel) | |
// | |
// **Provide: a function, turnSystem, that accepts joules, a cog, and a sprocket as input, and returns a promise that resolves when both gears stop turning | |
// cog and sprocket simulate the first gear in the two sets | |
// calls should be asynchronous and return promise that resolve when the gears stops turning | |
// | |
// **Provide: also supply the Gear class | |
// requires a constructor that accepts radiansPerJoule and radiansPerSecond which define the time and amount of turning based on the input energy | |
// also requires a property called radiansTurned which is set after turning happens | |
// All unit tests below must run and pass (run cmd: `node tests.js`) | |
var Gear = require('./index').Gear; | |
var turnSystem = require('./index').turnSystem; | |
(function testTurnSystemReturnsPromise() { | |
if (turnSystem(1, new Gear(1,1), new Gear(1,1)) instanceof Promise) { | |
console.info('testTurnSystemReturnsPromise passed.') | |
} else { | |
console.error('testTurnSystemReturnsPromise failed: returned instance is not a Promise') | |
} | |
})(); | |
(function testGearCtorDefinesRadiansTurned() { | |
var gear = new Gear(1,1); | |
if (gear.radiansTurned !== undefined) { | |
console.info('testGearCtorDefinesRadiansTurned passed.') | |
} else { | |
console.error('testGearCtorDefinesRadiansTurned failed: radiansTurned not defined by constructor') | |
} | |
})(); | |
(function testGearCtorSetsRadiansPerJoule() { | |
var radiansPerJoule = Math.ceil(Math.random(4)); | |
var gear = new Gear(radiansPerJoule, 1); | |
if (gear.radiansPerJoule === radiansPerJoule) { | |
console.info('testGearCtorSetsRadiansPerJoule passed.') | |
} else { | |
console.error('testGearCtorSetsRadiansPerJoule failed: radiansPerJoule not set by constructor') | |
} | |
})(); | |
(function testGearCtorSetsRadiansPerSecond() { | |
var radiansPerSecond = Math.ceil(Math.random(4)); | |
var gear = new Gear(1, radiansPerSecond); | |
if (gear.radiansPerSecond === radiansPerSecond) { | |
console.info('testGearCtorSetsRadiansPerSecond passed.') | |
} else { | |
console.error('testGearCtorSetsRadiansPerSecond failed: radiansPerJoule not set by constructor') | |
} | |
})(); | |
(function testTurnSystemIsInDefaultState() { | |
var cog = new Gear(6.0, 2.0); | |
var sprocket = new Gear(2.0, 1.0); | |
turnSystem(1, cog, sprocket); | |
if (cog.radiansTurned === 0 && sprocket.radiansTurned === 0) { | |
console.info('testTurnSystemIsInDefaultState passed.') | |
} else { | |
console.error('testTurnSystemIsInDefaultState failed: incorrect start state, radiansTurned should be 0') | |
} | |
})(); | |
(function testTurnSystemPromiseDoesResolve() { | |
var cog = new Gear(1.0, 1.0); | |
var sprocket = new Gear(1.0, 1.0); | |
var promise = turnSystem(1, cog, sprocket); | |
var counter = 0; | |
var isResolved = false; | |
promise.then(() => { isResolved = true }); | |
var handle = setInterval(() => { | |
if (isResolved) { | |
clearInterval(handle); | |
console.info('testTurnSystemPromiseDoesResolve passed.'); | |
} else if (counter > 5) { | |
clearInterval(handle); | |
console.error('testTurnSystemPromiseDoesResolve failed: promise not resolved') | |
} else { | |
counter++; | |
} | |
}, 1000); | |
})(); | |
(async function testAfterTurnSystemCogHasCorrectRadians() { | |
var cog = new Gear(6.0, 2.0); | |
var sprocket = new Gear(2.0, 1.0); | |
await turnSystem(1, cog, sprocket); | |
if (cog.radiansTurned === 6.0) { | |
console.info('testAfterTurnSystemCogHasCorrectRadians passed.') | |
} else { | |
console.error('testAfterTurnSystemCogHasCorrectRadians failed: incorrect radiansTurned') | |
} | |
})(); | |
(async function testAfterTurnSystemSprocketHasCorrectRadians() { | |
var cog = new Gear(6.0, 3.0); | |
var sprocket = new Gear(2.0, 1.0); | |
await turnSystem(1, cog, sprocket); | |
if (sprocket.radiansTurned === 2.0) { | |
console.info('testAfterTurnSystemSprocketHasCorrectRadians passed.') | |
} else { | |
console.error('testAfterTurnSystemSprocketHasCorrectRadians failed: incorrect radiansTurned') | |
} | |
})(); | |
(function testTurnSystemPromiseResolvesAfterCorrectNumberSeconds() { | |
var cog = new Gear(6.0, 3.0); | |
var sprocket = new Gear(2.0, 1.0); | |
var promise = turnSystem(2, cog, sprocket); | |
var counter = 1; | |
var isResolved = false; | |
promise.then(() => { isResolved = true }); | |
var handle = setInterval(() => { | |
if (isResolved) { | |
clearInterval(handle); | |
if (counter == 4) { | |
console.info('testTurnSystemPromiseResolvesAfterCorrectNumberSeconds passed.'); | |
} else { | |
console.error('testTurnSystemPromiseResolvesAfterCorrectNumberSeconds failed: expected 4 but was ' + counter + ' seconds') | |
} | |
} else if (counter > 12) { | |
clearInterval(handle); | |
console.error('testTurnSystemPromiseResolvesAfterCorrectNumberSeconds failed: did not resolve in time') | |
} else { | |
counter++; | |
} | |
}, 1000); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment