annotate Lib/test/test_sets.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents b303fb383fa4
children 51102abdec50 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #!/usr/bin/env python
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
40614
b303fb383fa4 Deprecate the sets module.
Brett Cannon <bcannon@gmail.com>
parents: 37408
diff changeset
3 import warnings
b303fb383fa4 Deprecate the sets module.
Brett Cannon <bcannon@gmail.com>
parents: 37408
diff changeset
4 warnings.filterwarnings("ignore", "the sets module is deprecated",
b303fb383fa4 Deprecate the sets module.
Brett Cannon <bcannon@gmail.com>
parents: 37408
diff changeset
5 DeprecationWarning, "test\.test_sets")
b303fb383fa4 Deprecate the sets module.
Brett Cannon <bcannon@gmail.com>
parents: 37408
diff changeset
6
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
7 import unittest, operator, copy, pickle, random
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 from sets import Set, ImmutableSet
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 from test import test_support
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 empty_set = Set()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 class TestBasicOps(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def test_repr(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 if self.repr is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30533
diff changeset
19 self.assertEqual(repr(self.set), self.repr)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 def test_length(self):
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
22 self.assertEqual(len(self.set), self.length)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 def test_self_equality(self):
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
25 self.assertEqual(self.set, self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 def test_equivalent_equality(self):
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
28 self.assertEqual(self.set, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def test_copy(self):
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
31 self.assertEqual(self.set.copy(), self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def test_self_union(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 result = self.set | self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
35 self.assertEqual(result, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def test_empty_union(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 result = self.set | empty_set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
39 self.assertEqual(result, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def test_union_empty(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 result = empty_set | self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
43 self.assertEqual(result, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 def test_self_intersection(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 result = self.set & self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
47 self.assertEqual(result, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 def test_empty_intersection(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 result = self.set & empty_set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
51 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def test_intersection_empty(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 result = empty_set & self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
55 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def test_self_symmetric_difference(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 result = self.set ^ self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
59 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def checkempty_symmetric_difference(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 result = self.set ^ empty_set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
63 self.assertEqual(result, self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def test_self_difference(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 result = self.set - self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
67 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def test_empty_difference(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 result = self.set - empty_set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
71 self.assertEqual(result, self.dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def test_empty_difference_rev(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 result = empty_set - self.set
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
75 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def test_iteration(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 for v in self.set:
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
79 self.assert_(v in self.values)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
26197
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
81 def test_pickling(self):
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
82 p = pickle.dumps(self.set)
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
83 copy = pickle.loads(p)
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
84 self.assertEqual(self.set, copy,
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
85 "%s != %s" % (self.set, copy))
cb71e7c620e2 Add getstate and setstate implementation to concrete set classes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26128
diff changeset
86
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 class TestBasicOpsEmpty(TestBasicOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 self.case = "empty set"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.values = []
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 self.set = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 self.dup = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 self.length = 0
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 self.repr = "Set([])"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 class TestBasicOpsSingleton(TestBasicOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self.case = "unit set (number)"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 self.values = [3]
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.set = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.dup = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 self.length = 1
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 self.repr = "Set([3])"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 def test_in(self):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
110 self.failUnless(3 in self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def test_not_in(self):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
113 self.failUnless(2 not in self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 class TestBasicOpsTuple(TestBasicOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 self.case = "unit set (tuple)"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 self.values = [(0, "zero")]
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 self.set = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 self.dup = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 self.length = 1
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self.repr = "Set([(0, 'zero')])"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 def test_in(self):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
127 self.failUnless((0, "zero") in self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 def test_not_in(self):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
130 self.failUnless(9 not in self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 class TestBasicOpsTriple(TestBasicOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 self.case = "triple set"
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 self.values = [0, "zero", operator.add]
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 self.set = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 self.dup = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 self.length = 3
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 self.repr = None
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
26101
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
145 def baditer():
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
146 raise TypeError
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
147 yield True
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
148
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
149 def gooditer():
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
150 yield True
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
151
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
152 class TestExceptionPropagation(unittest.TestCase):
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
153 """SF 628246: Set constructor should not trap iterator TypeErrors"""
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
154
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
155 def test_instanceWithException(self):
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
156 self.assertRaises(TypeError, Set, baditer())
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
157
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
158 def test_instancesWithoutException(self):
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
159 # All of these iterables should load without exception.
26101
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
160 Set([1,2,3])
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
161 Set((1,2,3))
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
162 Set({'one':1, 'two':2, 'three':3})
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
163 Set(xrange(3))
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
164 Set('abc')
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
165 Set(gooditer())
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
166
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
167 #==============================================================================
0820ad49829d Closes SF bug #628246.
Raymond Hettinger <python@rcn.com>
parents: 25031
diff changeset
168
24933
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
169 class TestSetOfSets(unittest.TestCase):
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
170 def test_constructor(self):
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
171 inner = Set([1])
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
172 outer = Set([inner])
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
173 element = outer.pop()
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
174 self.assertEqual(type(element), ImmutableSet)
25011
f36e16ca8f50 Expanded tests for sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24933
diff changeset
175 outer.add(inner) # Rebuild set of sets with .add method
f36e16ca8f50 Expanded tests for sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24933
diff changeset
176 outer.remove(inner)
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
177 self.assertEqual(outer, Set()) # Verify that remove worked
25011
f36e16ca8f50 Expanded tests for sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24933
diff changeset
178 outer.discard(inner) # Absence of KeyError indicates working fine
24933
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
179
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
180 #==============================================================================
9ee4f4cb9770 Add regression test for proper construction of sets of sets.
Raymond Hettinger <python@rcn.com>
parents: 24924
diff changeset
181
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 class TestBinaryOps(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 self.set = Set((2, 4, 6))
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
26408
3fa6ded0a1e9 SF 643115: Set._update() had a special case for dictionaries which allowed
Raymond Hettinger <python@rcn.com>
parents: 26200
diff changeset
186 def test_eq(self): # SF bug 643115
3fa6ded0a1e9 SF 643115: Set._update() had a special case for dictionaries which allowed
Raymond Hettinger <python@rcn.com>
parents: 26200
diff changeset
187 self.assertEqual(self.set, Set({2:1,4:3,6:5}))
3fa6ded0a1e9 SF 643115: Set._update() had a special case for dictionaries which allowed
Raymond Hettinger <python@rcn.com>
parents: 26200
diff changeset
188
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 def test_union_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 result = self.set | Set([2])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
191 self.assertEqual(result, Set((2, 4, 6)))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 def test_union_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 result = self.set | Set([2, 4, 6, 8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
195 self.assertEqual(result, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 def test_union_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 result = self.set | Set([3, 4, 5])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
199 self.assertEqual(result, Set([2, 3, 4, 5, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 def test_union_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 result = self.set | Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
203 self.assertEqual(result, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 def test_intersection_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 result = self.set & Set((2, 4))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
207 self.assertEqual(result, Set((2, 4)))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 def test_intersection_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 result = self.set & Set([2, 4, 6, 8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
211 self.assertEqual(result, Set([2, 4, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 def test_intersection_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 result = self.set & Set([3, 4, 5])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
215 self.assertEqual(result, Set([4]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 def test_intersection_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 result = self.set & Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
219 self.assertEqual(result, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 def test_sym_difference_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 result = self.set ^ Set((2, 4))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
223 self.assertEqual(result, Set([6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 def test_sym_difference_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 result = self.set ^ Set((2, 4, 6, 8))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
227 self.assertEqual(result, Set([8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 def test_sym_difference_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 result = self.set ^ Set((3, 4, 5))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
231 self.assertEqual(result, Set([2, 3, 5, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 def test_sym_difference_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 result = self.set ^ Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
235 self.assertEqual(result, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
237 def test_cmp(self):
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
238 a, b = Set('a'), Set('b')
28127
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
239 self.assertRaises(TypeError, cmp, a, b)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
240
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
241 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
28128
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
242 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
28127
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
243 # which Python thinks is good enough to synthesize a cmp() result
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
244 # without calling __cmp__.
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
245 self.assertEqual(cmp(a, a), 0)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
246
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
247 self.assertRaises(TypeError, cmp, a, 12)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
248 self.assertRaises(TypeError, cmp, "abc", a)
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
249
35337
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
250 def test_inplace_on_self(self):
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
251 t = self.set.copy()
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
252 t |= t
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
253 self.assertEqual(t, self.set)
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
254 t &= t
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
255 self.assertEqual(t, self.set)
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
256 t -= t
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
257 self.assertEqual(len(t), 0)
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
258 t = self.set.copy()
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
259 t ^= t
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
260 self.assertEqual(len(t), 0)
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
261
def434e2a81a Teach the sets module to correctly compute s-=s and s^=s as the empty set.
Raymond Hettinger <python@rcn.com>
parents: 32984
diff changeset
262
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 class TestUpdateOps(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 self.set = Set((2, 4, 6))
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 def test_union_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 self.set |= Set([2])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
271 self.assertEqual(self.set, Set((2, 4, 6)))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 def test_union_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 self.set |= Set([2, 4, 6, 8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
275 self.assertEqual(self.set, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 def test_union_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 self.set |= Set([3, 4, 5])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
279 self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 def test_union_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 self.set |= Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
283 self.assertEqual(self.set, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
285 def test_union_method_call(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
286 self.set.union_update(Set([3, 4, 5]))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
287 self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
288
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 def test_intersection_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 self.set &= Set((2, 4))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
291 self.assertEqual(self.set, Set((2, 4)))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 def test_intersection_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 self.set &= Set([2, 4, 6, 8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
295 self.assertEqual(self.set, Set([2, 4, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 def test_intersection_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 self.set &= Set([3, 4, 5])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
299 self.assertEqual(self.set, Set([4]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 def test_intersection_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 self.set &= Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
303 self.assertEqual(self.set, empty_set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
305 def test_intersection_method_call(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
306 self.set.intersection_update(Set([3, 4, 5]))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
307 self.assertEqual(self.set, Set([4]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
308
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 def test_sym_difference_subset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 self.set ^= Set((2, 4))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
311 self.assertEqual(self.set, Set([6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 def test_sym_difference_superset(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 self.set ^= Set((2, 4, 6, 8))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
315 self.assertEqual(self.set, Set([8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 def test_sym_difference_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 self.set ^= Set((3, 4, 5))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
319 self.assertEqual(self.set, Set([2, 3, 5, 6]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 def test_sym_difference_non_overlap(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 self.set ^= Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
323 self.assertEqual(self.set, Set([2, 4, 6, 8]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
325 def test_sym_difference_method_call(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
326 self.set.symmetric_difference_update(Set([3, 4, 5]))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
327 self.assertEqual(self.set, Set([2, 3, 5, 6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
328
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
329 def test_difference_subset(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
330 self.set -= Set((2, 4))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
331 self.assertEqual(self.set, Set([6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
332
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
333 def test_difference_superset(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
334 self.set -= Set((2, 4, 6, 8))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
335 self.assertEqual(self.set, Set([]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
336
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
337 def test_difference_overlap(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
338 self.set -= Set((3, 4, 5))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
339 self.assertEqual(self.set, Set([2, 6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
340
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
341 def test_difference_non_overlap(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
342 self.set -= Set([8])
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
343 self.assertEqual(self.set, Set([2, 4, 6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
344
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
345 def test_difference_method_call(self):
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
346 self.set.difference_update(Set([3, 4, 5]))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
347 self.assertEqual(self.set, Set([2, 6]))
25014
feeb0b02b8f2 At Tim Peter's suggestion, propagated GvR's binary operator changes to
Raymond Hettinger <python@rcn.com>
parents: 25011
diff changeset
348
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 class TestMutate(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 self.values = ["a", "b", "c"]
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 self.set = Set(self.values)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 def test_add_present(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 self.set.add("c")
25027
3484309573f2 Simplified code building sets of characters.
Tim Peters <tim.peters@gmail.com>
parents: 25026
diff changeset
358 self.assertEqual(self.set, Set("abc"))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 def test_add_absent(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 self.set.add("d")
25027
3484309573f2 Simplified code building sets of characters.
Tim Peters <tim.peters@gmail.com>
parents: 25026
diff changeset
362 self.assertEqual(self.set, Set("abcd"))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 def test_add_until_full(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 tmp = Set()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 expected_len = 0
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 for v in self.values:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 tmp.add(v)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 expected_len += 1
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
370 self.assertEqual(len(tmp), expected_len)
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
371 self.assertEqual(tmp, self.set)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 def test_remove_present(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 self.set.remove("b")
25027
3484309573f2 Simplified code building sets of characters.
Tim Peters <tim.peters@gmail.com>
parents: 25026
diff changeset
375 self.assertEqual(self.set, Set("ac"))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 def test_remove_absent(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 try:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 self.set.remove("d")
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
380 self.fail("Removing missing element should have raised LookupError")
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 except LookupError:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 pass
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 def test_remove_until_empty(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 expected_len = len(self.set)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 for v in self.values:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 self.set.remove(v)
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 expected_len -= 1
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
389 self.assertEqual(len(self.set), expected_len)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 def test_discard_present(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 self.set.discard("c")
25027
3484309573f2 Simplified code building sets of characters.
Tim Peters <tim.peters@gmail.com>
parents: 25026
diff changeset
393 self.assertEqual(self.set, Set("ab"))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 def test_discard_absent(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 self.set.discard("d")
25027
3484309573f2 Simplified code building sets of characters.
Tim Peters <tim.peters@gmail.com>
parents: 25026
diff changeset
397 self.assertEqual(self.set, Set("abc"))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 def test_clear(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 self.set.clear()
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
401 self.assertEqual(len(self.set), 0)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402
24924
a1143224ee0d Rename popitem() to pop(). (An idea from SF patch 597444.)
Guido van Rossum <guido@python.org>
parents: 24891
diff changeset
403 def test_pop(self):
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 popped = {}
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 while self.set:
24924
a1143224ee0d Rename popitem() to pop(). (An idea from SF patch 597444.)
Guido van Rossum <guido@python.org>
parents: 24891
diff changeset
406 popped[self.set.pop()] = None
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
407 self.assertEqual(len(popped), len(self.values))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 for v in self.values:
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
409 self.failUnless(v in popped)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 def test_update_empty_tuple(self):
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
412 self.set.union_update(())
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
413 self.assertEqual(self.set, Set(self.values))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
414
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 def test_update_unit_tuple_overlap(self):
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
416 self.set.union_update(("a",))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
417 self.assertEqual(self.set, Set(self.values))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 def test_update_unit_tuple_non_overlap(self):
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
420 self.set.union_update(("a", "z"))
25026
2c2b84bda141 Ack! Virtually every test here relied on an assert stmt. assert stmts
Tim Peters <tim.peters@gmail.com>
parents: 25025
diff changeset
421 self.assertEqual(self.set, Set(self.values + ["z"]))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 class TestSubsets(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
427 case2method = {"<=": "issubset",
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
428 ">=": "issuperset",
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
429 }
25031
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
430
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
431 reverse = {"==": "==",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
432 "!=": "!=",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
433 "<": ">",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
434 ">": "<",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
435 "<=": ">=",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
436 ">=": "<=",
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
437 }
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
438
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 def test_issubset(self):
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
440 x = self.left
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
441 y = self.right
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
442 for case in "!=", "==", "<", "<=", ">", ">=":
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
443 expected = case in self.cases
25031
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
444 # Test the binary infix spelling.
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
445 result = eval("x" + case + "y", locals())
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
446 self.assertEqual(result, expected)
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
447 # Test the "friendly" method-name spelling, if one exists.
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
448 if case in TestSubsets.case2method:
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
449 method = getattr(x, TestSubsets.case2method[case])
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
450 result = method(y)
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
451 self.assertEqual(result, expected)
25031
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
452
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
453 # Now do the same for the operands reversed.
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
454 rcase = TestSubsets.reverse[case]
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
455 result = eval("y" + rcase + "x", locals())
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
456 self.assertEqual(result, expected)
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
457 if rcase in TestSubsets.case2method:
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
458 method = getattr(y, TestSubsets.case2method[rcase])
40e9cfde1c50 Implemented <, <=, >, >= for sets, giving subset and proper-subset
Tim Peters <tim.peters@gmail.com>
parents: 25030
diff changeset
459 result = method(x)
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
460 self.assertEqual(result, expected)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 class TestSubsetEqualEmpty(TestSubsets):
25025
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
464 left = Set()
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
465 right = Set()
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
466 name = "both empty"
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
467 cases = "==", "<=", ">="
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 class TestSubsetEqualNonEmpty(TestSubsets):
25025
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
472 left = Set([1, 2])
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
473 right = Set([1, 2])
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
474 name = "equal pair"
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
475 cases = "==", "<=", ">="
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
476
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 class TestSubsetEmptyNonEmpty(TestSubsets):
25025
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
480 left = Set()
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
481 right = Set([1, 2])
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
482 name = "one empty, one non-empty"
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
483 cases = "!=", "<", "<="
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 class TestSubsetPartial(TestSubsets):
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26101
diff changeset
488 left = Set([1])
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26101
diff changeset
489 right = Set([1, 2])
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26101
diff changeset
490 name = "one a non-empty proper subset of other"
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26101
diff changeset
491 cases = "!=", "<", "<="
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 class TestSubsetNonOverlap(TestSubsets):
25025
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
496 left = Set([1])
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
497 right = Set([2])
8fa753a97077 Simplified the setup for is-subset testing.
Tim Peters <tim.peters@gmail.com>
parents: 25018
diff changeset
498 name = "neither empty, neither contains"
25030
8c61c92025b6 TestSubset(): Generalized the framework to support testing upcoming
Tim Peters <tim.peters@gmail.com>
parents: 25029
diff changeset
499 cases = "!="
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
502
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 class TestOnlySetsInBinaryOps(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
504
28127
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
505 def test_eq_ne(self):
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
506 # Unlike the others, this is testing that == and != *are* allowed.
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
507 self.assertEqual(self.other == self.set, False)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
508 self.assertEqual(self.set == self.other, False)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
509 self.assertEqual(self.other != self.set, True)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
510 self.assertEqual(self.set != self.other, True)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
511
28128
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
512 def test_ge_gt_le_lt(self):
28127
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
513 self.assertRaises(TypeError, lambda: self.set < self.other)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
514 self.assertRaises(TypeError, lambda: self.set <= self.other)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
515 self.assertRaises(TypeError, lambda: self.set > self.other)
5f923165319d SF bug 693121: Set == non-Set is a TypeError.
Tim Peters <tim.peters@gmail.com>
parents: 28126
diff changeset
516 self.assertRaises(TypeError, lambda: self.set >= self.other)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517
28128
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
518 self.assertRaises(TypeError, lambda: self.other < self.set)
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
519 self.assertRaises(TypeError, lambda: self.other <= self.set)
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
520 self.assertRaises(TypeError, lambda: self.other > self.set)
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
521 self.assertRaises(TypeError, lambda: self.other >= self.set)
87913529f248 Typo repairs in new code.
Tim Peters <tim.peters@gmail.com>
parents: 28127
diff changeset
522
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
523 def test_union_update_operator(self):
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 try:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 self.set |= self.other
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 except TypeError:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 pass
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
528 else:
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
529 self.fail("expected TypeError")
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
530
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
531 def test_union_update(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
532 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
533 self.set.union_update(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
534 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
535 self.assertRaises(TypeError, self.set.union_update, self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
536
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 def test_union(self):
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
538 self.assertRaises(TypeError, lambda: self.set | self.other)
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
539 self.assertRaises(TypeError, lambda: self.other | self.set)
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
540 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
541 self.set.union(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
542 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
543 self.assertRaises(TypeError, self.set.union, self.other)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
545 def test_intersection_update_operator(self):
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 try:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 self.set &= self.other
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 except TypeError:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 pass
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
550 else:
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
551 self.fail("expected TypeError")
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
553 def test_intersection_update(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
554 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
555 self.set.intersection_update(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
556 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
557 self.assertRaises(TypeError,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
558 self.set.intersection_update,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
559 self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
560
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 def test_intersection(self):
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
562 self.assertRaises(TypeError, lambda: self.set & self.other)
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
563 self.assertRaises(TypeError, lambda: self.other & self.set)
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
564 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
565 self.set.intersection(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
566 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
567 self.assertRaises(TypeError, self.set.intersection, self.other)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
569 def test_sym_difference_update_operator(self):
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 try:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 self.set ^= self.other
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 except TypeError:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 pass
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
574 else:
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
575 self.fail("expected TypeError")
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
577 def test_sym_difference_update(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
578 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
579 self.set.symmetric_difference_update(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
580 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
581 self.assertRaises(TypeError,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
582 self.set.symmetric_difference_update,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
583 self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
584
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 def test_sym_difference(self):
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
586 self.assertRaises(TypeError, lambda: self.set ^ self.other)
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
587 self.assertRaises(TypeError, lambda: self.other ^ self.set)
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
588 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
589 self.set.symmetric_difference(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
590 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
591 self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
592
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
593 def test_difference_update_operator(self):
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 try:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 self.set -= self.other
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 except TypeError:
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 pass
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
598 else:
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
599 self.fail("expected TypeError")
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
600
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
601 def test_difference_update(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
602 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
603 self.set.difference_update(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
604 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
605 self.assertRaises(TypeError,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
606 self.set.difference_update,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
607 self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
608
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 def test_difference(self):
28129
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
610 self.assertRaises(TypeError, lambda: self.set - self.other)
a24aabcf5ea2 TestOnlySetsInBinaryOps: Simplified the non-inplace tests by using
Tim Peters <tim.peters@gmail.com>
parents: 28128
diff changeset
611 self.assertRaises(TypeError, lambda: self.other - self.set)
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
612 if self.otherIsIterable:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
613 self.set.difference(self.other)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
614 else:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
615 self.assertRaises(TypeError, self.set.difference, self.other)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
616
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
618
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 self.set = Set((1, 2, 3))
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 self.other = 19
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
623 self.otherIsIterable = False
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
624
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 class TestOnlySetsDict(TestOnlySetsInBinaryOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 self.set = Set((1, 2, 3))
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 self.other = {1:2, 3:4}
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
631 self.otherIsIterable = True
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
634
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 self.set = Set((1, 2, 3))
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 self.other = operator.add
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
639 self.otherIsIterable = False
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
640
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
641 #------------------------------------------------------------------------------
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
642
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
643 class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
644 def setUp(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
645 self.set = Set((1, 2, 3))
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
646 self.other = (2, 4, 6)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
647 self.otherIsIterable = True
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
648
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
649 #------------------------------------------------------------------------------
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
650
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
651 class TestOnlySetsString(TestOnlySetsInBinaryOps):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
652 def setUp(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
653 self.set = Set((1, 2, 3))
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
654 self.other = 'abc'
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
655 self.otherIsIterable = True
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
656
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
657 #------------------------------------------------------------------------------
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
658
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
659 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
660 def setUp(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
661 def gen():
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
662 for i in xrange(0, 10, 2):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
663 yield i
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
664 self.set = Set((1, 2, 3))
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
665 self.other = gen()
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
666 self.otherIsIterable = True
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
668 #------------------------------------------------------------------------------
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
669
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
670 class TestOnlySetsofSets(TestOnlySetsInBinaryOps):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
671 def setUp(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
672 self.set = Set((1, 2, 3))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
673 self.other = [Set('ab'), ImmutableSet('cd')]
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
674 self.otherIsIterable = True
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
675
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 class TestCopying(unittest.TestCase):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 def test_copy(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 dup = self.set.copy()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 dup_list = list(dup); dup_list.sort()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 set_list = list(self.set); set_list.sort()
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
684 self.assertEqual(len(dup_list), len(set_list))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 for i in range(len(dup_list)):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
686 self.failUnless(dup_list[i] is set_list[i])
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 def test_deep_copy(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 dup = copy.deepcopy(self.set)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30533
diff changeset
690 ##print type(dup), repr(dup)
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 dup_list = list(dup); dup_list.sort()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 set_list = list(self.set); set_list.sort()
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
693 self.assertEqual(len(dup_list), len(set_list))
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 for i in range(len(dup_list)):
25029
aff067ba8d07 Rewrote all remaining assert stmts.
Tim Peters <tim.peters@gmail.com>
parents: 25028
diff changeset
695 self.assertEqual(dup_list[i], set_list[i])
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 class TestCopyingEmpty(TestCopying):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 self.set = Set()
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 class TestCopyingSingleton(TestCopying):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 self.set = Set(["hello"])
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 class TestCopyingTriple(TestCopying):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 self.set = Set(["zero", 0, None])
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
716
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 class TestCopyingTuple(TestCopying):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 self.set = Set([(1, 2)])
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 #------------------------------------------------------------------------------
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 class TestCopyingNested(TestCopying):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 def setUp(self):
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 self.set = Set([((1, 2), (3, 4))])
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 #==============================================================================
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
728
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
729 class TestIdentities(unittest.TestCase):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
730 def setUp(self):
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
731 self.a = Set([random.randrange(100) for i in xrange(50)])
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
732 self.b = Set([random.randrange(100) for i in xrange(50)])
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
733
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
734 def test_binopsVsSubsets(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
735 a, b = self.a, self.b
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
736 self.assert_(a - b <= a)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
737 self.assert_(b - a <= b)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
738 self.assert_(a & b <= a)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
739 self.assert_(a & b <= b)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
740 self.assert_(a | b >= a)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
741 self.assert_(a | b >= b)
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
742 self.assert_(a ^ b <= a | b)
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
743
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
744 def test_commutativity(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
745 a, b = self.a, self.b
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
746 self.assertEqual(a&b, b&a)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
747 self.assertEqual(a|b, b|a)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
748 self.assertEqual(a^b, b^a)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
749 if a != b:
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
750 self.assertNotEqual(a-b, b-a)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
751
30533
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
752 def test_reflexsive_relations(self):
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
753 a, zero = self.a, Set()
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
754 self.assertEqual(a ^ a, zero)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
755 self.assertEqual(a - a, zero)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
756 self.assertEqual(a | a, a)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
757 self.assertEqual(a & a, a)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
758 self.assert_(a <= a)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
759 self.assert_(a >= a)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
760 self.assert_(a == a)
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
761
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
762 def test_summations(self):
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
763 # check that sums of parts equal the whole
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
764 a, b = self.a, self.b
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
765 self.assertEqual((a-b)|(a&b)|(b-a), a|b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
766 self.assertEqual((a&b)|(a^b), a|b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
767 self.assertEqual(a|(b-a), a|b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
768 self.assertEqual((a-b)|b, a|b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
769 self.assertEqual((a-b)|(a&b), a)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
770 self.assertEqual((b-a)|(a&b), b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
771 self.assertEqual((a-b)|(b-a), a^b)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
772
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
773 def test_exclusion(self):
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
774 # check that inverse operations do not overlap
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
775 a, b, zero = self.a, self.b, Set()
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
776 self.assertEqual((a-b)&b, zero)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
777 self.assertEqual((b-a)&a, zero)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
778 self.assertEqual((a&b)&(a^b), zero)
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
779
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
780 def test_cardinality_relations(self):
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
781 a, b = self.a, self.b
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
782 self.assertEqual(len(a), len(a-b) + len(a&b))
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
783 self.assertEqual(len(b), len(b-a) + len(a&b))
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
784 self.assertEqual(len(a^b), len(a-b) + len(b-a))
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
785 self.assertEqual(len(a|b), len(a-b) + len(a&b) + len(b-a))
30533
a15b1c3c236a Add more identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30478
diff changeset
786 self.assertEqual(len(a^b) + len(a&b), len(a|b))
30478
b2919a4c558c Improve and expand identity tests.
Raymond Hettinger <python@rcn.com>
parents: 30247
diff changeset
787
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
788 #==============================================================================
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
789
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
790 libreftest = """
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
791 Example from the Library Reference: Doc/lib/libsets.tex
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
792
28126
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
793 >>> from sets import Set as Base # override _repr to get sorted output
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
794 >>> class Set(Base):
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
795 ... def _repr(self):
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
796 ... return Base._repr(self, sorted=True)
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
797 >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
798 >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
30173
9414886f5c7a Keep doctests in sync with the docs.
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
799 >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
9414886f5c7a Keep doctests in sync with the docs.
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
800 >>> employees = engineers | programmers | managers # union
9414886f5c7a Keep doctests in sync with the docs.
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
801 >>> engineering_management = engineers & managers # intersection
9414886f5c7a Keep doctests in sync with the docs.
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
802 >>> fulltime_management = managers - engineers - programmers # difference
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
803 >>> engineers.add('Marvin')
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
804 >>> print engineers
28126
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
805 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
806 >>> employees.issuperset(engineers) # superset test
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
807 False
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
808 >>> employees.union_update(engineers) # update from another set
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
809 >>> employees.issuperset(engineers)
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
810 True
30173
9414886f5c7a Keep doctests in sync with the docs.
Raymond Hettinger <python@rcn.com>
parents: 28917
diff changeset
811 >>> for group in [engineers, programmers, managers, employees]:
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
812 ... group.discard('Susan') # unconditionally remove element
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
813 ... print group
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
814 ...
28126
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
815 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
816 Set(['Jack', 'Janice', 'Sam'])
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
817 Set(['Jack', 'Jane', 'Zack'])
4cccef9b4d54 The doctest was printing Sets, but that's unreliable because set
Tim Peters <tim.peters@gmail.com>
parents: 27254
diff changeset
818 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
819 """
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
820
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
821 #==============================================================================
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
822
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
823 __test__ = {'libreftest' : libreftest}
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
824
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
825 def test_main(verbose=None):
37408
a60a9443e8da Use absolute imports
Neal Norwitz <nnorwitz@gmail.com>
parents: 35337
diff changeset
826 import doctest
a60a9443e8da Use absolute imports
Neal Norwitz <nnorwitz@gmail.com>
parents: 35337
diff changeset
827 from test import test_sets
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
828 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
829 TestSetOfSets,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
830 TestExceptionPropagation,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
831 TestBasicOpsEmpty,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
832 TestBasicOpsSingleton,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
833 TestBasicOpsTuple,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
834 TestBasicOpsTriple,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
835 TestBinaryOps,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
836 TestUpdateOps,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
837 TestMutate,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
838 TestSubsetEqualEmpty,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
839 TestSubsetEqualNonEmpty,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
840 TestSubsetEmptyNonEmpty,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
841 TestSubsetPartial,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
842 TestSubsetNonOverlap,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
843 TestOnlySetsNumeric,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
844 TestOnlySetsDict,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
845 TestOnlySetsOperator,
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
846 TestOnlySetsTuple,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
847 TestOnlySetsString,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
848 TestOnlySetsGenerator,
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 30176
diff changeset
849 TestOnlySetsofSets,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
850 TestCopyingEmpty,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
851 TestCopyingSingleton,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
852 TestCopyingTriple,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
853 TestCopyingTuple,
30176
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
854 TestCopyingNested,
3db50d31340d Improvements to set.py:
Raymond Hettinger <python@rcn.com>
parents: 30173
diff changeset
855 TestIdentities,
32984
4b3f8dd9269a Exercise DocTestSuite's search for __test__.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
856 doctest.DocTestSuite(test_sets),
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28129
diff changeset
857 )
24891
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
858
aebf8c52a842 Set classes and their unit tests, from sandbox.
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 if __name__ == "__main__":
27254
261547703443 * Add test for __cmp__()
Raymond Hettinger <python@rcn.com>
parents: 26408
diff changeset
860 test_main(verbose=True)