Initial commit for List TDD

This commit is contained in:
Nam Thai Nguyen 2018-02-14 17:10:18 +07:00
parent 372cba10bb
commit a67d99a09a
2 changed files with 516 additions and 0 deletions

View File

@ -0,0 +1,232 @@
package com.baeldung.java.list;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class CustomList<E> implements List<E> {
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();
}
@Override
public boolean addAll(Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends E> collection) {
throw new UnsupportedOperationException();
}
@Override
public E remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return internal.length;
}
@Override
public boolean isEmpty() {
return internal.length == 0;
}
@Override
public boolean contains(Object object) {
// the first cycle
// if (object.equals(internal[0])) {
// return true;
// }
for (Object element : internal) {
if (object.equals(element)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection<?> collection) {
// the first cycle
// for (Object element : collection) {
// if (element.equals(internal[0])) {
// return true;
// }
// }
// return false;
for (Object element : collection)
if (!contains(element)) {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public E set(int index, E element) {
E oldElement = (E) internal[index];
internal[index] = element;
return oldElement;
}
@Override
public void clear() {
internal = new Object[0];
}
@Override
public int indexOf(Object object) {
// the first cycle
// if (object.equals(internal[0])) {
// return 0;
// }
// return -1;
for (int i = 0; i < internal.length; i++) {
if (object.equals(internal[i])) {
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(Object object) {
// the first cycle
// if (object.equals(internal[0])) {
// return 0;
// }
// return -1;
for (int i = internal.length - 1; i >= 0; i--) {
if (object.equals(internal[i])) {
return i;
}
}
return -1;
}
@SuppressWarnings("unchecked")
@Override
public List<E> subList(int fromIndex, int toIndex) {
// the first cycle
// return (List<E>) Arrays.asList(internal);
Object[] temp = new Object[toIndex - fromIndex];
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
return (List<E>) Arrays.asList(temp);
}
@Override
public Object[] toArray() {
return Arrays.copyOf(internal, internal.length);
}
@SuppressWarnings("unchecked")
@Override
public <T> T[] toArray(T[] array) {
// the first cycle
// for (int i = 0; i < array.length; i++) {
// array[i] = (T) internal[i];
// }
// return array;
// the second cycle
// if (array.length < internal.length) {
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
// }
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
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;
}
return array;
}
@Override
public Iterator<E> iterator() {
return new CustomIterator();
}
@Override
public ListIterator<E> listIterator() {
return null;
}
@Override
public ListIterator<E> listIterator(int index) {
return null;
}
private class CustomIterator implements Iterator<E> {
int index;
@Override
public boolean hasNext() {
// the first cycle
// return true;
return index != internal.length;
}
@SuppressWarnings("unchecked")
@Override
public E next() {
// the first cycle
// return (E) CustomList.this.internal[0];
E element = (E) CustomList.this.internal[index];
index++;
return element;
}
}
}

View File

@ -0,0 +1,284 @@
package com.baeldung.java.list;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
public class CustomListUnitTest {
@Test
public void givenAddToTheEndAndGetImpl_whenCycle1_thenPasses() {
List<Object> 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() {
new CustomList<>().add(0, null);
}
@Test(expected = UnsupportedOperationException.class)
public void givenAddAllToTheEndImpl_whenCycle1_thenPasses() {
Collection<Object> collection = new ArrayList<>();
List<Object> list = new CustomList<>();
list.addAll(collection);
}
@Test(expected = UnsupportedOperationException.class)
public void givenAddAllToSpecifiedIndexImpl_whenCycle1_thenPasses() {
Collection<Object> collection = new ArrayList<>();
List<Object> list = new CustomList<>();
list.addAll(0, collection);
}
@Test(expected = UnsupportedOperationException.class)
public void givenRemoveAtSpecifiedIndexImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.remove(0);
}
@Test(expected = UnsupportedOperationException.class)
public void givenRemoveSpecifiedElementImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.remove("baeldung");
}
@Test(expected = UnsupportedOperationException.class)
public void givenRemoveAllImpl_whenCycle1_thenPasses() {
Collection<Object> collection = new ArrayList<>();
collection.add("baeldung");
List<Object> list = new CustomList<>();
list.removeAll(collection);
}
@Test(expected = UnsupportedOperationException.class)
public void givenRetainAllImpl_whenCycle1_thenPasses() {
Collection<Object> collection = new ArrayList<>();
collection.add("baeldung");
List<Object> list = new CustomList<>();
list.add("baeldung");
list.retainAll(collection);
}
@Test
public void givenSizeImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
assertEquals(0, list.size());
}
@Test
public void givenIsEmptyImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
assertTrue(list.isEmpty());
}
@Test
public void givenContainsImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
assertTrue(list.contains("baeldung"));
}
@Test
public void givenContainsAllImpl_whenCycle1_thenPasses() {
Collection<Object> collection = new ArrayList<>();
collection.add("baeldung");
List<Object> list = new CustomList<>();
list.add("baeldung");
assertTrue(list.containsAll(collection));
}
@Test
public void givenSetImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
Object element = list.set(0, null);
assertEquals("baeldung", element);
assertNull(list.get(0));
}
@Test
public void givenClearImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.clear();
assertTrue(list.isEmpty());
}
@Test
public void givenIndexOfImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
assertEquals(0, list.indexOf("baeldung"));
}
@Test
public void givenLastIndexOfImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
assertEquals(0, list.lastIndexOf("baeldung"));
}
@Test
public void givenSubListImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
List<Object> subList = list.subList(0, 1);
assertEquals("baeldung", subList.get(0));
}
@Test
public void givenToNewArrayImpl_whenCycle1_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
Object[] array = list.toArray();
assertEquals("baeldung", array[0]);
}
@Test
public void givenToExistingArrayImpl_whenCycle1_thenPasses() {
List<Object> 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<Object> list = new CustomList<>();
list.add("baeldung");
Iterator<Object> iterator = list.iterator();
assertTrue(iterator.hasNext());
assertEquals("baeldung", iterator.next());
}
@Test
public void givenAddToTheEndAndGetImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
boolean succeeded1 = list.add("baeldung");
boolean succeeded2 = list.add(".com");
Object element1 = list.get(0);
Object element2 = list.get(1);
assertTrue(succeeded1);
assertTrue(succeeded2);
assertEquals("baeldung", element1);
assertEquals(".com", element2);
}
@Test
public void givenContainsImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
assertFalse(list.contains("baeldung"));
list.add("baeldung");
assertTrue(list.contains("baeldung"));
}
@Test
public void givenContainsAllImpl_whenCycle2_thenPasses() {
Collection<Object> collection1 = new ArrayList<>();
collection1.add("baeldung");
collection1.add(".com");
Collection<Object> collection2 = new ArrayList<>();
collection2.add("baeldung");
List<Object> list = new CustomList<>();
list.add("baeldung");
assertFalse(list.containsAll(collection1));
assertTrue(list.containsAll(collection2));
}
@Test
public void givenIndexOfImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.add(".com");
list.add(".com");
assertEquals(1, list.indexOf(".com"));
assertEquals(-1, list.indexOf("com"));
}
@Test
public void givenLastIndexOfImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.add("baeldung");
list.add(".com");
assertEquals(1, list.lastIndexOf("baeldung"));
assertEquals(-1, list.indexOf("com"));
}
@Test
public void givenSubListImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
list.add(".");
list.add("com");
List<Object> subList = list.subList(1, 2);
assertEquals(1, subList.size());
assertEquals(".", subList.get(0));
}
@Test
public void givenToExistingArrayImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
String[] input = {};
String[] output = list.toArray(input);
assertArrayEquals(new String[] { "baeldung" }, output);
}
@Test
public void givenIteratorImpl_whenCycle2_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
Iterator<Object> iterator = list.iterator();
assertTrue(iterator.hasNext());
assertEquals("baeldung", iterator.next());
assertFalse(iterator.hasNext());
}
@Test
public void givenToExistingArrayImpl_whenCycle3_thenPasses() {
List<Object> list = new CustomList<>();
list.add("baeldung");
String[] input = new String[2];
String[] output = list.toArray(input);
assertArrayEquals(new String[] { "baeldung", null }, output);
}
}