[Python-checkins] python/dist/src/Lib/test test_set.py,1.5,1.6

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sat Nov 22 21:49:08 EST 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv14916/Lib/test
Modified Files:
	test_set.py 
Log Message:
* Simplify hash function and add test to show effectiveness of the hash 
 function.
* Add a better test for deepcopying.
* Add tests to show the __init__() function works like it does for list
 and tuple. Add related test.
* Have shallow copies of frozensets return self. Add related test.
* Have frozenset(f) return f if f is already a frozenset. Add related test.
* Beefed-up some existing tests.
Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_set.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** test_set.py	22 Nov 2003 03:55:23 -0000	1.5
--- test_set.py	23 Nov 2003 02:49:05 -0000	1.6
***************
*** 39,51 ****
 self.assert_(self.thetype(self.letters) in s)
 
- def test_copy(self):
- dup = self.s.copy()
- self.assertEqual(self.s, dup)
- self.assertNotEqual(id(self.s), id(dup))
- 
 def test_union(self):
 u = self.s.union(self.otherword)
 for c in self.letters:
 self.assertEqual(c in u, c in self.d or c in self.otherword)
 self.assertEqual(type(u), self.thetype)
 self.assertRaises(PassThru, self.s.union, check_pass_thru())
--- 39,47 ----
 self.assert_(self.thetype(self.letters) in s)
 
 def test_union(self):
 u = self.s.union(self.otherword)
 for c in self.letters:
 self.assertEqual(c in u, c in self.d or c in self.otherword)
+ self.assertEqual(self.s, self.thetype(self.word))
 self.assertEqual(type(u), self.thetype)
 self.assertRaises(PassThru, self.s.union, check_pass_thru())
***************
*** 67,70 ****
--- 63,67 ----
 for c in self.letters:
 self.assertEqual(c in i, c in self.d and c in self.otherword)
+ self.assertEqual(self.s, self.thetype(self.word))
 self.assertEqual(type(i), self.thetype)
 self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
***************
*** 85,88 ****
--- 82,86 ----
 for c in self.letters:
 self.assertEqual(c in i, c in self.d and c not in self.otherword)
+ self.assertEqual(self.s, self.thetype(self.word))
 self.assertEqual(type(i), self.thetype)
 self.assertRaises(PassThru, self.s.difference, check_pass_thru())
***************
*** 104,107 ****
--- 102,106 ----
 for c in self.letters:
 self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword))
+ self.assertEqual(self.s, self.thetype(self.word))
 self.assertEqual(type(i), self.thetype)
 self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
***************
*** 156,164 ****
 self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup))
 
 class TestSet(TestJointOps):
 thetype = set
 
 def test_init(self):
! s = set()
 s.__init__(self.word)
 self.assertEqual(s, set(self.word))
--- 155,180 ----
 self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup))
 
+ def test_deepcopy(self):
+ class Tracer:
+ def __init__(self, value):
+ self.value = value
+ def __hash__(self):
+ return self.value
+ def __deepcopy__(self, memo=None):
+ return Tracer(self.value + 1)
+ t = Tracer(10)
+ s = self.thetype([t])
+ dup = copy.deepcopy(s)
+ self.assertNotEqual(id(s), id(dup))
+ for elem in dup:
+ newt = elem
+ self.assertNotEqual(id(t), id(newt))
+ self.assertEqual(t.value + 1, newt.value)
+ 
 class TestSet(TestJointOps):
 thetype = set
 
 def test_init(self):
! s = self.thetype()
 s.__init__(self.word)
 self.assertEqual(s, set(self.word))
***************
*** 166,169 ****
--- 182,190 ----
 self.assertEqual(s, set(self.otherword))
 
+ def test_constructor_identity(self):
+ s = self.thetype(range(3))
+ t = self.thetype(s)
+ self.assertNotEqual(id(s), id(t))
+ 
 def test_hash(self):
 self.assertRaises(TypeError, hash, self.s)
***************
*** 173,176 ****
--- 194,202 ----
 self.assertEqual(self.s, set([]))
 
+ def test_copy(self):
+ dup = self.s.copy()
+ self.assertEqual(self.s, dup)
+ self.assertNotEqual(id(self.s), id(dup))
+ 
 def test_add(self):
 self.s.add('Q')
***************
*** 286,300 ****
 
 def test_init(self):
! s = frozenset()
! s.__init__(self.word)
! self.assertEqual(s, frozenset())
 
 def test_hash(self):
! self.assertEqual(hash(frozenset('abcdeb')), hash(frozenset('ebecda')))
 
 def test_frozen_as_dictkey(self):
 seq = range(10) + list('abcdefg') + ['apple']
! key1 = frozenset(seq)
! key2 = frozenset(reversed(seq))
 self.assertEqual(key1, key2)
 self.assertNotEqual(id(key1), id(key2))
--- 312,336 ----
 
 def test_init(self):
! s = self.thetype(self.word)
! s.__init__(self.otherword)
! self.assertEqual(s, set(self.word))
! 
! def test_constructor_identity(self):
! s = self.thetype(range(3))
! t = self.thetype(s)
! self.assertEqual(id(s), id(t))
 
 def test_hash(self):
! self.assertEqual(hash(self.thetype('abcdeb')),
! hash(self.thetype('ebecda')))
! 
! def test_copy(self):
! dup = self.s.copy()
! self.assertEqual(id(self.s), id(dup))
 
 def test_frozen_as_dictkey(self):
 seq = range(10) + list('abcdefg') + ['apple']
! key1 = self.thetype(seq)
! key2 = self.thetype(reversed(seq))
 self.assertEqual(key1, key2)
 self.assertNotEqual(id(key1), id(key2))
***************
*** 304,310 ****
 
 def test_hash_caching(self):
! f = frozenset('abcdcda')
 self.assertEqual(hash(f), hash(f))
 
 class FrozenSetSubclass(frozenset):
 pass
--- 340,355 ----
 
 def test_hash_caching(self):
! f = self.thetype('abcdcda')
 self.assertEqual(hash(f), hash(f))
 
+ def test_hash_effectiveness(self):
+ n = 13
+ rng = range(n)
+ hashvalues = set()
+ for i in xrange(2**n):
+ combination = [j for j in rng if (1<<j)&i]
+ hashvalues.add(hash(self.thetype(combination)))
+ self.assert_(len(hashvalues) >= 2**(n-2))
+ 
 class FrozenSetSubclass(frozenset):
 pass
***************
*** 312,315 ****
--- 357,374 ----
 class TestFrozenSetSubclass(TestFrozenSet):
 thetype = FrozenSetSubclass
+ 
+ def test_constructor_identity(self):
+ s = self.thetype(range(3))
+ t = self.thetype(s)
+ self.assertNotEqual(id(s), id(t))
+ 
+ def test_copy(self):
+ dup = self.s.copy()
+ self.assertNotEqual(id(self.s), id(dup))
+ 
+ def test_nested_empty_constructor(self):
+ s = self.thetype()
+ t = self.thetype(s)
+ self.assertEqual(s, t)
 
 # Tests taken from test_sets.py =============================================


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /