github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/test_fpformat.py (about)

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