]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_fpformat.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_fpformat.py
CommitLineData
4710c53d 1'''\r
2 Tests for fpformat module\r
3 Nick Mathewson\r
4'''\r
5from test.test_support import run_unittest, import_module\r
6import unittest\r
7fpformat = import_module('fpformat', deprecated=True)\r
8fix, sci, NotANumber = fpformat.fix, fpformat.sci, fpformat.NotANumber\r
9\r
10StringType = type('')\r
11\r
12# Test the old and obsolescent fpformat module.\r
13#\r
14# (It's obsolescent because fix(n,d) == "%.*f"%(d,n) and\r
15# sci(n,d) == "%.*e"%(d,n)\r
16# for all reasonable numeric n and d, except that sci gives 3 exponent\r
17# digits instead of 2.\r
18#\r
19# Differences only occur for unreasonable n and d. <.2 wink>)\r
20\r
21class FpformatTest(unittest.TestCase):\r
22\r
23 def checkFix(self, n, digits):\r
24 result = fix(n, digits)\r
25 if isinstance(n, StringType):\r
26 n = repr(n)\r
27 expected = "%.*f" % (digits, float(n))\r
28\r
29 self.assertEqual(result, expected)\r
30\r
31 def checkSci(self, n, digits):\r
32 result = sci(n, digits)\r
33 if isinstance(n, StringType):\r
34 n = repr(n)\r
35 expected = "%.*e" % (digits, float(n))\r
36 # add the extra 0 if needed\r
37 num, exp = expected.split("e")\r
38 if len(exp) < 4:\r
39 exp = exp[0] + "0" + exp[1:]\r
40 expected = "%se%s" % (num, exp)\r
41\r
42 self.assertEqual(result, expected)\r
43\r
44 def test_basic_cases(self):\r
45 self.assertEqual(fix(100.0/3, 3), '33.333')\r
46 self.assertEqual(sci(100.0/3, 3), '3.333e+001')\r
47\r
48 def test_reasonable_values(self):\r
49 for d in range(7):\r
50 for val in (1000.0/3, 1000, 1000.0, .002, 1.0/3, 1e10):\r
51 for realVal in (val, 1.0/val, -val, -1.0/val):\r
52 self.checkFix(realVal, d)\r
53 self.checkSci(realVal, d)\r
54\r
55 def test_failing_values(self):\r
56 # Now for 'unreasonable n and d'\r
57 self.assertEqual(fix(1.0, 1000), '1.'+('0'*1000))\r
58 self.assertEqual(sci("1"+('0'*1000), 0), '1e+1000')\r
59\r
60 # This behavior is inconsistent. sci raises an exception; fix doesn't.\r
61 yacht = "Throatwobbler Mangrove"\r
62 self.assertEqual(fix(yacht, 10), yacht)\r
63 try:\r
64 sci(yacht, 10)\r
65 except NotANumber:\r
66 pass\r
67 else:\r
68 self.fail("No exception on non-numeric sci")\r
69\r
70\r
71def test_main():\r
72 run_unittest(FpformatTest)\r
73\r
74\r
75if __name__ == "__main__":\r
76 test_main()\r