MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_round

Method test_round

Lib/test/test_long.py:1190–1262  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1188 self.assertEqual(((a - 1) ^ 510).bit_count(), exp - 8)
1189
1190 def test_round(self):
1191 # check round-half-even algorithm. For round to nearest ten;
1192 # rounding map is invariant under adding multiples of 20
1193 test_dict = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0,
1194 6:10, 7:10, 8:10, 9:10, 10:10, 11:10, 12:10, 13:10, 14:10,
1195 15:20, 16:20, 17:20, 18:20, 19:20}
1196 for offset in range(-520, 520, 20):
1197 for k, v in test_dict.items():
1198 got = round(k+offset, -1)
1199 expected = v+offset
1200 self.assertEqual(got, expected)
1201 self.assertIs(type(got), int)
1202
1203 # larger second argument
1204 self.assertEqual(round(-150, -2), -200)
1205 self.assertEqual(round(-149, -2), -100)
1206 self.assertEqual(round(-51, -2), -100)
1207 self.assertEqual(round(-50, -2), 0)
1208 self.assertEqual(round(-49, -2), 0)
1209 self.assertEqual(round(-1, -2), 0)
1210 self.assertEqual(round(0, -2), 0)
1211 self.assertEqual(round(1, -2), 0)
1212 self.assertEqual(round(49, -2), 0)
1213 self.assertEqual(round(50, -2), 0)
1214 self.assertEqual(round(51, -2), 100)
1215 self.assertEqual(round(149, -2), 100)
1216 self.assertEqual(round(150, -2), 200)
1217 self.assertEqual(round(250, -2), 200)
1218 self.assertEqual(round(251, -2), 300)
1219 self.assertEqual(round(172500, -3), 172000)
1220 self.assertEqual(round(173500, -3), 174000)
1221 self.assertEqual(round(31415926535, -1), 31415926540)
1222 self.assertEqual(round(31415926535, -2), 31415926500)
1223 self.assertEqual(round(31415926535, -3), 31415927000)
1224 self.assertEqual(round(31415926535, -4), 31415930000)
1225 self.assertEqual(round(31415926535, -5), 31415900000)
1226 self.assertEqual(round(31415926535, -6), 31416000000)
1227 self.assertEqual(round(31415926535, -7), 31420000000)
1228 self.assertEqual(round(31415926535, -8), 31400000000)
1229 self.assertEqual(round(31415926535, -9), 31000000000)
1230 self.assertEqual(round(31415926535, -10), 30000000000)
1231 self.assertEqual(round(31415926535, -11), 0)
1232 self.assertEqual(round(31415926535, -12), 0)
1233 self.assertEqual(round(31415926535, -999), 0)
1234
1235 # should get correct results even for huge inputs
1236 for k in range(10, 100):
1237 got = round(10**k + 324678, -3)
1238 expect = 10**k + 325000
1239 self.assertEqual(got, expect)
1240 self.assertIs(type(got), int)
1241
1242 # nonnegative second argument: round(x, n) should just return x
1243 for n in range(5):
1244 for i in range(100):
1245 x = random.randrange(-10000, 10000)
1246 got = round(x, n)
1247 self.assertEqual(got, x)

Callers

nothing calls this directly

Calls 6

roundFunction · 0.85
randrangeMethod · 0.80
itemsMethod · 0.45
assertEqualMethod · 0.45
assertIsMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected