From 6ac64f0e3df65abeb90250948b85498e679bc619 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Sat, 21 Apr 2018 10:45:02 -0700 Subject: [PATCH] Update tests. remove converted test for the time being --- .travis.yml | 2 +- libs/core/helpers.ts | 4 ++++ tests/hat-game.ts | 10 +++++----- tests/meteorite.ts | 10 +++++----- tests/pac-man-runaway.ts | 10 +++++----- tests/wg-catching-raindrops.ts | 2 +- tests/wg-dr-who.ts | 16 ++++++++-------- tests/wg-operation.ts | 2 +- tests/wg-operator-reaction-test.ts | 4 ++-- tests/wg-paintroller.ts | 6 +++--- tests/wg-perfect-pancakes.ts | 2 +- 11 files changed, 36 insertions(+), 32 deletions(-) diff --git a/.travis.yml b/.travis.yml index f47f920e..87d85f27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ script: - "(cd libs/lang-test0; node ../../node_modules/pxt-core/built/pxt.js test)" - "(cd libs/lang-test1; node ../../node_modules/pxt-core/built/pxt.js test)" - "node node_modules/pxt-core/built/pxt.js testdir tests" - - "(cd libs/hello; node ../../node_modules/pxt-core/built/pxt.js testconv http://az851932.vo.msecnd.net/files/td-converter-tests-v1.json)" +# - "(cd libs/hello; node ../../node_modules/pxt-core/built/pxt.js testconv http://az851932.vo.msecnd.net/files/td-converter-tests-v1.json)" sudo: false notifications: email: diff --git a/libs/core/helpers.ts b/libs/core/helpers.ts index 1e27c381..0f528076 100644 --- a/libs/core/helpers.ts +++ b/libs/core/helpers.ts @@ -14,4 +14,8 @@ namespace Math { export function randomBoolean(): boolean { return Math.random() >= 0.5; } + + export function randomInt(max: number): number { + return Math.floor(Math.random() * max); + } } \ No newline at end of file diff --git a/tests/hat-game.ts b/tests/hat-game.ts index 1f1b3e79..751113f2 100644 --- a/tests/hat-game.ts +++ b/tests/hat-game.ts @@ -197,7 +197,7 @@ function playLevel(level1: number) { led.plot(2 * i, 2) } basic.pause(1000) - correctBall = Math.random(3) + correctBall = Math.randomInt(3) revealBall(correctBall) basic.pause(1000) let swaps = 5 + 10 * level1 @@ -209,17 +209,17 @@ function playLevel(level1: number) { swapSpeed = 20 } for (let i1 = 0; i1 < swaps; i1++) { - let swapType = Math.random(3) - let not = Math.random(3) + let swapType = Math.randomInt(3) + let not = Math.randomInt(3) if (swapType < 2) { - let swapOrientation = Math.random(2) + let swapOrientation = Math.randomInt(2) if (swapOrientation == 0) { swapCups((not + 1) % 3, (not + 2) % 3, swapSpeed) } else { swapCups((not + 2) % 3, (not + 1) % 3, swapSpeed) } } else { - let swapOrientation1 = Math.random(2) + let swapOrientation1 = Math.randomInt(2) if (swapOrientation1 == 0) { swapFake((not + 1) % 3, (not + 2) % 3, swapSpeed) } else { diff --git a/tests/meteorite.ts b/tests/meteorite.ts index a877ab1b..297da4b4 100644 --- a/tests/meteorite.ts +++ b/tests/meteorite.ts @@ -38,9 +38,9 @@ input.onButtonPressed(Button.B, () => { led.plot(twoX, twoY) } }) -meteoriteOneX = Math.random(5) +meteoriteOneX = Math.randomInt(5) meteoriteOneY = 0 -meteoriteTwoX = Math.random(5) +meteoriteTwoX = Math.randomInt(5) meteoriteTwoY = -3 basic.pause(1000) for (let i = 0; i < 3; i++) { @@ -84,7 +84,7 @@ basic.forever(() => { } } while (Math.abs(meteoriteTwoX - meteoriteOneX) < 1) { - meteoriteOneX = Math.random(5) + meteoriteOneX = Math.randomInt(5) } meteoriteOneY = 0 counter = counter + 1 @@ -133,9 +133,9 @@ basic.forever(() => { } } - meteoriteTwoX = Math.random(5) + meteoriteTwoX = Math.randomInt(5) while (Math.abs(meteoriteTwoX - meteoriteOneX) < 1) { - meteoriteTwoX = Math.random(5) + meteoriteTwoX = Math.randomInt(5) } meteoriteTwoY = 0 counter = counter + 1 diff --git a/tests/pac-man-runaway.ts b/tests/pac-man-runaway.ts index be805bff..196150c2 100644 --- a/tests/pac-man-runaway.ts +++ b/tests/pac-man-runaway.ts @@ -25,7 +25,7 @@ class Entity { } else if (this.x == 4) { return - 1 } else { - return Math.random(2) * 2 - 1 + return Math.randomInt(2) * 2 - 1 } } @@ -35,7 +35,7 @@ class Entity { } else if (this.y == 4) { return - 1 } else { - return Math.random(2) * 2 - 1 + return Math.randomInt(2) * 2 - 1 } } @@ -162,7 +162,7 @@ function logic() { m.dirX = m.towardsX(person) m.dirY = m.towardsY(person) if (m.dirX != 0 && m.dirY != 0) { - let x = Math.random(2) + let x = Math.randomInt(2) if (x == 1) { m.dirX = 0 } else { @@ -224,7 +224,7 @@ function resetState() { for (let i = 0; i < totalMonsters; i++) { let busy = busyPos[i] let m = monsters[i] - m.x = (busy.x + Math.random(3)) - 1 - m.y = (busy.y + Math.random(3)) - 1 + m.x = (busy.x + Math.randomInt(3)) - 1 + m.y = (busy.y + Math.randomInt(3)) - 1 } } diff --git a/tests/wg-catching-raindrops.ts b/tests/wg-catching-raindrops.ts index 937abf67..a0920f49 100644 --- a/tests/wg-catching-raindrops.ts +++ b/tests/wg-catching-raindrops.ts @@ -64,7 +64,7 @@ function playGame(): number { while (true) { if (state == "NEWDROP") { // create a new drop at a random position - dropX = Math.random(5) + dropX = Math.randomInt(5) dropY = 0 state = "RAINING" } else if (state == "RAINING") { diff --git a/tests/wg-dr-who.ts b/tests/wg-dr-who.ts index 39dca079..204b59f2 100644 --- a/tests/wg-dr-who.ts +++ b/tests/wg-dr-who.ts @@ -127,7 +127,7 @@ function playCybermen(): number { } else { cybermanLineupCount = 0 // Move Cyberman closer to player, slowly - if (Math.random(100) < cybermanMoveXProbability) { + if (Math.randomInt(100) < cybermanMoveXProbability) { if (cybermanX > playerX) { cybermanX = cybermanX - 1 } else if (cybermanX < playerX) { @@ -170,7 +170,7 @@ function playDalek(): number { if (tilt > 2) { // Moving up, slowly if (userY < 4) { - if (Math.random(100) < userMoveSensitivity) { + if (Math.randomInt(100) < userMoveSensitivity) { userY = userY + 1 redraw = true } @@ -178,7 +178,7 @@ function playDalek(): number { } else if (tilt < 2) { // Moving down (slowly) if (userY > 0) { - if (Math.random(100) < userMoveSensitivity) { + if (Math.randomInt(100) < userMoveSensitivity) { userY = userY - 1 redraw = true } @@ -186,24 +186,24 @@ function playDalek(): number { } // Move the Dalek to line up with user if (dalekY < userY) { - if (Math.random(100) < dalekMoveSensitivity) { + if (Math.randomInt(100) < dalekMoveSensitivity) { dalekY = dalekY + 1 redraw = true } } else if (dalekY > userY) { - if (Math.random(100) < dalekMoveSensitivity) { + if (Math.randomInt(100) < dalekMoveSensitivity) { dalekY = dalekY - 1 redraw = true } } else { // Dalek lines up - if (Math.random(100) < dalekShootChance) { + if (Math.randomInt(100) < dalekShootChance) { // Shoot a raygun at the user for (let i = 0; i < 3; i++) { led.plot(i + 1, dalekY) basic.pause(100) } - if (Math.random(100) < dalekHitChance) { + if (Math.randomInt(100) < dalekHitChance) { // User has been hit, game over break } @@ -332,7 +332,7 @@ function playJudoonLanguage(): number { . # # # . # # # . . # . # . # # # # # # # . . . # `) let pages = "029 041 167 208 283" - let actualAnswer = Math.random(5) + let actualAnswer = Math.randomInt(5) let pos = 0 let redraw = true while (gameTime < maxGameTime) { diff --git a/tests/wg-operation.ts b/tests/wg-operation.ts index b349c6aa..c43532ab 100644 --- a/tests/wg-operation.ts +++ b/tests/wg-operation.ts @@ -156,7 +156,7 @@ function surgery(): number { `, 150) state = "SHOCKED" } else if (state == "SHOCKED") { - let recover = Math.random(100) + let recover = Math.randomInt(100) if (recover >= recoveryProbability) { state = "RECOVERED" } else { diff --git a/tests/wg-operator-reaction-test.ts b/tests/wg-operator-reaction-test.ts index ef9bb265..f262bc11 100644 --- a/tests/wg-operator-reaction-test.ts +++ b/tests/wg-operator-reaction-test.ts @@ -75,8 +75,8 @@ function alertnessTest() { // Run 10 random cognition response tests for (let i = 0; i < 9; i++) { // Choose random delay and random outcome - let delay = Math.random(5) + 5 - let outcome = Math.random(2) + let delay = Math.randomInt(5) + 5 + let outcome = Math.randomInt(2) // Draw moving dots on screen until delay expires basic.clearScreen() for (let j = 0; j < delay; j++) { diff --git a/tests/wg-paintroller.ts b/tests/wg-paintroller.ts index 918bdd3e..1f14efbc 100644 --- a/tests/wg-paintroller.ts +++ b/tests/wg-paintroller.ts @@ -54,7 +54,7 @@ function playOneGame(): number { . . . . . . # # # . . . # . . . . # . . . . # . . . . # . . . . # . . . # # # . . . . . . . . . . . . # # # . . . # . . . . # . . . . # . . . . # . . . . # . . . . # . . . . # . . . # # # . . . . . . `, 50) - let pos = Math.random(5) + let pos = Math.randomInt(5) if (xdir) { if (!canvas.pixel(pos, y)) { canvas.setPixel(pos, y, true) @@ -70,9 +70,9 @@ function playOneGame(): number { xdir = !xdir bearing = input.compassHeading() if (xdir) { - x = Math.random(5) + x = Math.randomInt(5) } else { - y = Math.random(5) + y = Math.randomInt(5) } } else { basic.pause(100) diff --git a/tests/wg-perfect-pancakes.ts b/tests/wg-perfect-pancakes.ts index 067c67dd..ed4b73c2 100644 --- a/tests/wg-perfect-pancakes.ts +++ b/tests/wg-perfect-pancakes.ts @@ -54,7 +54,7 @@ function runGameOnce() { } // Run code appropriate to the present state of the game if (state == "NEWSIDE") { - target_time = 5 + Math.random(5) + target_time = 5 + Math.randomInt(5) state = "COOKING" startTimer() } else if (state == "COOKING") {