79 lines
1.6 KiB
Plaintext
79 lines
1.6 KiB
Plaintext
# Test cases for booleans (compile and run)
|
|
|
|
[case testTrueAndFalse]
|
|
def t() -> bool:
|
|
return True
|
|
|
|
def f() -> bool:
|
|
return False
|
|
[file driver.py]
|
|
from native import t, f
|
|
print(t())
|
|
print(f())
|
|
[out]
|
|
True
|
|
False
|
|
|
|
[case testBoolOps]
|
|
def f(x: bool) -> bool:
|
|
if x:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
def test_if() -> None:
|
|
assert f(True) is False
|
|
assert f(False) is True
|
|
|
|
def test_bitwise_and() -> None:
|
|
# Use eval() to avoid constand folding
|
|
t = eval('True') # type: bool
|
|
f = eval('False') # type: bool
|
|
assert t & t == True
|
|
assert t & f == False
|
|
assert f & t == False
|
|
assert f & f == False
|
|
t &= t
|
|
assert t == True
|
|
t &= f
|
|
assert t == False
|
|
|
|
def test_bitwise_or() -> None:
|
|
# Use eval() to avoid constand folding
|
|
t = eval('True') # type: bool
|
|
f = eval('False') # type: bool
|
|
assert t | t == True
|
|
assert t | f == True
|
|
assert f | t == True
|
|
assert f | f == False
|
|
t |= f
|
|
assert t == True
|
|
f |= t
|
|
assert f == True
|
|
|
|
def test_bitwise_xor() -> None:
|
|
# Use eval() to avoid constand folding
|
|
t = eval('True') # type: bool
|
|
f = eval('False') # type: bool
|
|
assert t ^ t == False
|
|
assert t ^ f == True
|
|
assert f ^ t == True
|
|
assert f ^ f == False
|
|
t ^= f
|
|
assert t == True
|
|
t ^= t
|
|
assert t == False
|
|
f ^= f
|
|
assert f == False
|
|
|
|
[case testIsinstanceBool]
|
|
def test_isinstance_bool() -> None:
|
|
a = True
|
|
b = 1.0
|
|
c = 1
|
|
d = False
|
|
assert isinstance(a, bool) == True
|
|
assert isinstance(b, bool) == False
|
|
assert isinstance(c, bool) == False
|
|
assert isinstance(d, bool) == True
|