Commit e7a8b853dd081117001624e7db26bc34f7d41a25

Authored by Adrian Chirita
1 parent 2e93a1bc

små fiks

src/oblig_2/Main.java
... ... @@ -24,5 +24,7 @@ public class Main {
24 24  
25 25 System.out.println(i.next());
26 26 System.out.println(i.previous());
  27 +
  28 +
27 29 }
28 30 }
... ...
src/oblig_2/Tree.java 0 → 100644
  1 +package oblig_2;
  2 +
  3 +import java.util.Collection;
  4 +
  5 +public interface Tree<E> extends Collection<E> {
  6 + public boolean search(E e);
  7 + public boolean insert(E e);
  8 + public boolean delete(E e);
  9 + public int getSize();
  10 + public default void inorder() {
  11 + }
  12 + public default void postorder() {
  13 + }
  14 + public default void preorder() {
  15 + }
  16 + @Override
  17 + public default boolean isEmpty() {
  18 + return this.size() == 0;
  19 + }
  20 + @Override
  21 + public default boolean contains(Object e) {
  22 + return search((E)e);
  23 + }
  24 + @Override
  25 + public default boolean add(E e) {
  26 + return insert(e);
  27 + }
  28 + @Override
  29 + public default boolean remove(Object e) {
  30 + return delete((E) e);
  31 + }
  32 + @Override
  33 + public default int size() {
  34 + return getSize();
  35 + }
  36 + @Override
  37 + public default boolean containsAll(Collection<?> c) {
  38 + return false;
  39 + }
  40 + @Override
  41 + public default boolean addAll(Collection<? extends E> c) {
  42 + return false;
  43 + }
  44 + @Override
  45 + public default boolean removeAll(Collection<?> c) {
  46 + return false;
  47 + }
  48 + @Override
  49 + public default boolean retainAll(Collection<?> c) {
  50 + return false;
  51 + }
  52 + @Override
  53 + public default Object[] toArray() {
  54 + return null;
  55 + }
  56 + @Override
  57 + public default <T> T[] toArray(T[] array) {
  58 + return null;
  59 + }
  60 +}
... ...
src/oblig_2/TwoWayLinkedList.java
... ... @@ -6,29 +6,29 @@ import oblig_2.TwoWayLinkedList;
6 6  
7 7 public class TwoWayLinkedList<E> implements MyList<E> {
8 8 private Node<E> head, tail;
9   - private int size;
  9 + private int size = 0;
10 10  
11   - public TwoWayLinkedList() {
12   - head = null;
13   - tail = null;
14   - size = 0;
15   - }
  11 + public TwoWayLinkedList() { }
  12 +// Iuliia
16 13 public TwoWayLinkedList(E[] objects) {
17 14 for (int i = 0; i < objects.length; i++)
18 15 this.add(objects[i]);
19   - }
  16 + }
  17 +// Iuliia
20 18 public E getFirst() {
21 19 if(head == null) {
22 20 return null;
23 21 }
24 22 return head.element;
25   - }
  23 + }
  24 +// Georgii
26 25 public E getLast() {
27 26 if(tail == null) {
28 27 return null;
29 28 }
30 29 return tail.element;
31 30 }
  31 +// Iuliia
32 32 public void addFirst(E e) {
33 33 Node<E> currentFirst = head;
34 34 head = new Node<>(e);
... ... @@ -39,7 +39,8 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
39 39 currentFirst.previous = head;
40 40 }
41 41 size++;
42   - }
  42 + }
  43 + // Georgii
43 44 public void addLast(E e) {
44 45 Node<E> newNode = new Node<>(e);
45 46 if (tail == null) {
... ... @@ -52,6 +53,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
52 53 }
53 54 size++;
54 55 }
  56 +// Aleksei
55 57 public void add(int index, E e) {
56 58 if (index < 0 || index > size) {
57 59 throw new IndexOutOfBoundsException();
... ... @@ -77,7 +79,8 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
77 79 current.previous = toBeAdded;
78 80 size++;
79 81 }
80   - }
  82 + }
  83 +// Georgii
81 84 public E removeFirst() {
82 85 if (size == 0) {
83 86 return null;
... ... @@ -94,6 +97,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
94 97 return temp;
95 98 }
96 99 }
  100 + // Georgii
97 101 public E removeLast() {
98 102 if (size == 0) {
99 103 return null;
... ... @@ -112,6 +116,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
112 116 return temp;
113 117 }
114 118 }
  119 +// Iuliia
115 120 public E remove(int index) {
116 121 if(index < 0 || index >= size) {
117 122 throw new IndexOutOfBoundsException();
... ... @@ -137,6 +142,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
137 142 return current.element;
138 143 }
139 144 }
  145 + // Georgii
140 146 @Override
141 147 public String toString() {
142 148 StringBuilder result = new StringBuilder("[");
... ... @@ -154,14 +160,17 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
154 160 }
155 161 return result.toString();
156 162 }
  163 +// Georgii
157 164 public void clear() {
158 165 head = null;
159 166 tail = null;
160 167 size = 0;
161   - }
  168 + }
  169 +// Adrian
162 170 public boolean contains(Object e) {
163 171 return indexOf(e) >= 0 ? true:false ;
164   - }
  172 + }
  173 + // Aleksei
165 174 public E get(int index) {
166 175 if(index < 0 || index >= size) {
167 176 throw new IndexOutOfBoundsException();
... ... @@ -183,6 +192,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
183 192 return current.next.element;
184 193 }
185 194 }
  195 +// Aleksei
186 196 public int indexOf(Object e) {
187 197 if (head == null) {
188 198 return -1;
... ... @@ -196,6 +206,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
196 206 }
197 207 return -1;
198 208 }
  209 +// Aleksei
199 210 public int lastIndexOf(Object e) {
200 211 if(tail == null) {
201 212 return -1;
... ... @@ -232,10 +243,12 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
232 243 previous.next.element = e;
233 244 return replacedElement;
234 245 }
235   - }
  246 + }
  247 +// Adrian
236 248 public ListIterator<E> listIterator() {
237 249 return listIterator(0);
238   - }
  250 + }
  251 +// Adrian
239 252 public ListIterator<E> listIterator(int index) {
240 253 if (index < 0 || index > size) {
241 254 throw new IndexOutOfBoundsException(
... ... @@ -245,10 +258,12 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
245 258 else
246 259 return new LinkedListIterator(index);
247 260 }
  261 + // Adrian
248 262 @Override
249 263 public Iterator<E> iterator() {
250 264 return new LinkedListIterator();
251   - }
  265 + }
  266 + // Adrian
252 267 private class LinkedListIterator implements ListIterator<E> {
253 268 private Node<E> previous;
254 269 private Node<E> next = head;
... ... @@ -325,7 +340,8 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
325 340 @Override
326 341 public void add(E e) {
327 342 }
328   - }
  343 + }
  344 +// Iuliia
329 345 private class Node<E> {
330 346 E element;
331 347 Node<E> next;
... ... @@ -335,6 +351,7 @@ public class TwoWayLinkedList&lt;E&gt; implements MyList&lt;E&gt; {
335 351 element = o;
336 352 }
337 353 }
  354 +// Iuliia
338 355 @Override
339 356 public int size() {
340 357 return size;
... ...
src/test/TwoWayLinkedListTest.java
... ... @@ -21,93 +21,139 @@ public class TwoWayLinkedListTest {
21 21 String[] objects = {"one","two","three","four","one","two"};
22 22 data = new TwoWayLinkedList<String>(objects);
23 23 }
  24 + // Iuliia
  25 + @Test
  26 + public void sizeIsOk() {
  27 + assertEquals(6, data.size());
  28 + }
  29 +// Iuliia
24 30 @Test
25 31 public void getFirstReturnsFirstElement() {
26 32 assertEquals("one", data.getFirst());
27 33 }
  34 +// Iuliia
28 35 @Test
29 36 public void getFirstFromEmptyListReturnsNull() {
30 37 data.clear();
31 38 assertNull(data.getFirst());
32 39 }
  40 + // Georgii
33 41 @Test
34 42 public void getLastReturnsLastElement() {
35 43 assertEquals("two", data.getLast());
36 44 }
  45 + // Georgii
37 46 @Test
38 47 public void getLastFromEmptyListReturnsNull() {
39 48 data.clear();
40 49 assertNull(data.getLast());
41 50 }
  51 +// Iuliia
42 52 @Test
43   - public void addFirstPutsElementInFirstPosition() {
  53 + public void addFirstPutsInFirstPosition() {
44 54 data.addFirst("newFirst");
45   - assertEquals("newFirst", data.getFirst());
46   - data.clear();
47   - data.addFirst("anotherFirst");
48   - assertEquals("anotherFirst", data.getFirst());
  55 + assertEquals("newFirst", data.getFirst());
49 56 }
  57 + // Georgii
50 58 @Test
51   - public void addLastPutsElementInLastPosition() {
  59 + public void addLastPutsInLastPosition() {
52 60 data.addLast("newLast");
53   - assertEquals("newLast", data.getLast());
54   - data.clear();
55   - data.addFirst("anotherLast");
56   - assertEquals("anotherLast", data.getLast());
  61 + assertEquals("newLast", data.getLast());
57 62 }
  63 + // Aleksei
58 64 @Test
59 65 public void addMethodPutsElementInLastPosition() {
60 66 data.add("acer");
61 67 assertEquals("acer", data.getLast());
62 68 }
  69 + // Aleksei
63 70 @Test
64 71 public void addMethodOnEmptyListPutsElementInBothHeadAndTailPosition() {
65 72 data.clear();
66 73 data.add("element");
67   - assertEquals("element", data.getFirst());
68   - assertEquals("element", data.getLast());
69   - }
  74 + assertThat("element", allOf(is(data.getFirst()),is(data.getLast())));
  75 + }
  76 + // Aleksei
  77 + @Test
  78 + public void addToGivenIndexIsOk() {
  79 + data.add(3,"new");
  80 + assertEquals("new",data.get(3));
  81 + }
  82 +// Aleksei
  83 + @Test(expected = IndexOutOfBoundsException.class)
  84 + public void addToInvalidIndexThrowsException() {
  85 + data.add(data.size()+1,"Pluto");
  86 + }
  87 + // Georgii
70 88 @Test
71 89 public void removeFirstRemovesFirstElement() {
72 90 assertEquals("one",data.removeFirst());
73 91 assertThat(data.getFirst(),is("two"));
74 92 }
  93 + // Georgii
75 94 @Test
76 95 public void removeLastRemovesLastElement() {
77 96 assertEquals("two",data.removeLast());
78 97 assertThat(data.getLast(),is("one"));
79 98 }
  99 +// Iuliia
80 100 @Test
81 101 public void remove() {
82 102 assertEquals("three", data.remove(2));
83 103 assertEquals("two", data.remove(1));
84 104 assertEquals("one",data.remove(0));
85 105 }
86   - @Test
87   - public void addIndex() {
88   - data.add(3,"new");
89   - assertEquals("new",data.get(3));
90   - }
  106 +// Iuliia
  107 + @Test(expected = IndexOutOfBoundsException.class)
  108 + public void removeInvalidIndexThrowsException() {
  109 + data.remove(-1);
  110 + }
  111 + // Georgii
91 112 @Test
92 113 public void clearList() {
93 114 data.clear();
94   - assertEquals(0,data.size());
95   - assertNull(data.getFirst(),data.getLast());
  115 + assertEquals(0,data.size());
  116 + }
  117 +// Adrian
  118 + @Test
  119 + public void containsContainedElementReturnsTrue() {
  120 + assertThat(data.contains(data.getFirst()),is(true));
  121 + }
  122 +// Adrian
  123 + @Test
  124 + public void ifNotInListContainsReturnsFalse() {
  125 + assertThat(data.contains(data.getFirst()+ "error"),is(false));
  126 + }
  127 +
  128 +// Aleksei
  129 + @Test(expected = IndexOutOfBoundsException.class)
  130 + public void getInvalidIndexThrowsException() {
  131 + data.get(-1);
96 132 }
  133 + // Aleksei
97 134 @Test
98   - public void contains() {
99   - assertThat(data.contains("four"),is(true));
100   - assertFalse(data.contains("fou r"));
  135 + public void getFirstIndexIsHeadElement() {
  136 + assertEquals("one",data.get(0));
101 137 }
  138 +
  139 +// Aleksei
  140 + @Test
  141 + public void getLastIndexIsTailElement() {
  142 + assertEquals("two",data.get(5));
  143 + }
  144 + // Aleksei
102 145 @Test
103 146 public void indexOf() {
104 147 assertEquals(0,data.indexOf("one"));
105   - assertEquals(2,data.indexOf("three"));
  148 + assertEquals(2,data.indexOf("three"));
  149 + assertEquals(-1,data.indexOf("random random"));
106 150 }
  151 + // Aleksei
107 152 @Test
108 153 public void LastindexOf() {
109 154 assertEquals(4,data.lastIndexOf("one"));
110   - assertEquals(5,data.lastIndexOf("two"));
  155 + assertEquals(5,data.lastIndexOf("two"));
  156 + assertEquals(-1,data.lastIndexOf("random random"));
111 157 }
112 158  
113 159  
... ... @@ -150,7 +196,7 @@ public class TwoWayLinkedListTest {
150 196 data.clear();
151 197 itr = data.iterator();
152 198 assertFalse(itr.hasNext());
153   - }
  199 + }
154 200 @Test
155 201 public void successiveCallingNextAndPreviousReturnsSameElement() {
156 202 String listElement = data.get(0);
... ...