run-sets.test 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Test cases for sets (compile and run)
  2. [case testSets]
  3. from typing import Set, List
  4. def instantiateLiteral() -> Set[int]:
  5. return {1, 2, 3, 5, 8}
  6. def fromIterator() -> List[Set[int]]:
  7. a = set([1, 3, 5])
  8. b = set((1, 3, 5))
  9. c = set({1: '1', 3: '3', 5: '5'})
  10. d = set(x for x in range(1, 6, 2))
  11. e = set((x for x in range(1, 6, 2)))
  12. return [a, b, c, d, e]
  13. def fromIterator2() -> Set[int]:
  14. tmp_list = [1, 2, 3, 4, 5]
  15. return set((x + 1) for x in ((y * 10) for y in (z for z in tmp_list if z < 4)))
  16. def addIncrementing(s : Set[int]) -> None:
  17. for a in [1, 2, 3]:
  18. if a not in s:
  19. s.add(a)
  20. return
  21. def replaceWith1(s : Set[int]) -> None:
  22. s.clear()
  23. s.add(1)
  24. def remove1(s : Set[int]) -> None:
  25. s.remove(1)
  26. def discard1(s: Set[int]) -> None:
  27. s.discard(1)
  28. def pop(s : Set[int]) -> int:
  29. return s.pop()
  30. def update(s: Set[int], x: List[int]) -> None:
  31. s.update(x)
  32. [file driver.py]
  33. from native import instantiateLiteral
  34. from testutil import assertRaises
  35. val = instantiateLiteral()
  36. assert 1 in val
  37. assert 2 in val
  38. assert 3 in val
  39. assert 5 in val
  40. assert 8 in val
  41. assert len(val) == 5
  42. assert val == {1, 2, 3, 5, 8}
  43. s = 0
  44. for i in val:
  45. s += i
  46. assert s == 19
  47. from native import fromIterator
  48. sets = fromIterator()
  49. for s in sets:
  50. assert s == {1, 3, 5}
  51. from native import fromIterator2
  52. s = fromIterator2()
  53. assert s == {11, 21, 31}
  54. from native import addIncrementing
  55. s = set()
  56. addIncrementing(s)
  57. assert s == {1}
  58. addIncrementing(s)
  59. assert s == {1, 2}
  60. addIncrementing(s)
  61. assert s == {1, 2, 3}
  62. from native import replaceWith1
  63. s = {3, 7, 12}
  64. replaceWith1(s)
  65. assert s == {1}
  66. from native import remove1
  67. import traceback
  68. s = {1, 4, 6}
  69. remove1(s)
  70. assert s == {4, 6}
  71. with assertRaises(KeyError, '1'):
  72. remove1(s)
  73. from native import discard1
  74. s = {1, 4, 6}
  75. discard1(s)
  76. assert s == {4, 6}
  77. discard1(s)
  78. assert s == {4, 6}
  79. from native import pop
  80. s = {1, 2, 3}
  81. x = pop(s)
  82. assert len(s) == 2
  83. assert x in [1, 2, 3]
  84. y = pop(s)
  85. assert len(s) == 1
  86. assert y in [1, 2, 3]
  87. assert x != y
  88. z = pop(s)
  89. assert len(s) == 0
  90. assert z in [1, 2, 3]
  91. assert x != z
  92. assert y != z
  93. with assertRaises(KeyError, 'pop from an empty set'):
  94. pop(s)
  95. from native import update
  96. s = {1, 2, 3}
  97. update(s, [5, 4, 3])
  98. assert s == {1, 2, 3, 4, 5}
  99. [case testPrecomputedFrozenSets]
  100. from typing import Any
  101. from typing_extensions import Final
  102. CONST: Final = "CONST"
  103. non_const = "non_const"
  104. def main_set(item: Any) -> bool:
  105. return item in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
  106. def main_negated_set(item: Any) -> bool:
  107. return item not in {None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST}
  108. def non_final_name_set(item: Any) -> bool:
  109. return item in {non_const}
  110. s = set()
  111. for i in {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}:
  112. s.add(i)
  113. def test_in_set() -> None:
  114. for item in (None, False, 1, 2.0, "3", b"4", 5j, (6,), ((7,),), (), CONST):
  115. assert main_set(item), f"{item!r} should be in set_main"
  116. assert not main_negated_set(item), item
  117. global non_const
  118. assert non_final_name_set(non_const)
  119. non_const = "updated"
  120. assert non_final_name_set("updated")
  121. def test_for_set() -> None:
  122. assert not s ^ {None, False, 1, 2.0, "3", b"4", 5j, (6,), CONST}, s