forked from reingart/exercism
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustom_set_test.py
221 lines (179 loc) · 7.43 KB
/
custom_set_test.py
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
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/custom-set/canonical-data.json
# File last updated on 2023-12-27
import unittest
from custom_set import (
CustomSet,
)
class CustomSetTest(unittest.TestCase):
def test_sets_with_no_elements_are_empty(self):
sut = CustomSet()
self.assertIs(sut.isempty(), True)
def test_sets_with_elements_are_not_empty(self):
sut = CustomSet([1])
self.assertIs(sut.isempty(), False)
def test_nothing_is_contained_in_an_empty_set(self):
sut = CustomSet()
self.assertNotIn(1, sut)
def test_when_the_element_is_in_the_set(self):
sut = CustomSet([1, 2, 3])
self.assertIn(1, sut)
def test_when_the_element_is_not_in_the_set(self):
sut = CustomSet([1, 2, 3])
self.assertNotIn(4, sut)
def test_empty_set_is_a_subset_of_another_empty_set(self):
set1 = CustomSet()
set2 = CustomSet()
self.assertIs(set1.issubset(set2), True)
def test_empty_set_is_a_subset_of_non_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([1])
self.assertIs(set1.issubset(set2), True)
def test_non_empty_set_is_not_a_subset_of_empty_set(self):
set1 = CustomSet([1])
set2 = CustomSet()
self.assertIs(set1.issubset(set2), False)
def test_set_is_a_subset_of_set_with_exact_same_elements(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([1, 2, 3])
self.assertIs(set1.issubset(set2), True)
def test_set_is_a_subset_of_larger_set_with_same_elements(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([4, 1, 2, 3])
self.assertIs(set1.issubset(set2), True)
def test_set_is_not_a_subset_of_set_that_does_not_contain_its_elements(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([4, 1, 3])
self.assertIs(set1.issubset(set2), False)
def test_the_empty_set_is_disjoint_with_itself(self):
set1 = CustomSet()
set2 = CustomSet()
self.assertIs(set1.isdisjoint(set2), True)
def test_empty_set_is_disjoint_with_non_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([1])
self.assertIs(set1.isdisjoint(set2), True)
def test_non_empty_set_is_disjoint_with_empty_set(self):
set1 = CustomSet([1])
set2 = CustomSet()
self.assertIs(set1.isdisjoint(set2), True)
def test_sets_are_not_disjoint_if_they_share_an_element(self):
set1 = CustomSet([1, 2])
set2 = CustomSet([2, 3])
self.assertIs(set1.isdisjoint(set2), False)
def test_sets_are_disjoint_if_they_share_no_elements(self):
set1 = CustomSet([1, 2])
set2 = CustomSet([3, 4])
self.assertIs(set1.isdisjoint(set2), True)
def test_empty_sets_are_equal(self):
set1 = CustomSet()
set2 = CustomSet()
self.assertEqual(set1, set2)
def test_empty_set_is_not_equal_to_non_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([1, 2, 3])
self.assertNotEqual(set1, set2)
def test_non_empty_set_is_not_equal_to_empty_set(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet()
self.assertNotEqual(set1, set2)
def test_sets_with_the_same_elements_are_equal(self):
set1 = CustomSet([1, 2])
set2 = CustomSet([2, 1])
self.assertEqual(set1, set2)
def test_sets_with_different_elements_are_not_equal(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([1, 2, 4])
self.assertNotEqual(set1, set2)
def test_set_is_not_equal_to_larger_set_with_same_elements(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([1, 2, 3, 4])
self.assertNotEqual(set1, set2)
def test_set_is_equal_to_a_set_constructed_from_an_array_with_duplicates(self):
set1 = CustomSet([1])
set2 = CustomSet([1, 1])
self.assertEqual(set1, set2)
def test_add_to_empty_set(self):
sut = CustomSet()
expected = CustomSet([3])
sut.add(3)
self.assertEqual(sut, expected)
def test_add_to_non_empty_set(self):
sut = CustomSet([1, 2, 4])
expected = CustomSet([1, 2, 3, 4])
sut.add(3)
self.assertEqual(sut, expected)
def test_adding_an_existing_element_does_not_change_the_set(self):
sut = CustomSet([1, 2, 3])
expected = CustomSet([1, 2, 3])
sut.add(3)
self.assertEqual(sut, expected)
def test_intersection_of_two_empty_sets_is_an_empty_set(self):
set1 = CustomSet()
set2 = CustomSet()
expected = CustomSet()
self.assertEqual(set1.intersection(set2), expected)
def test_intersection_of_an_empty_set_and_non_empty_set_is_an_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([3, 2, 5])
expected = CustomSet()
self.assertEqual(set1.intersection(set2), expected)
def test_intersection_of_a_non_empty_set_and_an_empty_set_is_an_empty_set(self):
set1 = CustomSet([1, 2, 3, 4])
set2 = CustomSet()
expected = CustomSet()
self.assertEqual(set1.intersection(set2), expected)
def test_intersection_of_two_sets_with_no_shared_elements_is_an_empty_set(self):
set1 = CustomSet([1, 2, 3])
set2 = CustomSet([4, 5, 6])
expected = CustomSet()
self.assertEqual(set1.intersection(set2), expected)
def test_intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements(
self,
):
set1 = CustomSet([1, 2, 3, 4])
set2 = CustomSet([3, 2, 5])
expected = CustomSet([2, 3])
self.assertEqual(set1.intersection(set2), expected)
def test_difference_of_two_empty_sets_is_an_empty_set(self):
set1 = CustomSet()
set2 = CustomSet()
expected = CustomSet()
self.assertEqual(set1 - set2, expected)
def test_difference_of_empty_set_and_non_empty_set_is_an_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([3, 2, 5])
expected = CustomSet()
self.assertEqual(set1 - set2, expected)
def test_difference_of_a_non_empty_set_and_an_empty_set_is_the_non_empty_set(self):
set1 = CustomSet([1, 2, 3, 4])
set2 = CustomSet()
expected = CustomSet([1, 2, 3, 4])
self.assertEqual(set1 - set2, expected)
def test_difference_of_two_non_empty_sets_is_a_set_of_elements_that_are_only_in_the_first_set(
self,
):
set1 = CustomSet([3, 2, 1])
set2 = CustomSet([2, 4])
expected = CustomSet([1, 3])
self.assertEqual(set1 - set2, expected)
def test_union_of_empty_sets_is_an_empty_set(self):
set1 = CustomSet()
set2 = CustomSet()
expected = CustomSet()
self.assertEqual(set1 + set2, expected)
def test_union_of_an_empty_set_and_non_empty_set_is_the_non_empty_set(self):
set1 = CustomSet()
set2 = CustomSet([2])
expected = CustomSet([2])
self.assertEqual(set1 + set2, expected)
def test_union_of_a_non_empty_set_and_empty_set_is_the_non_empty_set(self):
set1 = CustomSet([1, 3])
set2 = CustomSet()
expected = CustomSet([1, 3])
self.assertEqual(set1 + set2, expected)
def test_union_of_non_empty_sets_contains_all_unique_elements(self):
set1 = CustomSet([1, 3])
set2 = CustomSet([2, 3])
expected = CustomSet([3, 2, 1])
self.assertEqual(set1 + set2, expected)