ยปCore Development>Code coverage>Lib/test/test_fpformat.py

Python code coverage for Lib/test/test_fpformat.py

#countcontent
1n/a'''
2n/a Tests for fpformat module
3n/a Nick Mathewson
41'''
51from test.test_support import run_unittest, import_module
61import unittest
71fpformat = import_module('fpformat', deprecated=True)
81fix, sci, NotANumber = fpformat.fix, fpformat.sci, fpformat.NotANumber
9n/a
101StringType = type('')
11n/a
12n/a# Test the old and obsolescent fpformat module.
13n/a#
14n/a# (It's obsolescent because fix(n,d) == "%.*f"%(d,n) and
15n/a# sci(n,d) == "%.*e"%(d,n)
16n/a# for all reasonable numeric n and d, except that sci gives 3 exponent
17n/a# digits instead of 2.
18n/a#
19n/a# Differences only occur for unreasonable n and d. <.2 wink>)
20n/a
212class FpformatTest(unittest.TestCase):
22n/a
231 def checkFix(self, n, digits):
24168 result = fix(n, digits)
25168 if isinstance(n, StringType):
260 n = repr(n)
27168 expected = "%.*f" % (digits, float(n))
28n/a
29168 self.assertEquals(result, expected)
30n/a
311 def checkSci(self, n, digits):
32168 result = sci(n, digits)
33168 if isinstance(n, StringType):
340 n = repr(n)
35168 expected = "%.*e" % (digits, float(n))
36n/a # add the extra 0 if needed
37168 num, exp = expected.split("e")
38168 if len(exp) < 4:
39168 exp = exp[0] + "0" + exp[1:]
40168 expected = "%se%s" % (num, exp)
41n/a
42168 self.assertEquals(result, expected)
43n/a
441 def test_basic_cases(self):
451 self.assertEquals(fix(100.0/3, 3), '33.333')
461 self.assertEquals(sci(100.0/3, 3), '3.333e+001')
47n/a
481 def test_reasonable_values(self):
498 for d in range(7):
5049 for val in (1000.0/3, 1000, 1000.0, .002, 1.0/3, 1e10):
51210 for realVal in (val, 1.0/val, -val, -1.0/val):
52168 self.checkFix(realVal, d)
53168 self.checkSci(realVal, d)
54n/a
551 def test_failing_values(self):
56n/a # Now for 'unreasonable n and d'
571 self.assertEquals(fix(1.0, 1000), '1.'+('0'*1000))
581 self.assertEquals(sci("1"+('0'*1000), 0), '1e+1000')
59n/a
60n/a # This behavior is inconsistent. sci raises an exception; fix doesn't.
611 yacht = "Throatwobbler Mangrove"
621 self.assertEquals(fix(yacht, 10), yacht)
631 try:
641 sci(yacht, 10)
651 except NotANumber:
661 pass
67n/a else:
680 self.fail("No exception on non-numeric sci")
69n/a
70n/a
711def test_main():
721 run_unittest(FpformatTest)
73n/a
74n/a
751if __name__ == "__main__":
760 test_main()