diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1b2425b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,299 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..7819aa1 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..2abb1d9 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,261 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList { + final int INITIAL_CAPACITY = 10; + private int[] buffer; + private int size; + + public ArrayIntList() { + buffer = new int[INITIAL_CAPACITY]; + size = 0; + } + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + if (size == buffer.length) { + resize(); + } + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[0] = value; + size++; + } + + private void resize() { + int temp[] = new int[buffer.length * 2]; + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + + buffer = temp; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + if (buffer.length == size) { + resize(); + } + buffer[size] = value; + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + if (index > size) { + throw new IndexOutOfBoundsException("Index is not valid"); + } else if (buffer.length == size || buffer.length - 1 == size) { + resize(); + } else { + if (index == size) { + addBack(value); + } else { + for (int i = size; i >= index; i--) { + buffer[i + 1] = buffer[i]; + } + buffer[index] = value; + size++; + } + } + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + if (!isEmpty()) { + if (size > 1) { + for (int i = 0; i < size; i++) { + buffer[i] = buffer[i + 1]; + } + } else { + buffer[0] = 0; + } + size--; + } else { + throw new IndexOutOfBoundsException("List is empty"); + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if (size == 0) { + throw new IndexOutOfBoundsException("ArrayList is empty"); + } + size--; + buffer[size] = 0; + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + if (index < size) { + int temp = buffer[index]; + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + size--; + return temp; + } + throw new IndexOutOfBoundsException(); + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + if (index < size) { + return buffer[index]; + } + throw new IndexOutOfBoundsException(); + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return indexOf(value) != -1; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + if (!isEmpty()) { + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; + } + } + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + if (size > 0) { + for (int i = 0; i < size; i++) { + buffer[i] = 0; + } + size = 0; + } + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new Iterator() { + int index = -1; + @Override + public boolean hasNext() { return (index < size); } + + @Override + public Object next() { + if (hasNext()) { + index++; + return buffer[index]; + } + throw new NoSuchElementException(""); + } + }; + } + + @Override + public String toString() { + if(size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + + for (int i = 0; i < size; i++) { + sb.append(buffer[i]); + if (i != size - 1) { + sb.append(", "); + } + } + + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..51977dc --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,316 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +public class ArrayIntListTest { + @Test + void addFrontEmpty() { + ArrayIntList list = new ArrayIntList(); + list.addFront(1); + assertEquals(1, list.size()); + assertEquals("[1]", list.toString()); + } + + @Test + void addFrontMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addFront(1); + list.addFront(2); + assertEquals(2, list.size()); + assertEquals("[2, 1]", list.toString()); + } + + @Test + void addBack() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + String out = theList.toString(); + assertEquals("[42]", out); + } + @Test + void addBackWithNonEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(3); + theList.addBack(96); + theList.addBack(1); + theList.addBack(42); + String out = theList.toString(); + assertEquals("[3, 96, 1, 42]", out); + } + @Test + void addBackWithResize() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(3); + theList.addBack(96); + theList.addBack(1); + theList.addBack(42); + theList.addBack(3); + theList.addBack(96); + theList.addBack(1); + theList.addBack(42); + theList.addBack(3); + theList.addBack(96); + theList.addBack(1); + theList.addBack(42); + String out = theList.toString(); + assertEquals("[3, 96, 1, 42, 3, 96, 1, 42, 3, 96, 1, 42]", out); + } + @Test + void addBackLargeAmount() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 1000000; i++) { + theList.addBack(i); + } + assertEquals(theList.size(), 1000000); + } + + @Test + void addToEmpty() { + ArrayIntList empty = new ArrayIntList(); + empty.add(0, 1); + assertEquals(empty.size(), 1); + } + + @Test + void addToSingle() { + ArrayIntList list = new ArrayIntList(); + list.add(0,1); + list.add(0,2); + String out = list.toString(); + assertEquals(out,"[2, 1]"); + } + + @Test + void addToMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addFront(1); + list.addFront(2); + list.addFront(4); + list.add(1, 3); + assertEquals("[4, 3, 2, 1]", list.toString()); + } + + @Test + void addToEndSingle() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.add(1, 2); + assertEquals(2, list.size()); + } + + @Test + void addToEndMultiple() { + ArrayIntList list = new ArrayIntList(); + list.add(0, 0); + list.add(1, 1); + list.add(2, 2); + } + + + @Test + void removeFrontEmpty() { + ArrayIntList list = new ArrayIntList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.removeFront()); + } + + @Test + void removeFrontSingle() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.removeFront(); + assertEquals(0, list.size()); + } + + @Test + void removeFrontMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + + list.removeFront(); + assertEquals(2, list.size()); + assertEquals("[2, 3]", list.toString()); + list.removeFront(); + assertEquals(1, list.size()); + assertEquals("[3]", list.toString()); + } + + @Test + void removeBackEmpty() { + ArrayIntList empty = new ArrayIntList(); + assertThrows(IndexOutOfBoundsException.class, + () -> empty.removeBack()); + } +// const function = () => {console.log("45")} +// function print() {console.log("45")} + @Test + void removeBackSingle() { + ArrayIntList list = new ArrayIntList(); + list.addFront(1); + assertEquals(list.size(), 1); + list.removeBack(); + assertEquals(list.size(), 0); + } + + @Test + void removeBackMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addFront(3); + list.addFront(2); + list.addFront(1); + list.removeBack(); + list.removeBack(); + list.removeBack(); + + + assertEquals(0, list.size()); + } + + @Test + void removeEmpty() { + ArrayIntList list = new ArrayIntList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0)); + } + + @Test + void removeSingle() { + ArrayIntList list = new ArrayIntList(); + list.addFront(1); + assertEquals(1, list.size()); + list.remove(0); + assertEquals(0, list.size()); + } + + @Test + void removeMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addFront(3); + list.addFront(2); + list.addFront(1); + list.remove(1); + assertEquals(2, list.size()); + assertEquals("[1, 3]", list.toString()); + list.remove(0); + assertEquals(1, list.size()); + assertEquals("[3]", list.toString()); + } + + @Test + void getEmpty() { + ArrayIntList list = new ArrayIntList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.get(0)); + } + + @Test + void getSingle() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertEquals(1, list.get(0)); + } + + @Test + void getMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + + assertEquals(3, list.get(2)); + assertEquals(2, list.get(1)); + } + + + @Test + void containsEmpty() { + ArrayIntList list = new ArrayIntList(); + assertFalse(list.contains(1)); + } + + @Test + void containsFull() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertTrue(list.contains(1)); + } + + @Test + void notContains() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertFalse(list.contains(2)); + } + + @Test + void indexOfEmpty() { + ArrayIntList list = new ArrayIntList(); + assertEquals(-1, list.indexOf(1)); + } + + @Test + void indexOfSingle() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertEquals(0, list.indexOf(1)); + } + + @Test + void indexOfMultiple() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertEquals(1, list.indexOf(2)); + } + + @Test + void isEmptyTrue() { + ArrayIntList list = new ArrayIntList(); + assertTrue(list.isEmpty()); + } + + @Test + void isEmptyFalse() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertFalse(list.isEmpty()); + } + + @Test + void sizeEmpty() { + ArrayIntList list = new ArrayIntList(); + assertEquals(0, list.size()); + } + + @Test + void sizeNotEmpty() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + assertEquals(1, list.size()); + } + + @Test + void clear() { + ArrayIntList list = new ArrayIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + + list.clear(); + assertEquals("[]", list.toString()); + } + + @Test + void testToString() { + ArrayIntList list = new ArrayIntList(); + assertEquals("[]", list.toString()); + list.addBack(1); + assertEquals("[1]", list.toString()); + } + + @Test + void testIteratorEmpty() { + ArrayIntList empty = new ArrayIntList(); + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..6d1ed4e --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,332 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList { + private Node head; + private int size = 0; + + /** + * + */ + private class Node { + Integer value; + Node next; + + private Node(Integer value, Node next) { + this.value = value; + this.next = next; + } + private Node(Integer value) { + this.value = value; + this.next = null; + } + } + + public LinkedIntList() { + this.head = null; + } + + // Time Complexity: O(1) + public LinkedIntList(Integer... value) { + this.head = new Node(value[0]); + for (Integer val : value) { + this.addBack(val); + } + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * Time Complexity: O(1) + * + * @param value value to be inserted + * + * + */ + @Override + public void addFront(int value) { + this.head = new Node(value, head); + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * Time Complexity: O(n) + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + if (head == null) { + head = new Node(value); + } else { + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = new Node(value); + } + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * Time Complexity: O(n) + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + if (index > size) { + throw new IndexOutOfBoundsException(""); + } else if (index == 0) { + addFront(value); + } else { + int currIndex = 0; + Node curr = head; + while (currIndex < index - 1) { + curr = curr.next; + currIndex++; + } + curr.next = new Node(value, curr.next); + } + size++; + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + * Time Complexity: O(1) + */ + @Override + public void removeFront() { + if (head != null) { + head = head.next; + size--; + } else { + throw new NullPointerException("List is empty"); + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + * Time Complexity: O(n) + */ + @Override + public void removeBack() { + if (head != null) { + if (head.next == null) { + head = null; + } else { + Node curr = head; + while (curr.next.next != null) { + curr = curr.next; + } + curr.next = null; + } + size--; + } else { + throw new NullPointerException(""); + } + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * Time Complexity: O(n) + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + if (!isEmpty() && index < size) { + if (index == 0) { + int temp = head.value; + head = head.next; + size--; + return temp; + } else { + int currIndex = 0; + Node curr = head; + while (currIndex + 1 != index) { + curr = curr.next; + currIndex++; + } + int temp = curr.next.value; + curr.next = curr.next.next; + return temp; + } + } + throw new IndexOutOfBoundsException("Not valid index"); + } + + /** + * Returns the value at the specified position in the list. + * Time Complexity: O(n) + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + if (index < size) { + if (head.next == null) { + return head.value; + } else { + Node curr = head; + int currIndex = 0; + while (currIndex != index) { + curr = curr.next; + currIndex++; + } + return curr.value; + } + } else { + throw new IndexOutOfBoundsException(""); + } + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + * Time Complexity: O(n) + */ + @Override + public boolean contains(int value) { + if (head != null) { + Node curr = head; + while (curr != null) { + if (curr.value == value) { + return true; + } + curr = curr.next; + } + } + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * Time Complexity: O(n) + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + if (head != null) { + Node curr = head; + int currIndex = 0; + while (curr != null) { + if (curr.value == value) { + return currIndex; + } + currIndex++; + curr = curr.next; + } + } + return -1; + } + + /** + * Returns true if this list contains no values. + * Time Complexity: O(1) + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return (head == null); + } + + /** + * Returns the number of values in this list. + * Time Complexity: O(1) + * + * @return the number of values in this list + */ + @Override + public int size() { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + * Time Complexity: O(1) + */ + @Override + public void clear() { + this.head = null; + } + + /** + * Returns an iterator over elements of type {@code T}. + * Time Complexity: O(1) + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new Iterator<>() { + private Node curr = (head == null ? null : head); + + // Time Complexity: O(1) + @Override + public boolean hasNext() { + if (curr == null) { + curr = head; + } + return curr != null; + } + + // Time Complexity: O(1) + @Override + public Integer next() { + if (hasNext()) { + Integer temp = curr.value; + curr = curr.next; + return temp; + } + throw new NullPointerException("No such next node"); + } + }; + } + + /** + * Time Complexity: O(n) + * @return String representation of the Linked List + */ + @Override + public String toString() { + if(size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + Node curr = head; + + while (curr.next != null) { + sb.append(curr.value); + sb.append(", "); + curr = curr.next; + } + sb.append(curr.value); + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java new file mode 100644 index 0000000..65d9941 --- /dev/null +++ b/src/LinkedIntListTest.java @@ -0,0 +1,351 @@ +import org.junit.jupiter.api.Test; + +import java.util.Iterator; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class LinkedIntListTest { + @Test + void addFrontEmpty() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + assertEquals("[1]", list.toString()); + } + + @Test + void addFrontSingle() { + LinkedIntList list = new LinkedIntList(); + list.addFront(2); + list.addFront(1); + assertEquals("[1, 2]", list.toString()); + } + + @Test + void addFrontMultiple() { + LinkedIntList list = new LinkedIntList(); + list.addFront(3); + list.addFront(2); + list.addFront(1); + assertEquals("[1, 2, 3]", list.toString()); + } + + @Test + void addBackEmpty() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + assertEquals("[1]", list.toString()); + } + + @Test + void addBackSingle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + assertEquals("[1, 2]", list.toString()); + } + + @Test + void addBackMultiple() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertEquals("[1, 2, 3]", list.toString()); + } + + @Test + void addEmpty() { + LinkedIntList list = new LinkedIntList(); + list.add(0, 1); + assertEquals("[1]", list.toString()); + } + + @Test + void addEmptySingleBack() { + LinkedIntList list = new LinkedIntList(); + list.add(0, 0); + list.add(1, 1); + assertEquals("[0, 1]", list.toString()); + } + + @Test + void addEmptySingleFront() { + LinkedIntList list = new LinkedIntList(); + list.add(0, 1); + list.add(0, 0); + assertEquals("[0, 1]", list.toString()); + } + + @Test + void addEmptyMultipleFront() { + LinkedIntList list = new LinkedIntList(); + list.add(0, 3); + list.add(0, 2); + list.add(0, 1); + assertEquals("[1, 2, 3]", list.toString()); + } + + @Test + void addEmptyMultipleBack() { + LinkedIntList list = new LinkedIntList(); + list.add(0, 0); + list.add(1, 1); + list.add(2, 3); + list.add(2, 2); + assertEquals("[0, 1, 2, 3]", list.toString()); + + } + + @Test + void removeFrontEmpty() { + LinkedIntList list = new LinkedIntList(); + assertThrows(NullPointerException.class, + () -> list.removeFront()); + } + + @Test + void removeFrontSingle() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + list.removeFront(); + assertEquals(0, list.size()); + } + + @Test + void removeFrontMultiple() { + LinkedIntList list = new LinkedIntList(); + list.addFront(3); + list.addFront(2); + list.addFront(1); + + list.removeFront(); + assertEquals("[2, 3]", list.toString()); + } + + @Test + void removeBackEmpty() { + LinkedIntList list = new LinkedIntList(); + assertThrows(NullPointerException.class, + () -> list.removeBack()); + } + + @Test + void removeBackSingle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.removeBack(); + assertEquals(0, list.size()); + } + + @Test + void removeBackMultiple() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + list.removeBack(); + assertEquals("[1, 2]", list.toString()); + } + + @Test + void removeEmpty() { + LinkedIntList list = new LinkedIntList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0)); + } + + @Test + void removeSingle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + int result = list.remove(0); + assertEquals("[]", list.toString()); + assertEquals(1, result); + } + + @Test + void removeMultiple() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + int result = list.remove(2); + assertEquals("[1, 2]", list.toString()); + assertEquals(3, result); + } + + @Test + void removeMultipleMiddle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + int result = list.remove(1); + assertEquals("[1, 3]", list.toString()); + assertEquals(2, result); + } + @Test + void getEmpty() { + LinkedIntList list = new LinkedIntList(); + assertThrows(IndexOutOfBoundsException.class, () -> list.get(0)); + } + + @Test + void getSingle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + int result = list.get(0); + assertEquals(1, result); + } + + @Test + void getMultipleEnd() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + int result = list.get(2); + assertEquals(3, result); + } + + @Test + void getMultipleMiddle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + int result = list.get(1); + assertEquals(2, result); + } + + @Test + void containsEmpty() { + LinkedIntList list = new LinkedIntList(); + assertFalse(list.contains(0)); + } + + @Test + void containsSingle() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + assertTrue(list.contains(1)); + } + + @Test + void containsMultipleMiddle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertTrue(list.contains(2)); + } + + @Test + void containsMultipleEnd() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertTrue(list.contains(3)); + } + + @Test + void indexOfEmpty() { + LinkedIntList list = new LinkedIntList(); + assertEquals(-1, list.indexOf(0)); + } + + @Test + void indexOfSingle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + assertEquals(0, list.indexOf(1)); + } + + @Test + void indexOfMultipleMiddle() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertEquals(1, list.indexOf(2)); + } + + @Test + void indexOfMultipleEnd() { + LinkedIntList list = new LinkedIntList(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + assertEquals(2, list.indexOf(3)); + } + + @Test + void isEmptyTrue() { + LinkedIntList list = new LinkedIntList(); + assertTrue(list.isEmpty()); + } + + @Test + void isEmptyFalse() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + assertFalse(list.isEmpty()); + } + + @Test + void sizeEmpty() { + LinkedIntList list = new LinkedIntList(); + assertEquals(0, list.size()); + } + + @Test + void sizeNotEmpty() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + assertEquals(1, list.size()); + } + + @Test + void clear() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + list.clear(); + assertTrue(list.isEmpty()); + } + + @Test + void iteratorSingle() { + LinkedIntList list = new LinkedIntList(); + Iterator itr = list.iterator(); + list.addBack(1); + assertTrue(itr.hasNext()); + assertEquals(1, itr.next()); + } + + @Test + void iteratorMultiple() { + LinkedIntList list = new LinkedIntList(); + Iterator itr = list.iterator(); + list.addBack(1); + list.addBack(2); + + assertTrue(itr.hasNext()); + assertEquals(1, itr.next()); + } + + @Test + void testToStringEmpty() { + LinkedIntList list = new LinkedIntList(); + assertEquals("[]", list.toString()); + } + + @Test + void testToStringPopulated() { + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + assertEquals("[1]", list.toString()); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 930198c..cd921e4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,10 @@ +import java.util.Iterator; +import java.util.LinkedList; + //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } } } \ No newline at end of file