def test__init_1(self): dogs = ((0, Tuple(('Cody', 5))), (1, Tuple(('Snoopy', 2)))) mset = mapset_.Mapset(dogs) self.assertEqual(len(mset._data), len(dogs)) self.assertIs(mset._data[0], dogs[0][1]) self.assertIs(mset._data[1], dogs[1][1]) self.assertEqual(mset._set.count(dogs[0][1]), 1) self.assertEqual(mset._set.count(dogs[1][1]), 1)
def test__clear_1(self): dogs = (('cody1', Tuple(('Cody', 5))), ('snoopy', Tuple( ('Snoopy', 2))), ('cody2', Tuple(('Cody', 5)))) mset = mapset_.Mapset(dogs) mset.clear() self.assertEqual(len(mset), 0) self.assertEqual(len(mset._data), 0) self.assertEqual(len(mset._set), 0)
def test__getitem_1(self): dogs = { 'cody1': Tuple(('Cody', 5)), 'snoopy': Tuple(('Snoopy', 2)), 'cody2': Tuple(('Cody', 5)) } mset = mapset_.Mapset(dogs) self.assertTrue(mset['cody1'] is dogs['cody1'] or mset['cody1'] is dogs['cody2']) self.assertTrue(mset['snoopy'] is dogs['snoopy']) self.assertTrue(mset['cody2'] is dogs['cody1'] or mset['cody2'] is dogs['cody2'])
def test__setdefault_1(self): dogs = (('cody', Tuple(('Cody', 5))), ('snoopy', Tuple(('Snoopy', 2)))) mset = mapset_.Mapset(dogs) lessie = Tuple(('Lessie', 4)) self.assertIs(mset.setdefault('lessie', lessie), lessie) self.assertIs(mset['lessie'], lessie) newsnoopy = Tuple(('Snoopy', 3)) self.assertIs(mset.setdefault('snoopy', newsnoopy), dogs[1][1]) self.assertIs(mset['snoopy'], dogs[1][1]) self.assertEqual(len(mset), 3) self.assertEqual(mset.count(Tuple(('Snoopy', 2))), 1)
def test__init_2(self): dogs = { 'cody1': Tuple(('Cody', 5)), 'snoopy': Tuple(('Snoopy', 2)), 'cody2': Tuple(('Cody', 5)) } mset = mapset_.Mapset(dogs) self.assertEqual(len(mset._data), len(dogs)) self.assertTrue(mset._data['cody1'] is dogs['cody1'] or mset._data['cody1'] is dogs['cody2']) self.assertTrue(mset._data['snoopy'] is dogs['snoopy']) self.assertTrue(mset._data['cody2'] is dogs['cody1'] or mset._data['cody2'] is dogs['cody2']) self.assertEqual(mset._set.count(Tuple(('Cody', 5))), 2) self.assertEqual(mset._set.count(Tuple(('Snoopy', 2))), 1)
def test__iter_1(self): dogs = (('cody1', Tuple(('Cody', 5))), ('snoopy', Tuple( ('Snoopy', 2))), ('cody2', Tuple(('Cody', 5)))) mset = mapset_.Mapset(dogs) keys = [d[0] for d in dogs] it = iter(mset) for i in range(3): x = next(it) self.assertTrue(x in keys) keys.remove(x) with self.assertRaises(StopIteration): next(it)
def test__popitem_1(self): dogs = [('cody1', Tuple(('Cody', 5))), ('snoopy', Tuple( ('Snoopy', 2))), ('cody2', Tuple(('Cody', 5)))] mset = mapset_.Mapset(dogs) self.assertEqual(len(mset), 3) for i in range(len(dogs), 0, -1): item = mset.popitem() index = dogs.index(item) self.assertEqual(len(mset), i - 1) self.assertTrue(item[0] not in mset) del dogs[index] with self.assertRaises(KeyError): mset.popitem()
def test__update_1(self): dogs = (('cody1', Tuple(('Cody', 5))), ('snoopy', Tuple( ('Snoopy', 2))), ('cody2', Tuple(('Cody', 5)))) mset = mapset_.Mapset() mset.update(dogs) self.assertEqual(len(mset), 3) self.assertIs(mset['cody1'], dogs[0][1]) self.assertIs(mset['snoopy'], dogs[1][1]) self.assertIs(mset['cody2'], dogs[0][1]) self.assertEqual(mset.count(Tuple(('Cody', 5))), 2) self.assertEqual(mset.count(Tuple(('Snoopy', 2))), 1) newsnoopy = Tuple(('Snoopy', 3)) lessie = Tuple(('Lessie', 4)) mset.update({'snoopy': newsnoopy, 'lessie': lessie}) self.assertEqual(len(mset), 4) self.assertIs(mset['snoopy'], newsnoopy) self.assertIs(mset['lessie'], lessie)
def test__delitem_1(self): dogs = { 'cody1': Tuple(('Cody', 5)), 'snoopy': Tuple(('Snoopy', 2)), 'cody2': Tuple(('Cody', 5)) } mset = mapset_.Mapset(dogs) self.assertEqual(len(mset), 3) del mset['cody1'] self.assertEqual(len(mset), 2) self.assertEqual(mset.count(Tuple(('Cody', 5))), 1) del mset['snoopy'] self.assertEqual(len(mset), 1) self.assertEqual(mset.count(Tuple(('Snoopy', 2))), 0) with self.assertRaises(KeyError): del mset['cody1'] with self.assertRaises(KeyError): del mset['snoopy'] with self.assertRaises(KeyError): del mset['foo']
def test__setitem_1(self): dogs = { 'cody1': Tuple(('Cody', 5)), 'snoopy': Tuple(('Snoopy', 2)), 'cody2': Tuple(('Cody', 5)) } mset = mapset_.Mapset() mset['cody1'] = dogs['cody1'] self.assertEqual(len(mset), 1) self.assertIs(mset['cody1'], dogs['cody1']) self.assertEqual(mset.count(Tuple(('Cody', 5))), 1) mset['snoopy'] = dogs['snoopy'] self.assertEqual(len(mset), 2) self.assertIs(mset['snoopy'], dogs['snoopy']) self.assertEqual(mset.count(Tuple(('Snoopy', 2))), 1) mset['cody2'] = dogs['cody2'] self.assertEqual(len(mset), 3) self.assertIs(mset['cody2'], dogs['cody1']) self.assertEqual(mset.count(Tuple(('Cody', 5))), 2) newsnoopy = Tuple(('Snoopy', 3)) mset['snoopy'] = newsnoopy self.assertEqual(len(mset), 3) self.assertIs(mset['snoopy'], newsnoopy) self.assertEqual(mset.count(Tuple(('Snoopy', 2))), 0) self.assertEqual(mset.count(Tuple(('Snoopy', 3))), 1) newcody = Tuple(('Cody', 6)) mset['cody2'] = newcody self.assertEqual(len(mset), 3) self.assertIs(mset['cody2'], newcody) self.assertEqual(mset.count(Tuple(('Cody', 5))), 1) self.assertEqual(mset.count(Tuple(('Cody', 6))), 1)
def test__pop_1(self): dogs = (('cody1', Tuple(('Cody', 5))), ('snoopy', Tuple( ('Snoopy', 2))), ('cody2', Tuple(('Cody', 5)))) mset = mapset_.Mapset(dogs) self.assertEqual(len(mset), 3) self.assertIs(mset.pop('cody1'), dogs[0][1]) self.assertEqual(len(mset), 2) self.assertTrue('cody1' not in mset) with self.assertRaises(KeyError): mset.pop('foo') self.assertIs(mset.pop('snoopy'), dogs[1][1]) self.assertEqual(len(mset), 1) self.assertTrue('snoopy' not in mset) self.assertIs(mset.pop('cody2'), dogs[0][1]) self.assertEqual(len(mset), 0) self.assertTrue('cody2' not in mset) with self.assertRaises(KeyError): mset.pop('cody2')
def test__repr_1(self): dogs = (('cody', Tuple(('Cody', 5))), ('snoopy', Tuple(('Snoopy', 2)))) mset = mapset_.Mapset(dogs) self.assertEqual(repr(mset), "Mapset(%s)" % repr(dict(mset.items())))