summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_fpformat.py
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-07-23 02:46:35 (GMT)
committerFred Drake <fdrake@acm.org>2001-07-23 02:46:35 (GMT)
commit1586136193112c3b7571efb37974e2bad4bd0a57 (patch)
tree6a639cdbe3ebfa1535316329dfe5cefebb09f9c7 /Lib/test/test_fpformat.py
parent9db2f571c382ce41c29a87aaf717549ed4fed701 (diff)
downloadcpython-1586136193112c3b7571efb37974e2bad4bd0a57.zip
cpython-1586136193112c3b7571efb37974e2bad4bd0a57.tar.gz
cpython-1586136193112c3b7571efb37974e2bad4bd0a57.tar.bz2
New tests by Nick Mathewson, for the fpformat module.
This closes SF patch #440290.
Diffstat (limited to 'Lib/test/test_fpformat.py')
-rw-r--r--Lib/test/test_fpformat.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/Lib/test/test_fpformat.py b/Lib/test/test_fpformat.py
new file mode 100644
index 0000000..1314654
--- /dev/null
+++ b/Lib/test/test_fpformat.py
@@ -0,0 +1,67 @@
+'''
+ Tests for fpformat module
+ Nick Mathewson
+'''
+from test_support import run_unittest
+import unittest
+from fpformat import fix, sci, NotANumber
+
+StringType = type('')
+
+# Test the old and obsolescent fpformat module.
+#
+# (It's obsolescent because fix(n,d) == "%.*f"%(d,n) and
+# sci(n,d) == "%.*e"%(d,n)
+# for all reasonable numeric n and d, except that sci gives 3 exponent
+# digits instead of 2.
+#
+# Differences only occur for unreasonable n and d. <.2 wink>)
+
+class FpformatTest(unittest.TestCase):
+
+ def checkFix(self, n, digits):
+ result = fix(n, digits)
+ if isinstance(n, StringType):
+ n = repr(n)
+ expected = "%.*f" % (digits, float(n))
+
+ self.assertEquals(result, expected)
+
+ def checkSci(self, n, digits):
+ result = sci(n, digits)
+ if isinstance(n, StringType):
+ n = repr(n)
+ expected = "%.*e" % (digits, float(n))
+ # add the extra 0
+ expected = expected[:-2]+'0'+expected[-2:]
+
+ self.assertEquals(result, expected)
+
+ def test_basic_cases(self):
+ self.assertEquals(fix(100.0/3, 3), '33.333')
+ self.assertEquals(sci(100.0/3, 3), '3.333e+001')
+
+ def test_reasonable_values(self):
+ for d in range(7):
+ for val in (1000.0/3, 1000, 1000.0, .002, 1.0/3, 1e10):
+ for realVal in (val, 1.0/val, -val, -1.0/val):
+ self.checkFix(realVal, d)
+ self.checkSci(realVal, d)
+
+ def test_failing_values(self):
+ # Now for 'unreasonable n and d'
+ self.assertEquals(fix(1.0, 1000), '1.'+('0'*1000))
+ self.assertEquals(sci("1"+('0'*1000), 0), '1e+1000')
+
+ # This behavior is inconsistent. sci raises an exception; fix doesn't.
+ yacht = "Throatwobbler Mangrove"
+ self.assertEquals(fix(yacht, 10), yacht)
+ try:
+ sci(yacht, 10)
+ except NotANumber:
+ pass
+ else:
+ self.fail("No exception on non-numeric sci")
+
+run_unittest(FpformatTest)
+