From 1cd716cd9437847cf2fd3291637fe52e38fdb49b Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 13 Feb 2024 09:30:30 -0800 Subject: [PATCH 1/6] First changes to add what implementations we'll be making. --- src/ArrayIntList.java | 81 ++++++++++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 69 +++++++++++++++++++++++++++++++++++ src/Main.java | 18 +++++----- 3 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 src/ArrayIntList.java create mode 100644 src/LinkedIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..82c4458 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,81 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList +{ + @Override + public void addFront(int value) + { + + } + + @Override + public void addBack(int value) + { + + } + + @Override + public void add(int index, int value) + { + + } + + @Override + public void removeFront() + { + + } + + @Override + public void removeBack() + { + + } + + @Override + public int remove(int index) + { + return 0; + } + + @Override + public int get(int index) + { + return 0; + } + + @Override + public boolean contains(int value) + { + return false; + } + + @Override + public int indexOf(int value) + { + return 0; + } + + @Override + public boolean isEmpty() + { + return false; + } + + @Override + public int size() + { + return 0; + } + + @Override + public void clear() + { + + } + + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..00f68f6 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,69 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList +{ + @Override + public void addFront(int value) { + + } + + @Override + public void addBack(int value) { + + } + + @Override + public void add(int index, int value) { + + } + + @Override + public void removeFront() { + + } + + @Override + public void removeBack() { + + } + + @Override + public int remove(int index) { + return 0; + } + + @Override + public int get(int index) { + return 0; + } + + @Override + public boolean contains(int value) { + return false; + } + + @Override + public int indexOf(int value) { + return 0; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public void clear() { + + } + + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..4a7fe02 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,15 @@ //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!"); + public static void main(String[] args) + { + IntList list1 = new ArrayIntList(); + IntList list2 = new LinkedIntList(); - 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); - } + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + + System.out.println(list1); } } \ No newline at end of file From e2e0f7ae804c19f4b4f8775463d3c4fd3e2a6c9d Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 13 Feb 2024 10:41:33 -0800 Subject: [PATCH 2/6] ArrayIntList implemented and mostly tested. --- src/ArrayIntList.java | 97 ++++++++++++++++++++++++++++++++++++++----- src/Main.java | 53 +++++++++++++++++++++-- 2 files changed, 135 insertions(+), 15 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 82c4458..a799602 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -2,76 +2,151 @@ public class ArrayIntList implements IntList { + private int[] buffer; + private int size; + public ArrayIntList() + { + buffer = new int[10]; + size = 0; + } + @Override public void addFront(int value) { - + checkSize(); + buffer[size] = value; + size++; } @Override public void addBack(int value) { - + checkSize(); + add(0, value); } @Override public void add(int index, int value) { - + checkSize(); + for(int i = size; i > index; i--) + { + buffer[i] = buffer[i-1]; + } + buffer[index] = value; + size ++; } @Override public void removeFront() { - + size--; } @Override public void removeBack() { - + remove(0); } @Override public int remove(int index) { - return 0; + checkIndex(index); + int returnInt = buffer[index]; + for(int i = index; i < size; i++) + { + buffer[i] = buffer[i + 1]; + } + size --; + return returnInt; } @Override public int get(int index) { - return 0; + checkIndex(index); + return buffer[index]; } @Override public boolean contains(int value) { - return false; + return indexOf(value) >= 0; } @Override public int indexOf(int value) { - return 0; + for (int i = 0; i < size; i++) + { + if(buffer[i] == value) { + return i; + } + } + return -1; } @Override public boolean isEmpty() { - return false; + return size == 0; } @Override public int size() { - return 0; + return size; } @Override public void clear() { + buffer = new int[10]; + size = 0; + } + private void checkIndex(int index) throws IndexOutOfBoundsException + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } + } + + public String toString() + { + if(size == 0) + { + return "[]"; + } + else + { + StringBuilder returnString = new StringBuilder(); + returnString.append("[").append(buffer[0]); + for(int i = 1; i < size; i++) + { + returnString.append(", ").append(buffer[i]); + } + returnString.append("]"); + return returnString.toString(); + } + } + private void checkSize() + { + if(size < buffer.length - 2) + { + buffer = resize(); + } + } + + private int[] resize() { + int[] returnInt = new int[buffer.length * 2]; + for (int i = 0; i < size; i++) + { + returnInt[i] = buffer[i]; + } + return returnInt; } @Override diff --git a/src/Main.java b/src/Main.java index 4a7fe02..10de3c4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,12 +4,57 @@ public class Main { public static void main(String[] args) { IntList list1 = new ArrayIntList(); - IntList list2 = new LinkedIntList(); + //IntList list2 = new LinkedIntList(); - list1.addBack(42); - list1.addBack(82); - list1.addBack(97); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(4); + list1.add(2, 5); + list1.addFront(9); + list1.removeBack(); + list1.removeFront(); + list1.clear(); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); System.out.println(list1); + + System.out.println(list1.contains(2)); + System.out.println(list1.indexOf(2)); + System.out.println(list1.contains(4)); + System.out.println(list1.indexOf(4)); + System.out.println(list1.size()); + System.out.println(list1.isEmpty()); + list1.clear(); + System.out.println(list1.isEmpty()); + list1.addBack(5); + System.out.println(list1.isEmpty()); + list1.removeFront(); + System.out.println(list1.isEmpty()); + + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(4); + list1.add(2, 5); + list1.addFront(9); + System.out.println(list1); + System.out.println(list1.get(2)); + System.out.println(list1); + list1.remove(2); + System.out.println(list1); + + } } \ No newline at end of file From ee833d8a10608cc2965bea447ee0374c225e7373 Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 13 Feb 2024 10:45:00 -0800 Subject: [PATCH 3/6] Added throws for removing from empty array. --- src/ArrayIntList.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index a799602..f088842 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -40,6 +40,10 @@ public void add(int index, int value) @Override public void removeFront() { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } size--; } @@ -52,6 +56,10 @@ public void removeBack() @Override public int remove(int index) { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } checkIndex(index); int returnInt = buffer[index]; for(int i = index; i < size; i++) From 808d3083ff5d3b5de0c654ad6f24ae0cac759d7f Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 13 Feb 2024 10:54:47 -0800 Subject: [PATCH 4/6] Fixed a resize mistake. --- src/ArrayIntList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index f088842..1c83da2 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -141,7 +141,7 @@ public String toString() } private void checkSize() { - if(size < buffer.length - 2) + if(size > buffer.length - 2) { buffer = resize(); } From 491f0692a2ea694b370fb542770c676c05ffe20e Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 27 Feb 2024 11:06:14 -0800 Subject: [PATCH 5/6] All lists should be working. Added testing. --- IntListReview.iml | 32 +++ src/ArrayIntList.java | 49 +++- src/ArrayIntListTest.java | 551 ++++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 270 ++++++++++++++++-- src/LinkedIntListTest.java | 553 +++++++++++++++++++++++++++++++++++++ src/Main.java | 12 +- 6 files changed, 1435 insertions(+), 32 deletions(-) create mode 100644 src/ArrayIntListTest.java create mode 100644 src/LinkedIntListTest.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..1df54e0 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 1c83da2..6853689 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class ArrayIntList implements IntList { @@ -11,7 +12,8 @@ public ArrayIntList() } @Override - public void addFront(int value) + // Normally fast (constant time unless you have to resize) + public void addBack(int value) { checkSize(); buffer[size] = value; @@ -19,26 +21,30 @@ public void addFront(int value) } @Override - public void addBack(int value) + //Slow must move every element in array O(N) + public void addFront(int value) { checkSize(); add(0, value); } @Override + //Slow, must move every element in array located above entry index O(N) public void add(int index, int value) { checkSize(); + size ++; + checkIndex(index); for(int i = size; i > index; i--) { buffer[i] = buffer[i-1]; } buffer[index] = value; - size ++; } @Override - public void removeFront() + // Fast, constant time. + public void removeBack() { if(size == 0) { @@ -48,12 +54,14 @@ public void removeFront() } @Override - public void removeBack() + //Slow must move every element in array O(N) + public void removeFront() { remove(0); } @Override + //Slow, must move every element in array located above entry index O(N) public int remove(int index) { if(size == 0) @@ -71,6 +79,7 @@ public int remove(int index) } @Override + //Very fast operation. O(1) constant time public int get(int index) { checkIndex(index); @@ -146,7 +155,7 @@ private void checkSize() buffer = resize(); } } - + // Slow because you must "touch" every element in array to copy to new array O(N) private int[] resize() { int[] returnInt = new int[buffer.length * 2]; @@ -159,6 +168,32 @@ private int[] resize() { @Override public Iterator iterator() { - return null; + return new ArrayIntListIterator(); + } + + private class ArrayIntListIterator implements Iterator + { + private int position; + + public ArrayIntListIterator() + { + position = 0; + } + + public boolean hasNext() + { + return position != size; + } + + public Integer next() + { + if(hasNext()) + { + Integer output = buffer[position]; + position++; + return output; + } + throw new NoSuchElementException("No elements left."); + } } } diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..644d889 --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,551 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayIntListTest { + @Test + void addEmptyFront() + { + // List of one + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + // List of one + @Test + void addToListOfOneFront() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(0); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42, 0]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addFront(0); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addFront(0); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000, 0]"); + } + // List of large + @Test + void addToListOfManyFront() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(0); + testList1.addFront(-42); + testList1.addFront(-51); + testList1.addFront(-41); + testList1.addFront(-31); + testList1.addFront(-21); + testList1.addFront(-11); + testList1.addFront(-1); + testList1.addFront(0); + testList1.addFront(1); + testList1.addFront(11); + testList1.addFront(21); + testList1.addFront(31); + testList1.addFront(41); + testList1.addFront(51); + testList1.addFront(61); + testList1.addFront(71); + testList1.addFront(81); + testList1.addFront(91); + testList1.addFront(101); + testList1.addFront(1000); + testList1.addFront(10000); + testList1.addFront(100000); + testList1.addFront(1000000); + testList1.addFront(10000000); + String out = testList1.toString(); + assertEquals(out, "[10000000, 1000000, 100000, 10000, 1000, 101, 91, 81, 71, 61, 51, 41, 31, 21, 11, 1, 0, -1, -11, -21, -31, -41, -51, -42, 0]"); + } + + @Test + void addEmptyBack() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void addToListOfOneBack() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[0, -42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addBack(0); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addBack(0); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[0, 420000]"); + } + @Test + void addToListOfManyBack() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void addToEmpty() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void removeFrontEmptyList() { + ArrayIntList testList1 = new ArrayIntList(); + try + { + testList1.removeFront(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeFrontListOfOne() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + testList2.removeFront(); + out = testList2.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + testList3.removeFront(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeFrontLargeList() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void removeBackEmptyList() + { + ArrayIntList testList1 = new ArrayIntList(); + try + { + testList1.removeBack(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeBackListOfOne() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + testList2.removeBack(); + out = testList2.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + testList3.removeBack(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeBackLargeList() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void remove() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.remove(0)); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + + assertEquals(10000000, testList1.remove(23)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + + assertEquals(21, testList1.remove(10)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void get() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.get(0)); + assertEquals(10000000, testList1.get(24)); + assertEquals(-11, testList1.get(6)); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void contains() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(true, testList1.contains(0)); + assertEquals(false, testList1.contains(24)); + assertEquals(true, testList1.contains(-31)); + assertEquals(true, testList1.contains(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void indexOf() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.indexOf(0)); + assertEquals(-1, testList1.indexOf(24)); + assertEquals(4, testList1.indexOf(-31)); + assertEquals(24, testList1.indexOf(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void isEmpty() { + ArrayIntList testList1 = new ArrayIntList(); + assertEquals(true, testList1.isEmpty()); + testList1.add(0,-42); + assertEquals(false, testList1.isEmpty()); + testList1.removeBack(); + assertEquals(true, testList1.isEmpty()); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + assertEquals(true, testList2.isEmpty()); + testList2.add(0,-42); + assertEquals(false, testList2.isEmpty()); + testList2.removeFront(); + assertEquals(true, testList2.isEmpty()); + out = testList2.toString(); + assertEquals(out, "[]"); + } + + @Test + void size() { + ArrayIntList testList1 = new ArrayIntList(); + assertEquals(0, testList1.size()); + testList1.addBack(0); + assertEquals(1, testList1.size()); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(25, testList1.size()); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + testList1.clear(); + assertEquals(0, testList1.size()); + } + + @Test + void clear() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.clear(); + + String out = testList1.toString(); + assertEquals(out, "[]"); + } +} \ No newline at end of file diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 00f68f6..fe0bb78 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -1,69 +1,299 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedIntList implements IntList { + int size; + IntNode headNode; + IntNode tailNode; + public LinkedIntList() + { + 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. + * This is a fast function. O(1), no loops required. + * + * @param value value to be inserted + */ @Override - public void addFront(int value) { - + public void addFront(int value) + { + if(size == 0) + { + headNode = new IntNode(value); + tailNode = headNode; + } + else + { + headNode = new IntNode(value, headNode); + } + size++; } @Override - public void addBack(int value) { - + public void addBack(int value) + { + if(size == 0) + { + headNode = new IntNode(value); + tailNode = headNode; + } + else + { + tailNode.setNext(new IntNode(value)); + tailNode = tailNode.nextNode(); + } + size++; } @Override - public void add(int index, int value) { - + public void add(int index, int value) throws IndexOutOfBoundsException + { + if(index > size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } + if(index == 0) + { + addFront(value); + } + else if(index == size-2) + { + addBack(value); + } + else + { + IntNode tempNode = headNode; + for (int i = 0; i < index -1; i ++) + { + tempNode = tempNode.nextNode(); + } + tempNode.setNext(new IntNode(value, tempNode.nextNode())); + size++; + } } @Override - public void removeFront() { - + public void removeFront() + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + if(size == 1) + { + clear(); + } + else { + headNode = headNode.nextNode(); + size--; + } } @Override - public void removeBack() { - + public void removeBack() + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + if(size == 1) + { + clear(); + } + else + { + remove(size - 1); + } } @Override - public int remove(int index) { - return 0; + public int remove(int index) + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + checkIndex(index); + if(size == 1) + { + int returnValue = headNode.innerValue; + clear(); + return returnValue; + } + if(index == 0) + { + int returnValue = headNode.innerValue; + removeFront(); + return returnValue; + } + else + { + IntNode tempNode = headNode; + int returnValue; + for (int i = 0; i < index - 1; i++) + { + tempNode = tempNode.nextNode(); + } + returnValue = tempNode.nextNode().innerValue; + if (index == size - 1) + { + tailNode = tempNode; + } + tempNode.setNext(tempNode.nextNode().nextNode()); + size--; + return returnValue; + } } @Override - public int get(int index) { - return 0; + public int get(int index) + { + checkIndex(index); + if(index == size-1) + { + return tailNode.innerValue; + } + else + { + IntNode tempNode = headNode; + for(int i = 0; i < index; i++) + { + tempNode = tempNode.nextNode(); + } + return tempNode.innerValue; + } } @Override - public boolean contains(int value) { + public boolean contains(int value) + { + IntNode tempNode = headNode; + for(int i = 0; i < size; i++) + { + if(tempNode.innerValue == value) + { + return true; + } + tempNode = tempNode.nextNode(); + } return false; } @Override public int indexOf(int value) { - return 0; + IntNode tempNode = headNode; + for(int i = 0; i < size; i++) + { + if(tempNode.innerValue == value) + { + return i; + } + tempNode = tempNode.nextNode(); + } + return -1; } @Override public boolean isEmpty() { - return false; + return size == 0; } @Override public int size() { - return 0; + return size; } @Override - public void clear() { + public void clear() + { + headNode = null; + tailNode = null; + size = 0; + } + @Override + public String toString() + { + StringBuilder outputString = new StringBuilder("["); + if(headNode!= null) + { + outputString.append(headNode.innerValue); + IntNode tempNode = headNode; + + while (tempNode.nextNode() != null) { + tempNode = tempNode.nextNode(); + outputString.append(", ").append(tempNode.innerValue); + } + } + outputString.append("]"); + return outputString.toString(); + } + private void checkIndex(int index) throws IndexOutOfBoundsException + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } } @Override public Iterator iterator() { - return null; + return new LinkedIntListIterator(); + } + private class LinkedIntListIterator implements Iterator + { + private IntNode position; + + public LinkedIntListIterator() + { + position = headNode; + } + + public boolean hasNext() + { + return position != null; + } + + public Integer next() + { + if(position != null) + { + Integer output = position.innerValue; + position = position.nextNode(); + return output; + } + throw new NoSuchElementException("No elements left."); + } + } + private static class IntNode + { + public int innerValue; + private IntNode nodePointer; + + public IntNode( int x ) + { + this(x, null); + } + public IntNode ( int x, IntNode connect) + { + innerValue = x; + nodePointer = connect; + } + public IntNode nextNode() + { + return nodePointer; + } + public void setNext(IntNode x) + { + nodePointer = x; + } + public boolean hasNext() + { + return nodePointer != null; + } } } diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java new file mode 100644 index 0000000..e402916 --- /dev/null +++ b/src/LinkedIntListTest.java @@ -0,0 +1,553 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest { + + // List of zero + @Test + void addEmptyFront() + { + // List of one + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + // List of one + @Test + void addToListOfOneFront() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(0); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42, 0]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addFront(0); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addFront(0); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000, 0]"); + } + // List of large + @Test + void addToListOfManyFront() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(0); + testList1.addFront(-42); + testList1.addFront(-51); + testList1.addFront(-41); + testList1.addFront(-31); + testList1.addFront(-21); + testList1.addFront(-11); + testList1.addFront(-1); + testList1.addFront(0); + testList1.addFront(1); + testList1.addFront(11); + testList1.addFront(21); + testList1.addFront(31); + testList1.addFront(41); + testList1.addFront(51); + testList1.addFront(61); + testList1.addFront(71); + testList1.addFront(81); + testList1.addFront(91); + testList1.addFront(101); + testList1.addFront(1000); + testList1.addFront(10000); + testList1.addFront(100000); + testList1.addFront(1000000); + testList1.addFront(10000000); + String out = testList1.toString(); + assertEquals(out, "[10000000, 1000000, 100000, 10000, 1000, 101, 91, 81, 71, 61, 51, 41, 31, 21, 11, 1, 0, -1, -11, -21, -31, -41, -51, -42, 0]"); + } + + @Test + void addEmptyBack() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void addToListOfOneBack() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[0, -42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addBack(0); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addBack(0); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[0, 420000]"); + } + @Test + void addToListOfManyBack() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void addToEmpty() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void removeFrontEmptyList() { + LinkedIntList testList1 = new LinkedIntList(); + try + { + testList1.removeFront(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeFrontListOfOne() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + testList2.removeFront(); + out = testList2.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + testList3.removeFront(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeFrontLargeList() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void removeBackEmptyList() + { + LinkedIntList testList1 = new LinkedIntList(); + try + { + testList1.removeBack(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeBackListOfOne() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + testList2.removeBack(); + out = testList2.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + testList3.removeBack(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeBackLargeList() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void remove() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.remove(0)); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + + assertEquals(10000000, testList1.remove(23)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + + assertEquals(21, testList1.remove(10)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void get() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.get(0)); + assertEquals(10000000, testList1.get(24)); + assertEquals(-11, testList1.get(6)); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void contains() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(true, testList1.contains(0)); + assertEquals(false, testList1.contains(24)); + assertEquals(true, testList1.contains(-31)); + assertEquals(true, testList1.contains(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void indexOf() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.indexOf(0)); + assertEquals(-1, testList1.indexOf(24)); + assertEquals(4, testList1.indexOf(-31)); + assertEquals(24, testList1.indexOf(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void isEmpty() { + LinkedIntList testList1 = new LinkedIntList(); + assertEquals(true, testList1.isEmpty()); + testList1.add(0,-42); + assertEquals(false, testList1.isEmpty()); + testList1.removeBack(); + assertEquals(true, testList1.isEmpty()); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + assertEquals(true, testList2.isEmpty()); + testList2.add(0,-42); + assertEquals(false, testList2.isEmpty()); + testList2.removeFront(); + assertEquals(true, testList2.isEmpty()); + out = testList2.toString(); + assertEquals(out, "[]"); + } + + @Test + void size() { + LinkedIntList testList1 = new LinkedIntList(); + assertEquals(0, testList1.size()); + testList1.addBack(0); + assertEquals(1, testList1.size()); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(25, testList1.size()); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + testList1.clear(); + assertEquals(0, testList1.size()); + } + + @Test + void clear() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.clear(); + + String out = testList1.toString(); + assertEquals(out, "[]"); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 10de3c4..c12a751 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,8 +3,8 @@ public class Main { public static void main(String[] args) { - IntList list1 = new ArrayIntList(); - //IntList list2 = new LinkedIntList(); + //IntList list1 = new ArrayIntList(); + IntList list1 = new LinkedIntList(); list1.addBack(1); list1.addBack(2); @@ -27,7 +27,11 @@ public static void main(String[] args) list1.addBack(1); list1.addBack(2); list1.addBack(3); - + for(int i : list1) + { + System.out.printf(i + ", "); + } + System.out.println(); System.out.println(list1); System.out.println(list1.contains(2)); @@ -54,7 +58,5 @@ public static void main(String[] args) System.out.println(list1); list1.remove(2); System.out.println(list1); - - } } \ No newline at end of file From 2fbb6312d211f27500eb2cd9cce6b70fbd27d27d Mon Sep 17 00:00:00 2001 From: EBoydGRC Date: Tue, 27 Feb 2024 11:11:04 -0800 Subject: [PATCH 6/6] Testing changed to use assertTrue and assertFalse. --- src/ArrayIntListTest.java | 20 ++++++++++---------- src/LinkedIntListTest.java | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java index 644d889..bb0f50f 100644 --- a/src/ArrayIntListTest.java +++ b/src/ArrayIntListTest.java @@ -410,10 +410,10 @@ void contains() { testList1.addBack(100000); testList1.addBack(1000000); testList1.addBack(10000000); - assertEquals(true, testList1.contains(0)); - assertEquals(false, testList1.contains(24)); - assertEquals(true, testList1.contains(-31)); - assertEquals(true, testList1.contains(10000000)); + assertTrue(testList1.contains(0)); + assertFalse(testList1.contains(24)); + assertTrue(testList1.contains(-31)); + assertTrue(testList1.contains(10000000)); String out = testList1.toString(); assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); @@ -459,20 +459,20 @@ void indexOf() { @Test void isEmpty() { ArrayIntList testList1 = new ArrayIntList(); - assertEquals(true, testList1.isEmpty()); + assertTrue(testList1.isEmpty()); testList1.add(0,-42); - assertEquals(false, testList1.isEmpty()); + assertFalse(testList1.isEmpty()); testList1.removeBack(); - assertEquals(true, testList1.isEmpty()); + assertTrue(testList1.isEmpty()); String out = testList1.toString(); assertEquals(out, "[]"); ArrayIntList testList2 = new ArrayIntList(); - assertEquals(true, testList2.isEmpty()); + assertTrue(testList2.isEmpty()); testList2.add(0,-42); - assertEquals(false, testList2.isEmpty()); + assertFalse(testList2.isEmpty()); testList2.removeFront(); - assertEquals(true, testList2.isEmpty()); + assertTrue(testList2.isEmpty()); out = testList2.toString(); assertEquals(out, "[]"); } diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java index e402916..ab85d13 100644 --- a/src/LinkedIntListTest.java +++ b/src/LinkedIntListTest.java @@ -412,10 +412,10 @@ void contains() { testList1.addBack(100000); testList1.addBack(1000000); testList1.addBack(10000000); - assertEquals(true, testList1.contains(0)); - assertEquals(false, testList1.contains(24)); - assertEquals(true, testList1.contains(-31)); - assertEquals(true, testList1.contains(10000000)); + assertTrue(testList1.contains(0)); + assertFalse(testList1.contains(24)); + assertTrue(testList1.contains(-31)); + assertTrue(testList1.contains(10000000)); String out = testList1.toString(); assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); @@ -461,20 +461,20 @@ void indexOf() { @Test void isEmpty() { LinkedIntList testList1 = new LinkedIntList(); - assertEquals(true, testList1.isEmpty()); + assertTrue(testList1.isEmpty()); testList1.add(0,-42); - assertEquals(false, testList1.isEmpty()); + assertFalse(testList1.isEmpty()); testList1.removeBack(); - assertEquals(true, testList1.isEmpty()); + assertTrue(testList1.isEmpty()); String out = testList1.toString(); assertEquals(out, "[]"); LinkedIntList testList2 = new LinkedIntList(); - assertEquals(true, testList2.isEmpty()); + assertTrue(testList2.isEmpty()); testList2.add(0,-42); - assertEquals(false, testList2.isEmpty()); + assertFalse(testList2.isEmpty()); testList2.removeFront(); - assertEquals(true, testList2.isEmpty()); + assertTrue(testList2.isEmpty()); out = testList2.toString(); assertEquals(out, "[]"); }