def test_list_item2(self): """ Test the _ListItem base class in the TrackedList container """ my_container = TrackedList() objects = [_ListItem() for i in range(100)] # Assign it a list container for obj in objects: obj.list = my_container my_container.append(obj) for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i) # Delete 10 random, but unique, objects from my_container choices = set() while len(choices) < 10: choices.add(random.choice(range(100))) choices = sorted(list(choices), reverse=True) for idx in choices: my_container.pop(idx) # Now check that the index of those 10 items are -1 for i in choices: self.assertEqual(objects[i].idx, -1) # Now check that the index of the remaining objects are correctly # updated for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i) # Now add those 10 objects back to the end of the list, and make sure # that the indexing is still correct, then delete those same items using # "del" instead of "pop" and make sure it still works for i in choices: my_container.append(objects[i]) objects = [obj for obj in my_container] # Re-sync ordering for i in choices: del my_container[i] for i in choices: self.assertEqual(objects[i].idx, -1) for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i) # Now try deleting a slice objects = [obj for obj in my_container] del my_container[::2] self.assertEqual(len(my_container), 45) for i in range(90): if i % 2 == 0: self.assertEqual(objects[i].idx, -1) else: self.assertEqual(objects[i].idx, i//2) for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i)
def test_list_item1(self): """ Test the _ListItem base class in standard containers """ my_container = list() objects = [_ListItem() for i in range(100)] # Assign it a list container for obj in objects: obj.list = my_container my_container.append(obj) for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i) # Delete 10 random, but unique, objects from my_container choices = set() while len(choices) < 10: choices.add(random.choice(range(100))) choices = sorted(list(choices), reverse=True) for idx in choices: my_container.pop(idx) # Now check that the index of those 10 items are -1 for i in choices: self.assertEqual(objects[i].idx, -1) # Now check that the index of the remaining objects are correctly # updated for i, obj in enumerate(my_container): self.assertEqual(obj.idx, i)