-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathtest_logical_compare.py
129 lines (87 loc) · 2.91 KB
/
test_logical_compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from lpython import i32, f64
def test_logical_compare_literal():
# Integers
print(1 or 3)
assert (1 or 3) == 1
print(1 and 3)
assert (1 and 3) == 3
print(2 or 3 or 5 or 6)
assert (2 or 3 or 5 or 6) == 2
print(1 and 3 or 2 and 4)
assert (1 and 3 or 2 and 4) == 3
print(1 or 3 and 0 or 4)
assert (1 or 3 and 0 or 4) == 1
print(1 and 3 or 2 and 0)
assert (1 and 3 or 2 and 0) == 3
print(1 and 0 or 3 and 4)
assert (1 and 0 or 3 and 4) == 4
# Floating-point numbers
print(1.33 or 6.67)
assert (1.33 or 6.67) == 1.33
print(1.33 and 6.67)
assert (1.33 and 6.67) == 6.67
print(1.33 or 6.67 and 3.33 or 0.0)
assert (1.33 or 6.67 and 3.33 or 0.0) == 1.33
print(1.33 and 6.67 or 3.33 and 0.0)
assert (1.33 and 6.67 or 3.33 and 0.0) == 6.67
print(1.33 and 0.0 and 3.33 and 6.67)
assert (1.33 and 0.0 and 3.33 and 6.67) == 0.0
# Strings
print("a" or "b")
assert ("a" or "b") == "a"
print("abc" or "b")
assert ("abc" or "b") == "abc"
print("a" and "b")
assert ("a" and "b") == "b"
print("a" or "b" and "c" or "d")
assert ("a" or "b" and "c" or "d") == "a"
print("" or " ")
assert ("" or " ") == " "
print("" and " " or "a" and "b" and "c")
assert ("" and " " or "a" and "b" and "c") == "c"
print("" and " " and "a" and "b" and "c")
assert ("" and " " and "a" and "b" and "c") == ""
def test_logical_compare_variable():
# Integers
i_a: i32 = 1
i_b: i32 = 3
print(i_a and i_b)
assert (i_a and i_b) == 3
print(i_a or i_b or 2 or 4)
assert (i_a or i_b or 2 or 4) == 1
print(i_a and i_b or 2 and 4)
assert (i_a and i_b or 2 and 4) == 3
print(i_a or i_b and 0 or 4)
assert (i_a or i_b and 0 or 4) == i_a
print(i_a and i_b or 2 and 0)
assert (i_a and i_b or 2 and 0) == i_b
print(i_a and 0 or i_b and 4)
assert (i_a and 0 or i_b and 4) == 4
print(i_a + i_b or 0 - 4)
assert (i_a + i_b or 0 - 4) == 4
# Floating-point numbers
f_a: f64 = 1.67
f_b: f64 = 3.33
print(f_a // f_b and f_a - f_b)
assert (f_a // f_b and f_a - f_b) == 0.0
print(f_a**3.0 or 3.0**f_a)
assert (f_a**3.0 or 3.0**f_a) == 4.657462999999999
print(f_a - 3.0 and f_a + 3.0 or f_b - 3.0 and f_b + 3.0)
assert (f_a - 3.0 and f_a + 3.0 or f_b - 3.0 and f_b + 3.0) == 4.67
# Strings
s_a: str = "a"
s_b: str = "b"
print(s_a or s_b)
assert (s_a or s_b) == s_a
print(s_a and s_b)
assert (s_a and s_b) == s_b
print(s_a + s_b or s_b + s_a)
assert (s_a + s_b or s_b + s_a) == "ab"
print(s_a[0] or s_b[-1])
assert (s_a[0] or s_b[-1]) == "a"
print(s_a[0] and s_b[-1])
assert (s_a[0] and s_b[-1]) == "b"
print(s_a + s_b or s_b + s_a + s_a[0] and s_b[-1])
assert (s_a + s_b or s_b + s_a + s_a[0] and s_b[-1]) == "ab"
test_logical_compare_literal()
test_logical_compare_variable()