test_unsigned_long.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python
  2. from __future__ import division
  3. import pytest
  4. import six
  5. from cyson import UInt
  6. if six.PY3:
  7. long = int
  8. def equals_with_type(data, etalon):
  9. return type(data) is type(etalon) and data == etalon
  10. def equals_as_uint(data, etalon):
  11. return type(data) is UInt and data == etalon
  12. N = long(12)
  13. UN = UInt(N)
  14. def test_uint64_initialization():
  15. assert UInt(2**63 - 1) == 2**63 - 1
  16. assert UInt() == UInt(0) == 0
  17. assert UInt(1) == 1
  18. assert UInt(2) == 2
  19. assert UInt(long(78)) == 78
  20. assert UInt(23.57) == 23
  21. assert UInt('111') == 111
  22. with pytest.raises(OverflowError):
  23. UInt(-10)
  24. def test_add():
  25. assert equals_as_uint(UN + 1, N + 1)
  26. assert equals_as_uint(UN + long(1), N + 1)
  27. assert equals_as_uint(UN + UInt(1), N + 1)
  28. assert equals_as_uint(1 + UN, 1 + N)
  29. assert equals_as_uint(long(1) + UN, long(1) + N)
  30. assert equals_as_uint(UInt(1) + UN, 1 + N)
  31. assert equals_with_type(UN + 1.1, N + 1.1)
  32. assert equals_with_type(1.1 + UN, 1.1 + N)
  33. assert equals_with_type(UN + int(-N - 1), N + int(-N - 1))
  34. assert equals_with_type(UN + long(-N - 1), N + long(-N - 1))
  35. assert equals_with_type(int(-N - 1) + UN, int(-N - 1) + N)
  36. assert equals_with_type(long(-N - 1) + UN, long(-N - 1) + N)
  37. def test_sub():
  38. assert equals_as_uint(UN - 1, N - 1)
  39. assert equals_as_uint(UN - long(1), N - long(1))
  40. assert equals_as_uint(UN - UInt(1), N - 1)
  41. assert equals_as_uint(13 - UN, 13 - UN)
  42. assert equals_as_uint(UInt(13) - UN, long(13) - N)
  43. assert equals_as_uint(long(13) - UN, long(13) - UN)
  44. assert equals_with_type(UN - 0.1, N - 0.1)
  45. assert equals_with_type(13.1 - UN, 13.1 - N)
  46. assert equals_with_type(1 - UN, long(1) - N)
  47. assert equals_with_type(long(1) - UN, long(1) - N)
  48. assert equals_with_type(UInt(1) - UN, long(1) - N)
  49. assert equals_with_type(UN - int(UN + 1), N - int(UN + 1))
  50. assert equals_with_type(UN - long(UN + 1), N - long(UN + 1))
  51. assert equals_with_type(UN - UInt(UN + 1), N - long(UN + 1))
  52. def test_mul():
  53. assert equals_as_uint(UN * 2, N * 2)
  54. assert equals_as_uint(UN * long(2), N * long(2))
  55. assert equals_as_uint(UN * UInt(2), N * long(2))
  56. assert equals_as_uint(2 * UN, 2 * N)
  57. assert equals_as_uint(long(2) * UN, long(2) * UN)
  58. assert equals_as_uint(UInt(2) * UN, long(2) * UN)
  59. assert equals_with_type(-3 * UN, -3 * N)
  60. assert equals_with_type(long(-3) * UN, long(-3) * N)
  61. assert equals_with_type(UN * -3, N * -3)
  62. assert equals_with_type(UN * long(-3), N * long(-3))
  63. assert equals_with_type(UN * 1.1, N * 1.1)
  64. assert equals_with_type(1.1 * UN, 1.1 * N)
  65. def test_truediv():
  66. assert equals_with_type(UN / 1, N / long(1))
  67. assert equals_with_type(UN / UInt(1), N / long(1))
  68. assert equals_with_type(1 / UN, long(1) / N)
  69. assert equals_with_type(UInt(1) / UN, long(1) / N)
  70. assert equals_with_type(UN / N, N / long(N))
  71. assert equals_with_type(UN / UInt(N), N / long(N))
  72. assert equals_with_type(UN / -1, N / long(-1))
  73. assert equals_with_type(-1 / UN, long(-1) / N)
  74. assert equals_with_type(UN / 1.1, N / 1.1)
  75. assert equals_with_type(1.1 / UN, 1.1 / N)
  76. def test_floordiv():
  77. # floor division (__floordiv__)
  78. assert equals_as_uint(UN // 1, N // 1)
  79. assert equals_as_uint(UN // long(1), N // long(1))
  80. assert equals_as_uint(UN // UInt(1), N // long(1))
  81. assert equals_as_uint(1 // UN, 1 // N)
  82. assert equals_as_uint(long(1) // UN, long(1) // N)
  83. assert equals_as_uint(UInt(1) // UN, long(1) // N)
  84. assert equals_as_uint(UN // N, N // N)
  85. assert equals_as_uint(UN // UN, N // N)
  86. assert equals_with_type(UN // -1, N // long(-1))
  87. assert equals_with_type(UN // long(-1), N // long(-1))
  88. assert equals_with_type(-1 // UN, -long(1) // N)
  89. assert equals_with_type(long(-1) // UN, long(-1) // N)
  90. assert equals_with_type(UN // 1.1, N // 1.1)
  91. assert equals_with_type(1.1 // UN, 1.1 // N)
  92. def test_mod():
  93. assert equals_as_uint(UN % 7, N % 7)
  94. assert equals_as_uint(UN % long(7), N % long(7))
  95. assert equals_as_uint(UN % UInt(7), N % long(7))
  96. assert equals_as_uint(23 % UN, 23 % N)
  97. assert equals_as_uint(long(23) % UN, long(23) % N)
  98. assert equals_as_uint(UInt(23) % UN, long(23) % N)
  99. assert equals_as_uint(-23 % UN, -23 % N)
  100. assert equals_as_uint(long(-23) % UN, long(-23) % N)
  101. assert equals_with_type(UN % -11, N % long(-11))
  102. assert equals_with_type(UN % long(-11), N % long(-11))
  103. def test_pow():
  104. assert equals_as_uint(UN ** 2, N ** 2)
  105. assert equals_as_uint(UN ** long(2), N ** long(2))
  106. assert equals_as_uint(UN ** UInt(2), N ** long(2))
  107. assert equals_as_uint(2 ** UN, 2 ** N)
  108. assert equals_as_uint(long(2) ** UN, long(2) ** N)
  109. assert equals_as_uint(UInt(2) ** UN, long(2) ** N)
  110. assert equals_with_type(UN ** -1, N ** long(-1))
  111. assert equals_with_type(UN ** long(-1), N ** -long(1))
  112. assert equals_with_type(UN ** 1.1, N ** 1.1)
  113. assert equals_with_type(UN ** -1.1, N ** -1.1)
  114. assert equals_with_type(1.1 ** UN, 1.1 ** N)
  115. assert equals_with_type(UN ** 0.5, N ** 0.5)
  116. assert equals_with_type(0.5 ** UN, 0.5 ** N)
  117. def test_neg():
  118. assert equals_with_type(-UN, -N)
  119. assert equals_with_type(-UInt(0), long(0))
  120. def test_pos():
  121. assert equals_as_uint(+UN, N)
  122. assert equals_as_uint(+UInt(0), 0)
  123. def test_abs():
  124. assert equals_as_uint(abs(UN), N)
  125. assert abs(UN) is UN
  126. def test_invert():
  127. assert equals_with_type(~UN, ~N)
  128. assert equals_with_type(~UInt(0), ~long(0))
  129. def test_lshift():
  130. assert equals_as_uint(1 << UN, 1 << N)
  131. assert equals_as_uint(long(1) << UN, long(1) << N)
  132. assert equals_as_uint(UInt(1) << UN, long(1) << N)
  133. assert equals_as_uint(UN << 2, N << 2)
  134. assert equals_as_uint(UN << long(2), N << 2)
  135. assert equals_as_uint(UN << UInt(2), N << 2)
  136. assert equals_with_type(-1 << UN, -1 << N)
  137. assert equals_with_type(long(-1) << UN, -long(1) << N)
  138. with pytest.raises(TypeError):
  139. UN << 1.1
  140. with pytest.raises(TypeError):
  141. 1.1 << UN
  142. with pytest.raises(ValueError):
  143. UN << -1
  144. def test_rshift():
  145. assert equals_as_uint(10000 >> UN, 10000 >> N)
  146. assert equals_as_uint(long(10000) >> UN, long(10000) >> N)
  147. assert equals_as_uint(UInt(10000) >> UN, long(10000) >> N)
  148. assert equals_as_uint(UN >> 2, N >> 2)
  149. assert equals_as_uint(UN >> long(2), N >> long(2))
  150. assert equals_as_uint(UN >> UInt(2), N >> long(2))
  151. assert equals_with_type(-10000 >> UN, -10000 >> N)
  152. assert equals_with_type(long(-10000) >> UN, long(-10000) >> N)
  153. with pytest.raises(TypeError):
  154. UN >> 1.1
  155. with pytest.raises(TypeError):
  156. 1.1 >> UN
  157. with pytest.raises(ValueError):
  158. UN >> -1
  159. def test_and():
  160. assert equals_as_uint(UN & 15, N & 15)
  161. assert equals_as_uint(UN & long(15), N & long(15))
  162. with pytest.raises(TypeError):
  163. UN & 1.1
  164. def test_or():
  165. assert equals_as_uint(UN | 15, N | 15)
  166. assert equals_as_uint(UN | long(15), N | long(15))
  167. with pytest.raises(TypeError):
  168. UN | 1.1
  169. def test_xor():
  170. assert equals_as_uint(UN ^ 9, N ^ 9)
  171. assert equals_as_uint(UN ^ long(9), N ^ long(9))
  172. with pytest.raises(TypeError):
  173. UN ^ 1.1