def test_findABSteps(): r = Solution().findABSteps([0, 0], [8, 8], Dice(1, 4, 2)) assert r == 45 r = Solution().findABSteps([3, 3], [0, 0], Dice(1, 4, 2)) assert r == 12 r = Solution().findABSteps([3, 3], [0, 0], Dice(1, 4, 2).rotate_clockwise().rotate_clockwise()) assert r == 10
def test_dice_rotate(): d = Dice(1, 4, 2) d.rotate_clockwise() assert d.state == (1, 2, 3) d.rotate_counterclockwise() assert d.state == (1, 4, 2) for _ in range(4): d.rotate_clockwise() assert d.state == (1, 4, 2) d.rotate_clockwise().rotate_clockwise() assert d.state == (1, 3, 5)
def test_should_be_in_range(self): """should return values between 1 and the number of sides on the dice""" results = Dice._roll(100, 2) self.assertTrue(1 in results) self.assertTrue(2 in results) self.assertFalse(0 in results) self.assertFalse(3 in results)
def test_parse_and_roll(self): """test that the results of parsing can be interpreted by roll""" d = Dice.from_string('2d8') self.assertEqual(d.amount, 2) self.assertEqual(d.sides, 8) # assert no errors result = d.roll() self.assertEqual(len(result), 2)
def test_rotate_dice_wisely(): d = Dice(1, 4, 2) r = Solution().rotate_dice_wisely([0, 0], [8, 8], d) assert r[0], r[1] == (8, 8) assert r[2].state == (1, 4, 2) r = Solution().rotate_dice_wisely([8, 0], [0, 8], d) assert r[0], r[1] == (8, 8) assert r[2].state == (1, 2, 3)
def test_find_lowest_cost_MN(): initial_dice = Dice(1, 4, 2) # if A, B in the same point, lowest cost is 0 r1 = Solution().findLowestCostInMN(1, 1, initial_dice) assert r1 == 0 # 1 * 3 area r2 = Solution().findLowestCostInMN(1, 3, initial_dice) # (1,4,2) -> +3 -> +1 = 4 assert r2 == 4 # 3 * 1 area r3 = Solution().findLowestCostInMN(3, 1, initial_dice) # (1,4,2) -> + 2 -> +1 = 3 assert r3 == 3 # 3 * 3 area r4 = Solution().findLowestCostInMN(3, 3, initial_dice) # (1,4,2) -> +3 + 1 + 2 +6 = 12 # +3+2+1+4 = 10 assert r4 == 10
def test_dice_move(): # Init a Dice with top: 1, left: 4, front: 2 d = Dice(1, 4, 2) d.move_down() # Dice move down assert d.state == (5, 4, 1) assert d.cost == d.bottom assert d.cost == 2 # Dice move right d.move_right() assert d.state == (4, 2, 1) assert d.cost == 3 + 2 # copied dice state and cost new_d = d.copy().move_right() assert new_d.state == (2, 3, 1) assert new_d.cost == 5 + new_d.bottom # origin Dice state should not change assert d.state == (4, 2, 1)
def test_roll(self): """roll should do things""" result = Dice._roll(1, 6) self.assertEqual(len(result), 1) self.assertTrue(result[0] >= 1 and result[0] <= 6)
def test_no_amount(self): """should assume that if there is no amount specified, roll 1""" result = Dice._parse('d4') self.assertEqual(result[0], 1)
def test_parse_dice(self): """in ideal usage, should return a 2-tuple of (amount, sides)""" result = Dice._parse('3d20') self.assertEqual(type(result), tuple) self.assertEqual(result[0], 3) self.assertEqual(result[1], 20)