Back to index

python3.2  3.2.2
Public Member Functions
test.test_float.RoundTestCase Class Reference

List of all members.

Public Member Functions

def test_inf_nan
def test_large_n
def test_small_n
def test_overflow
def test_previous_round_bugs
def test_matches_float_format
def test_format_specials

Detailed Description

Definition at line 658 of file test_float.py.


Member Function Documentation

Definition at line 735 of file test_float.py.

00735 
00736     def test_format_specials(self):
00737         # Test formatting of nans and infs.
00738 
00739         def test(fmt, value, expected):
00740             # Test with both % and format().
00741             self.assertEqual(fmt % value, expected, fmt)
00742             fmt = fmt[1:] # strip off the %
00743             self.assertEqual(format(value, fmt), expected, fmt)
00744 
00745         for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g',
00746                     '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']:
00747             pfmt = '%+' + fmt[1:]
00748             sfmt = '% ' + fmt[1:]
00749             test(fmt, INF, 'inf')
00750             test(fmt, -INF, '-inf')
00751             test(fmt, NAN, 'nan')
00752             test(fmt, -NAN, 'nan')
00753             # When asking for a sign, it's always provided. nans are
00754             #  always positive.
00755             test(pfmt, INF, '+inf')
00756             test(pfmt, -INF, '-inf')
00757             test(pfmt, NAN, '+nan')
00758             test(pfmt, -NAN, '+nan')
00759             # When using ' ' for a sign code, only infs can be negative.
00760             #  Others have a space.
00761             test(sfmt, INF, ' inf')
00762             test(sfmt, -INF, '-inf')
00763             test(sfmt, NAN, ' nan')
00764             test(sfmt, -NAN, ' nan')
00765 
00766 
00767 # Beginning with Python 2.6 float has cross platform compatible
# ways to create and represent inf and nan

Here is the call graph for this function:

Definition at line 660 of file test_float.py.

00660 
00661     def test_inf_nan(self):
00662         self.assertRaises(OverflowError, round, INF)
00663         self.assertRaises(OverflowError, round, -INF)
00664         self.assertRaises(ValueError, round, NAN)
00665         self.assertRaises(TypeError, round, INF, 0.0)
00666         self.assertRaises(TypeError, round, -INF, 1.0)
00667         self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer")
00668         self.assertRaises(TypeError, round, -0.0, 1j)

Here is the call graph for this function:

Definition at line 669 of file test_float.py.

00669 
00670     def test_large_n(self):
00671         for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]:
00672             self.assertEqual(round(123.456, n), 123.456)
00673             self.assertEqual(round(-123.456, n), -123.456)
00674             self.assertEqual(round(1e300, n), 1e300)
00675             self.assertEqual(round(1e-320, n), 1e-320)
00676         self.assertEqual(round(1e150, 300), 1e150)
00677         self.assertEqual(round(1e300, 307), 1e300)
00678         self.assertEqual(round(-3.1415, 308), -3.1415)
00679         self.assertEqual(round(1e150, 309), 1e150)
00680         self.assertEqual(round(1.4e-315, 315), 1e-315)

Here is the call graph for this function:

Definition at line 712 of file test_float.py.

00712 
00713     def test_matches_float_format(self):
00714         # round should give the same results as float formatting
00715         for i in range(500):
00716             x = i/1000.
00717             self.assertEqual(float(format(x, '.0f')), round(x, 0))
00718             self.assertEqual(float(format(x, '.1f')), round(x, 1))
00719             self.assertEqual(float(format(x, '.2f')), round(x, 2))
00720             self.assertEqual(float(format(x, '.3f')), round(x, 3))
00721 
00722         for i in range(5, 5000, 10):
00723             x = i/1000.
00724             self.assertEqual(float(format(x, '.0f')), round(x, 0))
00725             self.assertEqual(float(format(x, '.1f')), round(x, 1))
00726             self.assertEqual(float(format(x, '.2f')), round(x, 2))
00727             self.assertEqual(float(format(x, '.3f')), round(x, 3))
00728 
00729         for i in range(500):
00730             x = random.random()
00731             self.assertEqual(float(format(x, '.0f')), round(x, 0))
00732             self.assertEqual(float(format(x, '.1f')), round(x, 1))
00733             self.assertEqual(float(format(x, '.2f')), round(x, 2))
00734             self.assertEqual(float(format(x, '.3f')), round(x, 3))

Here is the call graph for this function:

Definition at line 688 of file test_float.py.

00688 
00689     def test_overflow(self):
00690         self.assertRaises(OverflowError, round, 1.6e308, -308)
00691         self.assertRaises(OverflowError, round, -1.7e308, -308)

Here is the call graph for this function:

Definition at line 694 of file test_float.py.

00694 
00695     def test_previous_round_bugs(self):
00696         # particular cases that have occurred in bug reports
00697         self.assertEqual(round(562949953421312.5, 1),
00698                           562949953421312.5)
00699         self.assertEqual(round(56294995342131.5, 3),
00700                          56294995342131.5)
00701         # round-half-even
00702         self.assertEqual(round(25.0, -1), 20.0)
00703         self.assertEqual(round(35.0, -1), 40.0)
00704         self.assertEqual(round(45.0, -1), 40.0)
00705         self.assertEqual(round(55.0, -1), 60.0)
00706         self.assertEqual(round(65.0, -1), 60.0)
00707         self.assertEqual(round(75.0, -1), 80.0)
00708         self.assertEqual(round(85.0, -1), 80.0)
00709         self.assertEqual(round(95.0, -1), 100.0)

Here is the call graph for this function:

Definition at line 681 of file test_float.py.

00681 
00682     def test_small_n(self):
00683         for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
00684             self.assertEqual(round(123.456, n), 0.0)
00685             self.assertEqual(round(-123.456, n), -0.0)
00686             self.assertEqual(round(1e300, n), 0.0)
00687             self.assertEqual(round(1e-320, n), 0.0)

Here is the call graph for this function:


The documentation for this class was generated from the following file: