-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-sets.test
More file actions
319 lines (254 loc) · 7.65 KB
/
run-sets.test
File metadata and controls
319 lines (254 loc) · 7.65 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
# Test cases for sets (compile and run)
[case testSets]
from typing import Set, List
def instantiateLiteral() -> Set[int]:
return {1, 2, 3, 5, 8}
def fromIterator() -> List[Set[int]]:
a = set([1, 3, 5])
b = set((1, 3, 5))
c = set({1: '1', 3: '3', 5: '5'})
d = set(x for x in range(1, 6, 2))
e = set((x for x in range(1, 6, 2)))
return [a, b, c, d, e]
def fromIterator2() -> Set[int]:
tmp_list = [1, 2, 3, 4, 5]
return set((x + 1) for x in ((y * 10) for y in (z for z in tmp_list if z < 4)))
def addIncrementing(s : Set[int]) -> None:
for a in [1, 2, 3]:
if a not in s:
s.add(a)
return
def replaceWith1(s : Set[int]) -> None:
s.clear()
s.add(1)
def remove1(s : Set[int]) -> None:
s.remove(1)
def discard1(s: Set[int]) -> None:
s.discard(1)
def pop(s : Set[int]) -> int:
return s.pop()
def update(s: Set[int], x: List[int]) -> None:
s.update(x)
[file driver.py]
from native import instantiateLiteral
from testutil import assertRaises
val = instantiateLiteral()
assert 1 in val
assert 2 in val
assert 3 in val
assert 5 in val
assert 8 in val
assert len(val) == 5
assert val == {1, 2, 3, 5, 8}
s = 0
for i in val:
s += i
assert s == 19
from native import fromIterator
sets = fromIterator()
for s in sets:
assert s == {1, 3, 5}
from native import fromIterator2
s = fromIterator2()
assert s == {11, 21, 31}
from native import addIncrementing
s = set()
addIncrementing(s)
assert s == {1}
addIncrementing(s)
assert s == {1, 2}
addIncrementing(s)
assert s == {1, 2, 3}
from native import replaceWith1
s = {3, 7, 12}
replaceWith1(s)
assert s == {1}
from native import remove1
import traceback
s = {1, 4, 6}
remove1(s)
assert s == {4, 6}
with assertRaises(KeyError, '1'):
remove1(s)
from native import discard1
s = {1, 4, 6}
discard1(s)
assert s == {4, 6}
discard1(s)
assert s == {4, 6}
from native import pop
s = {1, 2, 3}
x = pop(s)
assert len(s) == 2
assert x in [1, 2, 3]
y = pop(s)
assert len(s) == 1
assert y in [1, 2, 3]
assert x != y
z = pop(s)
assert len(s) == 0
assert z in [1, 2, 3]
assert x != z
assert y != z
with assertRaises(KeyError, 'pop from an empty set'):
pop(s)
from native import update
s = {1, 2, 3}
update(s, [5, 4, 3])
assert s == {1, 2, 3, 4, 5}
[case testFrozenSets]
from typing import FrozenSet, List, Any, cast
from testutil import assertRaises
def instantiateLiteral() -> FrozenSet[int]:
return frozenset((1, 2, 3, 5, 8))
def emptyFrozenSet1() -> FrozenSet[int]:
return frozenset()
def emptyFrozenSet2() -> FrozenSet[int]:
return frozenset(())
def fromIterator() -> List[FrozenSet[int]]:
a = frozenset([1, 3, 5])
b = frozenset((1, 3, 5))
c = frozenset({1, 3, 5})
d = frozenset({1: '1', 3: '3', 5: '5'})
e = frozenset(x for x in range(1, 6, 2))
f = frozenset((x for x in range(1, 6, 2)))
return [a, b, c, d, e, f]
def fromIterator2() -> FrozenSet[int]:
tmp_list = [1, 2, 3, 4, 5]
return frozenset((x + 1) for x in ((y * 10) for y in (z for z in tmp_list if z < 4)))
def castFrozenSet() -> FrozenSet[int]:
x: Any = frozenset((1, 2, 3, 5, 8))
return cast(FrozenSet, x)
def castFrozenSetError() -> FrozenSet[int]:
x: Any = {1, 2, 3, 5, 8}
return cast(FrozenSet, x)
def test_frozen_sets() -> None:
val = instantiateLiteral()
assert 1 in val
assert 2 in val
assert 3 in val
assert 5 in val
assert 8 in val
assert len(val) == 5
assert val == {1, 2, 3, 5, 8}
s = 0
for i in val:
s += i
assert s == 19
empty_set1 = emptyFrozenSet1()
assert empty_set1 == frozenset()
empty_set2 = emptyFrozenSet2()
assert empty_set2 == frozenset()
sets = fromIterator()
for s2 in sets:
assert s2 == {1, 3, 5}
s3 = fromIterator2()
assert s3 == {11, 21, 31}
val2 = castFrozenSet()
assert val2 == {1, 2, 3, 5, 8}
with assertRaises(TypeError, "frozenset object expected; got set"):
castFrozenSetError()
[case testFrozenSetsFromIterables]
from typing import FrozenSet
def f(x: int) -> int:
return x
def f1() -> FrozenSet[int]:
tmp_list = [1, 3, 5]
return frozenset(f(x) for x in tmp_list)
def f2() -> FrozenSet[int]:
tmp_tuple = (1, 3, 5)
return frozenset(f(x) for x in tmp_tuple)
def f3() -> FrozenSet[int]:
tmp_set = {1, 3, 5}
return frozenset(f(x) for x in tmp_set)
def f4() -> FrozenSet[int]:
tmp_dict = {1: '1', 3: '3', 5: '5'}
return frozenset(f(x) for x in tmp_dict)
def f5() -> FrozenSet[int]:
return frozenset(f(x) for x in range(1, 6, 2))
def f6() -> FrozenSet[int]:
return frozenset((f(x) for x in range(1, 6, 2)))
def g1(x: int) -> int:
return x
def g2(x: int) -> int:
return x * 10
def g3(x: int) -> int:
return x + 1
def g4() -> FrozenSet[int]:
tmp_list = [1, 2, 3, 4, 5]
return frozenset(g3(x) for x in (g2(y) for y in (g1(z) for z in tmp_list if z < 4)))
def test_frozen_sets_from_iterables() -> None:
val = frozenset({1, 3, 5})
assert f1() == val
assert f2() == val
assert f3() == val
assert f4() == val
assert f5() == val
assert f6() == val
assert g4() == frozenset({11, 21, 31})
[case testPrecomputedFrozenSets]
from typing import Final, Any
CONST: Final = "CONST"
non_const = "non_const"
def main_set(item: Any) -> bool:
return item in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
def main_negated_set(item: Any) -> bool:
return item not in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
def non_final_name_set(item: Any) -> bool:
return item in {non_const}
s = set()
for i in {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}:
s.add(i)
def test_in_set() -> None:
for item in (None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST):
assert main_set(item), f"{item!r} should be in set_main"
assert not main_negated_set(item), item
global non_const
assert non_final_name_set(non_const)
non_const = "updated"
assert non_final_name_set("updated")
def test_for_set() -> None:
assert not s ^ {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}, s
[case testIsInstance]
from copysubclass import subset, subfrozenset
def test_built_in_set() -> None:
assert isinstance(set(), set)
assert isinstance({'one', 'two'}, set)
assert isinstance({'a', 1}, set)
assert isinstance(subset(), set)
assert isinstance(subset({'one', 'two'}), set)
assert isinstance(subset({'a', 1}), set)
assert not isinstance(frozenset(), set)
assert not isinstance({}, set)
assert not isinstance([], set)
assert not isinstance((1,2,3), set)
assert not isinstance({1:'a', 2:'b'}, set)
assert not isinstance(int() + 1, set)
assert not isinstance(str() + 'a', set)
def test_user_defined_set() -> None:
from userdefinedset import set
assert isinstance(set(), set)
assert not isinstance({set()}, set)
def test_built_in_frozenset() -> None:
assert isinstance(frozenset(), frozenset)
assert isinstance(frozenset({'one', 'two'}), frozenset)
assert isinstance(frozenset({'a', 1}), frozenset)
assert isinstance(subfrozenset(), frozenset)
assert isinstance(subfrozenset({'one', 'two'}), frozenset)
assert isinstance(subfrozenset({'a', 1}), frozenset)
assert not isinstance(set(), frozenset)
assert not isinstance({}, frozenset)
assert not isinstance([], frozenset)
assert not isinstance((1,2,3), frozenset)
assert not isinstance({1:'a', 2:'b'}, frozenset)
assert not isinstance(int() + 1, frozenset)
assert not isinstance(str() + 'a', frozenset)
[file copysubclass.py]
from typing import Any
class subset(set[Any]):
pass
class subfrozenset(frozenset[Any]):
pass
[file userdefinedset.py]
class set:
pass