string_format2.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Change the following to True to get a much more comprehensive set of tests
  2. # to run, albeit, which take considerably longer.
  3. full_tests = False
  4. def test(fmt, *args):
  5. print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<')
  6. def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg):
  7. fmt = '{'
  8. if conv:
  9. fmt += '!'
  10. fmt += conv
  11. fmt += ':'
  12. if alignment:
  13. fmt += fill
  14. fmt += alignment
  15. fmt += sign
  16. fmt += prefix
  17. fmt += width
  18. if precision:
  19. fmt += '.'
  20. fmt += precision
  21. fmt += type
  22. fmt += '}'
  23. test(fmt, arg)
  24. if fill == '0' and alignment == '=':
  25. fmt = '{:'
  26. fmt += sign
  27. fmt += prefix
  28. fmt += width
  29. if precision:
  30. fmt += '.'
  31. fmt += precision
  32. fmt += type
  33. fmt += '}'
  34. test(fmt, arg)
  35. eg_nums = (0.0, -0.0, 0.1, 1.234, 12.3459, 1.23456789, 123456789.0, -0.0,
  36. -0.1, -1.234, -12.3459, 1e4, 1e-4, 1e5, 1e-5, 1e6, 1e-6, 1e10,
  37. 1e37, -1e37, 1e-37, -1e-37,
  38. 1.23456e8, 1.23456e7, 1.23456e6, 1.23456e5, 1.23456e4, 1.23456e3, 1.23456e2, 1.23456e1, 1.23456e0,
  39. 1.23456e-1, 1.23456e-2, 1.23456e-3, 1.23456e-4, 1.23456e-5, 1.23456e-6, 1.23456e-7, 1.23456e-8,
  40. -1.23456e8, -1.23456e7, -1.23456e6, -1.23456e5, -1.23456e4, -1.23456e3, -1.23456e2, -1.23456e1, -1.23456e0,
  41. -1.23456e-1, -1.23456e-2, -1.23456e-3, -1.23456e-4, -1.23456e-5, -1.23456e-6, -1.23456e-7, -1.23456e-8)
  42. if full_tests:
  43. for type in ('e', 'E', 'g', 'G', 'n'):
  44. for width in ('', '4', '6', '8', '10'):
  45. for alignment in ('', '<', '>', '=', '^'):
  46. for fill in ('', '@', '0', ' '):
  47. for sign in ('', '+', '-', ' '):
  48. for prec in ('', '1', '3', '6'):
  49. for num in eg_nums:
  50. test_fmt('', fill, alignment, sign, '', width, prec, type, num)
  51. # Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345)
  52. # rounds differently than print("%.3f", 1.2345);
  53. f_nums = (0.0, -0.0, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0,
  54. 0.0012, 0.0123, 0.1234, 1.23459, 12.3456,
  55. -0.0001, -0.001, -0.01, -0.1, -1.0, -10.0,
  56. -0.0012, -0.0123, -0.1234, -1.23459, -12.3456)
  57. if full_tests:
  58. for type in ('f', 'F'):
  59. for width in ('', '4', '6', '8', '10'):
  60. for alignment in ('', '<', '>', '=', '^'):
  61. for fill in ('', ' ', '0', '@'):
  62. for sign in ('', '+', '-', ' '):
  63. # An empty precision defaults to 6, but when uPy is
  64. # configured to use a float, we can only use a
  65. # precision of 6 with numbers less than 10 and still
  66. # get results that compare to CPython (which uses
  67. # long doubles).
  68. for prec in ('1', '2', '3'):
  69. for num in f_nums:
  70. test_fmt('', fill, alignment, sign, '', width, prec, type, num)
  71. for num in int_nums2:
  72. test_fmt('', fill, alignment, sign, '', width, '', type, num)
  73. pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99)
  74. pct_nums2 = (True, False, 1, 0, -1)
  75. if full_tests:
  76. type = '%'
  77. for width in ('', '4', '6', '8', '10'):
  78. for alignment in ('', '<', '>', '=', '^'):
  79. for fill in ('', ' ', '0', '@'):
  80. for sign in ('', '+', '-', ' '):
  81. # An empty precision defaults to 6, but when uPy is
  82. # configured to use a float, we can only use a
  83. # precision of 6 with numbers less than 10 and still
  84. # get results that compare to CPython (which uses
  85. # long doubles).
  86. for prec in ('1', '2', '3'):
  87. for num in pct_nums1:
  88. test_fmt('', fill, alignment, sign, '', width, prec, type, num)
  89. for num in pct_nums2:
  90. test_fmt('', fill, alignment, sign, '', width, '', type, num)
  91. else:
  92. for num in pct_nums1:
  93. test_fmt('', '', '', '', '', '', '1', '%', num)
  94. # We don't currently test a type of '' with floats (see the detailed comment
  95. # in objstr.c)