mirror of
https://github.com/DerTyp7/tdd-klausur.git
synced 2025-10-29 12:52:08 +01:00
Add ArrayList
This commit is contained in:
13
src/main/java/de/szut/App.java
Normal file
13
src/main/java/de/szut/App.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package de.szut;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
||||
55
src/main/java/de/szut/MyArrayList.java
Normal file
55
src/main/java/de/szut/MyArrayList.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package de.szut;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MyArrayList {
|
||||
|
||||
private static final int INITIAL_CAPACITY = 10;
|
||||
|
||||
private int[] array;
|
||||
private int size;
|
||||
|
||||
public MyArrayList() {
|
||||
this.array = new int[INITIAL_CAPACITY];
|
||||
this.size = 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean add(int value) {
|
||||
ensureCapacity();
|
||||
array[size++] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void remove(int index) {
|
||||
if (index < 0 || index >= size) {
|
||||
throw new RuntimeException("Dieser Index existiert nicht!");
|
||||
}
|
||||
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
|
||||
array[--size] = 0;
|
||||
}
|
||||
|
||||
|
||||
public boolean contains(int value) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (array[i] == value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ensureCapacity() {
|
||||
if (size == array.length) {
|
||||
int newCapacity = array.length + INITIAL_CAPACITY;
|
||||
array = Arrays.copyOf(array, newCapacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
144
src/test/java/de/szut/MyArrayListTest.java
Normal file
144
src/test/java/de/szut/MyArrayListTest.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package de.szut;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class MyArrayListTest {
|
||||
|
||||
private MyArrayList list;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
list = new MyArrayList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void add_ShouldReturnTrueAndIncreaseSize() {
|
||||
assertThat(list.size(), is(0));
|
||||
|
||||
boolean added1 = list.add(5);
|
||||
assertThat(added1, is(true));
|
||||
assertThat(list.size(), is(1));
|
||||
assertThat(list.contains(5), is(true));
|
||||
|
||||
boolean added2 = list.add(10);
|
||||
assertThat(added2, is(true));
|
||||
assertThat(list.size(), is(2));
|
||||
assertThat(list.contains(10), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove_ShouldRemoveElementAndDecreaseSize() {
|
||||
list.add(5);
|
||||
list.add(10);
|
||||
list.add(15);
|
||||
|
||||
assertThat(list.size(), is(3));
|
||||
|
||||
list.remove(1);
|
||||
|
||||
assertThat(list.size(), is(2));
|
||||
assertThat(list.contains(5), is(true));
|
||||
assertThat(list.contains(10), is(false));
|
||||
assertThat(list.contains(15), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove_ShouldThrowExceptionForInvalidIndex() {
|
||||
list.add(5);
|
||||
|
||||
assertThat(list.size(), is(1));
|
||||
|
||||
try {
|
||||
list.remove(1);
|
||||
fail("Expected RuntimeException was not thrown");
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e.getMessage(), is("Dieser Index existiert nicht!"));
|
||||
assertThat(list.size(), is(1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldReturnZeroInitially() {
|
||||
assertThat(list.size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldBeZeroInitially() {
|
||||
|
||||
assertThat(list.size(), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldIncreaseWhenElementAdded() {
|
||||
list.add(5);
|
||||
|
||||
assertThat(list.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldIncreaseProperlyWhenMultipleElementsAdded() {
|
||||
list.add(5);
|
||||
list.add(10);
|
||||
|
||||
assertThat(list.size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldDecreaseWhenElementRemoved() {
|
||||
list.add(5);
|
||||
list.add(10);
|
||||
|
||||
list.remove(0);
|
||||
|
||||
assertThat(list.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void size_ShouldNotDecreaseBelowZero() {
|
||||
list.add(5);
|
||||
|
||||
list.remove(0);
|
||||
|
||||
assertThat(list.size(), is(0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void contains_ShouldReturnTrueIfElementPresent() {
|
||||
list.add(5);
|
||||
list.add(10);
|
||||
list.add(15);
|
||||
|
||||
assertThat(list.contains(10), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contains_ShouldReturnFalseIfElementNotPresent() {
|
||||
list.add(5);
|
||||
list.add(10);
|
||||
list.add(15);
|
||||
|
||||
assertThat(list.contains(20), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dynamicSize_ShouldIncreaseAsElementsAdded() {
|
||||
for (int i = 1; i <= 15; i++) {
|
||||
boolean added = list.add(i);
|
||||
assertThat(added, is(true));
|
||||
}
|
||||
|
||||
assertThat(list.size(), is(15));
|
||||
|
||||
for (int i = 1; i <= 15; i++) {
|
||||
assertThat(list.contains(i), is(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user