/Lib/test/test_format.py

http://unladen-swallow.googlecode.com/ · Python · 295 lines · 239 code · 29 blank · 27 comment · 37 complexity · eea3c9ca6c3ee6debf5bec026b84d347 MD5 · raw file

  1. import sys
  2. from test.test_support import verbose, have_unicode, TestFailed
  3. import test.test_support as test_support
  4. import unittest
  5. maxsize = test_support.MAX_Py_ssize_t
  6. # test string formatting operator (I am not sure if this is being tested
  7. # elsewhere but, surely, some of the given cases are *not* tested because
  8. # they crash python)
  9. # test on unicode strings as well
  10. overflowok = 1
  11. overflowrequired = 0
  12. def testformat(formatstr, args, output=None, limit=None):
  13. if verbose:
  14. if output:
  15. print "%s %% %s =? %s ..." %\
  16. (repr(formatstr), repr(args), repr(output)),
  17. else:
  18. print "%s %% %s works? ..." % (repr(formatstr), repr(args)),
  19. try:
  20. result = formatstr % args
  21. except OverflowError:
  22. if not overflowok:
  23. raise
  24. if verbose:
  25. print 'overflow (this is fine)'
  26. else:
  27. if overflowrequired:
  28. if verbose:
  29. print 'no'
  30. print "overflow expected on %s %% %s" % \
  31. (repr(formatstr), repr(args))
  32. elif output and limit is None and result != output:
  33. if verbose:
  34. print 'no'
  35. print "%s %% %s == %s != %s" % \
  36. (repr(formatstr), repr(args), repr(result), repr(output))
  37. # when 'limit' is specified, it determines how many characters
  38. # must match exactly; lengths must always match.
  39. # ex: limit=5, '12345678' matches '12345___'
  40. # (mainly for floating point format tests for which an exact match
  41. # can't be guaranteed due to rounding and representation errors)
  42. elif output and limit is not None and (
  43. len(result)!=len(output) or result[:limit]!=output[:limit]):
  44. if verbose:
  45. print 'no'
  46. print "%s %% %s == %s != %s" % \
  47. (repr(formatstr), repr(args), repr(result), repr(output))
  48. else:
  49. if verbose:
  50. print 'yes'
  51. def testboth(formatstr, *args):
  52. testformat(formatstr, *args)
  53. if have_unicode:
  54. testformat(unicode(formatstr), *args)
  55. class FormatTest(unittest.TestCase):
  56. def test_format(self):
  57. testboth("%.1d", (1,), "1")
  58. testboth("%.*d", (sys.maxint,1)) # expect overflow
  59. testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
  60. testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
  61. testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
  62. testboth("%f", (1.0,), "1.000000")
  63. # these are trying to test the limits of the internal magic-number-length
  64. # formatting buffer, if that number changes then these tests are less
  65. # effective
  66. testboth("%#.*g", (109, -1.e+49/3.))
  67. testboth("%#.*g", (110, -1.e+49/3.))
  68. testboth("%#.*g", (110, -1.e+100/3.))
  69. # test some ridiculously large precision, expect overflow
  70. testboth('%12.*f', (123456, 1.0))
  71. # check for internal overflow validation on length of precision
  72. overflowrequired = 1
  73. testboth("%#.*g", (110, -1.e+100/3.))
  74. testboth("%#.*G", (110, -1.e+100/3.))
  75. testboth("%#.*f", (110, -1.e+100/3.))
  76. testboth("%#.*F", (110, -1.e+100/3.))
  77. overflowrequired = 0
  78. # Formatting of long integers. Overflow is not ok
  79. overflowok = 0
  80. testboth("%x", 10L, "a")
  81. testboth("%x", 100000000000L, "174876e800")
  82. testboth("%o", 10L, "12")
  83. testboth("%o", 100000000000L, "1351035564000")
  84. testboth("%d", 10L, "10")
  85. testboth("%d", 100000000000L, "100000000000")
  86. big = 123456789012345678901234567890L
  87. testboth("%d", big, "123456789012345678901234567890")
  88. testboth("%d", -big, "-123456789012345678901234567890")
  89. testboth("%5d", -big, "-123456789012345678901234567890")
  90. testboth("%31d", -big, "-123456789012345678901234567890")
  91. testboth("%32d", -big, " -123456789012345678901234567890")
  92. testboth("%-32d", -big, "-123456789012345678901234567890 ")
  93. testboth("%032d", -big, "-0123456789012345678901234567890")
  94. testboth("%-032d", -big, "-123456789012345678901234567890 ")
  95. testboth("%034d", -big, "-000123456789012345678901234567890")
  96. testboth("%034d", big, "0000123456789012345678901234567890")
  97. testboth("%0+34d", big, "+000123456789012345678901234567890")
  98. testboth("%+34d", big, " +123456789012345678901234567890")
  99. testboth("%34d", big, " 123456789012345678901234567890")
  100. testboth("%.2d", big, "123456789012345678901234567890")
  101. testboth("%.30d", big, "123456789012345678901234567890")
  102. testboth("%.31d", big, "0123456789012345678901234567890")
  103. testboth("%32.31d", big, " 0123456789012345678901234567890")
  104. testboth("%d", float(big), "123456________________________", 6)
  105. big = 0x1234567890abcdef12345L # 21 hex digits
  106. testboth("%x", big, "1234567890abcdef12345")
  107. testboth("%x", -big, "-1234567890abcdef12345")
  108. testboth("%5x", -big, "-1234567890abcdef12345")
  109. testboth("%22x", -big, "-1234567890abcdef12345")
  110. testboth("%23x", -big, " -1234567890abcdef12345")
  111. testboth("%-23x", -big, "-1234567890abcdef12345 ")
  112. testboth("%023x", -big, "-01234567890abcdef12345")
  113. testboth("%-023x", -big, "-1234567890abcdef12345 ")
  114. testboth("%025x", -big, "-0001234567890abcdef12345")
  115. testboth("%025x", big, "00001234567890abcdef12345")
  116. testboth("%0+25x", big, "+0001234567890abcdef12345")
  117. testboth("%+25x", big, " +1234567890abcdef12345")
  118. testboth("%25x", big, " 1234567890abcdef12345")
  119. testboth("%.2x", big, "1234567890abcdef12345")
  120. testboth("%.21x", big, "1234567890abcdef12345")
  121. testboth("%.22x", big, "01234567890abcdef12345")
  122. testboth("%23.22x", big, " 01234567890abcdef12345")
  123. testboth("%-23.22x", big, "01234567890abcdef12345 ")
  124. testboth("%X", big, "1234567890ABCDEF12345")
  125. testboth("%#X", big, "0X1234567890ABCDEF12345")
  126. testboth("%#x", big, "0x1234567890abcdef12345")
  127. testboth("%#x", -big, "-0x1234567890abcdef12345")
  128. testboth("%#.23x", -big, "-0x001234567890abcdef12345")
  129. testboth("%#+.23x", big, "+0x001234567890abcdef12345")
  130. testboth("%# .23x", big, " 0x001234567890abcdef12345")
  131. testboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
  132. testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
  133. testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
  134. testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
  135. testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
  136. # next one gets two leading zeroes from precision, and another from the
  137. # 0 flag and the width
  138. testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
  139. # same, except no 0 flag
  140. testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
  141. testboth("%x", float(big), "123456_______________", 6)
  142. big = 012345670123456701234567012345670L # 32 octal digits
  143. testboth("%o", big, "12345670123456701234567012345670")
  144. testboth("%o", -big, "-12345670123456701234567012345670")
  145. testboth("%5o", -big, "-12345670123456701234567012345670")
  146. testboth("%33o", -big, "-12345670123456701234567012345670")
  147. testboth("%34o", -big, " -12345670123456701234567012345670")
  148. testboth("%-34o", -big, "-12345670123456701234567012345670 ")
  149. testboth("%034o", -big, "-012345670123456701234567012345670")
  150. testboth("%-034o", -big, "-12345670123456701234567012345670 ")
  151. testboth("%036o", -big, "-00012345670123456701234567012345670")
  152. testboth("%036o", big, "000012345670123456701234567012345670")
  153. testboth("%0+36o", big, "+00012345670123456701234567012345670")
  154. testboth("%+36o", big, " +12345670123456701234567012345670")
  155. testboth("%36o", big, " 12345670123456701234567012345670")
  156. testboth("%.2o", big, "12345670123456701234567012345670")
  157. testboth("%.32o", big, "12345670123456701234567012345670")
  158. testboth("%.33o", big, "012345670123456701234567012345670")
  159. testboth("%34.33o", big, " 012345670123456701234567012345670")
  160. testboth("%-34.33o", big, "012345670123456701234567012345670 ")
  161. testboth("%o", big, "12345670123456701234567012345670")
  162. testboth("%#o", big, "012345670123456701234567012345670")
  163. testboth("%#o", -big, "-012345670123456701234567012345670")
  164. testboth("%#.34o", -big, "-0012345670123456701234567012345670")
  165. testboth("%#+.34o", big, "+0012345670123456701234567012345670")
  166. testboth("%# .34o", big, " 0012345670123456701234567012345670")
  167. testboth("%#+.34o", big, "+0012345670123456701234567012345670")
  168. testboth("%#-+.34o", big, "+0012345670123456701234567012345670")
  169. testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
  170. testboth("%#+37.34o", big, " +0012345670123456701234567012345670")
  171. # next one gets one leading zero from precision
  172. testboth("%.33o", big, "012345670123456701234567012345670")
  173. # base marker shouldn't change that, since "0" is redundant
  174. testboth("%#.33o", big, "012345670123456701234567012345670")
  175. # but reduce precision, and base marker should add a zero
  176. testboth("%#.32o", big, "012345670123456701234567012345670")
  177. # one leading zero from precision, and another from "0" flag & width
  178. testboth("%034.33o", big, "0012345670123456701234567012345670")
  179. # base marker shouldn't change that
  180. testboth("%0#34.33o", big, "0012345670123456701234567012345670")
  181. testboth("%o", float(big), "123456__________________________", 6)
  182. # Some small ints, in both Python int and long flavors).
  183. testboth("%d", 42, "42")
  184. testboth("%d", -42, "-42")
  185. testboth("%d", 42L, "42")
  186. testboth("%d", -42L, "-42")
  187. testboth("%d", 42.0, "42")
  188. testboth("%#x", 1, "0x1")
  189. testboth("%#x", 1L, "0x1")
  190. testboth("%#X", 1, "0X1")
  191. testboth("%#X", 1L, "0X1")
  192. testboth("%#x", 1.0, "0x1")
  193. testboth("%#o", 1, "01")
  194. testboth("%#o", 1L, "01")
  195. testboth("%#o", 0, "0")
  196. testboth("%#o", 0L, "0")
  197. testboth("%o", 0, "0")
  198. testboth("%o", 0L, "0")
  199. testboth("%d", 0, "0")
  200. testboth("%d", 0L, "0")
  201. testboth("%#x", 0, "0x0")
  202. testboth("%#x", 0L, "0x0")
  203. testboth("%#X", 0, "0X0")
  204. testboth("%#X", 0L, "0X0")
  205. testboth("%x", 0x42, "42")
  206. testboth("%x", -0x42, "-42")
  207. testboth("%x", 0x42L, "42")
  208. testboth("%x", -0x42L, "-42")
  209. testboth("%x", float(0x42), "42")
  210. testboth("%o", 042, "42")
  211. testboth("%o", -042, "-42")
  212. testboth("%o", 042L, "42")
  213. testboth("%o", -042L, "-42")
  214. testboth("%o", float(042), "42")
  215. # Test exception for unknown format characters
  216. if verbose:
  217. print 'Testing exceptions'
  218. def test_exc(formatstr, args, exception, excmsg):
  219. try:
  220. testformat(formatstr, args)
  221. except exception, exc:
  222. if str(exc) == excmsg:
  223. if verbose:
  224. print "yes"
  225. else:
  226. if verbose: print 'no'
  227. print 'Unexpected ', exception, ':', repr(str(exc))
  228. except:
  229. if verbose: print 'no'
  230. print 'Unexpected exception'
  231. raise
  232. else:
  233. raise TestFailed, 'did not get expected exception: %s' % excmsg
  234. test_exc('abc %a', 1, ValueError,
  235. "unsupported format character 'a' (0x61) at index 5")
  236. if have_unicode:
  237. test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
  238. "unsupported format character '?' (0x3000) at index 5")
  239. test_exc('%d', '1', TypeError, "%d format: a number is required, not str")
  240. test_exc('%g', '1', TypeError, "float argument required, not str")
  241. test_exc('no format', '1', TypeError,
  242. "not all arguments converted during string formatting")
  243. test_exc('no format', u'1', TypeError,
  244. "not all arguments converted during string formatting")
  245. test_exc(u'no format', '1', TypeError,
  246. "not all arguments converted during string formatting")
  247. test_exc(u'no format', u'1', TypeError,
  248. "not all arguments converted during string formatting")
  249. class Foobar(long):
  250. def __oct__(self):
  251. # Returning a non-string should not blow up.
  252. return self + 1
  253. test_exc('%o', Foobar(), TypeError,
  254. "expected string or Unicode object, long found")
  255. if maxsize == 2**31-1:
  256. # crashes 2.2.1 and earlier:
  257. try:
  258. "%*d"%(maxsize, -127)
  259. except MemoryError:
  260. pass
  261. else:
  262. raise TestFailed, '"%*d"%(maxsize, -127) should fail'
  263. def test_main():
  264. test_support.run_unittest(FormatTest)
  265. if __name__ == "__main__":
  266. unittest.main()