PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_int_literal.py

https://bitbucket.org/glix/python
Python | 191 lines | 123 code | 17 blank | 51 comment | 1 complexity | fed0a971a30e39a98e8005df19cdb72d MD5 | raw file
  1. """Test correct treatment of hex/oct constants.
  2. This is complex because of changes due to PEP 237.
  3. """
  4. import unittest
  5. from test import test_support
  6. import warnings
  7. warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
  8. "<string>")
  9. class TestHexOctBin(unittest.TestCase):
  10. def test_hex_baseline(self):
  11. # A few upper/lowercase tests
  12. self.assertEqual(0x0, 0X0)
  13. self.assertEqual(0x1, 0X1)
  14. self.assertEqual(0x123456789abcdef, 0X123456789abcdef)
  15. # Baseline tests
  16. self.assertEqual(0x0, 0)
  17. self.assertEqual(0x10, 16)
  18. self.assertEqual(0x7fffffff, 2147483647)
  19. self.assertEqual(0x7fffffffffffffff, 9223372036854775807)
  20. # Ditto with a minus sign and parentheses
  21. self.assertEqual(-(0x0), 0)
  22. self.assertEqual(-(0x10), -16)
  23. self.assertEqual(-(0x7fffffff), -2147483647)
  24. self.assertEqual(-(0x7fffffffffffffff), -9223372036854775807)
  25. # Ditto with a minus sign and NO parentheses
  26. self.assertEqual(-0x0, 0)
  27. self.assertEqual(-0x10, -16)
  28. self.assertEqual(-0x7fffffff, -2147483647)
  29. self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
  30. def test_hex_unsigned(self):
  31. # Positive constants
  32. self.assertEqual(0x80000000, 2147483648L)
  33. self.assertEqual(0xffffffff, 4294967295L)
  34. # Ditto with a minus sign and parentheses
  35. self.assertEqual(-(0x80000000), -2147483648L)
  36. self.assertEqual(-(0xffffffff), -4294967295L)
  37. # Ditto with a minus sign and NO parentheses
  38. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  39. self.assertEqual(-0x80000000, -2147483648L)
  40. self.assertEqual(-0xffffffff, -4294967295L)
  41. # Positive constants
  42. self.assertEqual(0x8000000000000000, 9223372036854775808L)
  43. self.assertEqual(0xffffffffffffffff, 18446744073709551615L)
  44. # Ditto with a minus sign and parentheses
  45. self.assertEqual(-(0x8000000000000000), -9223372036854775808L)
  46. self.assertEqual(-(0xffffffffffffffff), -18446744073709551615L)
  47. # Ditto with a minus sign and NO parentheses
  48. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  49. self.assertEqual(-0x8000000000000000, -9223372036854775808L)
  50. self.assertEqual(-0xffffffffffffffff, -18446744073709551615L)
  51. def test_oct_baseline(self):
  52. # Baseline tests
  53. self.assertEqual(00, 0)
  54. self.assertEqual(020, 16)
  55. self.assertEqual(017777777777, 2147483647)
  56. self.assertEqual(0777777777777777777777, 9223372036854775807)
  57. # Ditto with a minus sign and parentheses
  58. self.assertEqual(-(00), 0)
  59. self.assertEqual(-(020), -16)
  60. self.assertEqual(-(017777777777), -2147483647)
  61. self.assertEqual(-(0777777777777777777777), -9223372036854775807)
  62. # Ditto with a minus sign and NO parentheses
  63. self.assertEqual(-00, 0)
  64. self.assertEqual(-020, -16)
  65. self.assertEqual(-017777777777, -2147483647)
  66. self.assertEqual(-0777777777777777777777, -9223372036854775807)
  67. def test_oct_baseline_new(self):
  68. # A few upper/lowercase tests
  69. self.assertEqual(0o0, 0O0)
  70. self.assertEqual(0o1, 0O1)
  71. self.assertEqual(0o1234567, 0O1234567)
  72. # Baseline tests
  73. self.assertEqual(0o0, 0)
  74. self.assertEqual(0o20, 16)
  75. self.assertEqual(0o17777777777, 2147483647)
  76. self.assertEqual(0o777777777777777777777, 9223372036854775807)
  77. # Ditto with a minus sign and parentheses
  78. self.assertEqual(-(0o0), 0)
  79. self.assertEqual(-(0o20), -16)
  80. self.assertEqual(-(0o17777777777), -2147483647)
  81. self.assertEqual(-(0o777777777777777777777), -9223372036854775807)
  82. # Ditto with a minus sign and NO parentheses
  83. self.assertEqual(-0o0, 0)
  84. self.assertEqual(-0o20, -16)
  85. self.assertEqual(-0o17777777777, -2147483647)
  86. self.assertEqual(-0o777777777777777777777, -9223372036854775807)
  87. def test_oct_unsigned(self):
  88. # Positive constants
  89. self.assertEqual(020000000000, 2147483648L)
  90. self.assertEqual(037777777777, 4294967295L)
  91. # Ditto with a minus sign and parentheses
  92. self.assertEqual(-(020000000000), -2147483648L)
  93. self.assertEqual(-(037777777777), -4294967295L)
  94. # Ditto with a minus sign and NO parentheses
  95. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  96. self.assertEqual(-020000000000, -2147483648L)
  97. self.assertEqual(-037777777777, -4294967295L)
  98. # Positive constants
  99. self.assertEqual(01000000000000000000000, 9223372036854775808L)
  100. self.assertEqual(01777777777777777777777, 18446744073709551615L)
  101. # Ditto with a minus sign and parentheses
  102. self.assertEqual(-(01000000000000000000000), -9223372036854775808L)
  103. self.assertEqual(-(01777777777777777777777), -18446744073709551615L)
  104. # Ditto with a minus sign and NO parentheses
  105. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  106. self.assertEqual(-01000000000000000000000, -9223372036854775808L)
  107. self.assertEqual(-01777777777777777777777, -18446744073709551615L)
  108. def test_oct_unsigned_new(self):
  109. # Positive constants
  110. self.assertEqual(0o20000000000, 2147483648L)
  111. self.assertEqual(0o37777777777, 4294967295L)
  112. # Ditto with a minus sign and parentheses
  113. self.assertEqual(-(0o20000000000), -2147483648L)
  114. self.assertEqual(-(0o37777777777), -4294967295L)
  115. # Ditto with a minus sign and NO parentheses
  116. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  117. self.assertEqual(-0o20000000000, -2147483648L)
  118. self.assertEqual(-0o37777777777, -4294967295L)
  119. # Positive constants
  120. self.assertEqual(0o1000000000000000000000, 9223372036854775808L)
  121. self.assertEqual(0o1777777777777777777777, 18446744073709551615L)
  122. # Ditto with a minus sign and parentheses
  123. self.assertEqual(-(0o1000000000000000000000), -9223372036854775808L)
  124. self.assertEqual(-(0o1777777777777777777777), -18446744073709551615L)
  125. # Ditto with a minus sign and NO parentheses
  126. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  127. self.assertEqual(-0o1000000000000000000000, -9223372036854775808L)
  128. self.assertEqual(-0o1777777777777777777777, -18446744073709551615L)
  129. def test_bin_baseline(self):
  130. # A few upper/lowercase tests
  131. self.assertEqual(0b0, 0B0)
  132. self.assertEqual(0b1, 0B1)
  133. self.assertEqual(0b10101010101, 0B10101010101)
  134. # Baseline tests
  135. self.assertEqual(0b0, 0)
  136. self.assertEqual(0b10000, 16)
  137. self.assertEqual(0b1111111111111111111111111111111, 2147483647)
  138. self.assertEqual(0b111111111111111111111111111111111111111111111111111111111111111, 9223372036854775807)
  139. # Ditto with a minus sign and parentheses
  140. self.assertEqual(-(0b0), 0)
  141. self.assertEqual(-(0b10000), -16)
  142. self.assertEqual(-(0b1111111111111111111111111111111), -2147483647)
  143. self.assertEqual(-(0b111111111111111111111111111111111111111111111111111111111111111), -9223372036854775807)
  144. # Ditto with a minus sign and NO parentheses
  145. self.assertEqual(-0b0, 0)
  146. self.assertEqual(-0b10000, -16)
  147. self.assertEqual(-0b1111111111111111111111111111111, -2147483647)
  148. self.assertEqual(-0b111111111111111111111111111111111111111111111111111111111111111, -9223372036854775807)
  149. def test_bin_unsigned(self):
  150. # Positive constants
  151. self.assertEqual(0b10000000000000000000000000000000, 2147483648L)
  152. self.assertEqual(0b11111111111111111111111111111111, 4294967295L)
  153. # Ditto with a minus sign and parentheses
  154. self.assertEqual(-(0b10000000000000000000000000000000), -2147483648L)
  155. self.assertEqual(-(0b11111111111111111111111111111111), -4294967295L)
  156. # Ditto with a minus sign and NO parentheses
  157. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  158. self.assertEqual(-0b10000000000000000000000000000000, -2147483648L)
  159. self.assertEqual(-0b11111111111111111111111111111111, -4294967295L)
  160. # Positive constants
  161. self.assertEqual(0b1000000000000000000000000000000000000000000000000000000000000000, 9223372036854775808L)
  162. self.assertEqual(0b1111111111111111111111111111111111111111111111111111111111111111, 18446744073709551615L)
  163. # Ditto with a minus sign and parentheses
  164. self.assertEqual(-(0b1000000000000000000000000000000000000000000000000000000000000000), -9223372036854775808L)
  165. self.assertEqual(-(0b1111111111111111111111111111111111111111111111111111111111111111), -18446744073709551615L)
  166. # Ditto with a minus sign and NO parentheses
  167. # This failed in Python 2.2 through 2.2.2 and in 2.3a1
  168. self.assertEqual(-0b1000000000000000000000000000000000000000000000000000000000000000, -9223372036854775808L)
  169. self.assertEqual(-0b1111111111111111111111111111111111111111111111111111111111111111, -18446744073709551615L)
  170. def test_main():
  171. test_support.run_unittest(TestHexOctBin)
  172. if __name__ == "__main__":
  173. test_main()