def test_remove(self, list_): """Test removing an item from the set""" oset = weakref_utilities.OrderedSet(list_) self.assertEqual(len(oset), len(list_)) for item in list_: oset.remove(item) self.assertNotIn(item, oset) self.assertEqual(len(oset), 0)
def test_gc(self): """Test that items are correctly garbage collected""" # We use an ordered set to test garbage collecting. oset = weakref_utilities.OrderedSet() # Adding the ordered set should work like normal. wset = weakref_utilities.OrderedWeakSet() wset.add(oset) self.assertIn(oset, wset) # Deleting the ordered set should also remove it from the # weakref set. del oset self.assertEqual(len(wset), 0)
def test_str_repr(self, list_): oset = weakref_utilities.OrderedSet(list_) # The string representation should be the same as a list with # parenthesis. self.assertEqual( str(oset), str(list_).replace('[', '(').replace(']', ')'), ) # repr should produce the same output as str. self.assertEqual( repr(oset), str(oset), )
def test_discard(self, list_): """Test discarding an item from the set""" a, b = list_ oset = weakref_utilities.OrderedSet([a]) # Discarding something not in the set should not cause an error. # Anything in the set should remain there. oset.discard(b) self.assertIn(a, oset) self.assertNotIn(b, oset) # Discarding something that is in the set should remove it. oset.discard(a) self.assertNotIn(a, oset) self.assertNotIn(b, oset)
def test_add(self, list_): """Test adding an item to the set""" oset = weakref_utilities.OrderedSet() self.assertEqual(len(oset), 0) for item in list_: item_in = item in oset count = len(oset) oset.add(item) if item_in: self.assertEqual(len(oset), count) else: self.assertEqual(len(oset), count + 1) self.assertEqual(len(oset), len(set(list_)))
def test_pop(self, list_): """Test popping an item from the end of the set""" oset = weakref_utilities.OrderedSet(list_) for item in reversed(list_): self.assertEqual(oset.pop(), item)
def test_clear(self, list_): """Test clearing the set""" oset = weakref_utilities.OrderedSet(list_) self.assertEqual(len(oset), len(list_)) oset.clear() self.assertEqual(len(oset), 0)
def test_contains(self, list_): """Test querying the presence of an item in the set""" oset = weakref_utilities.OrderedSet(list_) for item in list_: self.assertIn(item, oset)
def test_argument_and_order(self, list_): """Test passing an argument to the constructor""" oset = weakref_utilities.OrderedSet(list_) self.assertSequenceEqual(list(oset), list_)