diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..66c15d2 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..d8e612b --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,297 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + private int size; + private E[] buffer; + + // constructor + public ArrayList() { + // initialize my fieldss + size = 0; + buffer = (E[]) new Object[10]; + } + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + // If the buffer is already full, we increase the size by 2 times + if (buffer.length == size) { + increaseSize(size * 2); + } + + for (int i = size; i > 0; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[0] = item; + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + // If the buffer is already full, we increase the size by 2 times + if (buffer.length == size) { + increaseSize(size * 2); + } + + size++; + buffer[size] = item; + } + + /** + * Add an item at specified index (position). + * + * @param index the index where the item should be added + * @param item the item to be added + */ + @Override + public void add(int index, E item) { + // If the buffer is already full, we increase the size by 2 times + if (buffer.length == size) { + increaseSize(size * 2); + } + + size++; + + for (int i = size; i >= index; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[index] = item; + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + // If the i is greater than the current size or less than 0 than we know that it's not possible + if (i > size || i < 0) { + return null; + } + else { + return buffer[i]; + } + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param i the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int i, E item) { + // If i is greater than 0 but equal or less than the size, we will know it's within size + if (i >= 0 && i <= size) { + buffer[i] = item; + } + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + // If the size is 0, that means that it's empty so there's no need + if (size == 0) { + return null; + } + + E removed = buffer[0]; + + buffer[0] = null; + + for (int i = 0; i < size; i++) { + buffer[i] = buffer[i + 1]; + } + + buffer[size] = null; // Changes the back to null since we would move everything to the left so the index should be nothing now + size--; + + return removed; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + // If the size is 0, that means that it's empty so there's no need + if (size == 0) { + return null; + } + + E removed = buffer[size]; + buffer[size] = null; + size--; + return removed; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + // If the size is not 0, run it + if (size != 0) { + // for each item in the array + for (int i = 0; i < size; i++) { + // if the item is the same remove it + if (buffer[i] == item) { + buffer[i] = null; + + // for each of the remaining index we will shift it to the left + for (int j = i; j < size; j++) { + buffer[i] = buffer[i + 1]; + } + + // Lower size by 1 + size--; + } + } + } + } + + /** + * Remove item at a specified index. + * + * @param index the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int index) { + // If the size is 0, that means that it's empty so there's no need + if (size == 0) { + return null; + } + else { + E removedInt = buffer[index]; + + for (int i = index; i <= size; i++) { + buffer[i] = buffer[i + 1]; + } + + size--; + return removedInt; + } + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + boolean doseItContains = false; + + for (int i = 0; i <= size; i++) { + if (buffer[i] == item) { + doseItContains = true; + } + } + + return doseItContains; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + if (size == 0) { + return true; + } + else { + return false; + } + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + //private fields + private int i; + + private ArrayListIterator() { + i = 0; + } + + /** + * Returns true if the iteration has more elements. + * + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() { + return i < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public E next() { + if (i >= size) { + throw new NoSuchElementException("i is now out of bounds"); + } + + E currentValue = buffer[i]; + i++; + + return currentValue; + } + } + + // resizing + private void increaseSize(int size) { + E[] placeholder = (E[]) new Object[size]; + System.arraycopy(buffer, 0, placeholder, 0, buffer.length); + buffer = placeholder; + } +} diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..5e36a4b --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,23 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +public interface Bag extends Iterable { + /** + * Add an item + * @param item the item to be added + */ + void add(E item); + + /** + * Checks to see if the bad is empty + * @return true of the stack is empty, false otherwise + */ + boolean isEmpty(); + + /** + * Returns a count of the number of items in the bag. + * @return the number of items in the bag + */ + int size(); +} diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..27be563 --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,68 @@ +import java.util.Iterator; + +public class LinkedBag implements Bag { + private Node first; // first node in list + private int size; // size + private class Node + { + E item; + Node next; + } + /** + * Add an item + * + * @param item the item to be added + */ + @Override + public void add(E item) { + // same as push() in Stack + Node oldfirst = first; + first = new Node(); + first.item = item; + first.next = oldfirst; + size++; + } + + /** + * Checks to see if the bad is empty + * + * @return true of the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns a count of the number of items in the bag. + * + * @return the number of items in the bag + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { return new ListIterator(); } + private class ListIterator implements Iterator + { + private Node current = first; + public boolean hasNext() + { return current != null; } + public void remove() { } + public E next() + { + E item = current.item; + current = current.next; + return item; + } + } + +} diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..c5982d9 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,292 @@ +import java.util.*; + +public class LinkedList implements List { + // define what a node is + private class Node { + E data; + Node next; + } + + // set up the head field + private Node head; + + // set up the size field + private int size; + + // adda constructor to initialize the fields + public LinkedList() { + head = null; + size = 0; + } + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + // set up a new node + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + Node theNewOne = new Node(); + theNewOne.data = item; + Node currentNode = head; + + while (currentNode.next != null) { + currentNode = currentNode.next; + } + currentNode.next = theNewOne; + + size++; + } + + /** + * Add an item at specified index (position). + * + * @param index the index where the item should be added + * @param item the item to be added + */ + @Override + public void add(int index, E item) { + Node currentNode = head; + E lastData; + + // If the index is less than or equal to size since it won't makes sense to work if it's bigger than the size + if (index <= size) { + for (int i = 0; i < size - index; i++) { + currentNode = currentNode.next; + } + + // First time + lastData = currentNode.next.data; // Grabs the next data so it won't be lost + currentNode.next = currentNode; // The next node is equal to the current node + currentNode.data = item; // Replaces the current data to the value since we are adding the index here + currentNode = currentNode.next; // Then we move to the next node + + // The rest of the time + for (int i = size + 1; i > index; i--) { + currentNode.data = lastData; + lastData = currentNode.next.data; // Grabs the next data so it won't be lost + currentNode.next = currentNode; // The next node is equal to the current node + currentNode = currentNode.next; // Then we move to the next node + } + + size++; + } + } + + /** + * Get the item at a specified index. + * + * @param index the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int index) { + Node currentNode = head; + + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + + return currentNode.data; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param index the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int index, E item) { + Node currentNode = head; + + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + + currentNode.data = item; + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + Node currentNode = head; + E placeholder = currentNode.data; + + for (int i = 0; i < size - 1; i++) { + currentNode.data = currentNode.next.data; + currentNode = currentNode.next; + } + + size--; + + return placeholder; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + Node currentNode = head; + + while(currentNode.next != null) { + currentNode = currentNode.next; + } + + E placeholder = currentNode.data; + currentNode.data = null; + currentNode.next = null; + + size--; + + return placeholder; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + Node currentNode = head; + + for (int i = 0; i < size; i++) { + if (currentNode.data == item) { + currentNode.data = null; + } + currentNode = currentNode.next; + } + } + + /** + * Remove item at a specified index. + * + * @param index the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int index) { + Node currentNode = head; + + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + + return currentNode.data; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + Node currentNode = head; + boolean containValue = false; + + for (int i = 0; i < size; i++) { + if (currentNode.data == item) { + containValue = true; + } + + currentNode = currentNode.next; + } + + return containValue; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + if (size > 0) { + return false; + } + else { + return true; + } + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new LinkedList.LinkedListIterator(); + } + + public class LinkedListIterator implements Iterator { + + private Node currentNode; + + private LinkedListIterator() { + currentNode = head; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return currentNode != null; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public E next() { + E currentNodeData = currentNode.data; + currentNode = currentNode.next; + return currentNodeData; + } + } +} diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..9dd541e --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,90 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import java.util.Iterator; + +public class LinkedQueue implements Queue { + private Node first; // link to least recently added node + private Node last; // link to most recently added node + private int size; // number of items on the queue + private class Node + { // nested class to define nodes + E item; + Node next; + } + + /** + * Add an item to the queue. + * + * @param item the item to be added + */ + @Override + public void enqueue(E item) { + // Add item to the end of the list. + Node oldlast = last; + last = new Node(); + last.item = item; + last.next = null; + if (isEmpty()) first = last; + else oldlast.next = last; + size++; + } + + /** + * Remove an item from the queue. + * + * @return the item that was removed + */ + @Override + public E dequeue() { + // Remove item from the beginning of the list. + E item = first.item; + first = first.next; + if (isEmpty()) last = null; + size--; + return item; + } + + /** + * Checks to see if the queue is empty. + * + * @return true if the queue is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return first == null; + } + + /** + * Returns a count of the number of items in the queue. + * + * @return the number of items in the queue + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { return new ListIterator(); } + private class ListIterator implements Iterator + { + private Node current = first; + public boolean hasNext() + { return current != null; } + public void remove() { } + public E next() + { + E item = current.item; + current = current.next; + return item; + } + } +} diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..0db1ded --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,98 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import java.util.Iterator; + +public class LinkedStack implements Stack { + private Node first; // top of stack (most recently added node) + private int size; // number of items + private class Node + { // nested class to define nodes + E item; + Node next; + } + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + // Add item to top of stack. + Node oldfirst = first; + first = new Node(); + first.item = item; + first.next = oldfirst; + size++; + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + // Remove item from top of stack. + E item = first.item; + first = first.next; + size--; + return item; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return first.item; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { return new ListIterator(); } + + private class ListIterator implements Iterator + { + private Node current = first; + public boolean hasNext() + { return current != null; } + public void remove() { } + public E next() + { + E item = current.item; + current = current.next; + return item; + } + } +} diff --git a/src/List.java b/src/List.java index 2bf4aef..1bf0622 100644 --- a/src/List.java +++ b/src/List.java @@ -1,3 +1,7 @@ +/* +* SDEV333 Best class :D +* Ming Li +*/ /*** * List interface (API / abstract data type) * @param Class or data type of the items in the list. diff --git a/src/Main.java b/src/Main.java index 8f8f984..79fc661 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,7 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ //TIP To Run code, press or // click the icon in the gutter. public class Main { @@ -5,6 +9,5 @@ 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.println("Hello and welcome!"); - } } \ No newline at end of file diff --git a/src/MathSet.java b/src/MathSet.java index 5b87e0d..10417af 100644 --- a/src/MathSet.java +++ b/src/MathSet.java @@ -1,3 +1,7 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ /** * MathSet API (interface / abstract data type) * represents a mathematical set. Sets in mathematics diff --git a/src/Queue.java b/src/Queue.java index ab5ca29..ab98650 100644 --- a/src/Queue.java +++ b/src/Queue.java @@ -1,3 +1,7 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ /** * FIFO (first-in, first-out) Queue API * @param class / data type of the items in the queue diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..2148178 --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,100 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ + +import java.util.Iterator; + +public class ResizingArrayStack implements Stack { + private int size; + private E[] buffer; + + public ResizingArrayStack() { + // initialize my fieldss + size = 0; + buffer = (E[]) new Object[10]; + } + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + // Add item to top of stack. + if (size == buffer.length) resize(2*buffer.length); + buffer[size++] = item; + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + // Remove item from top of stack. + E item = buffer[--size]; + buffer[size] = null; // Avoid loitering (see text). + if (size > 0 && size == buffer.length/4) resize(buffer.length/2); + return item; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return buffer[0]; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new ReverseArrayIterator(); + } + + private void resize(int max) + { // Move stack to a new array of size max. + E[] temp = (E[]) new Object[max]; + for (int i = 0; i < size; i++) + temp[i] = buffer[i]; + buffer = temp; + } + + private class ReverseArrayIterator implements Iterator + { // Support LIFO iteration. + private int i = size; + public boolean hasNext() { return i > 0; } + public E next() { return buffer[--i]; } + public void remove() { } + } +} diff --git a/src/Stack.java b/src/Stack.java index bb1000d..81489a2 100644 --- a/src/Stack.java +++ b/src/Stack.java @@ -1,3 +1,7 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ /** * Stack (LIFO: last-in, first-out) API * @param class / data type of the items in the stack diff --git a/src/StackTestClient.java b/src/StackTestClient.java new file mode 100644 index 0000000..293b765 --- /dev/null +++ b/src/StackTestClient.java @@ -0,0 +1,30 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import java.util.*; + +public class StackTestClient { + public static void main(String[] args) { + // Stack s = new ResizingArrayStack<>(); + Stack s = new LinkedStack<>(); + + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + + // while there is next + while (in.hasNext()) { + // saves the next item + String item = in.next(); + + // if "-" push "item", otherwise print s.pop() + if (!item.equals("-")) { + s.push(item); + } + else if (!s.isEmpty()) { + System.out.println(s.pop() + " "); + } + } + + System.out.println("(" + s.size() + " left on the stack)"); + } +} diff --git a/src/Stats.java b/src/Stats.java new file mode 100644 index 0000000..c2b99e1 --- /dev/null +++ b/src/Stats.java @@ -0,0 +1,41 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import java.util.*; + +public class Stats { + public static void main(String[] args) { + Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); + + Bag numbers = new LinkedBag<>(); + + while (in.hasNextDouble()) { + double num = in.nextDouble(); + + // Print out the num + System.out.println(num); + + numbers.add(num); + } + + int N = numbers.size(); + + double sum = 0.0; + + for (double x : numbers) + sum += x; + + double mean = sum/N; + + sum = 0.0; + + for (double x : numbers) + sum += (x - mean)*(x - mean); + + double std = Math.sqrt(sum/(N-1)); + + System.out.println("Mean: " + mean); + System.out.println("Std dev: "+ std); + } +} diff --git a/test/ArrayListTest.java b/test/ArrayListTest.java new file mode 100644 index 0000000..5933624 --- /dev/null +++ b/test/ArrayListTest.java @@ -0,0 +1,101 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import static org.junit.jupiter.api.Assertions.*; + +public class ArrayListTest { + + // testing placeholders + ArrayList arrayTest = new ArrayList<>(); + + @org.junit.jupiter.api.Test + void addFront() { + arrayTest.addFront(1); + assertEquals(1, arrayTest.get(0)); + } + + @org.junit.jupiter.api.Test + void addBack() { + arrayTest.addBack(1); + assertEquals(1, arrayTest.get(arrayTest.size())); + } + + @org.junit.jupiter.api.Test + void add() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + arrayTest.addFront(4); + + arrayTest.add(3, 2); + assertEquals(2, arrayTest.get(3)); + } + + @org.junit.jupiter.api.Test + void get() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + arrayTest.addFront(4); + + arrayTest.get(1); + assertEquals(3, arrayTest.get(1)); + } + + @org.junit.jupiter.api.Test + void set() { + arrayTest.addFront(1); + + arrayTest.set(0, 21); + assertEquals(21, arrayTest.get(0)); + } + + @org.junit.jupiter.api.Test + void removeFront() { + arrayTest.addFront(2); + arrayTest.addFront(1); + + arrayTest.removeFront(); + assertEquals(2, arrayTest.get(0)); + } + + @org.junit.jupiter.api.Test + void removeBack() { + arrayTest.addFront(2); + arrayTest.addFront(1); + + arrayTest.removeBack(); + assertEquals(2, arrayTest.get(arrayTest.size())); + } + + @org.junit.jupiter.api.Test + void remove() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + arrayTest.addFront(4); + + assertEquals(2, arrayTest.remove(2)); + } + + @org.junit.jupiter.api.Test + void contains() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + arrayTest.addFront(42); + + assertEquals(true, arrayTest.contains(42)); + } + + @org.junit.jupiter.api.Test + void isEmpty() { + assertEquals(true, arrayTest.isEmpty()); + } + + @org.junit.jupiter.api.Test + void size() { + assertEquals(0, arrayTest.size()); + } +} diff --git a/test/LinkedListTest.java b/test/LinkedListTest.java new file mode 100644 index 0000000..f188d6a --- /dev/null +++ b/test/LinkedListTest.java @@ -0,0 +1,108 @@ +/* + * SDEV333 Best class :D + * Ming Li + */ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + + // testing placeholders + LinkedList arrayTest = new LinkedList<>(); + + @Test + void addFront() { + arrayTest.addFront(1); + assertEquals(1, arrayTest.get(0)); + } + + @Test + void addBack() { + arrayTest.addFront(1); + arrayTest.addFront(1); + + arrayTest.addBack(2); + assertEquals(2, arrayTest.get(arrayTest.size() - 1)); + } + + @Test + void add() { + arrayTest.addFront(1); + arrayTest.add(1, 21); + assertEquals(21, arrayTest.get(1)); + } + + @Test + void get() { + arrayTest.addFront(2); + arrayTest.addFront(32); + arrayTest.addFront(2); + + assertEquals(32, arrayTest.get(1)); + } + + @Test + void set() { + arrayTest.addFront(2); + arrayTest.addFront(32); + arrayTest.addFront(2); + + arrayTest.set(1, 200); + assertEquals(200, arrayTest.get(1)); + } + + @Test + void removeFront() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + + arrayTest.removeFront(); + assertEquals(2, arrayTest.get(0)); + } + + @Test + void removeBack() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + + arrayTest.removeBack(); + assertEquals(2, arrayTest.get(arrayTest.size() - 1)); + } + + @Test + void remove() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + + assertEquals(2, arrayTest.remove(1)); + } + + @Test + void contains() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(53); + + assertEquals(true, arrayTest.contains(53)); + } + + @Test + void isEmpty() { + arrayTest.addFront(1); + + assertEquals(false, arrayTest.isEmpty()); + } + + @Test + void size() { + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + + assertEquals(3, arrayTest.size()); + } +} \ No newline at end of file