test_format.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import unittest
  2. import sys
  3. verbose = False
  4. have_unicode=False
  5. # Adapted from Python 2.7.x test_format.py
  6. # testformat(formatstr, *args, **kwargs)
  7. class FormatTest(unittest.TestCase):
  8. def doboth(self, formatstr, *args, **kwargs):
  9. if verbose:
  10. print "Testing ", formatstr, " on ", args[1]
  11. self.assertEqual(formatstr%args[0], args[1])
  12. if have_unicode:
  13. self.assertEqual(unicode(formatstr)%args[0], args[1])
  14. def test_format(self):
  15. # self.doboth("%.1d", (1,), "1")
  16. # self.doboth("%.*d", (sys.maxint,1), overflowok=True) # expect overflow
  17. self.doboth("%.100d", (1,), '00000000000000000000000000000000000000'
  18. '000000000000000000000000000000000000000000000000000000'
  19. '00000001', overflowok=True)
  20. self.doboth("%#.117x", (1,), '0x00000000000000000000000000000000000'
  21. '000000000000000000000000000000000000000000000000000000'
  22. '0000000000000000000000000001',
  23. overflowok=True)
  24. self.doboth("%#.118x", (1,), '0x00000000000000000000000000000000000'
  25. '000000000000000000000000000000000000000000000000000000'
  26. '00000000000000000000000000001',
  27. overflowok=True)
  28. # self.doboth("%f", (1.0,), "1.000000")
  29. # these are trying to test the limits of the internal magic-number-length
  30. # formatting buffer, if that number changes then these tests are less
  31. # effective
  32. # self.doboth("%#.*g", (109, -1.e+49/3.))
  33. # self.doboth("%#.*g", (110, -1.e+49/3.))
  34. # self.doboth("%#.*g", (110, -1.e+100/3.))
  35. #
  36. # test some ridiculously large precision, expect overflow
  37. # self.doboth('%12.*f', (123456, 1.0))
  38. # check for internal overflow validation on length of precision
  39. # these tests should no longer cause overflow in Python
  40. # 2.7/3.1 and later.
  41. # self.doboth("%#.*g", (110, -1.e+100/3.))
  42. # self.doboth("%#.*G", (110, -1.e+100/3.))
  43. # self.doboth("%#.*f", (110, -1.e+100/3.))
  44. # self.doboth("%#.*F", (110, -1.e+100/3.))
  45. # Formatting of long integers. Overflow is not ok
  46. self.doboth("%x", 10L, "a")
  47. self.doboth("%x", 100000000000L, "174876e800")
  48. self.doboth("%o", 10L, "12")
  49. self.doboth("%o", 100000000000L, "1351035564000")
  50. self.doboth("%d", 10L, "10")
  51. self.doboth("%d", 100000000000L, "100000000000")
  52. big = 123456789012345678901234567890L
  53. self.doboth("%d", big, "123456789012345678901234567890")
  54. self.doboth("%d", -big, "-123456789012345678901234567890")
  55. self.doboth("%5d", -big, "-123456789012345678901234567890")
  56. self.doboth("%31d", -big, "-123456789012345678901234567890")
  57. self.doboth("%32d", -big, " -123456789012345678901234567890")
  58. self.doboth("%-32d", -big, "-123456789012345678901234567890 ")
  59. self.doboth("%032d", -big, "-0123456789012345678901234567890")
  60. self.doboth("%-032d", -big, "-123456789012345678901234567890 ")
  61. self.doboth("%034d", -big, "-000123456789012345678901234567890")
  62. self.doboth("%034d", big, "0000123456789012345678901234567890")
  63. self.doboth("%0+34d", big, "+000123456789012345678901234567890")
  64. self.doboth("%+34d", big, " +123456789012345678901234567890")
  65. self.doboth("%34d", big, " 123456789012345678901234567890")
  66. self.doboth("%.2d", big, "123456789012345678901234567890")
  67. self.doboth("%.30d", big, "123456789012345678901234567890")
  68. self.doboth("%.31d", big, "0123456789012345678901234567890")
  69. self.doboth("%32.31d", big, " 0123456789012345678901234567890")
  70. # self.doboth("%d", float(big), "123456________________________", 6)
  71. big = 0x1234567890abcdef12345L # 21 hex digits
  72. self.doboth("%x", big, "1234567890abcdef12345")
  73. self.doboth("%x", -big, "-1234567890abcdef12345")
  74. self.doboth("%5x", -big, "-1234567890abcdef12345")
  75. self.doboth("%22x", -big, "-1234567890abcdef12345")
  76. self.doboth("%23x", -big, " -1234567890abcdef12345")
  77. self.doboth("%-23x", -big, "-1234567890abcdef12345 ")
  78. self.doboth("%023x", -big, "-01234567890abcdef12345")
  79. self.doboth("%-023x", -big, "-1234567890abcdef12345 ")
  80. self.doboth("%025x", -big, "-0001234567890abcdef12345")
  81. self.doboth("%025x", big, "00001234567890abcdef12345")
  82. self.doboth("%0+25x", big, "+0001234567890abcdef12345")
  83. self.doboth("%+25x", big, " +1234567890abcdef12345")
  84. self.doboth("%25x", big, " 1234567890abcdef12345")
  85. self.doboth("%.2x", big, "1234567890abcdef12345")
  86. self.doboth("%.21x", big, "1234567890abcdef12345")
  87. self.doboth("%.22x", big, "01234567890abcdef12345")
  88. self.doboth("%23.22x", big, " 01234567890abcdef12345")
  89. self.doboth("%-23.22x", big, "01234567890abcdef12345 ")
  90. self.doboth("%X", big, "1234567890ABCDEF12345")
  91. self.doboth("%#X", big, "0X1234567890ABCDEF12345")
  92. self.doboth("%#x", big, "0x1234567890abcdef12345")
  93. self.doboth("%#x", -big, "-0x1234567890abcdef12345")
  94. self.doboth("%#.23x", -big, "-0x001234567890abcdef12345")
  95. self.doboth("%#+.23x", big, "+0x001234567890abcdef12345")
  96. self.doboth("%# .23x", big, " 0x001234567890abcdef12345")
  97. self.doboth("%#+.23X", big, "+0X001234567890ABCDEF12345")
  98. self.doboth("%#-+.23X", big, "+0X001234567890ABCDEF12345")
  99. self.doboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
  100. self.doboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
  101. self.doboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
  102. # next one gets two leading zeroes from precision, and another from the
  103. # 0 flag and the width
  104. self.doboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
  105. # same, except no 0 flag
  106. self.doboth("%#+27.23X", big, " +0X001234567890ABCDEF12345")
  107. # self.doboth("%x", float(big), "123456_______________", 6)
  108. big = 012345670123456701234567012345670L # 32 octal digits
  109. self.doboth("%o", big, "12345670123456701234567012345670")
  110. self.doboth("%o", -big, "-12345670123456701234567012345670")
  111. self.doboth("%5o", -big, "-12345670123456701234567012345670")
  112. self.doboth("%33o", -big, "-12345670123456701234567012345670")
  113. self.doboth("%34o", -big, " -12345670123456701234567012345670")
  114. self.doboth("%-34o", -big, "-12345670123456701234567012345670 ")
  115. self.doboth("%034o", -big, "-012345670123456701234567012345670")
  116. self.doboth("%-034o", -big, "-12345670123456701234567012345670 ")
  117. self.doboth("%036o", -big, "-00012345670123456701234567012345670")
  118. self.doboth("%036o", big, "000012345670123456701234567012345670")
  119. self.doboth("%0+36o", big, "+00012345670123456701234567012345670")
  120. self.doboth("%+36o", big, " +12345670123456701234567012345670")
  121. self.doboth("%36o", big, " 12345670123456701234567012345670")
  122. self.doboth("%.2o", big, "12345670123456701234567012345670")
  123. self.doboth("%.32o", big, "12345670123456701234567012345670")
  124. self.doboth("%.33o", big, "012345670123456701234567012345670")
  125. self.doboth("%34.33o", big, " 012345670123456701234567012345670")
  126. self.doboth("%-34.33o", big, "012345670123456701234567012345670 ")
  127. self.doboth("%o", big, "12345670123456701234567012345670")
  128. self.doboth("%#o", big, "012345670123456701234567012345670")
  129. self.doboth("%#o", -big, "-012345670123456701234567012345670")
  130. self.doboth("%#.34o", -big, "-0012345670123456701234567012345670")
  131. self.doboth("%#+.34o", big, "+0012345670123456701234567012345670")
  132. self.doboth("%# .34o", big, " 0012345670123456701234567012345670")
  133. self.doboth("%#+.34o", big, "+0012345670123456701234567012345670")
  134. self.doboth("%#-+.34o", big, "+0012345670123456701234567012345670")
  135. self.doboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ")
  136. self.doboth("%#+37.34o", big, " +0012345670123456701234567012345670")
  137. # next one gets one leading zero from precision
  138. self.doboth("%.33o", big, "012345670123456701234567012345670")
  139. # base marker shouldn't change that, since "0" is redundant
  140. self.doboth("%#.33o", big, "012345670123456701234567012345670")
  141. # but reduce precision, and base marker should add a zero
  142. self.doboth("%#.32o", big, "012345670123456701234567012345670")
  143. # one leading zero from precision, and another from "0" flag & width
  144. self.doboth("%034.33o", big, "0012345670123456701234567012345670")
  145. # base marker shouldn't change that
  146. self.doboth("%0#34.33o", big, "0012345670123456701234567012345670")
  147. # self.doboth("%o", float(big), "123456__________________________", 6)
  148. # Some small ints, in both Python int and long flavors).
  149. self.doboth("%d", 42, "42")
  150. self.doboth("%d", -42, "-42")
  151. self.doboth("%d", 42L, "42")
  152. self.doboth("%d", -42L, "-42")
  153. self.doboth("%d", 42.0, "42")
  154. self.doboth("%#x", 1, "0x1")
  155. self.doboth("%#x", 1L, "0x1")
  156. self.doboth("%#X", 1, "0X1")
  157. self.doboth("%#X", 1L, "0X1")
  158. self.doboth("%#x", 1.0, "0x1")
  159. self.doboth("%#o", 1, "01")
  160. self.doboth("%#o", 1L, "01")
  161. self.doboth("%#o", 0, "0")
  162. self.doboth("%#o", 0L, "0")
  163. self.doboth("%o", 0, "0")
  164. self.doboth("%o", 0L, "0")
  165. self.doboth("%d", 0, "0")
  166. self.doboth("%d", 0L, "0")
  167. self.doboth("%#x", 0, "0x0")
  168. self.doboth("%#x", 0L, "0x0")
  169. self.doboth("%#X", 0, "0X0")
  170. self.doboth("%#X", 0L, "0X0")
  171. self.doboth("%x", 0x42, "42")
  172. self.doboth("%x", -0x42, "-42")
  173. self.doboth("%x", 0x42L, "42")
  174. self.doboth("%x", -0x42L, "-42")
  175. self.doboth("%x", float(0x42), "42")
  176. self.doboth("%o", 042, "42")
  177. self.doboth("%o", -042, "-42")
  178. self.doboth("%o", 042L, "42")
  179. self.doboth("%o", -042L, "-42")
  180. self.doboth("%o", float(042), "42")
  181. self.doboth("%4s%4s%4s", ("dez", "okt","hex"), " dez okt hex")
  182. self.doboth("%(thing)s", {'thing': 'abc'}, "abc")
  183. self.doboth("%(thing)4s", {'thing': 'abc'}, " abc")
  184. self.doboth("%s", True, "True")
  185. self.doboth("%s",{'a':1}, "{'a': 1}")
  186. self.doboth("%s",[], "[]")
  187. # alternate float formatting
  188. # testformat('%g', 1.1, '1.1')
  189. # testformat('%#g', 1.1, '1.10000')
  190. if __name__ == "__main__":
  191. print "hello"
  192. unittest.main()