Commit 7dc8808446714a2e87c900a9b242bb919204a266

Authored by Adrian Chirita
1 parent 7c2016d3

noen feil i tester og kode. alt funker nå, trenger flere tester

src/oblig_2/Main.java
... ... @@ -10,5 +10,6 @@ public class Main {
10 10 twoWayLinkedList.addFirst("Vladimir");
11 11 twoWayLinkedList.addLast("Nikolay");
12 12 System.out.println(twoWayLinkedList.toString());
  13 + System.out.println(twoWayLinkedList.contains("Nikolay"));
13 14 }
14 15 }
... ...
src/oblig_2/TwoWayLinkedList.java
... ... @@ -16,9 +16,15 @@ public class TwoWayLinkedList<E> implements MyList<E> {
16 16 this.add(objects[i]);
17 17 }
18 18 public E getFirst() {
  19 + if(head == null) {
  20 + return null;
  21 + }
19 22 return head.element;
20 23 }
21 24 public E getLast() {
  25 + if(tail == null) {
  26 + return null;
  27 + }
22 28 return tail.element;
23 29 }
24 30 public void addFirst(E e) {
... ... @@ -58,7 +64,7 @@ public class TwoWayLinkedList<E> implements MyList<E> {
58 64 Node<E> toBeAdded = new Node<>(e);
59 65 Node<E> previous = head;
60 66  
61   - for(int i = 0; i < index; i++) {
  67 + for(int i = 1; i < index; i++) {
62 68 previous = previous.next;
63 69 }
64 70 Node<E> current = previous.next;
... ... @@ -117,7 +123,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
117 123 else {
118 124 Node<E> previous = head;
119 125  
120   - for(int i = 0; i < index; i++) {
  126 + for(int i = 1; i < index; i++) {
121 127 previous = previous.next;
122 128 }
123 129  
... ... @@ -168,7 +174,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
168 174 }
169 175 else {
170 176 Node<E> current = head;
171   - for (int i = 0; i < index; i++) {
  177 + for (int i = 1; i < index; i++) {
172 178 current = current.next;
173 179 }
174 180 return current.next.element;
... ... @@ -193,7 +199,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
193 199 }
194 200 else {
195 201 Node<E> current = tail;
196   - for(int i = size-1; i >= 0; i--) {
  202 + for(int i = size-1; i > 0; i--) {
197 203 if(current.element.equals(e)) return i;
198 204 current = current.previous;
199 205 }
... ... @@ -216,7 +222,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
216 222 }
217 223 else {
218 224 Node<E> previous = head;
219   - for (int i = 0; i < index; i++) {
  225 + for (int i = 1; i < index; i++) {
220 226 previous = previous.next;
221 227 }
222 228 E replacedElement = previous.next.element;
... ... @@ -264,7 +270,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
264 270 current = tail;
265 271 }
266 272 else {
267   - for (int i = 0; i < index; i++)
  273 + for (int i = 1; i < index; i++)
268 274 current = current.next;
269 275 }
270 276 this.index = index;
... ...
src/test/TwoWayLinkedListTest.java
... ... @@ -71,73 +71,71 @@ public class TwoWayLinkedListTest {
71 71 }
72 72 @Test
73 73 public void remove() {
74   - data.add("Kitkat");
75   - data.add("Pie");
76   - data.add("Oreo");
  74 + data.addLast("Kitkat");
  75 + data.addLast("Pie");
  76 + data.addLast("Oreo");
77 77 assertEquals("Oreo", data.remove(2));
78 78 assertEquals("Pie", data.remove(1));
79 79 assertEquals("Kitkat",data.remove(0));
80 80 }
81 81 @Test
82 82 public void addIndex() {
83   - data.add(0,"Vitkat");
84   - data.add(1,"Kitkat");
85   - data.add(2,"Pie");
86   - data.add(3,"Oreo");
  83 + data.addLast("Vitkat");
  84 + data.addLast("Kitkat");
  85 + data.addLast("Pie");
  86 + data.addLast("Oreo");
87 87 assertEquals("Pie",data.get(2));
88 88 }
89 89 @Test
90 90 public void clear() {
91   - data.add("Vitkat");
92   - data.add("Kitkat");
93   - data.add("Pie");
94   - data.add("Oreo");
  91 + data.addLast("Vitkat");
  92 + data.addLast("Kitkat");
  93 + data.addLast("Pie");
  94 + data.addLast("Oreo");
95 95 assertEquals("Pie",data.get(2));
96 96 data.clear();
97   - assertEquals(null,data.getFirst());
  97 + assertEquals(0,data.size());
98 98 }
99 99 @Test
100 100 public void clearList() {
101   - data.add(0,"Vitkat");
102   - data.add(1,"Kitkat");
103   - data.add(2,"Pie");
104   - data.add(3,"Oreo");
  101 + data.addLast("Vitkat");
  102 + data.addLast("Kitkat");
  103 + data.addLast("Pie");
  104 + data.addLast("Oreo");
105 105 assertEquals("Pie",data.get(2));
106 106 data.clear();
107   - assertEquals(null,data.get(0));
108   - assertNotEquals("Kitkat",data.get(1));
109   - assertEquals(null,data.get(2));
110   - assertEquals(null,data.get(3));
  107 + assertEquals(0,data.size());
  108 + assertNull(data.getFirst(),data.getLast());
111 109 }
112 110 @Test
113 111 public void contains() {
114   - data.add(0,"Vitkat");
115   - data.add(1,"Kitkat");
116   - data.add(2,"Pie");
117   - data.add(3,"Oreo");
118   -
119   - assertEquals(true,data.contains("Oreo"));
120   - assertNotEquals(true,data.contains("Snikers"));
  112 + data.addLast("Vitkat");
  113 + data.addLast("Kitkat");
  114 + data.addLast("Pie");
  115 + data.addLast("Oreo");
  116 +
  117 + assertThat(data.contains("Oreo"),is(true));
  118 + assertFalse(data.contains("Snikers"));
121 119 }
122 120 @Test
123 121 public void indexOf() {
124   - data.add(0,"Vitkat");
125   - data.add(1,"Kitkat");
126   - data.add(2,"Pie");
127   - data.add(3,"Oreo");
128   - data.add(4,"Vitkat");
129   - data.add(2,"Pie");
  122 + data.addLast("Vitkat");
  123 + data.addLast("Kitkat");
  124 + data.addLast("Pie");
  125 + data.addLast("Oreo");
  126 + data.addLast("Vitkat");
  127 + data.addLast("Pie");
130 128 assertEquals(0,data.indexOf("Vitkat"));
131 129 assertEquals(2,data.indexOf("Pie"));
132 130 }
133 131 @Test
134 132 public void LastindexOf() {
135   - data.add(0,"Vitkat");
136   - data.add(1,"Kitkat");
137   - data.add(2,"Pie");
138   - data.add(3,"Oreo");
139   - data.add(4,"Vitkat");
140   - data.add(5,"Pie");
  133 + data.addLast("Vitkat");
  134 + data.addLast("Kitkat");
  135 + data.addLast("Pie");
  136 + data.addLast("Oreo");
  137 + data.addLast("Vitkat");
  138 + data.addLast("Pie");
141 139 assertEquals(4,data.lastIndexOf("Vitkat"));
142 140 assertEquals(5,data.lastIndexOf("Pie"));
143 141 }
... ...