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