annotate Lib/test/test_fpformat.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 614c84670c63
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 '''
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 Tests for fpformat module
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 Nick Mathewson
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 '''
46845
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 24364
diff changeset
5 from test.test_support import run_unittest, import_module
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 import unittest
46845
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 24364
diff changeset
7 fpformat = import_module('fpformat', deprecated=True)
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 24364
diff changeset
8 fix, sci, NotANumber = fpformat.fix, fpformat.sci, fpformat.NotANumber
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10 StringType = type('')
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 # Test the old and obsolescent fpformat module.
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 #
18802
998f80514a13 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18799
diff changeset
14 # (It's obsolescent because fix(n,d) == "%.*f"%(d,n) and
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15 # sci(n,d) == "%.*e"%(d,n)
18802
998f80514a13 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18799
diff changeset
16 # for all reasonable numeric n and d, except that sci gives 3 exponent
998f80514a13 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18799
diff changeset
17 # digits instead of 2.
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 #
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 # Differences only occur for unreasonable n and d. <.2 wink>)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 class FpformatTest(unittest.TestCase):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 def checkFix(self, n, digits):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24 result = fix(n, digits)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 if isinstance(n, StringType):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 n = repr(n)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27 expected = "%.*f" % (digits, float(n))
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
29 self.assertEqual(result, expected)
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 def checkSci(self, n, digits):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 result = sci(n, digits)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 if isinstance(n, StringType):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
34 n = repr(n)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 expected = "%.*e" % (digits, float(n))
18807
1b6868e89a23 Make this test work under Windows as well.
Fred Drake <fdrake@acm.org>
parents: 18802
diff changeset
36 # add the extra 0 if needed
1b6868e89a23 Make this test work under Windows as well.
Fred Drake <fdrake@acm.org>
parents: 18802
diff changeset
37 num, exp = expected.split("e")
1b6868e89a23 Make this test work under Windows as well.
Fred Drake <fdrake@acm.org>
parents: 18802
diff changeset
38 if len(exp) < 4:
1b6868e89a23 Make this test work under Windows as well.
Fred Drake <fdrake@acm.org>
parents: 18802
diff changeset
39 exp = exp[0] + "0" + exp[1:]
1b6868e89a23 Make this test work under Windows as well.
Fred Drake <fdrake@acm.org>
parents: 18802
diff changeset
40 expected = "%se%s" % (num, exp)
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
41
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
42 self.assertEqual(result, expected)
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
43
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
44 def test_basic_cases(self):
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
45 self.assertEqual(fix(100.0/3, 3), '33.333')
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
46 self.assertEqual(sci(100.0/3, 3), '3.333e+001')
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
47
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
48 def test_reasonable_values(self):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
49 for d in range(7):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
50 for val in (1000.0/3, 1000, 1000.0, .002, 1.0/3, 1e10):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
51 for realVal in (val, 1.0/val, -val, -1.0/val):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
52 self.checkFix(realVal, d)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
53 self.checkSci(realVal, d)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
54
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
55 def test_failing_values(self):
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
56 # Now for 'unreasonable n and d'
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
57 self.assertEqual(fix(1.0, 1000), '1.'+('0'*1000))
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
58 self.assertEqual(sci("1"+('0'*1000), 0), '1e+1000')
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
59
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60 # This behavior is inconsistent. sci raises an exception; fix doesn't.
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 yacht = "Throatwobbler Mangrove"
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 46845
diff changeset
62 self.assertEqual(fix(yacht, 10), yacht)
18799
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 try:
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 sci(yacht, 10)
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 except NotANumber:
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
66 pass
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67 else:
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
68 self.fail("No exception on non-numeric sci")
96455f4abadd New tests by Nick Mathewson, for the fpformat module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
69
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
70
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
71 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
72 run_unittest(FpformatTest)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
73
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
74
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
75 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18807
diff changeset
76 test_main()