diff --git a/core-java/src/main/java/com/baeldung/java/list/CustomList.java b/core-java/src/main/java/com/baeldung/java/list/CustomList.java index bed9cefeee..bc1321b1a3 100644 --- a/core-java/src/main/java/com/baeldung/java/list/CustomList.java +++ b/core-java/src/main/java/com/baeldung/java/list/CustomList.java @@ -9,26 +9,6 @@ import java.util.ListIterator; public class CustomList implements List { private Object[] internal = {}; - @Override - public boolean add(E element) { - // the first cycle - // internal = new Object[1]; - // internal[0] = element; - // return true; - - Object[] temp = new Object[internal.length + 1]; - System.arraycopy(internal, 0, temp, 0, internal.length); - temp[internal.length] = element; - internal = temp; - return true; - } - - @SuppressWarnings("unchecked") - @Override - public E get(int index) { - return (E) internal[index]; - } - @Override public void add(int index, E element) { throw new UnsupportedOperationException(); @@ -75,11 +55,28 @@ public class CustomList implements List { } @Override - public boolean contains(Object object) { + public boolean add(E element) { // the first cycle - // if (object.equals(internal[0])) { - // return true; - // } + // internal = new Object[1]; + // internal[0] = element; + // return true; + + Object[] temp = new Object[internal.length + 1]; + System.arraycopy(internal, 0, temp, 0, internal.length); + temp[internal.length] = element; + internal = temp; + return true; + } + + @SuppressWarnings("unchecked") + @Override + public E get(int index) { + return (E) internal[index]; + } + + @Override + public boolean contains(Object object) { + // return false for (Object element : internal) { if (object.equals(element)) { @@ -171,9 +168,7 @@ public class CustomList implements List { @Override public T[] toArray(T[] array) { // the first cycle - // for (int i = 0; i < array.length; i++) { - // array[i] = (T) internal[i]; - // } + // array[0] = (T) internal[0]; // return array; // the second cycle @@ -185,6 +180,7 @@ public class CustomList implements List { if (array.length < internal.length) { return (T[]) Arrays.copyOf(internal, internal.length, array.getClass()); } + System.arraycopy(internal, 0, array, 0, internal.length); if (array.length > internal.length) { array[internal.length] = null; @@ -204,6 +200,7 @@ public class CustomList implements List { @Override public ListIterator listIterator(int index) { + // ignored for brevity return null; } diff --git a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java index bc4c95dc79..471d4f8a63 100644 --- a/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java +++ b/core-java/src/test/java/com/baeldung/java/list/CustomListUnitTest.java @@ -14,51 +14,41 @@ import java.util.List; import org.junit.Test; public class CustomListUnitTest { - @Test - public void givenAddToTheEndAndGetImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - boolean succeeded = list.add("baeldung"); - Object element = list.get(0); - - assertTrue(succeeded); - assertEquals("baeldung", element); - } - @Test(expected = UnsupportedOperationException.class) - public void givenAddToSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenAddToSpecifiedIndex_thenExceptionIsThrown() { new CustomList<>().add(0, null); } @Test(expected = UnsupportedOperationException.class) - public void givenAddAllToTheEndImpl_whenCycle1_thenPasses() { + public void whenAddAllToTheEnd_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); List list = new CustomList<>(); list.addAll(collection); } @Test(expected = UnsupportedOperationException.class) - public void givenAddAllToSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenAddAllToSpecifiedIndex_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); List list = new CustomList<>(); list.addAll(0, collection); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveAtSpecifiedIndexImpl_whenCycle1_thenPasses() { + public void whenRemoveAtSpecifiedIndex_thenExceptionIsThrown() { List list = new CustomList<>(); list.add("baeldung"); list.remove(0); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveSpecifiedElementImpl_whenCycle1_thenPasses() { + public void whenRemoveSpecifiedElement_thenExceptionIsThrown() { List list = new CustomList<>(); list.add("baeldung"); list.remove("baeldung"); } @Test(expected = UnsupportedOperationException.class) - public void givenRemoveAllImpl_whenCycle1_thenPasses() { + public void whenRemoveAll_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); collection.add("baeldung"); List list = new CustomList<>(); @@ -66,7 +56,7 @@ public class CustomListUnitTest { } @Test(expected = UnsupportedOperationException.class) - public void givenRetainAllImpl_whenCycle1_thenPasses() { + public void whenRetainAll_thenExceptionIsThrown() { Collection collection = new ArrayList<>(); collection.add("baeldung"); List list = new CustomList<>(); @@ -75,112 +65,31 @@ public class CustomListUnitTest { } @Test - public void givenSizeImpl_whenCycle1_thenPasses() { + public void whenSize_thenNumberOfElementsIsReturned() { List list = new CustomList<>(); assertEquals(0, list.size()); } @Test - public void givenIsEmptyImpl_whenCycle1_thenPasses() { + public void whenIsEmpty_thenWhetherListIsEmptyIsReturned() { List list = new CustomList<>(); assertTrue(list.isEmpty()); } @Test - public void givenContainsImpl_whenCycle1_thenPasses() { + public void whenAddToTheEndAndGet_thenAddedElementIsReturned() { List list = new CustomList<>(); - list.add("baeldung"); - - assertTrue(list.contains("baeldung")); - } - - @Test - public void givenContainsAllImpl_whenCycle1_thenPasses() { - Collection collection = new ArrayList<>(); - collection.add("baeldung"); - List list = new CustomList<>(); - list.add("baeldung"); - - assertTrue(list.containsAll(collection)); - } - - @Test - public void givenSetImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Object element = list.set(0, null); + boolean succeeded = list.add("baeldung"); + Object element = list.get(0); + assertTrue(succeeded); assertEquals("baeldung", element); - assertNull(list.get(0)); } @Test - public void givenClearImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - list.clear(); - - assertTrue(list.isEmpty()); - } - - @Test - public void givenIndexOfImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - - assertEquals(0, list.indexOf("baeldung")); - } - - @Test - public void givenLastIndexOfImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - - assertEquals(0, list.lastIndexOf("baeldung")); - } - - @Test - public void givenSubListImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - List subList = list.subList(0, 1); - - assertEquals("baeldung", subList.get(0)); - } - - @Test - public void givenToNewArrayImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Object[] array = list.toArray(); - - assertEquals("baeldung", array[0]); - } - - @Test - public void givenToExistingArrayImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - String[] input = new String[1]; - String[] output = list.toArray(input); - - assertEquals("baeldung", output[0]); - } - - @Test - public void givenIteratorImpl_whenCycle1_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - Iterator iterator = list.iterator(); - - assertTrue(iterator.hasNext()); - assertEquals("baeldung", iterator.next()); - } - - @Test - public void givenAddToTheEndAndGetImpl_whenCycle2_thenPasses() { + public void whenAddToTheEndAndGet_thenAddedElemenetsAreReturned() { List list = new CustomList<>(); boolean succeeded1 = list.add("baeldung"); boolean succeeded2 = list.add(".com"); @@ -194,16 +103,32 @@ public class CustomListUnitTest { } @Test - public void givenContainsImpl_whenCycle2_thenPasses() { + public void whenContains_thenFalseIsReturned() { List list = new CustomList<>(); - assertFalse(list.contains("baeldung")); + assertFalse(list.contains("baeldung")); + } + + @Test + public void whenContains_thenTrueIsReturned() { + List list = new CustomList<>(); list.add("baeldung"); + assertTrue(list.contains("baeldung")); } @Test - public void givenContainsAllImpl_whenCycle2_thenPasses() { + public void whenContainsAll_thenTrueIsReturned() { + Collection collection = new ArrayList<>(); + collection.add("baeldung"); + List list = new CustomList<>(); + list.add("baeldung"); + + assertTrue(list.containsAll(collection)); + } + + @Test + public void whenContainsAll_thenEitherTrueOfFalseIsReturned() { Collection collection1 = new ArrayList<>(); collection1.add("baeldung"); collection1.add(".com"); @@ -218,7 +143,34 @@ public class CustomListUnitTest { } @Test - public void givenIndexOfImpl_whenCycle2_thenPasses() { + public void whenSet_thenOldElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object element = list.set(0, null); + + assertEquals("baeldung", element); + assertNull(list.get(0)); + } + + @Test + public void whenClear_thenAllElementsAreRemoved() { + List list = new CustomList<>(); + list.add("baeldung"); + list.clear(); + + assertTrue(list.isEmpty()); + } + + @Test + public void whenIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.indexOf("baeldung")); + } + + @Test + public void whenIndexOf_thenPositiveOrNegativeIndexIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add(".com"); @@ -229,7 +181,15 @@ public class CustomListUnitTest { } @Test - public void givenLastIndexOfImpl_whenCycle2_thenPasses() { + public void whenLastIndexOf_thenIndexZeroIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + + assertEquals(0, list.lastIndexOf("baeldung")); + } + + @Test + public void whenLastIndexOf_thenPositiveOrNegativeIndexIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("baeldung"); @@ -240,7 +200,16 @@ public class CustomListUnitTest { } @Test - public void givenSubListImpl_whenCycle2_thenPasses() { + public void whenSubList_thenListContainingFirstElementIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + List subList = list.subList(0, 1); + + assertEquals("baeldung", subList.get(0)); + } + + @Test + public void whenSubList_thenListContainingSecondElementIsReturned() { List list = new CustomList<>(); list.add("baeldung"); list.add("."); @@ -252,7 +221,26 @@ public class CustomListUnitTest { } @Test - public void givenToExistingArrayImpl_whenCycle2_thenPasses() { + public void whenToNewArray_thenArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + Object[] array = list.toArray(); + + assertEquals("baeldung", array[0]); + } + + @Test + public void whenToArray_thenOldArrayIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[1]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung" }, input); + } + + @Test + public void whenToArray_thenNewArrayIsReturned() { List list = new CustomList<>(); list.add("baeldung"); String[] input = {}; @@ -262,7 +250,27 @@ public class CustomListUnitTest { } @Test - public void givenIteratorImpl_whenCycle2_thenPasses() { + public void whenToArray_thenNewArrayWithTrailingNullIsReturned() { + List list = new CustomList<>(); + list.add("baeldung"); + String[] input = new String[2]; + String[] output = list.toArray(input); + + assertArrayEquals(new String[] { "baeldung", null }, output); + } + + @Test + public void whenIterator_thenOneCheckIsCorrect() { + List list = new CustomList<>(); + list.add("baeldung"); + Iterator iterator = list.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals("baeldung", iterator.next()); + } + + @Test + public void whenIterator_thenTwoChecksAreCorrect() { List list = new CustomList<>(); list.add("baeldung"); Iterator iterator = list.iterator(); @@ -271,14 +279,4 @@ public class CustomListUnitTest { assertEquals("baeldung", iterator.next()); assertFalse(iterator.hasNext()); } - - @Test - public void givenToExistingArrayImpl_whenCycle3_thenPasses() { - List list = new CustomList<>(); - list.add("baeldung"); - String[] input = new String[2]; - String[] output = list.toArray(input); - - assertArrayEquals(new String[] { "baeldung", null }, output); - } }