source: src/test/java/list/ListWithRemoveTest.java@ 127

Last change on this file since 127 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.9 KB
Line 
1package list;
2
3import static org.junit.Assert.assertEquals;
4
5import java.math.BigInteger;
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.List;
9import java.util.Random;
10
11import org.junit.Test;
12
13import genius.core.list.ImArrayList;
14import genius.core.list.ImmutableList;
15import genius.core.list.ListWithRemove;
16import genius.core.list.PermutationsWithReturn;
17
18public class ListWithRemoveTest {
19
20 final static List<String> data = Arrays.asList(new String[] { "a", "b", "c", "d", "e" });
21
22 @Test
23 public void RemoveTest0() {
24 ListWithRemove<String> remList = new ListWithRemove<>(new ImArrayList<>(data));
25 assertEquals("a", remList.get(BigInteger.ZERO));
26 remList = remList.remove(BigInteger.ZERO);
27 assertEquals("b", remList.get(BigInteger.ZERO));
28 assertEquals(BigInteger.valueOf(4), remList.size());
29 }
30
31 @Test
32 public void RemoveTest1() {
33 ListWithRemove<String> remList = new ListWithRemove<>(new ImArrayList<>(data));
34 assertEquals("a", remList.get(BigInteger.ZERO));
35 remList = remList.remove(BigInteger.ONE);
36 assertEquals("a", remList.get(BigInteger.ZERO));
37 assertEquals(BigInteger.valueOf(4), remList.size());
38 }
39
40 @Test
41 public void RemoveTest1b() {
42 ListWithRemove<String> remList = new ListWithRemove<>(new ImArrayList<>(data));
43 assertEquals("b", remList.get(BigInteger.ONE));
44 remList = remList.remove(BigInteger.ONE);
45 assertEquals("c", remList.get(BigInteger.ONE));
46 assertEquals(BigInteger.valueOf(4), remList.size());
47 }
48
49 @Test
50 public void RemoveTestLarge() {
51 PermutationsWithReturn<String> perm = new PermutationsWithReturn<>(new ImArrayList<>(data), 10);
52 // assertEquals(BigInteger.valueOf(9765625), perm.size());
53 ListWithRemove<ImmutableList<String>> permutation = new ListWithRemove<>(perm);
54 System.out.println(permutation);
55 permutation.remove(BigInteger.valueOf(838232));
56 System.out.println(permutation);
57
58 }
59
60 @Test
61 public void testRemoveListMultipleTimes() {
62
63 for (int n = 0; n < 20; n++) {
64 testRemoveRandomTillEmpty();
65 }
66 }
67
68 /**
69 * remove random data items till lists are empty
70 */
71 @Test
72 public void testRemoveRandomTillEmpty() {
73 ImmutableList<String> list = new ImArrayList<>(data);
74 ListWithRemove<String> remlist = new ListWithRemove<>(list);
75 ArrayList<String> copy = new ArrayList<>(data);
76
77 while (!copy.isEmpty()) {
78 Random r = new Random();
79
80 int n = r.nextInt(copy.size());
81 remlist = remlist.remove(BigInteger.valueOf(n));
82 copy.remove(n);
83
84 checkListsEqual(remlist, copy);
85 }
86
87 }
88
89 @Test
90 public void testImmutability() {
91 List<String> copy = new ArrayList<>(data);
92
93 ImmutableList<String> list = new ImArrayList<>(copy);
94 copy.remove(1);
95 checkListsEqual(list, data);
96 }
97
98 private void checkListsEqual(ImmutableList<String> list1, List<String> list2) {
99 assertEquals(list1.size().intValue(), list2.size());
100 for (int n = 0; n < list2.size(); n++) {
101 assertEquals(list1.get(BigInteger.valueOf(n)), list2.get(n));
102 }
103
104 }
105}
Note: See TracBrowser for help on using the repository browser.