Commit 16554b41a5c5315b7721b67e4115ca8392716bb6

Authored by Adrian Chirita
1 parent 9dabe795

tester innhold er forandret igjen, flere tester.

src/oblig_2/TwoWayLinkedList.java
... ... @@ -342,7 +342,7 @@ public class TwoWayLinkedList<E> implements MyList<E> {
342 342 }
343 343 @Override
344 344 public void remove() {
345   - if (current.previous == null || !removeCalled) {
  345 + if (current.previous == null || removeCalled) {
346 346 throw new IllegalStateException();
347 347 }
348 348 else if (current.previous.previous == null) {
... ...
src/test/TwoWayLinkedListTest.java
... ... @@ -17,49 +17,46 @@ public class TwoWayLinkedListTest {
17 17 @Before
18 18 public void setUp(){
19 19 data = new TwoWayLinkedList<>();
  20 + data.add("Vitkat");
  21 + data.add("Kitkat");
  22 + data.add("Pie");
  23 + data.add("Oreo");
  24 + data.add("Vitkat");
  25 + data.add("Pie");
20 26 }
21 27 @Test
22 28 public void getFirst() {
23   - String result = "Kitkat";
24   - data.add(result);
25   - data.add("Pie");
26   - data.add("KitKat");
27   - assertEquals(result, data.getFirst());
  29 + assertEquals("Vitkat", data.getFirst());
28 30 }
29 31 @Test
30   - public void getLast() {
31   - String result = "Oreo";
32   - data.add("Marshmallow");
33   - data.add("Kitkat");
34   - data.add(result);
35   - assertEquals(result, data.getLast());
  32 + public void getLast() {
  33 + assertEquals("Pie", data.getLast());
36 34 }
37 35 @Test
38   - public void addFirst() {
39   - String result = "Oreo";
40   - data.addFirst(result);
41   - assertEquals(result, data.getFirst());
  36 + public void addFirst() {
  37 + data.addFirst("newFirst");
  38 + assertEquals("newFirst", data.getFirst());
42 39 }
43 40 @Test
44   - public void addLast() {
45   - String result = "Oreo";
46   - data.addLast(result);
47   - assertEquals(result, data.getLast());
  41 + public void addLast() {
  42 + data.addLast("newLast");
  43 + assertEquals("newLast", data.getLast());
48 44 }
49 45 @Test
50   - public void add() {
51   - int n = 5;
52   - for (int i = 1; i <= n; i++) data.addLast("test"+i);
53   - assertEquals(n, data.size());
54   - assertEquals("test"+n, data.getLast());
  46 + public void addMethodPutsElementInLastPosition() {
  47 + data.add("1");
  48 + assertEquals("1", data.getLast());
55 49 }
56 50 @Test
57   - public void removeFirst() {
58   - data.add("Kitkat");
59   - data.add("Pie");
60   - data.add("Oreo");
61   - assertEquals("Kitkat",data.removeFirst());
62   - assertNotEquals("Kitkat",data.getFirst());
  51 + public void addMethodOnEmptyListPutsElementInFirstPosition() {
  52 + data.clear();
  53 + data.add("0");
  54 + assertEquals("0", data.getFirst());
  55 + }
  56 + @Test
  57 + public void removeFirst() {
  58 + assertEquals("Vitkat",data.removeFirst());
  59 + assertEquals("Kitkat",data.getFirst());
63 60 }
64 61 @Test
65 62 public void removeLast() {
... ... @@ -70,89 +67,49 @@ public class TwoWayLinkedListTest {
70 67 assertNotEquals("Oreo",data.getLast());
71 68 }
72 69 @Test
73   - public void remove() {
74   - data.add("Kitkat");
75   - data.add("Pie");
76   - data.add("Oreo");
77   - assertEquals("Oreo", data.remove(2));
78   - assertEquals("Pie", data.remove(1));
79   - assertEquals("Kitkat",data.remove(0));
  70 + public void remove() {
  71 + assertEquals("Pie", data.remove(2));
  72 + assertEquals("Kitkat", data.remove(1));
  73 + assertEquals("Vitkat",data.remove(0));
80 74 }
81 75 @Test
82 76 public void addIndex() {
83   - data.add("Vitkat");
84   - data.add("Kitkat");
85   - data.add("Pie");
86   - data.add("Oreo");
87   - assertEquals("Pie",data.get(2));
88   - }
89   - @Test
90   - public void clear() {
91   - data.add("Vitkat");
92   - data.add("Kitkat");
93   - data.add("Pie");
94   - data.add("Oreo");
95   - assertEquals("Pie",data.get(2));
96   - data.clear();
97   - assertEquals(0,data.size());
98   - }
  77 + data.add(3,"new");
  78 + assertEquals("new",data.get(3));
  79 + }
99 80 @Test
100   - public void clearList() {
101   - data.add("Vitkat");
102   - data.add("Kitkat");
103   - data.add("Pie");
104   - data.add("Oreo");
105   - assertEquals("Pie",data.get(2));
  81 + public void clearList() {
106 82 data.clear();
107 83 assertEquals(0,data.size());
108 84 assertNull(data.getFirst(),data.getLast());
109 85 }
110 86 @Test
111   - public void contains() {
112   - data.add("Vitkat");
113   - data.add("Kitkat");
114   - data.add("Pie");
115   - data.add("Oreo");
116   -
  87 + public void contains() {
117 88 assertThat(data.contains("Oreo"),is(true));
118 89 assertFalse(data.contains("Snikers"));
119 90 }
120 91 @Test
121   - public void indexOf() {
122   - data.add("Vitkat");
123   - data.add("Kitkat");
124   - data.add("Pie");
125   - data.add("Oreo");
126   - data.add("Vitkat");
127   - data.add("Pie");
  92 + public void indexOf() {
128 93 assertEquals(0,data.indexOf("Vitkat"));
129 94 assertEquals(2,data.indexOf("Pie"));
130 95 }
131 96 @Test
132   - public void LastindexOf() {
133   - data.add("Vitkat");
134   - data.add("Kitkat");
135   - data.add("Pie");
136   - data.add("Oreo");
137   - data.add("Vitkat");
138   - data.add("Pie");
  97 + public void LastindexOf() {
139 98 assertEquals(4,data.lastIndexOf("Vitkat"));
140 99 assertEquals(5,data.lastIndexOf("Pie"));
141 100 }
142 101 @Test
143   - public void toObjectArrayMethodInMyListIsOk() {
144   - data.add("Kitkat");
145   - data.add("Pie");
146   - Object[] dataArray = data.toArray();
147   - assertThat(dataArray[0],is("Kitkat"));
  102 + public void firstCallNextMethodInIteratorReturnsFirstElement() {
  103 + itr = data.iterator();
  104 + assertThat(itr.next(),is("Vitkat"));
148 105 }
149   - @Test
150   - public void genericToArrayMethodInMyListIsOk() {
151   - data.add("Kitkat");
152   - data.add("Pie");
153   - String[] array = {"one","two",null,null};
154   - String[] dataInArray = data.toArray(array);
155   - assertThat(dataInArray[0],is("Kitkat"));
  106 + @Test(expected = IllegalStateException.class)
  107 + public void callingRemoveMethodTwiceFromIteratorThrowsException() {
  108 + itr = data.iterator();
  109 + itr.next();
  110 + itr.remove();
  111 + itr.remove();
  112 + assertThat(data.getFirst(),is("Kitkat"));
156 113 }
157 114 @Test
158 115 public void firstCallNextIteratorMethodReturnsFirstElement() {
... ... @@ -163,8 +120,28 @@ public class TwoWayLinkedListTest {
163 120 }
164 121 @Test(expected = java.util.NoSuchElementException.class)
165 122 public void callNextIteratorMethodOnEmptyListThrowsException() {
  123 + data.clear();
166 124 itr = data.iterator();
167 125 itr.next();
168   - }
  126 + }
  127 + @Test
  128 + public void toObjectArrayMethodInMyListIsOk() {
  129 + Object[] dataArray = data.toArray();
  130 + assertThat(dataArray[3],is("Oreo"));
  131 + }
  132 + @Test
  133 + public void genericToArrayMethodInMyListIsOk() {
  134 + data.clear();
  135 + data.add("Kitkat");
  136 + data.add("Pie");
  137 +
  138 + String[] array = {"one","two"};
  139 + String[] dataInArray = data.toArray(array);
  140 + assertThat(dataInArray[0],is("Kitkat"));
  141 +
  142 + String[] newArray = {"one"};
  143 + String[] newDataInArray = data.toArray(array);
  144 + assertThat(newDataInArray[0],is("Kitkat"));
  145 + }
169 146  
170 147 }
171 148 \ No newline at end of file
... ...