| 1 |
# Test cases for booleans (compile and run) |
| 2 |
|
| 3 |
[case testTrueAndFalse] |
| 4 |
def t() -> bool: |
| 5 |
return True |
| 6 |
|
| 7 |
def f() -> bool: |
| 8 |
return False |
| 9 |
[file driver.py] |
| 10 |
from native import t, f |
| 11 |
print(t()) |
| 12 |
print(f()) |
| 13 |
[out] |
| 14 |
True |
| 15 |
False |
| 16 |
|
| 17 |
[case testBoolOps] |
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
from typing import Optional, Any |
| 21 |
MYPY = False |
| 22 |
if MYPY: |
| 23 |
from mypy_extensions import i64 |
| 24 |
|
| 25 |
def f(x: bool) -> bool: |
| 26 |
if x: |
| 27 |
return False |
| 28 |
else: |
| 29 |
return True |
| 30 |
|
| 31 |
def test_if() -> None: |
| 32 |
assert f(True) is False |
| 33 |
assert f(False) is True |
| 34 |
|
| 35 |
def test_bitwise_and() -> None: |
| 36 |
# Use eval() to avoid constant folding |
| 37 |
t: bool = eval('True') |
| 38 |
f: bool = eval('False') |
| 39 |
assert t & t == True |
| 40 |
assert t & f == False |
| 41 |
assert f & t == False |
| 42 |
assert f & f == False |
| 43 |
t &= t |
| 44 |
assert t == True |
| 45 |
t &= f |
| 46 |
assert t == False |
| 47 |
|
| 48 |
def test_bitwise_or() -> None: |
| 49 |
# Use eval() to avoid constant folding |
| 50 |
t: bool = eval('True') |
| 51 |
f: bool = eval('False') |
| 52 |
assert t | t == True |
| 53 |
assert t | f == True |
| 54 |
assert f | t == True |
| 55 |
assert f | f == False |
| 56 |
t |= f |
| 57 |
assert t == True |
| 58 |
f |= t |
| 59 |
assert f == True |
| 60 |
|
| 61 |
def test_bitwise_xor() -> None: |
| 62 |
# Use eval() to avoid constant folding |
| 63 |
t: bool = eval('True') |
| 64 |
f: bool = eval('False') |
| 65 |
assert t ^ t == False |
| 66 |
assert t ^ f == True |
| 67 |
assert f ^ t == True |
| 68 |
assert f ^ f == False |
| 69 |
t ^= f |
| 70 |
assert t == True |
| 71 |
t ^= t |
| 72 |
assert t == False |
| 73 |
f ^= f |
| 74 |
assert f == False |
| 75 |
|
| 76 |
def test_isinstance_bool() -> None: |
| 77 |
a = True |
| 78 |
b = 1.0 |
| 79 |
c = 1 |
| 80 |
d = False |
| 81 |
assert isinstance(a, bool) == True |
| 82 |
assert isinstance(b, bool) == False |
| 83 |
assert isinstance(c, bool) == False |
| 84 |
assert isinstance(d, bool) == True |
| 85 |
|
| 86 |
class C: pass |
| 87 |
class D: |
| 88 |
def __init__(self, b: bool) -> None: |
| 89 |
self.b = b |
| 90 |
|
| 91 |
def __bool__(self) -> bool: |
| 92 |
return self.b |
| 93 |
|
| 94 |
class E: pass |
| 95 |
class F(E): |
| 96 |
def __init__(self, b: bool) -> None: |
| 97 |
self.b = b |
| 98 |
|
| 99 |
def __bool__(self) -> bool: |
| 100 |
return self.b |
| 101 |
|
| 102 |
def optional_to_bool1(o: Optional[C]) -> bool: |
| 103 |
return bool(o) |
| 104 |
|
| 105 |
def optional_to_bool2(o: Optional[D]) -> bool: |
| 106 |
return bool(o) |
| 107 |
|
| 108 |
def optional_to_bool3(o: Optional[E]) -> bool: |
| 109 |
return bool(o) |
| 110 |
|
| 111 |
def test_optional_to_bool() -> None: |
| 112 |
assert not optional_to_bool1(None) |
| 113 |
assert optional_to_bool1(C()) |
| 114 |
assert not optional_to_bool2(None) |
| 115 |
assert not optional_to_bool2(D(False)) |
| 116 |
assert optional_to_bool2(D(True)) |
| 117 |
assert not optional_to_bool3(None) |
| 118 |
assert optional_to_bool3(E()) |
| 119 |
assert not optional_to_bool3(F(False)) |
| 120 |
assert optional_to_bool3(F(True)) |
| 121 |
|
| 122 |
def not_c(c: C) -> bool: |
| 123 |
return not c |
| 124 |
|
| 125 |
def not_c_opt(c: C | None) -> bool: |
| 126 |
return not c |
| 127 |
|
| 128 |
def not_d(d: D) -> bool: |
| 129 |
return not d |
| 130 |
|
| 131 |
def not_d_opt(d: D | None) -> bool: |
| 132 |
return not d |
| 133 |
|
| 134 |
def test_not_instance() -> None: |
| 135 |
assert not not_c(C()) |
| 136 |
assert not_c_opt(None) |
| 137 |
assert not not_c_opt(C()) |
| 138 |
|
| 139 |
assert not_d(D(False)) |
| 140 |
assert not not_d(D(True)) |
| 141 |
assert not_d_opt(D(False)) |
| 142 |
assert not_d_opt(None) |
| 143 |
assert not not_d_opt(D(True)) |
| 144 |
|
| 145 |
def test_any_to_bool() -> None: |
| 146 |
a: Any = int() |
| 147 |
b: Any = a + 1 |
| 148 |
assert not bool(a) |
| 149 |
assert bool(b) |
| 150 |
|
| 151 |
def eq(x: bool, y: bool) -> bool: |
| 152 |
return x == y |
| 153 |
|
| 154 |
def ne(x: bool, y: bool) -> bool: |
| 155 |
return x != y |
| 156 |
|
| 157 |
def lt(x: bool, y: bool) -> bool: |
| 158 |
return x < y |
| 159 |
|
| 160 |
def le(x: bool, y: bool) -> bool: |
| 161 |
return x <= y |
| 162 |
|
| 163 |
def gt(x: bool, y: bool) -> bool: |
| 164 |
return x > y |
| 165 |
|
| 166 |
def ge(x: bool, y: bool) -> bool: |
| 167 |
return x >= y |
| 168 |
|
| 169 |
def test_comparisons() -> None: |
| 170 |
for x in True, False: |
| 171 |
for y in True, False: |
| 172 |
x2: Any = x |
| 173 |
y2: Any = y |
| 174 |
assert eq(x, y) == (x2 == y2) |
| 175 |
assert ne(x, y) == (x2 != y2) |
| 176 |
assert lt(x, y) == (x2 < y2) |
| 177 |
assert le(x, y) == (x2 <= y2) |
| 178 |
assert gt(x, y) == (x2 > y2) |
| 179 |
assert ge(x, y) == (x2 >= y2) |
| 180 |
|
| 181 |
def eq_mixed(x: bool, y: int) -> bool: |
| 182 |
return x == y |
| 183 |
|
| 184 |
def neq_mixed(x: int, y: bool) -> bool: |
| 185 |
return x != y |
| 186 |
|
| 187 |
def lt_mixed(x: bool, y: int) -> bool: |
| 188 |
return x < y |
| 189 |
|
| 190 |
def gt_mixed(x: int, y: bool) -> bool: |
| 191 |
return x > y |
| 192 |
|
| 193 |
def test_mixed_comparisons() -> None: |
| 194 |
for x in True, False: |
| 195 |
for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70: |
| 196 |
assert eq_mixed(x, n) == (int(x) == n) |
| 197 |
assert neq_mixed(n, x) == (n != int(x)) |
| 198 |
assert lt_mixed(x, n) == (int(x) < n) |
| 199 |
assert gt_mixed(n, x) == (n > int(x)) |
| 200 |
|
| 201 |
def add(x: bool, y: bool) -> int: |
| 202 |
return x + y |
| 203 |
|
| 204 |
def add_mixed(b: bool, n: int) -> int: |
| 205 |
return b + n |
| 206 |
|
| 207 |
def sub_mixed(n: int, b: bool) -> int: |
| 208 |
return n - b |
| 209 |
|
| 210 |
def test_arithmetic() -> None: |
| 211 |
for x in True, False: |
| 212 |
for y in True, False: |
| 213 |
assert add(x, y) == int(x) + int(y) |
| 214 |
for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70: |
| 215 |
assert add_mixed(x, n) == int(x) + n |
| 216 |
assert sub_mixed(n, x) == n - int(x) |
| 217 |
|
| 218 |
def add_mixed_i64(b: bool, n: i64) -> i64: |
| 219 |
return b + n |
| 220 |
|
| 221 |
def sub_mixed_i64(n: i64, b: bool) -> i64: |
| 222 |
return n - b |
| 223 |
|
| 224 |
def test_arithmetic_i64() -> None: |
| 225 |
for x in True, False: |
| 226 |
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62: |
| 227 |
assert add_mixed_i64(x, n) == int(x) + n |
| 228 |
assert sub_mixed_i64(n, x) == n - int(x) |
| 229 |
|
| 230 |
def eq_mixed_i64(x: bool, y: i64) -> bool: |
| 231 |
return x == y |
| 232 |
|
| 233 |
def neq_mixed_i64(x: i64, y: bool) -> bool: |
| 234 |
return x != y |
| 235 |
|
| 236 |
def lt_mixed_i64(x: bool, y: i64) -> bool: |
| 237 |
return x < y |
| 238 |
|
| 239 |
def gt_mixed_i64(x: i64, y: bool) -> bool: |
| 240 |
return x > y |
| 241 |
|
| 242 |
def test_mixed_comparisons_i64() -> None: |
| 243 |
for x in True, False: |
| 244 |
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62: |
| 245 |
assert eq_mixed_i64(x, n) == (int(x) == n) |
| 246 |
assert neq_mixed_i64(n, x) == (n != int(x)) |
| 247 |
assert lt_mixed_i64(x, n) == (int(x) < n) |
| 248 |
assert gt_mixed_i64(n, x) == (n > int(x)) |
| 249 |
|
| 250 |
def not_object(x: object) -> bool: |
| 251 |
return not x |
| 252 |
|
| 253 |
def not_str(x: str) -> bool: |
| 254 |
return not x |
| 255 |
|
| 256 |
def not_int(x: int) -> bool: |
| 257 |
return not x |
| 258 |
|
| 259 |
def not_list(x: list[int]) -> bool: |
| 260 |
return not x |
| 261 |
|
| 262 |
def not_tuple(x: tuple[int, ...]) -> bool: |
| 263 |
return not x |
| 264 |
|
| 265 |
def not_dict(x: dict[str, int]) -> bool: |
| 266 |
return not x |
| 267 |
|
| 268 |
def test_not_object() -> None: |
| 269 |
assert not_object(None) |
| 270 |
assert not_object([]) |
| 271 |
assert not_object(0) |
| 272 |
assert not not_object(1) |
| 273 |
assert not not_object([1]) |
| 274 |
|
| 275 |
def test_not_str() -> None: |
| 276 |
assert not_str(str()) |
| 277 |
assert not not_str('x' + str()) |
| 278 |
|
| 279 |
def test_not_int() -> None: |
| 280 |
assert not_int(int('0')) |
| 281 |
assert not not_int(int('1')) |
| 282 |
assert not not_int(int('-1')) |
| 283 |
|
| 284 |
def test_not_list() -> None: |
| 285 |
assert not_list([]) |
| 286 |
assert not not_list([1]) |
| 287 |
|
| 288 |
def test_not_tuple() -> None: |
| 289 |
assert not_tuple(()) |
| 290 |
assert not not_tuple((1,)) |
| 291 |
|
| 292 |
def test_not_dict() -> None: |
| 293 |
assert not_dict({}) |
| 294 |
assert not not_dict({'x': 1}) |
| 295 |
|
| 296 |
def not_str_opt(x: str | None) -> bool: |
| 297 |
return not x |
| 298 |
|
| 299 |
def not_int_opt(x: int | None) -> bool: |
| 300 |
return not x |
| 301 |
|
| 302 |
def not_list_opt(x: list[int] | None) -> bool: |
| 303 |
return not x |
| 304 |
|
| 305 |
def not_tuple_opt(x: tuple[int, ...] | None) -> bool: |
| 306 |
return not x |
| 307 |
|
| 308 |
def not_dict_opt(x: dict[str, int] | None) -> bool: |
| 309 |
return not x |
| 310 |
|
| 311 |
def test_not_str_opt() -> None: |
| 312 |
assert not_str_opt(str()) |
| 313 |
assert not_str_opt(None) |
| 314 |
assert not not_str_opt('x' + str()) |
| 315 |
|
| 316 |
def test_not_int_opt() -> None: |
| 317 |
assert not_int_opt(int('0')) |
| 318 |
assert not_int_opt(None) |
| 319 |
assert not not_int_opt(int('1')) |
| 320 |
assert not not_int_opt(int('-1')) |
| 321 |
|
| 322 |
def test_not_list_opt() -> None: |
| 323 |
assert not_list_opt([]) |
| 324 |
assert not_list_opt(None) |
| 325 |
assert not not_list_opt([1]) |
| 326 |
|
| 327 |
def test_not_tuple_opt() -> None: |
| 328 |
assert not_tuple_opt(()) |
| 329 |
assert not_tuple_opt(None) |
| 330 |
assert not not_tuple_opt((1,)) |
| 331 |
|
| 332 |
def test_not_dict_opt() -> None: |
| 333 |
assert not_dict_opt({}) |
| 334 |
assert not_dict_opt(None) |
| 335 |
assert not not_dict_opt({'x': 1}) |
| 336 |
|
| 337 |
[case testBoolMixInt] |
| 338 |
def test_mix() -> None: |
| 339 |
y = False |
| 340 |
print((y or 0) and True) |
| 341 |
[out] |
| 342 |
0 |
| 343 |
|
| 344 |
[case testIsInstance] |
| 345 |
from typing import Any |
| 346 |
def test_built_in() -> None: |
| 347 |
true: Any = True |
| 348 |
false: Any = False |
| 349 |
assert isinstance(true, bool) |
| 350 |
assert isinstance(false, bool) |
| 351 |
|
| 352 |
assert not isinstance(set(), bool) |
| 353 |
assert not isinstance((), bool) |
| 354 |
assert not isinstance((True, False), bool) |
| 355 |
assert not isinstance({False, True}, bool) |
| 356 |
assert not isinstance(int() + 1, bool) |
| 357 |
assert not isinstance(str() + 'False', bool) |
| 358 |
|
| 359 |
def test_user_defined() -> None: |
| 360 |
from userdefinedbool import bool |
| 361 |
|
| 362 |
b: Any = True |
| 363 |
assert isinstance(bool(), bool) |
| 364 |
assert not isinstance(b, bool) |
| 365 |
|
| 366 |
[file userdefinedbool.py] |
| 367 |
class bool: |
| 368 |
pass |