-
Notifications
You must be signed in to change notification settings - Fork 1
Tests und Implementierung ArrayList #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,145 @@ | ||
| import java.util.Collection; | ||
| import java.util.Iterator; | ||
|
|
||
| public class MyListImplementation<T> implements MyList<T> { | ||
|
|
||
| private Object[] elements; | ||
| private int length = 0; | ||
|
|
||
| public MyListImplementation() { | ||
| this.elements = new Object[0]; | ||
| } | ||
|
|
||
| public int size() { | ||
| return 0; | ||
| return this.elements.length; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return false; | ||
| return this.size() == 0; | ||
| } | ||
|
|
||
| public boolean contains(Object o) { | ||
|
|
||
| for (int i = 0; i < this.length; i++) { | ||
| if (this.elements[i].equals(o)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public boolean add(T t) { | ||
| return false; | ||
| Object[] newElements = new Object[++this.length]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ein kleiner Grundsatzgedanke, den man immer im Hinterkopf behalten sollte: Die interne Datenstruktur weg-abstrahieren. Es gibt hier eine Funktion für die Länge, nämlich size(). Es macht absolut Sinn, auch intern mit den selben Methoden zu arbeiten. Das macht späteres Refactoring deutlich einfacher! |
||
| for (int i = 0; i < this.length - 1; i++) { | ||
| newElements[i] = this.elements[i]; | ||
| } | ||
| newElements[this.length - 1] = t; | ||
| this.elements = newElements; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean remove(Object o) { | ||
| return false; | ||
|
|
||
| if (this.contains(o)) { | ||
| Object[] newElements = new Object[--this.length]; | ||
| for (int i = 0, j = 0; i <= this.length; i++) { | ||
| if (!this.elements[i].equals(o)) { | ||
| newElements[j] = this.elements[i]; | ||
| j++; | ||
| } | ||
| } | ||
| this.elements = newElements; | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public boolean containsAll(Collection<?> c) { | ||
| return false; | ||
| Iterator<?> iterator = c.iterator(); | ||
| for (int i = 0; i < c.size(); i++) { | ||
| if (this.contains(iterator.next())) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wenn du mit einem iterator arbeitest, kannst du auch immer while (iterator.hasNext()) machen! |
||
| continue; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public boolean addAll(Collection<? extends T> c) { | ||
| return false; | ||
| Iterator<?> iterator = c.iterator(); | ||
| for (int i = 0; i < c.size(); i++) { | ||
| this.add((T) iterator.next()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hier auch. Iterator lässt sich gut mit while() kombinieren! |
||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public boolean addAll(int index, Collection<? extends T> c) { | ||
|
|
||
| Iterator<?> iterator = c.iterator(); | ||
| for (int i = index; i < index + c.size(); i++) { | ||
| this.add(i, (T) iterator.next()); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public boolean removeAll(Collection<?> c) { | ||
| return false; | ||
|
|
||
| Iterator<?> iterator = c.iterator(); | ||
| boolean[] returnValues = new boolean[c.size()]; | ||
| for (int i = 0; i < c.size(); i++) { | ||
| returnValues[i] = this.remove(iterator.next()); | ||
| } | ||
| for (int i = 0; i < c.size(); i++) { | ||
| if (returnValues[i] == false) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public void clear() { | ||
|
|
||
| this.elements = new Object[0]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Es macht evtl. Sinn, hier mit null oder einem private static final EMPTY object zu arbeiten. |
||
| } | ||
|
|
||
| public T get(int index) { | ||
| return null; | ||
| return (T) this.elements[index]; | ||
| } | ||
|
|
||
| public T set(int index, T element) { | ||
| return null; | ||
| Object oldValue = this.elements[index]; | ||
| this.elements[index] = element; | ||
| return (T) oldValue; | ||
| } | ||
|
|
||
| public void add(int index, T element) { | ||
| Object[] newElements = new Object[++this.length]; | ||
| for (int i = 0; i < index; i++) { | ||
| newElements[i] = this.get(i); | ||
| } | ||
| newElements[index] = element; | ||
| for (int i = index + 1; i < this.size() + 1; i++) { | ||
| newElements[i] = this.get(i - 1); | ||
| } | ||
| this.elements = newElements; | ||
|
|
||
| } | ||
|
|
||
| public T remove(int index) { | ||
| return null; | ||
| T removedValue = this.get(index); | ||
| this.remove(removedValue); | ||
| return removedValue; | ||
| } | ||
|
|
||
| public int indexOf(Object o) { | ||
| return 0; | ||
|
|
||
| for (int i = 0; i < elements.length; i++) { | ||
| if (this.elements[i].equals(o)) { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class MyListImplementationTest { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Versuch dich nächstes mal an das pattern with...should für Testnamen zu halten. Das macht es ein bisschen lesbarer, was die Tests aussagen sollen |
||
|
|
||
| @BeforeEach | ||
| void init(){ | ||
| } | ||
|
|
||
| @Test | ||
| void testIsEmpty() { | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| assertThat(list.isEmpty()).isEqualTo(true); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdd() { | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add(1); | ||
| list.add(2); | ||
| assertThat(list.isEmpty()).isEqualTo(false); | ||
| assertThat(list.size()).isEqualTo(2); | ||
| } | ||
|
|
||
| @Test | ||
| void testContains() { | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add(1); | ||
| list.add(2); | ||
| assertThat(list.contains(1)).isEqualTo(true); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gerne in 2 Testfälle spalten. Einen Positiv-Test und einen Negativ-Test. Das wären dann: Grundsätzlich eine "fachlichkeit" pro Testfall! |
||
| assertThat(list.contains(3)).isEqualTo(false); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemove() { | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| assertThat(list.remove("C")).isEqualTo(false); | ||
| list.remove("B"); | ||
| assertThat(list.size()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testContainsAll(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.add("C"); | ||
| assertThat(list.containsAll(List.of("A", "B"))).isEqualTo(true); | ||
| assertThat(list.containsAll(List.of("A", "D"))).isEqualTo(false); | ||
| } | ||
|
|
||
| @Test | ||
| void testAddAll(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.addAll(List.of("A", "B")); | ||
| assertThat(list.containsAll(List.of("A", "B"))).isEqualTo(true); | ||
| assertThat(list.containsAll(List.of("A", "D"))).isEqualTo(false); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveAll(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.addAll(List.of("A", "B")); | ||
| assertThat(list.removeAll(List.of("A"))).isEqualTo(true); | ||
| assertThat(list.removeAll(List.of("D"))).isEqualTo(false); | ||
| } | ||
|
|
||
| @Test | ||
| void testGet(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.add("C"); | ||
|
|
||
| assertThat(list.get(0)).isEqualTo("A"); | ||
| assertThat(list.get(2)).isEqualTo("C"); | ||
| } | ||
|
|
||
| @Test | ||
| void testSet(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.add("C"); | ||
| list.set(0, "C"); | ||
| assertThat(list.get(0)).isEqualTo("C"); | ||
| } | ||
|
|
||
| @Test | ||
| void testIndexOf(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.add("C"); | ||
|
|
||
| assertThat(list.indexOf("C")).isEqualTo(2); | ||
| assertThat(list.indexOf("D")).isEqualTo(-1); | ||
| } | ||
|
|
||
| @Test | ||
| void testAddByIndex(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.add(1, "C"); | ||
|
|
||
| assertThat(list.get(1)).isEqualTo("C"); | ||
| assertThat(list.get(2)).isEqualTo("B"); | ||
| } | ||
|
|
||
| @Test | ||
| void testRemoveByIndex(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.remove(1); | ||
|
|
||
| assertThat(list.get(0)).isEqualTo("A"); | ||
| assertThat(list.size()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void testAddAllByIndex(){ | ||
| MyListImplementation list = new MyListImplementation<Integer>(); | ||
| list.add("A"); | ||
| list.add("B"); | ||
| list.addAll(1, List.of("C", "D")); | ||
|
|
||
| assertThat(list.get(1)).isEqualTo("C"); | ||
| assertThat(list.get(2)).isEqualTo("D"); | ||
| assertThat(list.get(3)).isEqualTo("B"); | ||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object ist natürlich nicht so ideal. Es soll doch ein T sein