Back to index

python3.2  3.2.2
Public Member Functions
test.test_format.FormatTest Class Reference

List of all members.

Public Member Functions

def test_format

Detailed Description

Definition at line 49 of file test_format.py.


Member Function Documentation

Definition at line 50 of file test_format.py.

00050 
00051     def test_format(self):
00052         testformat("%.1d", (1,), "1")
00053         testformat("%.*d", (sys.maxsize,1), overflowok=True)  # expect overflow
00054         testformat("%.100d", (1,), '00000000000000000000000000000000000000'
00055                  '000000000000000000000000000000000000000000000000000000'
00056                  '00000001', overflowok=True)
00057         testformat("%#.117x", (1,), '0x00000000000000000000000000000000000'
00058                  '000000000000000000000000000000000000000000000000000000'
00059                  '0000000000000000000000000001',
00060                  overflowok=True)
00061         testformat("%#.118x", (1,), '0x00000000000000000000000000000000000'
00062                  '000000000000000000000000000000000000000000000000000000'
00063                  '00000000000000000000000000001',
00064                  overflowok=True)
00065 
00066         testformat("%f", (1.0,), "1.000000")
00067         # these are trying to test the limits of the internal magic-number-length
00068         # formatting buffer, if that number changes then these tests are less
00069         # effective
00070         testformat("%#.*g", (109, -1.e+49/3.))
00071         testformat("%#.*g", (110, -1.e+49/3.))
00072         testformat("%#.*g", (110, -1.e+100/3.))
00073         # test some ridiculously large precision, expect overflow
00074         testformat('%12.*f', (123456, 1.0))
00075 
00076         # check for internal overflow validation on length of precision
00077         # these tests should no longer cause overflow in Python
00078         # 2.7/3.1 and later.
00079         testformat("%#.*g", (110, -1.e+100/3.))
00080         testformat("%#.*G", (110, -1.e+100/3.))
00081         testformat("%#.*f", (110, -1.e+100/3.))
00082         testformat("%#.*F", (110, -1.e+100/3.))
00083         # Formatting of integers. Overflow is not ok
00084         testformat("%x", 10, "a")
00085         testformat("%x", 100000000000, "174876e800")
00086         testformat("%o", 10, "12")
00087         testformat("%o", 100000000000, "1351035564000")
00088         testformat("%d", 10, "10")
00089         testformat("%d", 100000000000, "100000000000")
00090         big = 123456789012345678901234567890
00091         testformat("%d", big, "123456789012345678901234567890")
00092         testformat("%d", -big, "-123456789012345678901234567890")
00093         testformat("%5d", -big, "-123456789012345678901234567890")
00094         testformat("%31d", -big, "-123456789012345678901234567890")
00095         testformat("%32d", -big, " -123456789012345678901234567890")
00096         testformat("%-32d", -big, "-123456789012345678901234567890 ")
00097         testformat("%032d", -big, "-0123456789012345678901234567890")
00098         testformat("%-032d", -big, "-123456789012345678901234567890 ")
00099         testformat("%034d", -big, "-000123456789012345678901234567890")
00100         testformat("%034d", big, "0000123456789012345678901234567890")
00101         testformat("%0+34d", big, "+000123456789012345678901234567890")
00102         testformat("%+34d", big, "   +123456789012345678901234567890")
00103         testformat("%34d", big, "    123456789012345678901234567890")
00104         testformat("%.2d", big, "123456789012345678901234567890")
00105         testformat("%.30d", big, "123456789012345678901234567890")
00106         testformat("%.31d", big, "0123456789012345678901234567890")
00107         testformat("%32.31d", big, " 0123456789012345678901234567890")
00108         testformat("%d", float(big), "123456________________________", 6)
00109         big = 0x1234567890abcdef12345  # 21 hex digits
00110         testformat("%x", big, "1234567890abcdef12345")
00111         testformat("%x", -big, "-1234567890abcdef12345")
00112         testformat("%5x", -big, "-1234567890abcdef12345")
00113         testformat("%22x", -big, "-1234567890abcdef12345")
00114         testformat("%23x", -big, " -1234567890abcdef12345")
00115         testformat("%-23x", -big, "-1234567890abcdef12345 ")
00116         testformat("%023x", -big, "-01234567890abcdef12345")
00117         testformat("%-023x", -big, "-1234567890abcdef12345 ")
00118         testformat("%025x", -big, "-0001234567890abcdef12345")
00119         testformat("%025x", big, "00001234567890abcdef12345")
00120         testformat("%0+25x", big, "+0001234567890abcdef12345")
00121         testformat("%+25x", big, "   +1234567890abcdef12345")
00122         testformat("%25x", big, "    1234567890abcdef12345")
00123         testformat("%.2x", big, "1234567890abcdef12345")
00124         testformat("%.21x", big, "1234567890abcdef12345")
00125         testformat("%.22x", big, "01234567890abcdef12345")
00126         testformat("%23.22x", big, " 01234567890abcdef12345")
00127         testformat("%-23.22x", big, "01234567890abcdef12345 ")
00128         testformat("%X", big, "1234567890ABCDEF12345")
00129         testformat("%#X", big, "0X1234567890ABCDEF12345")
00130         testformat("%#x", big, "0x1234567890abcdef12345")
00131         testformat("%#x", -big, "-0x1234567890abcdef12345")
00132         testformat("%#.23x", -big, "-0x001234567890abcdef12345")
00133         testformat("%#+.23x", big, "+0x001234567890abcdef12345")
00134         testformat("%# .23x", big, " 0x001234567890abcdef12345")
00135         testformat("%#+.23X", big, "+0X001234567890ABCDEF12345")
00136         testformat("%#-+.23X", big, "+0X001234567890ABCDEF12345")
00137         testformat("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
00138         testformat("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
00139         testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
00140         # next one gets two leading zeroes from precision, and another from the
00141         # 0 flag and the width
00142         testformat("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
00143         # same, except no 0 flag
00144         testformat("%#+27.23X", big, " +0X001234567890ABCDEF12345")
00145         testformat("%x", float(big), "123456_______________", 6)
00146         big = 0o12345670123456701234567012345670  # 32 octal digits
00147         testformat("%o", big, "12345670123456701234567012345670")
00148         testformat("%o", -big, "-12345670123456701234567012345670")
00149         testformat("%5o", -big, "-12345670123456701234567012345670")
00150         testformat("%33o", -big, "-12345670123456701234567012345670")
00151         testformat("%34o", -big, " -12345670123456701234567012345670")
00152         testformat("%-34o", -big, "-12345670123456701234567012345670 ")
00153         testformat("%034o", -big, "-012345670123456701234567012345670")
00154         testformat("%-034o", -big, "-12345670123456701234567012345670 ")
00155         testformat("%036o", -big, "-00012345670123456701234567012345670")
00156         testformat("%036o", big, "000012345670123456701234567012345670")
00157         testformat("%0+36o", big, "+00012345670123456701234567012345670")
00158         testformat("%+36o", big, "   +12345670123456701234567012345670")
00159         testformat("%36o", big, "    12345670123456701234567012345670")
00160         testformat("%.2o", big, "12345670123456701234567012345670")
00161         testformat("%.32o", big, "12345670123456701234567012345670")
00162         testformat("%.33o", big, "012345670123456701234567012345670")
00163         testformat("%34.33o", big, " 012345670123456701234567012345670")
00164         testformat("%-34.33o", big, "012345670123456701234567012345670 ")
00165         testformat("%o", big, "12345670123456701234567012345670")
00166         testformat("%#o", big, "0o12345670123456701234567012345670")
00167         testformat("%#o", -big, "-0o12345670123456701234567012345670")
00168         testformat("%#.34o", -big, "-0o0012345670123456701234567012345670")
00169         testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
00170         testformat("%# .34o", big, " 0o0012345670123456701234567012345670")
00171         testformat("%#+.34o", big, "+0o0012345670123456701234567012345670")
00172         testformat("%#-+.34o", big, "+0o0012345670123456701234567012345670")
00173         testformat("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
00174         testformat("%#+37.34o", big, "+0o0012345670123456701234567012345670")
00175         # next one gets one leading zero from precision
00176         testformat("%.33o", big, "012345670123456701234567012345670")
00177         # base marker shouldn't change that, since "0" is redundant
00178         testformat("%#.33o", big, "0o012345670123456701234567012345670")
00179         # but reduce precision, and base marker should add a zero
00180         testformat("%#.32o", big, "0o12345670123456701234567012345670")
00181         # one leading zero from precision, and another from "0" flag & width
00182         testformat("%034.33o", big, "0012345670123456701234567012345670")
00183         # base marker shouldn't change that
00184         testformat("%0#34.33o", big, "0o012345670123456701234567012345670")
00185         testformat("%o", float(big), "123456__________________________", 6)
00186         # Some small ints, in both Python int and flavors).
00187         testformat("%d", 42, "42")
00188         testformat("%d", -42, "-42")
00189         testformat("%d", 42, "42")
00190         testformat("%d", -42, "-42")
00191         testformat("%d", 42.0, "42")
00192         testformat("%#x", 1, "0x1")
00193         testformat("%#x", 1, "0x1")
00194         testformat("%#X", 1, "0X1")
00195         testformat("%#X", 1, "0X1")
00196         testformat("%#x", 1.0, "0x1")
00197         testformat("%#o", 1, "0o1")
00198         testformat("%#o", 1, "0o1")
00199         testformat("%#o", 0, "0o0")
00200         testformat("%#o", 0, "0o0")
00201         testformat("%o", 0, "0")
00202         testformat("%o", 0, "0")
00203         testformat("%d", 0, "0")
00204         testformat("%d", 0, "0")
00205         testformat("%#x", 0, "0x0")
00206         testformat("%#x", 0, "0x0")
00207         testformat("%#X", 0, "0X0")
00208         testformat("%#X", 0, "0X0")
00209         testformat("%x", 0x42, "42")
00210         testformat("%x", -0x42, "-42")
00211         testformat("%x", 0x42, "42")
00212         testformat("%x", -0x42, "-42")
00213         testformat("%x", float(0x42), "42")
00214         testformat("%o", 0o42, "42")
00215         testformat("%o", -0o42, "-42")
00216         testformat("%o", 0o42, "42")
00217         testformat("%o", -0o42, "-42")
00218         testformat("%o", float(0o42), "42")
00219         testformat("%r", "\u0378", "'\\u0378'")  # non printable
00220         testformat("%a", "\u0378", "'\\u0378'")  # non printable
00221         testformat("%r", "\u0374", "'\u0374'")   # printable
00222         testformat("%a", "\u0374", "'\\u0374'")  # printable
00223 
00224         # alternate float formatting
00225         testformat('%g', 1.1, '1.1')
00226         testformat('%#g', 1.1, '1.10000')
00227 
00228         # Test exception for unknown format characters
00229         if verbose:
00230             print('Testing exceptions')
00231         def test_exc(formatstr, args, exception, excmsg):
00232             try:
00233                 testformat(formatstr, args)
00234             except exception as exc:
00235                 if str(exc) == excmsg:
00236                     if verbose:
00237                         print("yes")
00238                 else:
00239                     if verbose: print('no')
00240                     print('Unexpected ', exception, ':', repr(str(exc)))
00241             except:
00242                 if verbose: print('no')
00243                 print('Unexpected exception')
00244                 raise
00245             else:
00246                 raise TestFailed('did not get expected exception: %s' % excmsg)
00247         test_exc('abc %b', 1, ValueError,
00248                  "unsupported format character 'b' (0x62) at index 5")
00249         #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
00250         #         "unsupported format character '?' (0x3000) at index 5")
00251         test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
00252         test_exc('%g', '1', TypeError, "a float is required")
00253         test_exc('no format', '1', TypeError,
00254                  "not all arguments converted during string formatting")
00255         test_exc('no format', '1', TypeError,
00256                  "not all arguments converted during string formatting")
00257 
00258         if maxsize == 2**31-1:
00259             # crashes 2.2.1 and earlier:
00260             try:
00261                 "%*d"%(maxsize, -127)
00262             except MemoryError:
00263                 pass
00264             else:
00265                 raise TestFailed('"%*d"%(maxsize, -127) should fail')

Here is the call graph for this function:


The documentation for this class was generated from the following file: