summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sets.py
blob: db344967a0afcdf1cb930569c0fb9631218c1497 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/usr/bin/env python

import unittest, operator, copy
from sets import Set, ImmutableSet
from test import test_support

empty_set = Set()

#==============================================================================

class TestBasicOps(unittest.TestCase):

    def test_repr(self):
        if self.repr is not None:
            assert `self.set` == self.repr, "Wrong representation for " + self.case

    def test_length(self):
        assert len(self.set) == self.length, "Wrong length for " + self.case

    def test_self_equality(self):
        assert self.set == self.set, "Self-equality failed for " + self.case

    def test_equivalent_equality(self):
        assert self.set == self.dup, "Equivalent equality failed for " + self.case

    def test_copy(self):
        assert self.set.copy() == self.dup, "Copy and comparison failed for " + self.case

    def test_self_union(self):
        result = self.set | self.set
        assert result == self.dup, "Self-union failed for " + self.case

    def test_empty_union(self):
        result = self.set | empty_set
        assert result == self.dup, "Union with empty failed for " + self.case

    def test_union_empty(self):
        result = empty_set | self.set
        assert result == self.dup, "Union with empty failed for " + self.case

    def test_self_intersection(self):
        result = self.set & self.set
        assert result == self.dup, "Self-intersection failed for " + self.case

    def test_empty_intersection(self):
        result = self.set & empty_set
        assert result == empty_set, "Intersection with empty failed for " + self.case

    def test_intersection_empty(self):
        result = empty_set & self.set
        assert result == empty_set, "Intersection with empty failed for " + self.case

    def test_self_symmetric_difference(self):
        result = self.set ^ self.set
        assert result == empty_set, "Self-symdiff failed for " + self.case

    def checkempty_symmetric_difference(self):
        result = self.set ^ empty_set
        assert result == self.set, "Symdiff with empty failed for " + self.case

    def test_self_difference(self):
        result = self.set - self.set
        assert result == empty_set, "Self-difference failed for " + self.case

    def test_empty_difference(self):
        result = self.set - empty_set
        assert result == self.dup, "Difference with empty failed for " + self.case

    def test_empty_difference_rev(self):
        result = empty_set - self.set
        assert result == empty_set, "Difference from empty failed for " + self.case

    def test_iteration(self):
        for v in self.set:
            assert v in self.values, "Missing item in iteration for " + self.case

#------------------------------------------------------------------------------

class TestBasicOpsEmpty(TestBasicOps):
    def setUp(self):
        self.case   = "empty set"
        self.values = []
        self.set    = Set(self.values)
        self.dup    = Set(self.values)
        self.length = 0
        self.repr   = "Set([])"

#------------------------------------------------------------------------------

class TestBasicOpsSingleton(TestBasicOps):
    def setUp(self):
        self.case   = "unit set (number)"
        self.values = [3]
        self.set    = Set(self.values)
        self.dup    = Set(self.values)
        self.length = 1
        self.repr   = "Set([3])"

    def test_in(self):
        assert 3 in self.set, "Valueship for unit set"

    def test_not_in(self):
        assert 2 not in self.set, "Non-valueship for unit set"

#------------------------------------------------------------------------------

class TestBasicOpsTuple(TestBasicOps):
    def setUp(self):
        self.case   = "unit set (tuple)"
        self.values = [(0, "zero")]
        self.set    = Set(self.values)
        self.dup    = Set(self.values)
        self.length = 1
        self.repr   = "Set([(0, 'zero')])"

    def test_in(self):
        assert (0, "zero") in self.set, "Valueship for tuple set"

    def test_not_in(self):
        assert 9 not in self.set, "Non-valueship for tuple set"

#------------------------------------------------------------------------------

class TestBasicOpsTriple(TestBasicOps):
    def setUp(self):
        self.case   = "triple set"
        self.values = [0, "zero", operator.add]
        self.set    = Set(self.values)
        self.dup    = Set(self.values)
        self.length = 3
        self.repr   = None

#==============================================================================

class TestSetOfSets(unittest.TestCase):
    def test_constructor(self):
        inner = Set([1])
        outer = Set([inner])
        element = outer.pop()
        assert type(element) == ImmutableSet, "Construct set of sets"
        outer.add(inner)        # Rebuild set of sets with .add method
        outer.remove(inner)
        assert outer == Set()   # Verify that remove worked
        outer.discard(inner)    # Absence of KeyError indicates working fine

#==============================================================================

class TestBinaryOps(unittest.TestCase):
    def setUp(self):
        self.set = Set((2, 4, 6))

    def test_union_subset(self):
        result = self.set | Set([2])
        assert result == Set((2, 4, 6)), "Subset union"

    def test_union_superset(self):
        result = self.set | Set([2, 4, 6, 8])
        assert result == Set([2, 4, 6, 8]), "Superset union"

    def test_union_overlap(self):
        result = self.set | Set([3, 4, 5])
        assert result == Set([2, 3, 4, 5, 6]), "Overlapping union"

    def test_union_non_overlap(self):
        result = self.set | Set([8])
        assert result == Set([2, 4, 6, 8]), "Non-overlapping union"

    def test_intersection_subset(self):
        result = self.set & Set((2, 4))
        assert result == Set((2, 4)), "Subset intersection"

    def test_intersection_superset(self):
        result = self.set & Set([2, 4, 6, 8])
        assert result == Set([2, 4, 6]), "Superset intersection"

    def test_intersection_overlap(self):
        result = self.set & Set([3, 4, 5])
        assert result == Set([4]), "Overlapping intersection"

    def test_intersection_non_overlap(self):
        result = self.set & Set([8])
        assert result == empty_set, "Non-overlapping intersection"

    def test_sym_difference_subset(self):
        result = self.set ^ Set((2, 4))
        assert result == Set([6]), "Subset symmetric difference"

    def test_sym_difference_superset(self):
        result = self.set ^ Set((2, 4, 6, 8))
        assert result == Set([8]), "Superset symmetric difference"

    def test_sym_difference_overlap(self):
        result = self.set ^ Set((3, 4, 5))
        assert result == Set([2, 3, 5, 6]), "Overlapping symmetric difference"

    def test_sym_difference_non_overlap(self):
        result = self.set ^ Set([8])
        assert result == Set([2, 4, 6, 8]), "Non-overlapping symmetric difference"

#==============================================================================

class TestUpdateOps(unittest.TestCase):
    def setUp(self):
        self.set = Set((2, 4, 6))

    def test_union_subset(self):
        self.set |= Set([2])
        assert self.set == Set((2, 4, 6)), "Subset union"

    def test_union_superset(self):
        self.set |= Set([2, 4, 6, 8])
        assert self.set == Set([2, 4, 6, 8]), "Superset union"

    def test_union_overlap(self):
        self.set |= Set([3, 4, 5])
        assert self.set == Set([2, 3, 4, 5, 6]), "Overlapping union"

    def test_union_non_overlap(self):
        self.set |= Set([8])
        assert self.set == Set([2, 4, 6, 8]), "Non-overlapping union"

    def test_union_method_call(self):
        self.set.union_update(Set([3, 4, 5]))
        assert self.set == Set([2, 3, 4, 5, 6]), "Union method call"

    def test_intersection_subset(self):
        self.set &= Set((2, 4))
        assert self.set == Set((2, 4)), "Subset intersection"

    def test_intersection_superset(self):
        self.set &= Set([2, 4, 6, 8])
        assert self.set == Set([2, 4, 6]), "Superset intersection"

    def test_intersection_overlap(self):
        self.set &= Set([3, 4, 5])
        assert self.set == Set([4]), "Overlapping intersection"

    def test_intersection_non_overlap(self):
        self.set &= Set([8])
        assert self.set == empty_set, "Non-overlapping intersection"

    def test_intersection_method_call(self):
        self.set.intersection_update(Set([3, 4, 5]))
        assert self.set == Set([4]), "Intersection method call"

    def test_sym_difference_subset(self):
        self.set ^= Set((2, 4))
        assert self.set == Set([6]), "Subset symmetric difference"

    def test_sym_difference_superset(self):
        self.set ^= Set((2, 4, 6, 8))
        assert self.set == Set([8]), "Superset symmetric difference"

    def test_sym_difference_overlap(self):
        self.set ^= Set((3, 4, 5))
        assert self.set == Set([2, 3, 5, 6]), "Overlapping symmetric difference"

    def test_sym_difference_non_overlap(self):
        self.set ^= Set([8])
        assert self.set == Set([2, 4, 6, 8]), "Non-overlapping symmetric difference"

    def test_sym_difference_method_call(self):
        self.set.symmetric_difference_update(Set([3, 4, 5]))
        assert self.set == Set([2, 3, 5, 6]), "Symmetric difference method call"

    def test_difference_subset(self):
        self.set -= Set((2, 4))
        assert self.set == Set([6]), "Subset difference"

    def test_difference_superset(self):
        self.set -= Set((2, 4, 6, 8))
        assert self.set == Set([]), "Superset difference"

    def test_difference_overlap(self):
        self.set -= Set((3, 4, 5))
        assert self.set == Set([2, 6]), "Overlapping difference"

    def test_difference_non_overlap(self):
        self.set -= Set([8])
        assert self.set == Set([2, 4, 6]), "Non-overlapping difference"

    def test_difference_method_call(self):
        self.set.difference_update(Set([3, 4, 5]))
        assert self.set == Set([2, 6]), "Difference method call"

#==============================================================================

class TestMutate(unittest.TestCase):
    def setUp(self):
        self.values = ["a", "b", "c"]
        self.set = Set(self.values)

    def test_add_present(self):
        self.set.add("c")
        assert self.set == Set(("a", "b", "c")), "Adding present element"

    def test_add_absent(self):
        self.set.add("d")
        assert self.set == Set(("a", "b", "c", "d")), "Adding missing element"

    def test_add_until_full(self):
        tmp = Set()
        expected_len = 0
        for v in self.values:
            tmp.add(v)
            expected_len += 1
            assert len(tmp) == expected_len, "Adding values one by one to temporary"
        assert tmp == self.set, "Adding values one by one"

    def test_remove_present(self):
        self.set.remove("b")
        assert self.set == Set(("a", "c")), "Removing present element"

    def test_remove_absent(self):
        try:
            self.set.remove("d")
            assert 0, "Removing missing element"
        except LookupError:
            pass

    def test_remove_until_empty(self):
        expected_len = len(self.set)
        for v in self.values:
            self.set.remove(v)
            expected_len -= 1
            assert len(self.set) == expected_len, "Removing values one by one"

    def test_discard_present(self):
        self.set.discard("c")
        assert self.set == Set(("a", "b")), "Discarding present element"

    def test_discard_absent(self):
        self.set.discard("d")
        assert self.set == Set(("a", "b", "c")), "Discarding missing element"

    def test_clear(self):
        self.set.clear()
        assert len(self.set) == 0, "Clearing set"

    def test_pop(self):
        popped = {}
        while self.set:
            popped[self.set.pop()] = None
        assert len(popped) == len(self.values), "Popping items"
        for v in self.values:
            assert v in popped, "Popping items"

    def test_update_empty_tuple(self):
        self.set.update(())
        assert self.set == Set(self.values), "Updating with empty tuple"

    def test_update_unit_tuple_overlap(self):
        self.set.update(("a",))
        assert self.set == Set(self.values), "Updating with overlapping unit tuple"

    def test_update_unit_tuple_non_overlap(self):
        self.set.update(("a", "z"))
        assert self.set == Set(self.values + ["z"]), "Updating with non-overlapping unit tuple"

#==============================================================================

class TestSubsets(unittest.TestCase):

    def test_issubset(self):
        result = self.left.issubset(self.right)
        if "<" in self.cases:
            assert result, "subset: " + self.name
        else:
            assert not result, "non-subset: " + self.name

#------------------------------------------------------------------------------

class TestSubsetEqualEmpty(TestSubsets):
    left  = Set()
    right = Set()
    name  = "both empty"
    cases = "<>"

#------------------------------------------------------------------------------

class TestSubsetEqualNonEmpty(TestSubsets):
    left  = Set([1, 2])
    right = Set([1, 2])
    name  = "equal pair"
    cases = "<>"

#------------------------------------------------------------------------------

class TestSubsetEmptyNonEmpty(TestSubsets):
    left  = Set()
    right = Set([1, 2])
    name  = "one empty, one non-empty"
    cases = "<"

#------------------------------------------------------------------------------

class TestSubsetPartial(TestSubsets):
   left  = Set([1])
   right = Set([1, 2])
   name  = "one a non-empty subset of other"
   cases = "<"

#------------------------------------------------------------------------------

class TestSubsetNonOverlap(TestSubsets):
    left  = Set([1])
    right = Set([2])
    name  = "neither empty, neither contains"
    cases = ""

#==============================================================================

class TestOnlySetsInBinaryOps(unittest.TestCase):

    def test_cmp(self):
        try:
            self.other == self.set
            assert 0, "Comparison with non-set on left"
        except TypeError:
            pass
        try:
            self.set != self.other
            assert 0, "Comparison with non-set on right"
        except TypeError:
            pass

    def test_union_update(self):
        try:
            self.set |= self.other
            assert 0, "Union update with non-set"
        except TypeError:
            pass

    def test_union(self):
        try:
            self.other | self.set
            assert 0, "Union with non-set on left"
        except TypeError:
            pass
        try:
            self.set | self.other
            assert 0, "Union with non-set on right"
        except TypeError:
            pass

    def test_intersection_update(self):
        try:
            self.set &= self.other
            assert 0, "Intersection update with non-set"
        except TypeError:
            pass

    def test_intersection(self):
        try:
            self.other & self.set
            assert 0, "Intersection with non-set on left"
        except TypeError:
            pass
        try:
            self.set & self.other
            assert 0, "Intersection with non-set on right"
        except TypeError:
            pass

    def test_sym_difference_update(self):
        try:
            self.set ^= self.other
            assert 0, "Symmetric difference update with non-set"
        except TypeError:
            pass

    def test_sym_difference(self):
        try:
            self.other ^ self.set
            assert 0, "Symmetric difference with non-set on left"
        except TypeError:
            pass
        try:
            self.set ^ self.other
            assert 0, "Symmetric difference with non-set on right"
        except TypeError:
            pass

    def test_difference_update(self):
        try:
            self.set -= self.other
            assert 0, "Symmetric difference update with non-set"
        except TypeError:
            pass

    def test_difference(self):
        try:
            self.other - self.set
            assert 0, "Symmetric difference with non-set on left"
        except TypeError:
            pass
        try:
            self.set - self.other
            assert 0, "Symmetric difference with non-set on right"
        except TypeError:
            pass

#------------------------------------------------------------------------------

class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
    def setUp(self):
        self.set   = Set((1, 2, 3))
        self.other = 19

#------------------------------------------------------------------------------

class TestOnlySetsDict(TestOnlySetsInBinaryOps):
    def setUp(self):
        self.set   = Set((1, 2, 3))
        self.other = {1:2, 3:4}

#------------------------------------------------------------------------------

class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
    def setUp(self):
        self.set   = Set((1, 2, 3))
        self.other = operator.add

#==============================================================================

class TestCopying(unittest.TestCase):

    def test_copy(self):
        dup = self.set.copy()
        dup_list = list(dup); dup_list.sort()
        set_list = list(self.set); set_list.sort()
        assert len(dup_list) == len(set_list), "Unequal lengths after copy"
        for i in range(len(dup_list)):
            assert dup_list[i] is set_list[i], "Non-identical items after copy"

    def test_deep_copy(self):
        dup = copy.deepcopy(self.set)
        ##print type(dup), `dup`
        dup_list = list(dup); dup_list.sort()
        set_list = list(self.set); set_list.sort()
        assert len(dup_list) == len(set_list), "Unequal lengths after deep copy"
        for i in range(len(dup_list)):
            assert dup_list[i] == set_list[i], "Unequal items after deep copy"

#------------------------------------------------------------------------------

class TestCopyingEmpty(TestCopying):
    def setUp(self):
        self.set = Set()

#------------------------------------------------------------------------------

class TestCopyingSingleton(TestCopying):
    def setUp(self):
        self.set = Set(["hello"])

#------------------------------------------------------------------------------

class TestCopyingTriple(TestCopying):
    def setUp(self):
        self.set = Set(["zero", 0, None])

#------------------------------------------------------------------------------

class TestCopyingTuple(TestCopying):
    def setUp(self):
        self.set = Set([(1, 2)])

#------------------------------------------------------------------------------

class TestCopyingNested(TestCopying):
    def setUp(self):
        self.set = Set([((1, 2), (3, 4))])

#==============================================================================

def makeAllTests():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestSetOfSets))
    suite.addTest(unittest.makeSuite(TestBasicOpsEmpty))
    suite.addTest(unittest.makeSuite(TestBasicOpsSingleton))
    suite.addTest(unittest.makeSuite(TestBasicOpsTuple))
    suite.addTest(unittest.makeSuite(TestBasicOpsTriple))
    suite.addTest(unittest.makeSuite(TestBinaryOps))
    suite.addTest(unittest.makeSuite(TestUpdateOps))
    suite.addTest(unittest.makeSuite(TestMutate))
    suite.addTest(unittest.makeSuite(TestSubsetEqualEmpty))
    suite.addTest(unittest.makeSuite(TestSubsetEqualNonEmpty))
    suite.addTest(unittest.makeSuite(TestSubsetEmptyNonEmpty))
    suite.addTest(unittest.makeSuite(TestSubsetPartial))
    suite.addTest(unittest.makeSuite(TestSubsetNonOverlap))
    suite.addTest(unittest.makeSuite(TestOnlySetsNumeric))
    suite.addTest(unittest.makeSuite(TestOnlySetsDict))
    suite.addTest(unittest.makeSuite(TestOnlySetsOperator))
    suite.addTest(unittest.makeSuite(TestCopyingEmpty))
    suite.addTest(unittest.makeSuite(TestCopyingSingleton))
    suite.addTest(unittest.makeSuite(TestCopyingTriple))
    suite.addTest(unittest.makeSuite(TestCopyingTuple))
    suite.addTest(unittest.makeSuite(TestCopyingNested))
    return suite

#------------------------------------------------------------------------------

def test_main():
    suite = makeAllTests()
    test_support.run_suite(suite)

if __name__ == "__main__":
    test_main()