# 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