def test_second_argument_type(self): # floats should be illegal self.assertRaises(TypeError, round3, 3.14159, 2.0) # None should be allowed self.assertEqual(round3(1.0, None), 1) # the following would raise an error with the built-in Python3.5 round: # TypeError: 'NoneType' object cannot be interpreted as an integer self.assertEqual(round3(1, None), 1)
def test_second_argument_type(self): # any type with an __index__ method should be permitted as # a second argument self.assertAlmostEqual(round3(12.34, True), 12.3) class MyIndex(object): def __index__(self): return 4 self.assertAlmostEqual(round3(-0.123456, MyIndex()), -0.1235) # but floats should be illegal self.assertRaises(TypeError, round3, 3.14159, 2.0)
def normalizeRounding(value): """ Normalizes rounding. Python 2 and Python 3 handing the rounding of halves (0.5, 1.5, etc) differently. This normalizes rounding to be the same (Python 3 style) in both environments. * **value** must be an :ref:`type-int-float` * Returned value is a ``int`` """ if not isinstance(value, (int, float)): raise TypeError("Value to round must be an int or float, not %s." % type(value).__name__) return round3(value)
def test_halfway_cases(self): self.assertAlmostEqual(round3(0.125, 2), 0.12) self.assertAlmostEqual(round3(0.375, 2), 0.38) self.assertAlmostEqual(round3(0.625, 2), 0.62) self.assertAlmostEqual(round3(0.875, 2), 0.88) self.assertAlmostEqual(round3(-0.125, 2), -0.12) self.assertAlmostEqual(round3(-0.375, 2), -0.38) self.assertAlmostEqual(round3(-0.625, 2), -0.62) self.assertAlmostEqual(round3(-0.875, 2), -0.88) self.assertAlmostEqual(round3(0.25, 1), 0.2) self.assertAlmostEqual(round3(0.75, 1), 0.8) self.assertAlmostEqual(round3(-0.25, 1), -0.2) self.assertAlmostEqual(round3(-0.75, 1), -0.8) self.assertEqual(round3(-6.5, 0), -6.0) self.assertEqual(round3(-5.5, 0), -6.0) self.assertEqual(round3(-1.5, 0), -2.0) self.assertEqual(round3(-0.5, 0), 0.0) self.assertEqual(round3(0.5, 0), 0.0) self.assertEqual(round3(1.5, 0), 2.0) self.assertEqual(round3(2.5, 0), 2.0) self.assertEqual(round3(3.5, 0), 4.0) self.assertEqual(round3(4.5, 0), 4.0) self.assertEqual(round3(5.5, 0), 6.0) self.assertEqual(round3(6.5, 0), 6.0) # same but without an explicit second argument; in 2.x these # will give floats self.assertEqual(round3(-6.5), -6) self.assertEqual(round3(-5.5), -6) self.assertEqual(round3(-1.5), -2.0) self.assertEqual(round3(-0.5), 0) self.assertEqual(round3(0.5), 0) self.assertEqual(round3(1.5), 2) self.assertEqual(round3(2.5), 2) self.assertEqual(round3(3.5), 4) self.assertEqual(round3(4.5), 4) self.assertEqual(round3(5.5), 6) self.assertEqual(round3(6.5), 6) # no ndigits and input is already an integer: output == input rv = round3(1) self.assertEqual(rv, 1) self.assertTrue(isinstance(rv, int)) rv = round3(1.0) self.assertEqual(rv, 1) self.assertTrue(isinstance(rv, int)) self.assertEqual(round3(-25.0, -1), -20.0) self.assertEqual(round3(-15.0, -1), -20.0) self.assertEqual(round3(-5.0, -1), 0.0) self.assertEqual(round3(5.0, -1), 0.0) self.assertEqual(round3(15.0, -1), 20.0) self.assertEqual(round3(25.0, -1), 20.0) self.assertEqual(round3(35.0, -1), 40.0) self.assertEqual(round3(45.0, -1), 40.0) self.assertEqual(round3(55.0, -1), 60.0) self.assertEqual(round3(65.0, -1), 60.0) self.assertEqual(round3(75.0, -1), 80.0) self.assertEqual(round3(85.0, -1), 80.0) self.assertEqual(round3(95.0, -1), 100.0) self.assertEqual(round3(12325.0, -1), 12320.0) self.assertEqual(round3(0, -1), 0.0) self.assertEqual(round3(350.0, -2), 400.0) self.assertEqual(round3(450.0, -2), 400.0) self.assertAlmostEqual(round3(0.5e21, -21), 0.0) self.assertAlmostEqual(round3(1.5e21, -21), 2e21) self.assertAlmostEqual(round3(2.5e21, -21), 2e21) self.assertAlmostEqual(round3(5.5e21, -21), 6e21) self.assertAlmostEqual(round3(8.5e21, -21), 8e21) self.assertAlmostEqual(round3(-1.5e22, -22), -2e22) self.assertAlmostEqual(round3(-0.5e22, -22), 0.0) self.assertAlmostEqual(round3(0.5e22, -22), 0.0) self.assertAlmostEqual(round3(1.5e22, -22), 2e22)
def test_halfway_cases(self): self.assertAlmostEqual(round3(0.125, 2), 0.12) self.assertAlmostEqual(round3(0.375, 2), 0.38) self.assertAlmostEqual(round3(0.625, 2), 0.62) self.assertAlmostEqual(round3(0.875, 2), 0.88) self.assertAlmostEqual(round3(-0.125, 2), -0.12) self.assertAlmostEqual(round3(-0.375, 2), -0.38) self.assertAlmostEqual(round3(-0.625, 2), -0.62) self.assertAlmostEqual(round3(-0.875, 2), -0.88) self.assertAlmostEqual(round3(0.25, 1), 0.2) self.assertAlmostEqual(round3(0.75, 1), 0.8) self.assertAlmostEqual(round3(-0.25, 1), -0.2) self.assertAlmostEqual(round3(-0.75, 1), -0.8) self.assertEqual(round3(-6.5, 0), -6.0) self.assertEqual(round3(-5.5, 0), -6.0) self.assertEqual(round3(-1.5, 0), -2.0) self.assertEqual(round3(-0.5, 0), 0.0) self.assertEqual(round3(0.5, 0), 0.0) self.assertEqual(round3(1.5, 0), 2.0) self.assertEqual(round3(2.5, 0), 2.0) self.assertEqual(round3(3.5, 0), 4.0) self.assertEqual(round3(4.5, 0), 4.0) self.assertEqual(round3(5.5, 0), 6.0) self.assertEqual(round3(6.5, 0), 6.0) # same but without an explicit second argument; in 2.x these # will give floats self.assertEqual(round3(-6.5), -6) self.assertEqual(round3(-5.5), -6) self.assertEqual(round3(-1.5), -2.0) self.assertEqual(round3(-0.5), 0) self.assertEqual(round3(0.5), 0) self.assertEqual(round3(1.5), 2) self.assertEqual(round3(2.5), 2) self.assertEqual(round3(3.5), 4) self.assertEqual(round3(4.5), 4) self.assertEqual(round3(5.5), 6) self.assertEqual(round3(6.5), 6) self.assertEqual(round3(-25.0, -1), -20.0) self.assertEqual(round3(-15.0, -1), -20.0) self.assertEqual(round3(-5.0, -1), 0.0) self.assertEqual(round3(5.0, -1), 0.0) self.assertEqual(round3(15.0, -1), 20.0) self.assertEqual(round3(25.0, -1), 20.0) self.assertEqual(round3(35.0, -1), 40.0) self.assertEqual(round3(45.0, -1), 40.0) self.assertEqual(round3(55.0, -1), 60.0) self.assertEqual(round3(65.0, -1), 60.0) self.assertEqual(round3(75.0, -1), 80.0) self.assertEqual(round3(85.0, -1), 80.0) self.assertEqual(round3(95.0, -1), 100.0) self.assertEqual(round3(12325.0, -1), 12320.0) self.assertEqual(round3(0, -1), 0.0) self.assertEqual(round3(350.0, -2), 400.0) self.assertEqual(round3(450.0, -2), 400.0) self.assertAlmostEqual(round3(0.5e21, -21), 0.0) self.assertAlmostEqual(round3(1.5e21, -21), 2e21) self.assertAlmostEqual(round3(2.5e21, -21), 2e21) self.assertAlmostEqual(round3(5.5e21, -21), 6e21) self.assertAlmostEqual(round3(8.5e21, -21), 8e21) self.assertAlmostEqual(round3(-1.5e22, -22), -2e22) self.assertAlmostEqual(round3(-0.5e22, -22), 0.0) self.assertAlmostEqual(round3(0.5e22, -22), 0.0) self.assertAlmostEqual(round3(1.5e22, -22), 2e22)