-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-bools.test
More file actions
368 lines (291 loc) · 8.32 KB
/
run-bools.test
File metadata and controls
368 lines (291 loc) · 8.32 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# 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]
from __future__ import annotations
from typing import Optional, Any
MYPY = False
if MYPY:
from mypy_extensions import i64
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 constant folding
t: bool = eval('True')
f: bool = eval('False')
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 constant folding
t: bool = eval('True')
f: bool = eval('False')
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 constant folding
t: bool = eval('True')
f: bool = eval('False')
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
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
class C: pass
class D:
def __init__(self, b: bool) -> None:
self.b = b
def __bool__(self) -> bool:
return self.b
class E: pass
class F(E):
def __init__(self, b: bool) -> None:
self.b = b
def __bool__(self) -> bool:
return self.b
def optional_to_bool1(o: Optional[C]) -> bool:
return bool(o)
def optional_to_bool2(o: Optional[D]) -> bool:
return bool(o)
def optional_to_bool3(o: Optional[E]) -> bool:
return bool(o)
def test_optional_to_bool() -> None:
assert not optional_to_bool1(None)
assert optional_to_bool1(C())
assert not optional_to_bool2(None)
assert not optional_to_bool2(D(False))
assert optional_to_bool2(D(True))
assert not optional_to_bool3(None)
assert optional_to_bool3(E())
assert not optional_to_bool3(F(False))
assert optional_to_bool3(F(True))
def not_c(c: C) -> bool:
return not c
def not_c_opt(c: C | None) -> bool:
return not c
def not_d(d: D) -> bool:
return not d
def not_d_opt(d: D | None) -> bool:
return not d
def test_not_instance() -> None:
assert not not_c(C())
assert not_c_opt(None)
assert not not_c_opt(C())
assert not_d(D(False))
assert not not_d(D(True))
assert not_d_opt(D(False))
assert not_d_opt(None)
assert not not_d_opt(D(True))
def test_any_to_bool() -> None:
a: Any = int()
b: Any = a + 1
assert not bool(a)
assert bool(b)
def eq(x: bool, y: bool) -> bool:
return x == y
def ne(x: bool, y: bool) -> bool:
return x != y
def lt(x: bool, y: bool) -> bool:
return x < y
def le(x: bool, y: bool) -> bool:
return x <= y
def gt(x: bool, y: bool) -> bool:
return x > y
def ge(x: bool, y: bool) -> bool:
return x >= y
def test_comparisons() -> None:
for x in True, False:
for y in True, False:
x2: Any = x
y2: Any = y
assert eq(x, y) == (x2 == y2)
assert ne(x, y) == (x2 != y2)
assert lt(x, y) == (x2 < y2)
assert le(x, y) == (x2 <= y2)
assert gt(x, y) == (x2 > y2)
assert ge(x, y) == (x2 >= y2)
def eq_mixed(x: bool, y: int) -> bool:
return x == y
def neq_mixed(x: int, y: bool) -> bool:
return x != y
def lt_mixed(x: bool, y: int) -> bool:
return x < y
def gt_mixed(x: int, y: bool) -> bool:
return x > y
def test_mixed_comparisons() -> None:
for x in True, False:
for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70:
assert eq_mixed(x, n) == (int(x) == n)
assert neq_mixed(n, x) == (n != int(x))
assert lt_mixed(x, n) == (int(x) < n)
assert gt_mixed(n, x) == (n > int(x))
def add(x: bool, y: bool) -> int:
return x + y
def add_mixed(b: bool, n: int) -> int:
return b + n
def sub_mixed(n: int, b: bool) -> int:
return n - b
def test_arithmetic() -> None:
for x in True, False:
for y in True, False:
assert add(x, y) == int(x) + int(y)
for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70:
assert add_mixed(x, n) == int(x) + n
assert sub_mixed(n, x) == n - int(x)
def add_mixed_i64(b: bool, n: i64) -> i64:
return b + n
def sub_mixed_i64(n: i64, b: bool) -> i64:
return n - b
def test_arithmetic_i64() -> None:
for x in True, False:
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
assert add_mixed_i64(x, n) == int(x) + n
assert sub_mixed_i64(n, x) == n - int(x)
def eq_mixed_i64(x: bool, y: i64) -> bool:
return x == y
def neq_mixed_i64(x: i64, y: bool) -> bool:
return x != y
def lt_mixed_i64(x: bool, y: i64) -> bool:
return x < y
def gt_mixed_i64(x: i64, y: bool) -> bool:
return x > y
def test_mixed_comparisons_i64() -> None:
for x in True, False:
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
assert eq_mixed_i64(x, n) == (int(x) == n)
assert neq_mixed_i64(n, x) == (n != int(x))
assert lt_mixed_i64(x, n) == (int(x) < n)
assert gt_mixed_i64(n, x) == (n > int(x))
def not_object(x: object) -> bool:
return not x
def not_str(x: str) -> bool:
return not x
def not_int(x: int) -> bool:
return not x
def not_list(x: list[int]) -> bool:
return not x
def not_tuple(x: tuple[int, ...]) -> bool:
return not x
def not_dict(x: dict[str, int]) -> bool:
return not x
def test_not_object() -> None:
assert not_object(None)
assert not_object([])
assert not_object(0)
assert not not_object(1)
assert not not_object([1])
def test_not_str() -> None:
assert not_str(str())
assert not not_str('x' + str())
def test_not_int() -> None:
assert not_int(int('0'))
assert not not_int(int('1'))
assert not not_int(int('-1'))
def test_not_list() -> None:
assert not_list([])
assert not not_list([1])
def test_not_tuple() -> None:
assert not_tuple(())
assert not not_tuple((1,))
def test_not_dict() -> None:
assert not_dict({})
assert not not_dict({'x': 1})
def not_str_opt(x: str | None) -> bool:
return not x
def not_int_opt(x: int | None) -> bool:
return not x
def not_list_opt(x: list[int] | None) -> bool:
return not x
def not_tuple_opt(x: tuple[int, ...] | None) -> bool:
return not x
def not_dict_opt(x: dict[str, int] | None) -> bool:
return not x
def test_not_str_opt() -> None:
assert not_str_opt(str())
assert not_str_opt(None)
assert not not_str_opt('x' + str())
def test_not_int_opt() -> None:
assert not_int_opt(int('0'))
assert not_int_opt(None)
assert not not_int_opt(int('1'))
assert not not_int_opt(int('-1'))
def test_not_list_opt() -> None:
assert not_list_opt([])
assert not_list_opt(None)
assert not not_list_opt([1])
def test_not_tuple_opt() -> None:
assert not_tuple_opt(())
assert not_tuple_opt(None)
assert not not_tuple_opt((1,))
def test_not_dict_opt() -> None:
assert not_dict_opt({})
assert not_dict_opt(None)
assert not not_dict_opt({'x': 1})
[case testBoolMixInt]
def test_mix() -> None:
y = False
print((y or 0) and True)
[out]
0
[case testIsInstance]
from typing import Any
def test_built_in() -> None:
true: Any = True
false: Any = False
assert isinstance(true, bool)
assert isinstance(false, bool)
assert not isinstance(set(), bool)
assert not isinstance((), bool)
assert not isinstance((True, False), bool)
assert not isinstance({False, True}, bool)
assert not isinstance(int() + 1, bool)
assert not isinstance(str() + 'False', bool)
def test_user_defined() -> None:
from userdefinedbool import bool
b: Any = True
assert isinstance(bool(), bool)
assert not isinstance(b, bool)
[file userdefinedbool.py]
class bool:
pass