Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 1 | from test_support import verbose |
Eric S. Raymond | d8c628b | 2001-02-09 11:46:37 +0000 | [diff] [blame] | 2 | import sys |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 3 | |
| 4 | # test string formatting operator (I am not sure if this is being tested |
| 5 | # elsewhere but, surely, some of the given cases are *not* tested because |
| 6 | # they crash python) |
| 7 | # test on unicode strings as well |
| 8 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 9 | overflowok = 1 |
| 10 | |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 11 | def testformat(formatstr, args, output=None): |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 12 | if verbose: |
| 13 | if output: |
| 14 | print "%s %% %s =? %s ..." %\ |
| 15 | (repr(formatstr), repr(args), repr(output)), |
| 16 | else: |
| 17 | print "%s %% %s works? ..." % (repr(formatstr), repr(args)), |
| 18 | try: |
| 19 | result = formatstr % args |
| 20 | except OverflowError: |
| 21 | if not overflowok: |
| 22 | raise |
| 23 | if verbose: |
| 24 | print 'overflow (this is fine)' |
| 25 | else: |
| 26 | if output and result != output: |
| 27 | if verbose: |
| 28 | print 'no' |
| 29 | print "%s %% %s == %s != %s" %\ |
| 30 | (repr(formatstr), repr(args), repr(result), repr(output)) |
| 31 | else: |
| 32 | if verbose: |
| 33 | print 'yes' |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 34 | |
| 35 | def testboth(formatstr, *args): |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 36 | testformat(formatstr, *args) |
| 37 | testformat(unicode(formatstr), *args) |
| 38 | |
Marc-André Lemburg | d70141a | 2000-06-30 10:26:29 +0000 | [diff] [blame] | 39 | |
| 40 | testboth("%.1d", (1,), "1") |
| 41 | testboth("%.*d", (sys.maxint,1)) # expect overflow |
| 42 | testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 43 | testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 44 | testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') |
| 45 | |
| 46 | testboth("%f", (1.0,), "1.000000") |
| 47 | # these are trying to test the limits of the internal magic-number-length |
| 48 | # formatting buffer, if that number changes then these tests are less |
| 49 | # effective |
| 50 | testboth("%#.*g", (109, -1.e+49/3.)) |
| 51 | testboth("%#.*g", (110, -1.e+49/3.)) |
| 52 | testboth("%#.*g", (110, -1.e+100/3.)) |
| 53 | |
| 54 | # test some ridiculously large precision, expect overflow |
| 55 | testboth('%12.*f', (123456, 1.0)) |
| 56 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 57 | # Formatting of long integers. Overflow is not ok |
| 58 | overflowok = 0 |
| 59 | testboth("%x", 10L, "a") |
| 60 | testboth("%x", 100000000000L, "174876e800") |
| 61 | testboth("%o", 10L, "12") |
| 62 | testboth("%o", 100000000000L, "1351035564000") |
| 63 | testboth("%d", 10L, "10") |
| 64 | testboth("%d", 100000000000L, "100000000000") |
| 65 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 66 | big = 123456789012345678901234567890L |
| 67 | testboth("%d", big, "123456789012345678901234567890") |
| 68 | testboth("%d", -big, "-123456789012345678901234567890") |
| 69 | testboth("%5d", -big, "-123456789012345678901234567890") |
| 70 | testboth("%31d", -big, "-123456789012345678901234567890") |
| 71 | testboth("%32d", -big, " -123456789012345678901234567890") |
| 72 | testboth("%-32d", -big, "-123456789012345678901234567890 ") |
| 73 | testboth("%032d", -big, "-0123456789012345678901234567890") |
| 74 | testboth("%-032d", -big, "-123456789012345678901234567890 ") |
| 75 | testboth("%034d", -big, "-000123456789012345678901234567890") |
| 76 | testboth("%034d", big, "0000123456789012345678901234567890") |
| 77 | testboth("%0+34d", big, "+000123456789012345678901234567890") |
| 78 | testboth("%+34d", big, " +123456789012345678901234567890") |
| 79 | testboth("%34d", big, " 123456789012345678901234567890") |
| 80 | testboth("%.2d", big, "123456789012345678901234567890") |
| 81 | testboth("%.30d", big, "123456789012345678901234567890") |
| 82 | testboth("%.31d", big, "0123456789012345678901234567890") |
| 83 | testboth("%32.31d", big, " 0123456789012345678901234567890") |
| 84 | |
| 85 | big = 0x1234567890abcdef12345L # 21 hex digits |
| 86 | testboth("%x", big, "1234567890abcdef12345") |
| 87 | testboth("%x", -big, "-1234567890abcdef12345") |
| 88 | testboth("%5x", -big, "-1234567890abcdef12345") |
| 89 | testboth("%22x", -big, "-1234567890abcdef12345") |
| 90 | testboth("%23x", -big, " -1234567890abcdef12345") |
| 91 | testboth("%-23x", -big, "-1234567890abcdef12345 ") |
| 92 | testboth("%023x", -big, "-01234567890abcdef12345") |
| 93 | testboth("%-023x", -big, "-1234567890abcdef12345 ") |
| 94 | testboth("%025x", -big, "-0001234567890abcdef12345") |
| 95 | testboth("%025x", big, "00001234567890abcdef12345") |
| 96 | testboth("%0+25x", big, "+0001234567890abcdef12345") |
| 97 | testboth("%+25x", big, " +1234567890abcdef12345") |
| 98 | testboth("%25x", big, " 1234567890abcdef12345") |
| 99 | testboth("%.2x", big, "1234567890abcdef12345") |
| 100 | testboth("%.21x", big, "1234567890abcdef12345") |
| 101 | testboth("%.22x", big, "01234567890abcdef12345") |
| 102 | testboth("%23.22x", big, " 01234567890abcdef12345") |
| 103 | testboth("%-23.22x", big, "01234567890abcdef12345 ") |
| 104 | testboth("%X", big, "1234567890ABCDEF12345") |
| 105 | testboth("%#X", big, "0X1234567890ABCDEF12345") |
| 106 | testboth("%#x", big, "0x1234567890abcdef12345") |
| 107 | testboth("%#x", -big, "-0x1234567890abcdef12345") |
| 108 | testboth("%#.23x", -big, "-0x001234567890abcdef12345") |
| 109 | testboth("%#+.23x", big, "+0x001234567890abcdef12345") |
| 110 | testboth("%# .23x", big, " 0x001234567890abcdef12345") |
| 111 | testboth("%#+.23X", big, "+0X001234567890ABCDEF12345") |
| 112 | testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345") |
| 113 | testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345") |
| 114 | testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ") |
| 115 | testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 116 | # next one gets two leading zeroes from precision, and another from the |
| 117 | # 0 flag and the width |
| 118 | testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
| 119 | # same, except no 0 flag |
| 120 | testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 121 | |
| 122 | big = 012345670123456701234567012345670L # 32 octal digits |
| 123 | testboth("%o", big, "12345670123456701234567012345670") |
| 124 | testboth("%o", -big, "-12345670123456701234567012345670") |
| 125 | testboth("%5o", -big, "-12345670123456701234567012345670") |
| 126 | testboth("%33o", -big, "-12345670123456701234567012345670") |
| 127 | testboth("%34o", -big, " -12345670123456701234567012345670") |
| 128 | testboth("%-34o", -big, "-12345670123456701234567012345670 ") |
| 129 | testboth("%034o", -big, "-012345670123456701234567012345670") |
| 130 | testboth("%-034o", -big, "-12345670123456701234567012345670 ") |
| 131 | testboth("%036o", -big, "-00012345670123456701234567012345670") |
| 132 | testboth("%036o", big, "000012345670123456701234567012345670") |
| 133 | testboth("%0+36o", big, "+00012345670123456701234567012345670") |
| 134 | testboth("%+36o", big, " +12345670123456701234567012345670") |
| 135 | testboth("%36o", big, " 12345670123456701234567012345670") |
| 136 | testboth("%.2o", big, "12345670123456701234567012345670") |
| 137 | testboth("%.32o", big, "12345670123456701234567012345670") |
| 138 | testboth("%.33o", big, "012345670123456701234567012345670") |
| 139 | testboth("%34.33o", big, " 012345670123456701234567012345670") |
| 140 | testboth("%-34.33o", big, "012345670123456701234567012345670 ") |
| 141 | testboth("%o", big, "12345670123456701234567012345670") |
| 142 | testboth("%#o", big, "012345670123456701234567012345670") |
| 143 | testboth("%#o", -big, "-012345670123456701234567012345670") |
| 144 | testboth("%#.34o", -big, "-0012345670123456701234567012345670") |
| 145 | testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 146 | testboth("%# .34o", big, " 0012345670123456701234567012345670") |
| 147 | testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 148 | testboth("%#-+.34o", big, "+0012345670123456701234567012345670") |
| 149 | testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ") |
| 150 | testboth("%#+37.34o", big, " +0012345670123456701234567012345670") |
| 151 | # next one gets one leading zero from precision |
| 152 | testboth("%.33o", big, "012345670123456701234567012345670") |
| 153 | # base marker shouldn't change that, since "0" is redundant |
| 154 | testboth("%#.33o", big, "012345670123456701234567012345670") |
| 155 | # but reduce precision, and base marker should add a zero |
| 156 | testboth("%#.32o", big, "012345670123456701234567012345670") |
| 157 | # one leading zero from precision, and another from "0" flag & width |
| 158 | testboth("%034.33o", big, "0012345670123456701234567012345670") |
| 159 | # base marker shouldn't change that |
| 160 | testboth("%0#34.33o", big, "0012345670123456701234567012345670") |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 161 | |
| 162 | # Some small ints, in both Python int and long flavors). |
| 163 | testboth("%d", 42, "42") |
| 164 | testboth("%d", -42, "-42") |
| 165 | testboth("%d", 42L, "42") |
| 166 | testboth("%d", -42L, "-42") |
Tim Peters | 711088d | 2001-04-12 00:35:51 +0000 | [diff] [blame] | 167 | testboth("%#x", 1, "0x1") |
| 168 | testboth("%#x", 1L, "0x1") |
| 169 | testboth("%#X", 1, "0X1") |
| 170 | testboth("%#X", 1L, "0X1") |
| 171 | testboth("%#o", 1, "01") |
| 172 | testboth("%#o", 1L, "01") |
| 173 | testboth("%#o", 0, "0") |
| 174 | testboth("%#o", 0L, "0") |
| 175 | testboth("%o", 0, "0") |
| 176 | testboth("%o", 0L, "0") |
| 177 | testboth("%d", 0, "0") |
| 178 | testboth("%d", 0L, "0") |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 179 | testboth("%#x", 0, "0x0") |
| 180 | testboth("%#x", 0L, "0x0") |
| 181 | testboth("%#X", 0, "0X0") |
| 182 | testboth("%#X", 0L, "0X0") |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 183 | |
| 184 | testboth("%x", 0x42, "42") |
| 185 | # testboth("%x", -0x42, "ffffffbe") # Alas, that's specific to 32-bit machines |
| 186 | testboth("%x", 0x42L, "42") |
| 187 | testboth("%x", -0x42L, "-42") |
| 188 | |
| 189 | testboth("%o", 042, "42") |
| 190 | # testboth("%o", -042, "37777777736") # Alas, that's specific to 32-bit machines |
| 191 | testboth("%o", 042L, "42") |
| 192 | testboth("%o", -042L, "-42") |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 193 | |
| 194 | # Test exception for unknown format characters |
| 195 | if verbose: |
| 196 | print 'Testing exceptions' |
| 197 | |
| 198 | def test_exc(formatstr, args, exception, excmsg): |
| 199 | try: |
| 200 | testformat(formatstr, args) |
| 201 | except exception, exc: |
| 202 | if str(exc) == excmsg: |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 203 | if verbose: |
Andrew M. Kuchling | 4d192b3 | 2000-12-15 13:09:06 +0000 | [diff] [blame] | 204 | print "yes" |
| 205 | else: |
| 206 | if verbose: print 'no' |
| 207 | print 'Unexpected ', exception, ':', repr(str(exc)) |
| 208 | except: |
| 209 | if verbose: print 'no' |
| 210 | print 'Unexpected exception' |
| 211 | raise |
| 212 | |
| 213 | test_exc('abc %a', 1, ValueError, |
| 214 | "unsupported format character 'a' (0x61) at index 5") |
| 215 | test_exc(u'abc %\u3000', 1, ValueError, |
Andrew M. Kuchling | c867f74 | 2000-12-20 00:55:46 +0000 | [diff] [blame] | 216 | "unsupported format character '?' (0x3000) at index 5") |