run-bools.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # Test cases for booleans (compile and run)
  2. [case testTrueAndFalse]
  3. def t() -> bool:
  4. return True
  5. def f() -> bool:
  6. return False
  7. [file driver.py]
  8. from native import t, f
  9. print(t())
  10. print(f())
  11. [out]
  12. True
  13. False
  14. [case testBoolOps]
  15. from typing import Optional, Any
  16. MYPY = False
  17. if MYPY:
  18. from mypy_extensions import i64
  19. def f(x: bool) -> bool:
  20. if x:
  21. return False
  22. else:
  23. return True
  24. def test_if() -> None:
  25. assert f(True) is False
  26. assert f(False) is True
  27. def test_bitwise_and() -> None:
  28. # Use eval() to avoid constand folding
  29. t: bool = eval('True')
  30. f: bool = eval('False')
  31. assert t & t == True
  32. assert t & f == False
  33. assert f & t == False
  34. assert f & f == False
  35. t &= t
  36. assert t == True
  37. t &= f
  38. assert t == False
  39. def test_bitwise_or() -> None:
  40. # Use eval() to avoid constand folding
  41. t: bool = eval('True')
  42. f: bool = eval('False')
  43. assert t | t == True
  44. assert t | f == True
  45. assert f | t == True
  46. assert f | f == False
  47. t |= f
  48. assert t == True
  49. f |= t
  50. assert f == True
  51. def test_bitwise_xor() -> None:
  52. # Use eval() to avoid constand folding
  53. t: bool = eval('True')
  54. f: bool = eval('False')
  55. assert t ^ t == False
  56. assert t ^ f == True
  57. assert f ^ t == True
  58. assert f ^ f == False
  59. t ^= f
  60. assert t == True
  61. t ^= t
  62. assert t == False
  63. f ^= f
  64. assert f == False
  65. def test_isinstance_bool() -> None:
  66. a = True
  67. b = 1.0
  68. c = 1
  69. d = False
  70. assert isinstance(a, bool) == True
  71. assert isinstance(b, bool) == False
  72. assert isinstance(c, bool) == False
  73. assert isinstance(d, bool) == True
  74. class C: pass
  75. class D:
  76. def __init__(self, b: bool) -> None:
  77. self.b = b
  78. def __bool__(self) -> bool:
  79. return self.b
  80. class E: pass
  81. class F(E):
  82. def __init__(self, b: bool) -> None:
  83. self.b = b
  84. def __bool__(self) -> bool:
  85. return self.b
  86. def optional_to_bool1(o: Optional[C]) -> bool:
  87. return bool(o)
  88. def optional_to_bool2(o: Optional[D]) -> bool:
  89. return bool(o)
  90. def optional_to_bool3(o: Optional[E]) -> bool:
  91. return bool(o)
  92. def test_optional_to_bool() -> None:
  93. assert not optional_to_bool1(None)
  94. assert optional_to_bool1(C())
  95. assert not optional_to_bool2(None)
  96. assert not optional_to_bool2(D(False))
  97. assert optional_to_bool2(D(True))
  98. assert not optional_to_bool3(None)
  99. assert optional_to_bool3(E())
  100. assert not optional_to_bool3(F(False))
  101. assert optional_to_bool3(F(True))
  102. def test_any_to_bool() -> None:
  103. a: Any = int()
  104. b: Any = a + 1
  105. assert not bool(a)
  106. assert bool(b)
  107. def eq(x: bool, y: bool) -> bool:
  108. return x == y
  109. def ne(x: bool, y: bool) -> bool:
  110. return x != y
  111. def lt(x: bool, y: bool) -> bool:
  112. return x < y
  113. def le(x: bool, y: bool) -> bool:
  114. return x <= y
  115. def gt(x: bool, y: bool) -> bool:
  116. return x > y
  117. def ge(x: bool, y: bool) -> bool:
  118. return x >= y
  119. def test_comparisons() -> None:
  120. for x in True, False:
  121. for y in True, False:
  122. x2: Any = x
  123. y2: Any = y
  124. assert eq(x, y) == (x2 == y2)
  125. assert ne(x, y) == (x2 != y2)
  126. assert lt(x, y) == (x2 < y2)
  127. assert le(x, y) == (x2 <= y2)
  128. assert gt(x, y) == (x2 > y2)
  129. assert ge(x, y) == (x2 >= y2)
  130. def eq_mixed(x: bool, y: int) -> bool:
  131. return x == y
  132. def neq_mixed(x: int, y: bool) -> bool:
  133. return x != y
  134. def lt_mixed(x: bool, y: int) -> bool:
  135. return x < y
  136. def gt_mixed(x: int, y: bool) -> bool:
  137. return x > y
  138. def test_mixed_comparisons() -> None:
  139. for x in True, False:
  140. for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70:
  141. assert eq_mixed(x, n) == (int(x) == n)
  142. assert neq_mixed(n, x) == (n != int(x))
  143. assert lt_mixed(x, n) == (int(x) < n)
  144. assert gt_mixed(n, x) == (n > int(x))
  145. def add(x: bool, y: bool) -> int:
  146. return x + y
  147. def add_mixed(b: bool, n: int) -> int:
  148. return b + n
  149. def sub_mixed(n: int, b: bool) -> int:
  150. return n - b
  151. def test_arithmetic() -> None:
  152. for x in True, False:
  153. for y in True, False:
  154. assert add(x, y) == int(x) + int(y)
  155. for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70:
  156. assert add_mixed(x, n) == int(x) + n
  157. assert sub_mixed(n, x) == n - int(x)
  158. def add_mixed_i64(b: bool, n: i64) -> i64:
  159. return b + n
  160. def sub_mixed_i64(n: i64, b: bool) -> i64:
  161. return n - b
  162. def test_arithmetic_i64() -> None:
  163. for x in True, False:
  164. for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
  165. assert add_mixed_i64(x, n) == int(x) + n
  166. assert sub_mixed_i64(n, x) == n - int(x)
  167. def eq_mixed_i64(x: bool, y: i64) -> bool:
  168. return x == y
  169. def neq_mixed_i64(x: i64, y: bool) -> bool:
  170. return x != y
  171. def lt_mixed_i64(x: bool, y: i64) -> bool:
  172. return x < y
  173. def gt_mixed_i64(x: i64, y: bool) -> bool:
  174. return x > y
  175. def test_mixed_comparisons_i64() -> None:
  176. for x in True, False:
  177. for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
  178. assert eq_mixed_i64(x, n) == (int(x) == n)
  179. assert neq_mixed_i64(n, x) == (n != int(x))
  180. assert lt_mixed_i64(x, n) == (int(x) < n)
  181. assert gt_mixed_i64(n, x) == (n > int(x))
  182. [case testBoolMixInt]
  183. y = False
  184. print((y or 0) and True)
  185. [out]
  186. 0