|
1 | | -# Change the following to True to get a much more comprehensive set of tests |
2 | | -# to run, albeit, which take considerably longer. |
3 | | - |
4 | | -full_tests = False |
5 | | - |
6 | 1 | def test(fmt, *args): |
7 | 2 | print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<') |
8 | 3 |
|
@@ -38,105 +33,6 @@ def test(fmt, *args): |
38 | 33 | #print("%.2f" % (1.750000 % 0.08333333333)) |
39 | 34 | #print("%.12f" % (1.750000 % 0.08333333333)) |
40 | 35 |
|
41 | | -def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg): |
42 | | - fmt = '{' |
43 | | - if conv: |
44 | | - fmt += '!' |
45 | | - fmt += conv |
46 | | - fmt += ':' |
47 | | - if alignment: |
48 | | - fmt += fill |
49 | | - fmt += alignment |
50 | | - fmt += sign |
51 | | - fmt += prefix |
52 | | - fmt += width |
53 | | - if precision: |
54 | | - fmt += '.' |
55 | | - fmt += precision |
56 | | - fmt += type |
57 | | - fmt += '}' |
58 | | - test(fmt, arg) |
59 | | - if fill == '0' and alignment == '=': |
60 | | - fmt = '{:' |
61 | | - fmt += sign |
62 | | - fmt += prefix |
63 | | - fmt += width |
64 | | - if precision: |
65 | | - fmt += '.' |
66 | | - fmt += precision |
67 | | - fmt += type |
68 | | - fmt += '}' |
69 | | - test(fmt, arg) |
70 | | - |
71 | | -eg_nums = (0.0, -0.0, 0.1, 1.234, 12.3459, 1.23456789, 123456789.0, -0.0, |
72 | | - -0.1, -1.234, -12.3459, 1e4, 1e-4, 1e5, 1e-5, 1e6, 1e-6, 1e10, |
73 | | - 1e37, -1e37, 1e-37, -1e-37, |
74 | | - 1.23456e8, 1.23456e7, 1.23456e6, 1.23456e5, 1.23456e4, 1.23456e3, 1.23456e2, 1.23456e1, 1.23456e0, |
75 | | - 1.23456e-1, 1.23456e-2, 1.23456e-3, 1.23456e-4, 1.23456e-5, 1.23456e-6, 1.23456e-7, 1.23456e-8, |
76 | | - -1.23456e8, -1.23456e7, -1.23456e6, -1.23456e5, -1.23456e4, -1.23456e3, -1.23456e2, -1.23456e1, -1.23456e0, |
77 | | - -1.23456e-1, -1.23456e-2, -1.23456e-3, -1.23456e-4, -1.23456e-5, -1.23456e-6, -1.23456e-7, -1.23456e-8) |
78 | | - |
79 | | -if full_tests: |
80 | | - for type in ('e', 'E', 'g', 'G', 'n'): |
81 | | - for width in ('', '4', '6', '8', '10'): |
82 | | - for alignment in ('', '<', '>', '=', '^'): |
83 | | - for fill in ('', '@', '0', ' '): |
84 | | - for sign in ('', '+', '-', ' '): |
85 | | - for prec in ('', '1', '3', '6'): |
86 | | - for num in eg_nums: |
87 | | - test_fmt('', fill, alignment, sign, '', width, prec, type, num) |
88 | | - |
89 | | -# Note: We use 1.23459 rather than 1.2345 because '{:3f}'.format(1.2345) |
90 | | -# rounds differently than print("%.3f", 1.2345); |
91 | | - |
92 | | -f_nums = (0.0, -0.0, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, |
93 | | - 0.0012, 0.0123, 0.1234, 1.23459, 12.3456, |
94 | | - -0.0001, -0.001, -0.01, -0.1, -1.0, -10.0, |
95 | | - -0.0012, -0.0123, -0.1234, -1.23459, -12.3456) |
96 | | - |
97 | | -if full_tests: |
98 | | - for type in ('f', 'F'): |
99 | | - for width in ('', '4', '6', '8', '10'): |
100 | | - for alignment in ('', '<', '>', '=', '^'): |
101 | | - for fill in ('', ' ', '0', '@'): |
102 | | - for sign in ('', '+', '-', ' '): |
103 | | - # An empty precision defaults to 6, but when uPy is |
104 | | - # configured to use a float, we can only use a |
105 | | - # precision of 6 with numbers less than 10 and still |
106 | | - # get results that compare to CPython (which uses |
107 | | - # long doubles). |
108 | | - for prec in ('1', '2', '3'): |
109 | | - for num in f_nums: |
110 | | - test_fmt('', fill, alignment, sign, '', width, prec, type, num) |
111 | | - for num in int_nums2: |
112 | | - test_fmt('', fill, alignment, sign, '', width, '', type, num) |
113 | | - |
114 | | -pct_nums1 = (0.1, 0.58, 0.99, -0.1, -0.58, -0.99) |
115 | | -pct_nums2 = (True, False, 1, 0, -1) |
116 | | - |
117 | | -if full_tests: |
118 | | - type = '%' |
119 | | - for width in ('', '4', '6', '8', '10'): |
120 | | - for alignment in ('', '<', '>', '=', '^'): |
121 | | - for fill in ('', ' ', '0', '@'): |
122 | | - for sign in ('', '+', '-', ' '): |
123 | | - # An empty precision defaults to 6, but when uPy is |
124 | | - # configured to use a float, we can only use a |
125 | | - # precision of 6 with numbers less than 10 and still |
126 | | - # get results that compare to CPython (which uses |
127 | | - # long doubles). |
128 | | - for prec in ('1', '2', '3'): |
129 | | - for num in pct_nums1: |
130 | | - test_fmt('', fill, alignment, sign, '', width, prec, type, num) |
131 | | - for num in pct_nums2: |
132 | | - test_fmt('', fill, alignment, sign, '', width, '', type, num) |
133 | | -else: |
134 | | - for num in pct_nums1: |
135 | | - test_fmt('', '', '', '', '', '', '1', '%', num) |
136 | | - |
137 | | -# We don't currently test a type of '' with floats (see the detailed comment |
138 | | -# in objstr.c) |
139 | | - |
140 | 36 | # tests for errors in format string |
141 | 37 |
|
142 | 38 | try: |
|
0 commit comments