from unittest.case import TestCase from typing import List from tudelft.utilities.immutablelist.ListWithRemove import ListWithRemove from tudelft.utilities.immutablelist.FixedList import FixedList from tudelft.utilities.immutablelist.ImmutableList import ImmutableList import random class ListWithRemoveTest(TestCase): data:List[str] = [ "a", "b", "c", "d", "e" ] def testRemoveTest0(self): remList:ListWithRemove[str] = ListWithRemove( FixedList(self.data), set()) self.assertEqual("a", remList.get(0)) remList = remList.remove(0) self.assertEqual("b", remList.get(0)) self.assertEquals(4, remList.size()) def testRemoveTest1(self): remList:ListWithRemove[str] = ListWithRemove( FixedList(self.data), set()) self.assertEqual("a", remList.get(0)) remList = remList.remove(1) self.assertEqual("a", remList.get(0)) self.assertEqual(4, remList.size()) def testRemoveTest1b(self): remList:ListWithRemove[str] = ListWithRemove( FixedList(self.data), set()) self.assertEqual("b", remList.get(1)) remList = remList.remove(1); self.assertEqual("c", remList.get(1)) self.assertEqual(4, remList.size()) # def testRemoveTestLarge(self): # PermutationsWithReturn perm = new PermutationsWithReturn<>( # new FixedList<>(data), 10); # // assertEquals(BigInteger.valueOf(9765625), perm.size()); # ListWithRemove> permutation = new ListWithRemove<>( # perm, Collections.emptySet()); # System.out.println(permutation); # permutation.remove(BigInteger.valueOf(838232)); # System.out.println(permutation); # # } def testRemoveListMultipleTimes(self): for n in range(20): self.removeRandomTillEmpty() def removeRandomTillEmpty(self): ''' remove random data items till lists are empty ''' alist:ImmutableList[str] = FixedList(self.data) remlist:ListWithRemove[str] = ListWithRemove(alist,set()) copy:List[str] = list(self.data) while copy!=[]: n:int = random.randint(0,len(copy)-1) remlist = remlist.remove(n) copy.pop(n) self.checkListsEqual(remlist, copy) def testImmutability(self): copy:List[str] = list(self.data) alist:ImmutableList[str] = FixedList(copy) copy.pop(1) self.checkListsEqual(alist, self.data) def checkListsEqual(self, list1:ImmutableList[str] , list2:List[str] ) : self.assertEqual(list1.size(), len(list2)) for n in range(len(list2)): self.assertEqual(list1.get(n), list2[n])