Debian Codegraph @

mypyc/test-data/run-sets.test

raw
1 # Test cases for sets (compile and run)
2
3 [case testSets]
4 from typing import Set, List
5 def instantiateLiteral() -> Set[int]:
6 return {1, 2, 3, 5, 8}
7
8 def fromIterator() -> List[Set[int]]:
9 a = set([1, 3, 5])
10 b = set((1, 3, 5))
11 c = set({1: '1', 3: '3', 5: '5'})
12 d = set(x for x in range(1, 6, 2))
13 e = set((x for x in range(1, 6, 2)))
14 return [a, b, c, d, e]
15
16 def fromIterator2() -> Set[int]:
17 tmp_list = [1, 2, 3, 4, 5]
18 return set((x + 1) for x in ((y * 10) for y in (z for z in tmp_list if z < 4)))
19
20 def addIncrementing(s : Set[int]) -> None:
21 for a in [1, 2, 3]:
22 if a not in s:
23 s.add(a)
24 return
25
26 def replaceWith1(s : Set[int]) -> None:
27 s.clear()
28 s.add(1)
29
30 def remove1(s : Set[int]) -> None:
31 s.remove(1)
32
33 def discard1(s: Set[int]) -> None:
34 s.discard(1)
35
36 def pop(s : Set[int]) -> int:
37 return s.pop()
38
39 def update(s: Set[int], x: List[int]) -> None:
40 s.update(x)
41
42 [file driver.py]
43 from native import instantiateLiteral
44 from testutil import assertRaises
45
46 val = instantiateLiteral()
47 assert 1 in val
48 assert 2 in val
49 assert 3 in val
50 assert 5 in val
51 assert 8 in val
52 assert len(val) == 5
53 assert val == {1, 2, 3, 5, 8}
54 s = 0
55 for i in val:
56 s += i
57 assert s == 19
58
59 from native import fromIterator
60 sets = fromIterator()
61 for s in sets:
62 assert s == {1, 3, 5}
63
64 from native import fromIterator2
65 s = fromIterator2()
66 assert s == {11, 21, 31}
67
68 from native import addIncrementing
69 s = set()
70 addIncrementing(s)
71 assert s == {1}
72 addIncrementing(s)
73 assert s == {1, 2}
74 addIncrementing(s)
75 assert s == {1, 2, 3}
76
77 from native import replaceWith1
78 s = {3, 7, 12}
79 replaceWith1(s)
80 assert s == {1}
81
82 from native import remove1
83 import traceback
84 s = {1, 4, 6}
85 remove1(s)
86 assert s == {4, 6}
87 with assertRaises(KeyError, '1'):
88 remove1(s)
89
90 from native import discard1
91 s = {1, 4, 6}
92 discard1(s)
93 assert s == {4, 6}
94 discard1(s)
95 assert s == {4, 6}
96
97 from native import pop
98 s = {1, 2, 3}
99 x = pop(s)
100 assert len(s) == 2
101 assert x in [1, 2, 3]
102 y = pop(s)
103 assert len(s) == 1
104 assert y in [1, 2, 3]
105 assert x != y
106 z = pop(s)
107 assert len(s) == 0
108 assert z in [1, 2, 3]
109 assert x != z
110 assert y != z
111 with assertRaises(KeyError, 'pop from an empty set'):
112 pop(s)
113
114 from native import update
115 s = {1, 2, 3}
116 update(s, [5, 4, 3])
117 assert s == {1, 2, 3, 4, 5}
118
119 [case testFrozenSets]
120 from typing import FrozenSet, List, Any, cast
121 from testutil import assertRaises
122
123 def instantiateLiteral() -> FrozenSet[int]:
124 return frozenset((1, 2, 3, 5, 8))
125
126 def emptyFrozenSet1() -> FrozenSet[int]:
127 return frozenset()
128
129 def emptyFrozenSet2() -> FrozenSet[int]:
130 return frozenset(())
131
132 def fromIterator() -> List[FrozenSet[int]]:
133 a = frozenset([1, 3, 5])
134 b = frozenset((1, 3, 5))
135 c = frozenset({1, 3, 5})
136 d = frozenset({1: '1', 3: '3', 5: '5'})
137 e = frozenset(x for x in range(1, 6, 2))
138 f = frozenset((x for x in range(1, 6, 2)))
139 return [a, b, c, d, e, f]
140
141 def fromIterator2() -> FrozenSet[int]:
142 tmp_list = [1, 2, 3, 4, 5]
143 return frozenset((x + 1) for x in ((y * 10) for y in (z for z in tmp_list if z < 4)))
144
145 def castFrozenSet() -> FrozenSet[int]:
146 x: Any = frozenset((1, 2, 3, 5, 8))
147 return cast(FrozenSet, x)
148
149 def castFrozenSetError() -> FrozenSet[int]:
150 x: Any = {1, 2, 3, 5, 8}
151 return cast(FrozenSet, x)
152
153 def test_frozen_sets() -> None:
154 val = instantiateLiteral()
155 assert 1 in val
156 assert 2 in val
157 assert 3 in val
158 assert 5 in val
159 assert 8 in val
160 assert len(val) == 5
161 assert val == {1, 2, 3, 5, 8}
162 s = 0
163 for i in val:
164 s += i
165 assert s == 19
166
167 empty_set1 = emptyFrozenSet1()
168 assert empty_set1 == frozenset()
169
170 empty_set2 = emptyFrozenSet2()
171 assert empty_set2 == frozenset()
172
173 sets = fromIterator()
174 for s2 in sets:
175 assert s2 == {1, 3, 5}
176
177 s3 = fromIterator2()
178 assert s3 == {11, 21, 31}
179
180 val2 = castFrozenSet()
181 assert val2 == {1, 2, 3, 5, 8}
182
183 with assertRaises(TypeError, "frozenset object expected; got set"):
184 castFrozenSetError()
185
186 [case testFrozenSetsFromIterables]
187 from typing import FrozenSet
188
189 def f(x: int) -> int:
190 return x
191
192 def f1() -> FrozenSet[int]:
193 tmp_list = [1, 3, 5]
194 return frozenset(f(x) for x in tmp_list)
195
196 def f2() -> FrozenSet[int]:
197 tmp_tuple = (1, 3, 5)
198 return frozenset(f(x) for x in tmp_tuple)
199
200 def f3() -> FrozenSet[int]:
201 tmp_set = {1, 3, 5}
202 return frozenset(f(x) for x in tmp_set)
203
204 def f4() -> FrozenSet[int]:
205 tmp_dict = {1: '1', 3: '3', 5: '5'}
206 return frozenset(f(x) for x in tmp_dict)
207
208 def f5() -> FrozenSet[int]:
209 return frozenset(f(x) for x in range(1, 6, 2))
210
211 def f6() -> FrozenSet[int]:
212 return frozenset((f(x) for x in range(1, 6, 2)))
213
214 def g1(x: int) -> int:
215 return x
216
217 def g2(x: int) -> int:
218 return x * 10
219
220 def g3(x: int) -> int:
221 return x + 1
222
223 def g4() -> FrozenSet[int]:
224 tmp_list = [1, 2, 3, 4, 5]
225 return frozenset(g3(x) for x in (g2(y) for y in (g1(z) for z in tmp_list if z < 4)))
226
227 def test_frozen_sets_from_iterables() -> None:
228 val = frozenset({1, 3, 5})
229 assert f1() == val
230 assert f2() == val
231 assert f3() == val
232 assert f4() == val
233 assert f5() == val
234 assert f6() == val
235 assert g4() == frozenset({11, 21, 31})
236
237 [case testPrecomputedFrozenSets]
238 from typing import Final, Any
239
240 CONST: Final = "CONST"
241 non_const = "non_const"
242
243 def main_set(item: Any) -> bool:
244 return item in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
245
246 def main_negated_set(item: Any) -> bool:
247 return item not in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
248
249 def non_final_name_set(item: Any) -> bool:
250 return item in {non_const}
251
252 s = set()
253 for i in {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}:
254 s.add(i)
255
256 def test_in_set() -> None:
257 for item in (None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST):
258 assert main_set(item), f"{item!r} should be in set_main"
259 assert not main_negated_set(item), item
260
261 global non_const
262 assert non_final_name_set(non_const)
263 non_const = "updated"
264 assert non_final_name_set("updated")
265
266 def test_for_set() -> None:
267 assert not s ^ {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}, s
268
269 [case testIsInstance]
270 from copysubclass import subset, subfrozenset
271 def test_built_in_set() -> None:
272 assert isinstance(set(), set)
273 assert isinstance({'one', 'two'}, set)
274 assert isinstance({'a', 1}, set)
275 assert isinstance(subset(), set)
276 assert isinstance(subset({'one', 'two'}), set)
277 assert isinstance(subset({'a', 1}), set)
278
279 assert not isinstance(frozenset(), set)
280 assert not isinstance({}, set)
281 assert not isinstance([], set)
282 assert not isinstance((1,2,3), set)
283 assert not isinstance({1:'a', 2:'b'}, set)
284 assert not isinstance(int() + 1, set)
285 assert not isinstance(str() + 'a', set)
286
287 def test_user_defined_set() -> None:
288 from userdefinedset import set
289
290 assert isinstance(set(), set)
291 assert not isinstance({set()}, set)
292
293 def test_built_in_frozenset() -> None:
294 assert isinstance(frozenset(), frozenset)
295 assert isinstance(frozenset({'one', 'two'}), frozenset)
296 assert isinstance(frozenset({'a', 1}), frozenset)
297 assert isinstance(subfrozenset(), frozenset)
298 assert isinstance(subfrozenset({'one', 'two'}), frozenset)
299 assert isinstance(subfrozenset({'a', 1}), frozenset)
300
301 assert not isinstance(set(), frozenset)
302 assert not isinstance({}, frozenset)
303 assert not isinstance([], frozenset)
304 assert not isinstance((1,2,3), frozenset)
305 assert not isinstance({1:'a', 2:'b'}, frozenset)
306 assert not isinstance(int() + 1, frozenset)
307 assert not isinstance(str() + 'a', frozenset)
308
309 [file copysubclass.py]
310 from typing import Any
311 class subset(set[Any]):
312 pass
313
314 class subfrozenset(frozenset[Any]):
315 pass
316
317 [file userdefinedset.py]
318 class set:
319 pass