bpo-36546: More tests: type preservation and equal inputs (#13000)

This commit is contained in:
Raymond Hettinger 2019-04-28 21:31:55 -07:00 committed by GitHub
parent 86f0c8215c
commit db81ba1393
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 12 deletions

View File

@ -2153,12 +2153,11 @@ class TestQuantiles(unittest.TestCase):
]: ]:
self.assertEqual(expected, quantiles(data, n=n)) self.assertEqual(expected, quantiles(data, n=n))
self.assertEqual(len(quantiles(data, n=n)), n - 1) self.assertEqual(len(quantiles(data, n=n)), n - 1)
self.assertEqual(list(map(float, expected)), # Preserve datatype when possible
quantiles(map(Decimal, data), n=n)) for datatype in (float, Decimal, Fraction):
self.assertEqual(list(map(Decimal, expected)), result = quantiles(map(datatype, data), n=n)
quantiles(map(Decimal, data), n=n)) self.assertTrue(all(type(x) == datatype) for x in result)
self.assertEqual(list(map(Fraction, expected)), self.assertEqual(result, list(map(datatype, expected)))
quantiles(map(Fraction, data), n=n))
# Invariant under tranlation and scaling # Invariant under tranlation and scaling
def f(x): def f(x):
return 3.5 * x - 1234.675 return 3.5 * x - 1234.675
@ -2199,12 +2198,11 @@ class TestQuantiles(unittest.TestCase):
]: ]:
self.assertEqual(expected, quantiles(data, n=n, method="inclusive")) self.assertEqual(expected, quantiles(data, n=n, method="inclusive"))
self.assertEqual(len(quantiles(data, n=n, method="inclusive")), n - 1) self.assertEqual(len(quantiles(data, n=n, method="inclusive")), n - 1)
self.assertEqual(list(map(float, expected)), # Preserve datatype when possible
quantiles(map(Decimal, data), n=n, method="inclusive")) for datatype in (float, Decimal, Fraction):
self.assertEqual(list(map(Decimal, expected)), result = quantiles(map(datatype, data), n=n, method="inclusive")
quantiles(map(Decimal, data), n=n, method="inclusive")) self.assertTrue(all(type(x) == datatype) for x in result)
self.assertEqual(list(map(Fraction, expected)), self.assertEqual(result, list(map(datatype, expected)))
quantiles(map(Fraction, data), n=n, method="inclusive"))
# Invariant under tranlation and scaling # Invariant under tranlation and scaling
def f(x): def f(x):
return 3.5 * x - 1234.675 return 3.5 * x - 1234.675
@ -2222,6 +2220,14 @@ class TestQuantiles(unittest.TestCase):
self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001) self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001)
for e, a in zip(expected, actual))) for e, a in zip(expected, actual)))
def test_equal_inputs(self):
quantiles = statistics.quantiles
for n in range(2, 10):
data = [10.0] * n
self.assertEqual(quantiles(data), [10.0, 10.0, 10.0])
self.assertEqual(quantiles(data, method='inclusive'),
[10.0, 10.0, 10.0])
def test_equal_sized_groups(self): def test_equal_sized_groups(self):
quantiles = statistics.quantiles quantiles = statistics.quantiles
total = 10_000 total = 10_000