Abgabe: Mika
This commit is contained in:
parent
55c51c4f15
commit
96a1239780
@ -1,5 +1,59 @@
|
|||||||
package de.szut.mylists;
|
package de.szut.mylists;
|
||||||
|
|
||||||
public class MyArrayList {
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class MyArrayList {
|
||||||
|
private Integer[] internalArray = new Integer[10];
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public void add(int value) {
|
||||||
|
if(size == internalArray.length)
|
||||||
|
resize();
|
||||||
|
|
||||||
|
internalArray[size++] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int get(int index) {
|
||||||
|
if(index < 0 || index >= size)
|
||||||
|
throw new RuntimeException("Dieser Index existiert nicht!");
|
||||||
|
|
||||||
|
return internalArray[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(int index) {
|
||||||
|
Integer[] newArray;
|
||||||
|
|
||||||
|
if(index < 0 || index >= size)
|
||||||
|
throw new RuntimeException("Dieser Index existiert nicht!");
|
||||||
|
|
||||||
|
newArray = new Integer[internalArray.length];
|
||||||
|
|
||||||
|
int newIndexCounter = 0;
|
||||||
|
for(int i = 0; i < size; i++) {
|
||||||
|
if(i == index)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
newArray[newIndexCounter++] = internalArray[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
internalArray = newArray;
|
||||||
|
size = newIndexCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains(int value) {
|
||||||
|
if(size == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return Arrays.stream(internalArray).anyMatch(e -> e != null && e.equals(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resize() {
|
||||||
|
Integer[] newArray = new Integer[internalArray.length + 10];
|
||||||
|
System.arraycopy(internalArray, 0, newArray, 0, size);
|
||||||
|
internalArray = newArray;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,125 @@
|
|||||||
package de.szut.mylists;
|
package de.szut.mylists;
|
||||||
|
|
||||||
public class MyArrayListTest {
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
public class MyArrayListTest {
|
||||||
|
MyArrayList list;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
list = new MyArrayList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testIfAddDoesNotThrowsException(List<Integer> data) {
|
||||||
|
assertThatNoException().isThrownBy(() -> data.forEach(list::add));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testThatRemoveThrowsExceptionWhenNegativeIndex(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> list.remove(-1))
|
||||||
|
.isExactlyInstanceOf(RuntimeException.class)
|
||||||
|
.hasMessage("Dieser Index existiert nicht!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testThatGetThrowsExceptionWhenNegativeIndex(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> list.get(-1))
|
||||||
|
.isExactlyInstanceOf(RuntimeException.class)
|
||||||
|
.hasMessage("Dieser Index existiert nicht!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testThatRemoveActuallyRemovesElement(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
list.remove(0);
|
||||||
|
assertThat(list.get(0)).isNotEqualTo(data.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testThatRemoveDecreasesSize(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
list.remove(0);
|
||||||
|
assertThat(list.size()).isEqualTo(data.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testIfRemovingRearrangeArray(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
list.remove(0);
|
||||||
|
assertThat(list.get(0)).isEqualTo(data.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIfRemovingInvalidIndexIsThrowing_RuntimeException() {
|
||||||
|
list.add(1);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> list.remove(1))
|
||||||
|
.isExactlyInstanceOf(RuntimeException.class)
|
||||||
|
.hasMessage("Dieser Index existiert nicht!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIfRemovingValidIndexIsNotThrowing_RuntimeException() {
|
||||||
|
list.add(1);
|
||||||
|
assertThatNoException().isThrownBy(() -> list.remove(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testIfSizeGivesCorrectNumberOfElements(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
assertThat(list.size()).isEqualTo(data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("generateData")
|
||||||
|
public void testIfContainsReturnsTrueIfItFindsTheElement(List<Integer> data) {
|
||||||
|
data.forEach(list::add);
|
||||||
|
|
||||||
|
data.forEach(e -> assertThat(list.contains(e)).isTrue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIfContainsReturnsFalseIfItDoesNotFindTheElement() {
|
||||||
|
list.add(1);
|
||||||
|
|
||||||
|
assertThat(list.contains(2)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIfContainsOnEmptyListDoesNotThrowError() {
|
||||||
|
assertThatNoException().isThrownBy(() -> list.contains(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> generateData() {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(List.of(1, 2, 3)),
|
||||||
|
Arguments.of(List.of(8, 2)),
|
||||||
|
Arguments.of(List.of(-1, -98, Integer.MIN_VALUE, Integer.MAX_VALUE))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user