source: utilitiespy/test/immutablelist/ListWithRemoveTest.py@ 1271

Last change on this file since 1271 was 1002, checked in by wouter, 3 months ago

#340 added tests, found issue with translation, fixed, all tests OK, redeployed 1.0.7.

File size: 2.7 KB
Line 
1
2
3from unittest.case import TestCase
4from typing import List
5from tudelft.utilities.immutablelist.ListWithRemove import ListWithRemove
6from tudelft.utilities.immutablelist.FixedList import FixedList
7from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
8import random
9
10class ListWithRemoveTest(TestCase):
11
12 data:List[str] = [ "a", "b", "c", "d", "e" ]
13
14 def testRemoveTest0(self):
15 remList:ListWithRemove[str] = ListWithRemove(
16 FixedList(self.data), set())
17 self.assertEqual("a", remList.get(0))
18 remList = remList.remove(0)
19 self.assertEqual("b", remList.get(0))
20 self.assertEquals(4, remList.size())
21
22 def testRemoveTest1(self):
23 remList:ListWithRemove[str] = ListWithRemove(
24 FixedList(self.data), set())
25 self.assertEqual("a", remList.get(0))
26 remList = remList.remove(1)
27 self.assertEqual("a", remList.get(0))
28 self.assertEqual(4, remList.size())
29
30 def testRemoveTest1b(self):
31 remList:ListWithRemove[str] = ListWithRemove(
32 FixedList(self.data), set())
33 self.assertEqual("b", remList.get(1))
34 remList = remList.remove(1);
35 self.assertEqual("c", remList.get(1))
36 self.assertEqual(4, remList.size())
37
38 # def testRemoveTestLarge(self):
39 # PermutationsWithReturn<String> perm = new PermutationsWithReturn<>(
40 # new FixedList<>(data), 10);
41 # // assertEquals(BigInteger.valueOf(9765625), perm.size());
42 # ListWithRemove<ImmutableList<String>> permutation = new ListWithRemove<>(
43 # perm, Collections.emptySet());
44 # System.out.println(permutation);
45 # permutation.remove(BigInteger.valueOf(838232));
46 # System.out.println(permutation);
47 #
48 # }
49
50 def testRemoveListMultipleTimes(self):
51
52 for n in range(20):
53 self.removeRandomTillEmpty()
54
55 def removeRandomTillEmpty(self):
56 '''
57 remove random data items till lists are empty
58 '''
59 alist:ImmutableList[str] = FixedList(self.data)
60 remlist:ListWithRemove[str] = ListWithRemove(alist,set())
61 copy:List[str] = list(self.data)
62
63 while copy!=[]:
64 n:int = random.randint(0,len(copy)-1)
65 remlist = remlist.remove(n)
66 copy.pop(n)
67 self.checkListsEqual(remlist, copy)
68
69 def testImmutability(self):
70 copy:List[str] = list(self.data)
71
72 alist:ImmutableList[str] = FixedList(copy)
73 copy.pop(1)
74 self.checkListsEqual(alist, self.data)
75
76 def checkListsEqual(self, list1:ImmutableList[str] ,
77 list2:List[str] ) :
78 self.assertEqual(list1.size(), len(list2))
79 for n in range(len(list2)):
80 self.assertEqual(list1.get(n), list2[n])
Note: See TracBrowser for help on using the repository browser.