diff --git a/.gitignore b/.gitignore index aa241d3..f5c955d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ .settings .project +.idea + # Mobile Tools for Java (J2ME) .mtj.tmp/ diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..c20d346 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2526f7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ca63f44 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/generics.iml b/generics.iml new file mode 100644 index 0000000..9717c26 --- /dev/null +++ b/generics.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java new file mode 100644 index 0000000..651f58f --- /dev/null +++ b/src/main/java/MyArrayList.java @@ -0,0 +1,154 @@ +import java.util.Arrays; + +public class MyArrayList { + + private int size; + private static final int DEFAULT_CAPACITY = 10; + private Object[] inputArray; + + + public MyArrayList() { + this(DEFAULT_CAPACITY); + } + + public MyArrayList(int capacity) { + this.inputArray = new Object[capacity]; + this.size = 0; + } + + /** + * @return current number of elements in this list + */ + public int size() { + return size; + } + + /** + * @return true if this list contains no elements + */ + public Boolean isEmpty() { + if (this.size == 0) { + return true; + } + return false; + } + + /** + * Increases the capacity of the ArrayList instance, if necessary, + * to ensure that it can hold at least the number of elements + */ + private void ensureCapacity() { + if (size >= this.inputArray.length) { + int newCapacity = this.inputArray.length + DEFAULT_CAPACITY; + this.inputArray = Arrays.copyOf(this.inputArray, newCapacity); + } + } + + /** + * Appends the specified element to the end of this list + * + * @param addMe element to be appended to this list + * @return true if appended and false if not appended + */ + public Boolean add(E addMe) { + ensureCapacity(); + this.inputArray[size++] = addMe; + return true; + } + + /** + * Inserts the specified element at the specified position in this list. + * Shifts the element currently at that position(if any) and any subsequent + * elements to the right(adds 1 to the other indices). + * + * @param index at which specified element is to be inserted + * @param addMe element to be inserted to this list + * @return true if appended and false if not appended + */ + public Boolean add(int index, E addMe) { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index is too large or negative"); + } + int length = size() - index; + ensureCapacity(); + System.arraycopy(this.inputArray, index, this.inputArray, index + 1, length); + this.inputArray[index] = addMe; + return true; + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent elements to the left(subtracts one from their indices). + * + * @param index of element to be removed + * @return the element that was removed from the list + */ + @SuppressWarnings("unchecked") + public E remove(int index) { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index is too large or negative"); + } + E storePreviousElement = (E) this.inputArray[index]; + int length = size() - index; + if (length > 0) { + System.arraycopy(this.inputArray, index + 1, this.inputArray, index, length); + this.inputArray[size--] = null; + } + return storePreviousElement; + } + + /** + * Returns the element at the specified position in this list + * + * @param index of element to return + * @return element at the specified position in this list + */ + @SuppressWarnings("unchecked") + public E get(Integer index) { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index is too large or negative"); + } + return (E) this.inputArray[index]; + } + + /** + * Removes all of the elements from this list + * The list will be empty after this call returns. + */ + public void clear() { + this.inputArray = new Object[DEFAULT_CAPACITY]; + this.size = 0; + } + + /** + * @param element to be checked + * @return true if list contains the specified element. + */ + public Boolean contains(E element) { + for (Object obj : this.inputArray) { + if (obj != null && obj.equals(element)) + return true; + } + return false; + } + + /** + * Replaces the element at the specified position in this list with the specified element. + * + * @param index of the element to replace + * @param element to be stored at the specified position + * @return the element previously at the specified position + */ + @SuppressWarnings("unchecked") + public E set(int index, E element) { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index is too large or negative"); + } + E storePreviousElement = (E) this.inputArray[index]; + this.inputArray[index] = element; + return storePreviousElement; + } + +} + + diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java new file mode 100644 index 0000000..8db4dde --- /dev/null +++ b/src/main/java/MySet.java @@ -0,0 +1,253 @@ +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Collection; + + +public class MySet { + + private static final int DEFAULT_CAPACITY = 10; + + private Object[] inputArray; + private int size; + + public MySet() { + this(DEFAULT_CAPACITY); + } + + public MySet(int capacity) { + this.inputArray = new Object[capacity]; + this.size = 0; + } + + + /** + * Get set size + * + * @return current number of elements in this list + */ + public int size() { + return this.size; + } + + /** + * Add Object o to set + * + * @param element - object to be added to this set + * @return true if this set did not already contain the specified element. false if set already contains element + * and leaves the set unchanged + */ + public boolean add(E element) { + if (element == null) { + throw new NullPointerException("Specified element can't be null"); + } + if (this.contains(element)) { + return false; + } + + this.ensureCapacity(); + this.inputArray[this.size++] = element; + + return true; + } + + /** + * Check if set is empty + * + * @return true if this set contains no elements + */ + public boolean isEmpty() { + return this.size == 0; + } + + /** + * @param element to be checked + * @return true if this set contains the specified element + */ + public Boolean contains(E element) { + if (element == null) { + throw new NullPointerException("Specified element can't be null"); + } + for (int i = 0; i < this.size; i++) { + if (this.inputArray[i].equals(element)) { + return true; + } + } + return false; + } + + /** + * Removes the specified element from this set if it is present + * + * @param element - element to be removed from this set, if present + * @return true if this set contained the specified element + */ + public boolean remove(E element) { + if (element == null) { + throw new NullPointerException("Specified element can't be null"); + } + boolean canRemove = false; + int indexOf = 0; + for (int i = 0; i < this.inputArray.length; i++) { + if (this.inputArray[i] != null && this.inputArray[i].equals(element)) { + indexOf = i; + canRemove = true; + break; + } + } + + if (canRemove) { + this.inputArray[indexOf] = null; + this.inputArray = shiftAllNullsToEnd(this.inputArray); + this.size--; + } + + return canRemove; + } + + /** + * Adds all of the elements in the specified collection to this set if they're not already present + * returns false if ANY element could not be added. If the specified collection is also a set, the + * addAll operation effectively modifies this set so that its value is the union of the two sets. + * + * @param container collection containing elements to be added to this set + * @return true if this set changed as a result of the call + */ + public boolean addAll(Collection container) { + boolean allAdded = container.size() > 0; + for (E obj : container) { + if (!this.add(obj)) { + allAdded = false; + } + } + return allAdded; + } + + /** + * @param container collection to be checked for containment in this set + * @return true if this set contains all of the elements of the specified collection. + * If the specified collection is also a set, this method returns true if it is a subset of this set. + */ + public boolean containsAll(Collection container) { + for (E obj : container) { + if (!this.contains(obj)) { + return false; + } + } + return true; + } + + /** + * Remove all elements from set except those in c, keeps matching elements + * + * @param container collection containing elements to be retained in this set + * @return true if this set changed as a result of the call. + */ + public boolean retainAll(Collection container) { + for (int i = 0; i < this.inputArray.length; i++) { + if (!container.contains(this.inputArray[i])) { + this.inputArray[i] = null; + this.size--; + } + } + this.inputArray = shiftAllNullsToEnd(this.inputArray); + return true; + } + + /** + * Remove all elements that are also in c + * @param container collection containing elements to be removed from this set + * @return true if ANY elements are removed + */ + public boolean removeAll(Collection container) { + boolean anyRemoved = false; + for (E obj : container) { + if (this.remove(obj)) { + anyRemoved = true; + } + } + return anyRemoved; + } + + /** + * Removes all of the elements from this set + * The set will be empty after this call returns. + */ + public void clear() { + this.inputArray = new Object[DEFAULT_CAPACITY]; + this.size = 0; + } + + /** + * Get array representation of set + * makes a copy of input array and removes all the nulls. + * + * @return array with only elements + */ + @SuppressWarnings("unchecked") + public E[] toArray(E[] in) { + Object[] t = trimTrailingNull(this.inputArray); + return (E[]) Arrays.copyOf(t, t.length, in.getClass()); + } + + /** + * @param arr array to be altered assumed that all non-null values are contiguous starting at index 0 + * @return array with with trailing nulls removed + * Do not make generic, internal to class, + */ + @SuppressWarnings("unchecked") + private static Object[] trimTrailingNull(Object[] arr) { + int lastNotNull = 0; + for (int i = 0; i < arr.length; i++) { + if (arr[i] != null) { + lastNotNull++; + } else { + break; + } + } + if (lastNotNull == 0) { + return new Object[0]; + } + + Object[] trimmed = new Object[lastNotNull]; + for (int i = 0; i < lastNotNull; i++) { + trimmed[i] = arr[i]; + } + + return trimmed; + } + + /** + * Shift all null values to the end and make non-null values contiguous starting at index 0 + * + * @param arr array to be altered + * @return array altered + * Do not make generic, internal to class, everything internal is object, everything external is type + */ + private static Object[] shiftAllNullsToEnd(Object[] arr) { + int j = 0; + Object[] result = new Object[arr.length]; + for (int i = 0; i < arr.length; i++) { + if (arr[i] != null) { + result[j++] = arr[i]; + } + } + + return result; + } + + /** + * Increases the capacity of the backing array, if necessary, + * to ensure that it can hold at least the number of elements + */ + private void ensureCapacity() { + if (this.size >= this.inputArray.length) { + int newCapacity = this.inputArray.length + DEFAULT_CAPACITY; + this.inputArray = Arrays.copyOf(this.inputArray, newCapacity); + } + } + +} + + + + diff --git a/src/test/java/TestMyArrayList.java b/src/test/java/TestMyArrayList.java new file mode 100644 index 0000000..94f991a --- /dev/null +++ b/src/test/java/TestMyArrayList.java @@ -0,0 +1,450 @@ +import org.junit.Assert; +import org.junit.Test; + +public class TestMyArrayList { + //---------------Test Constructor + @Test + public void testConstructorDefaultSize() { + int expectedElementCount = 0; + MyArrayList genericUtility = new MyArrayList<>(); + + int actualLength = genericUtility.size(); + + Assert.assertEquals(expectedElementCount, actualLength); + } + + @Test + public void testConstructorSizeIncrease() { + int expectedSize = 3; + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("andTheOtherThing"); + + int actualLength = genericUtility.size(); + + Assert.assertEquals(expectedSize, actualLength); + } + + //---------------Test isEmpty + @Test + public void testIsSizeEmptyReturnsFalse() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("andTheOtherThing"); + + Boolean actualElements = genericUtility.isEmpty(); + + Assert.assertEquals(false, actualElements); + } + + @Test + public void testIsSizeEmptyReturnsTrue() { + MyArrayList genericUtility = new MyArrayList<>(); + + Boolean actualElements = genericUtility.isEmpty(); + + Assert.assertEquals(true, actualElements); + } + + + //---------------Test add append + @Test + public void testAddIntegerExpectTrue() { + Integer addMe = 25; + MyArrayList genericUtility = new MyArrayList<>(); + + Boolean actual = genericUtility.add(addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddLongExpectTrue() { + Long addMe = 25L; + MyArrayList genericUtility = new MyArrayList<>(); + + Boolean actual = genericUtility.add(addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddStringExpectTrue() { + String addMe = "last"; + MyArrayList genericUtility = new MyArrayList<>(); + + Boolean actual = genericUtility.add(addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddObjectExpectTrue() { + Object addMe = "last"; + MyArrayList genericUtility = new MyArrayList<>(); + + Boolean actual = genericUtility.add(addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddAppendsAtEndOfList() { + MyArrayList genericUtility = new MyArrayList<>(); + String expected = "andTheOtherThing"; + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add(expected); + + String actual = genericUtility.get(2); + + Assert.assertEquals(expected, actual); + } + + //---------------Test addInsert + @Test + public void testAddInsertsAtIndex() { + MyArrayList genericUtility = new MyArrayList<>(); + Integer expected = 1000; + genericUtility.add(25); + genericUtility.add(10); + genericUtility.add(1); + genericUtility.add(1, expected); + + Integer actual = genericUtility.get(1); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testAddShiftsIndexToRightByOne() { + MyArrayList genericUtility = new MyArrayList<>(); + Integer expected = 10; + genericUtility.add(25); + genericUtility.add(10); + genericUtility.add(1); + genericUtility.add(1, 50); + + Integer actual = genericUtility.get(2); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testAddInsertLongAtIndexReturnTrue() { + Long addMe = 5L; + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(25L); + genericUtility.add(10L); + genericUtility.add(1L); + + Boolean actual = genericUtility.add(2, addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddInsertStringAtIndexReturnTrue() { + String addMe = "last"; + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + + Boolean actual = genericUtility.add(1, addMe); + + Assert.assertTrue(actual); + } + + @Test + public void testAddInsertObjectAtIndexReturnTrue() { + Object addMe = "last"; + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + + Boolean actual = genericUtility.add(2, addMe); + + Assert.assertTrue(actual); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddInsertAtIndexUpperBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(0, 1); + genericUtility.add(1, 3); + genericUtility.add(2, 5); + genericUtility.add(3, 6); + genericUtility.add(4, 10); + + genericUtility.add(10, 100); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddInsertAtIndexLowerBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(0, 1); + genericUtility.add(1, 3); + genericUtility.add(2, 5); + genericUtility.add(3, 6); + genericUtility.add(4, 10); + + genericUtility.add(-1, 100); + } + + //---------------Test remove + @Test + public void testRemoveReturnsElementRemoved() { + Object expected = "that"; + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add(expected); + genericUtility.add("stuff"); + + Object actual = genericUtility.remove(1); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveNoLongerContainsElement() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + + genericUtility.remove(2); + Boolean actual = genericUtility.contains("AndtheOtherThing"); + + Assert.assertEquals(false, actual); + } + + @Test + public void testRemoveShiftsIndexByOneToLeft() { + MyArrayList genericUtility = new MyArrayList<>(); + Object expected = "AndtheOtherThing"; + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + + genericUtility.remove(1); + Object actual = genericUtility.get(1); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testRemoveNullAtLastIndex() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + + genericUtility.remove(2); + Boolean actual = genericUtility.contains("AndtheOtherThing"); + + Assert.assertEquals(false, actual); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveAtIndexUpperBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(0, 1); + genericUtility.add(1, 3); + genericUtility.add(2, 5); + genericUtility.add(3, 6); + genericUtility.add(4, 10); + + genericUtility.remove(10); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveAtIndexLowerBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(0, 1); + genericUtility.add(1, 3); + genericUtility.add(2, 5); + genericUtility.add(3, 6); + genericUtility.add(4, 10); + + genericUtility.remove(-1); + } + + + //---------------Test get + @Test(expected = IndexOutOfBoundsException.class) + public void testGetThrowsExceptionWhenEmpty() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(1); + genericUtility.add(2); + genericUtility.add(3); + genericUtility.add(3); + genericUtility.add(4); + + genericUtility.clear(); + int actual = genericUtility.get(0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetThrowsExceptionLowerBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(1); + genericUtility.add(2); + genericUtility.add(3); + genericUtility.add(3); + genericUtility.add(4); + + int actual = genericUtility.get(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetThrowsExceptionUpperBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(1); + genericUtility.add(2); + genericUtility.add(3); + genericUtility.add(3); + genericUtility.add(4); + + int actual = genericUtility.get(5); + } + + //---------------Test clear + @Test + public void testClearSetsSizeToZero() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add(1); + genericUtility.add(2); + genericUtility.add(3); + genericUtility.add(3); + genericUtility.add(4); + + genericUtility.clear(); + int actual = genericUtility.size(); + + Assert.assertEquals(0, actual); + } + + + //---------------Test set + @Test + public void testSetOverwritesPreviousElement() { + MyArrayList genericUtility = new MyArrayList<>(); + String expected = "stuff"; + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + genericUtility.set(1, "stuff"); + String actual = genericUtility.get(1); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetReturnsPreviousElement() { + MyArrayList genericUtility = new MyArrayList<>(); + String expected = "thatThing"; + genericUtility.add("thisThing"); + genericUtility.add(expected); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + String actual = genericUtility.set(1, "stuff"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSetArraySizeUnchanged() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + int expected = 5; + + genericUtility.set(1, "stuff"); + int actual = genericUtility.size(); + + Assert.assertEquals(expected, actual); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testSetThrowsExceptionWhenEmpty() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + genericUtility.clear(); + + String actual = genericUtility.set(0, "stuff"); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testSetThrowsExceptionLowerBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + genericUtility.set(-1, "stuff"); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testSetThrowsExceptionUpperBound() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + genericUtility.set(5, "stuff"); + } + //---------------Test contains + + @Test + public void testContainsTrue() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + Boolean actual = genericUtility.contains("theOtherThing"); + + Assert.assertEquals(true, actual); + + } + + @Test + public void testContainsFalse() { + MyArrayList genericUtility = new MyArrayList<>(); + genericUtility.add("thisThing"); + genericUtility.add("thatThing"); + genericUtility.add("theOtherThing"); + genericUtility.add("everyThing"); + genericUtility.add("allTheThings"); + + Boolean actual = genericUtility.contains("SoManyThings"); + + Assert.assertEquals(false, actual); + + } + +} + diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java new file mode 100644 index 0000000..c95e6fa --- /dev/null +++ b/src/test/java/TestMySet.java @@ -0,0 +1,667 @@ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; + +public class TestMySet { + + //---------------test MySet constructor + + @Test + public void testConstructorDefaultSize() { + int expectedElementCount = 0; + MySet testSet = new MySet<>(); + + int actualElementCount = testSet.size(); + + Assert.assertEquals(expectedElementCount, actualElementCount); + } + + @Test + public void testConstructorSizeIncrease() { + int expectedSize = 3; + MySet testSet = new MySet<>(); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + + int actualLength = testSet.size(); + + Assert.assertEquals(expectedSize, actualLength); + } + + @Test + public void testConstructoreIngnoreDup() { + MySet testSet = new MySet<>(); + + boolean unique = testSet.add(10); + boolean dup = testSet.add(10); + + Assert.assertTrue(unique); + Assert.assertFalse(dup); + Assert.assertEquals(1, testSet.size()); + } + + @Test + public void testConstructorIgnoresMultipleDups() { + MySet testSet = new MySet<>(); + testSet.add(10); + testSet.add(20); + + boolean dup = testSet.add(10); + boolean dup2 = testSet.add(20); + boolean dup3 = testSet.add(20); + + Assert.assertFalse(dup); + Assert.assertFalse(dup2); + Assert.assertFalse(dup3); + Assert.assertEquals(2, testSet.size()); + } + + + //------------>What I'm trying to do here is test that the constructor assigns correct type to generic object. + //not sure if I'm testing this correctly. Come back to it. + @Test + public void testConstructorLongArray() { + MySet testSet = new MySet<>(); + testSet.add(50L); + testSet.add(60L); + testSet.add(70L); + int expected = 3; + + int actual = testSet.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorStringArray() { + MySet testSet = new MySet<>(); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + int expected = 3; + + int actual = testSet.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testConstructorObjectArray() { + MySet testSet = new MySet<>(); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + int expected = 3; + + int actual = testSet.size(); + + Assert.assertEquals(expected, actual); + } + + + //---------------test MySet size() + + @Test + public void testSizeReturnsElementCountIfLessThanCapacity() { + MySet testSet = new MySet<>(20); + testSet.add(10); + testSet.add(11); + testSet.add(12); + int expected = 3; + + int actual = testSet.size(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testSizeReturnsElementCountIfMaxCapacity() { + MySet testSet = new MySet<>(5); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + testSet.add("allTheThings"); + testSet.add("SomeThings"); + int expected = 5; + + int actual = testSet.size(); + + Assert.assertEquals(expected, actual); + } + + + //---------------test MySet isEmpty() + @Test + public void testIsEmptyReturnsFalse() { + MySet testSet = new MySet<>(5); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + + Boolean actualElements = testSet.isEmpty(); + + Assert.assertEquals(false, actualElements); + } + + @Test + public void testIsEmptyReturnsTrue() { + MySet testSet = new MySet<>(0); + + Boolean actualElements = testSet.isEmpty(); + + Assert.assertEquals(true, actualElements); + } + + //---------------test MySet contains() + + @Test + public void testContainsTrue() { + MySet testSet = new MySet<>(5); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("something"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.contains("something"); + + Assert.assertEquals(true, actual); + + } + + @Test + public void testContainsFalse() { + MySet testSet = new MySet<>(5); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.contains("SoManyThings"); + + Assert.assertEquals(false, actual); + + } + + + @Test(expected = NullPointerException.class) + public void testContainsThrowsExceptionIfElementSpecifiedNull() { + MySet testSet = new MySet<>(); + testSet.contains(null); + } + + //------------test Myset Bool add(E e) + + @Test //Check that element is in list after adding + public void testAdd_SetContainsElementAfterAdd_True() { + MySet testSet = new MySet<>(); + testSet.add(10); + + Boolean actual = testSet.contains(10); + + Assert.assertEquals(true, actual); + } + + + @Test //return true if set did not contain the specified element + public void testAdd_DoesNotContainElement_True() { + MySet testSet = new MySet<>(); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.add("something"); + + Assert.assertEquals(true, actual); + } + + @Test//return false if set already contains element + public void testAdd_AlreadyContainsElement_False() { + MySet testSet = new MySet<>(); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.add("thatThing"); + + Assert.assertEquals(false, actual); + } + + @Test //returns false if set already contains element make sure leaves set unchanged + public void testAdd_AlreadyContainsElement_AddDoesNotAlterSet_True() { + MySet testSet = new MySet<>(); + testSet.add(10); + testSet.add(20); + testSet.add(30); + testSet.add(20); + int expected = 3; + int actual = testSet.size(); + Assert.assertEquals(expected, actual); + } + + @Test //make sure you can't add duplicates + public void testAdd_MultipleDuplicates_False() { + MySet testSet = new MySet<>(5); + boolean unique1 = testSet.add(10); + boolean dup1 = testSet.add(10); + boolean unique2 = testSet.add(20); + boolean dup2 = testSet.add(10); + boolean dup3 = testSet.add(20); + + Assert.assertTrue(unique1); + Assert.assertFalse(dup1); + Assert.assertTrue(unique2); + Assert.assertFalse(dup2); + Assert.assertFalse(dup3); + Assert.assertEquals(2, testSet.size()); + } + + @Test(expected = NullPointerException.class) + public void testAdd_ThrowsExceptionIfElementSpecifiedNull() { + MySet testObject = new MySet<>(); + testObject.add(null); + } + + + //------------test Myset Bool remove(Object o) + + @Test //remove returns false when the element does not exist + public void testRemove_DoesNotContainElementAndRemoves_False() { + MySet testSet = new MySet<>(); + testSet.add(10); + testSet.add(20); + testSet.add(30); + + Boolean actual = testSet.remove(400); + + Assert.assertEquals(false, actual); + } + + @Test //removes element from list and returns true + public void testRemove_ContainsElementAndRemoves_True() { + MySet testObject = new MySet<>(); + testObject.add(10); + testObject.add(20); + testObject.add(30); + + Boolean actual = testObject.remove(20); + + Assert.assertEquals(true, actual); + + } + + @Test //remove element from list and check to see if it contains that element + public void testRemove_SetNoLongerContainsElement_False() { + + MySet testSet = new MySet<>(); + testSet.add("this"); + testSet.add("that"); + testSet.add("theOtherThing"); + + testSet.remove("that"); + Boolean actual = testSet.contains("that"); + + Assert.assertEquals(false, actual); + + } + + @Test(expected = NullPointerException.class) + public void testRemove_ThrowsExceptionIfElementSpecifiedNull() { + MySet testSet = new MySet<>(); + testSet.remove(null); + } + + //------------test Myset void clear() + + @Test + public void testClearSetsSizeToZero() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(3); + testSet.add(4); + + testSet.clear(); + int actual = testSet.size(); + + Assert.assertEquals(0, actual); + + } + + //---------------test MySet toArray() Object[] toArray() + + @Test//check nulls that populate the original capacity of an array(10) are not returned + public void testToArrayAllNullsRemoved() { + MySet testSet = new MySet<>(); + Integer[] actual = testSet.toArray(new Integer[0]); + Integer[] expected = {}; + Assert.assertArrayEquals(expected, actual); + } + + @Test //check no loss of non-null elements + public void testToArrayAllElementsReturned() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + Integer[] actual = testSet.toArray(new Integer[0]); + Integer[] expected = {1, 2, 3, 4}; + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testToArray_ModifyReturnedArray_DoesNotModify_OriginialArray() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + Integer[] actual = testSet.toArray(new Integer[0]); + actual[1] = 100; + Integer[] expectedOriginal = {1, 2, 3, 4}; + Integer[] expectedModified = {1, 100, 3, 4}; + + Assert.assertNotEquals(expectedOriginal[1], expectedModified[1]); + } + + @Test //check works with type double + public void testToArray_Double() { + MySet testSet = new MySet<>(); + testSet.add(1.0001); + testSet.add(2.0001); + testSet.add(3.0001); + testSet.add(4.0001); + Double[] actual = testSet.toArray(new Double[0]); + Double[] expected = {1.0001, 2.0001, 3.0001, 4.0001}; + Assert.assertArrayEquals(expected, actual); + } + + @Test //check works with type long + public void testToArray_Long() { + MySet testSet = new MySet<>(); + testSet.add(1L); + testSet.add(2L); + testSet.add(3L); + testSet.add(4L); + Long[] actual = testSet.toArray(new Long[0]); + Long[] expected = {1L, 2L, 3L, 4L}; + Assert.assertArrayEquals(expected, actual); + } + + @Test //check works with type String + public void testToArray_String() { + MySet testSet = new MySet<>(); + testSet.add("f"); + testSet.add("d"); + testSet.add("c"); + testSet.add("w"); + String[] actual = testSet.toArray(new String[0]); + String[] expected = {"f", "d", "c", "w"}; + Assert.assertArrayEquals(expected, actual); + } + + + //------------test Myset Bool containsAll(Collection c) + + @Test + public void testContainsAll_True() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + ArrayList check = new ArrayList(); + check.add(1); + check.add(2); + check.add(3); + check.add(4); + + boolean actual = testSet.containsAll(check); + + Assert.assertTrue(actual); + } + + @Test + public void testContainsAll_False() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + ArrayList check = new ArrayList(); + check.add(5); + check.add(6); + check.add(7); + check.add(8); + + boolean actual = testSet.containsAll(check); + + Assert.assertFalse(actual); + } + + @Test + public void testContainsAll_subsetOfSet_True() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + ArrayList check = new ArrayList(); + check.add(1); + check.add(2); + + boolean actual = testSet.containsAll(check); + + Assert.assertTrue(actual); + } + + @Test(expected = NullPointerException.class) + public void testContainsAll_ThrowsExceptionIfElementSpecifiedNull() { + MySet testObject = new MySet<>(); + testObject.containsAll(null); + } + + //------------test Myset Bool addAll(Collection c) + + @Test + public void testAddAll_Union2Sets() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("this"); + testSet.add("is"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("super"); + testList2.add("fun"); + testList2.add("!"); + String[] expected = new String[]{"yes", "this", "is", "super", "fun", "!"}; + + testSet.addAll(testList2); + String[] actual = testSet.toArray(new String[0]); + + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testAddAll_Union2Sets_WithOnlyUniqueValues() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("is"); + testSet.add("fun"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("is"); + testList2.add("fun"); + String[] expected = new String[]{"yes", "is", "fun", "this"}; + + testSet.addAll(testList2); + String[] actual = testSet.toArray(new String[0]); + + Assert.assertArrayEquals(expected, actual); + } + + //------------test Myset boolean retainAll(Collection c) + + @Test //returning matching elements + public void testRetainAll_RetainOnlyDups() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("is"); + testSet.add("fun"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("is"); + testList2.add("fun"); + String[] expected = new String[]{"is", "fun"}; + + testSet.retainAll(testList2); + String[] actual = testSet.toArray(new String[0]); + + Assert.assertArrayEquals(expected, actual); + } + + @Test //if no matching elements then return empty array + public void testRetainAll_NoMatches_ReturnEmptyArray() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("like"); + testSet.add("dog"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("is"); + testList2.add("fun"); + String[] expected = new String[]{}; + + testSet.retainAll(testList2); + String[] actual = testSet.toArray(new String[0]); + + Assert.assertArrayEquals(expected, actual); + } + + @Test(expected = NullPointerException.class) + public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("like"); + testSet.add("dog"); + + testSet.retainAll(null); + + } +//------------test Myset boolean removeAll(Collection c) + /** Removes from this set all of its elements that are contained in the specified collection (optional operation). + If the specified collection is also a set, this operation effectively modifies this set so that its value is + the asymmetric set difference of the two sets. + Parameters: + c - collection containing elements to be removed from this set + Returns: + true if this set changed as a result of the call + NullPointerException - if this set contains a null element and the specified collection does not permit null + elements (optional), or if the specified collection is null + See Also: + remove(Object), contains(Object)*/ + + @Test //remove all items from list2 that are in list 1 + public void testRemoveAll_() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("is"); + testSet.add("fun"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("is"); + testList2.add("fun"); + String[] expected = new String[]{"yes"}; + + testSet.removeAll(testList2); + String[] actual = testSet.toArray(new String[0]); + + Assert.assertArrayEquals(expected, actual); + } + + @Test //if removed returned true + public void testRemoveAll_ReturnsTrueIfChanged() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("is"); + testSet.add("fun"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("is"); + testList2.add("fun"); + + Boolean actual =testSet.removeAll(testList2); + + Assert.assertEquals(true, actual); + } + + @Test //if nothing removed return false + public void testRemoveAll_ReturnsFalseIfNoChanged() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("no"); + testSet.add("maybe"); + ArrayList testList2 = new ArrayList<>(); + testList2.add("this"); + testList2.add("that"); + testList2.add("theOtherThing"); + + Boolean actual =testSet.removeAll(testList2); + + Assert.assertEquals(false, actual); + } + + @Test(expected = NullPointerException.class) + public void testRemoveAll_ThrowsExceptionIfElementSpecifiedNull() { + MySet testSet = new MySet<>(); + testSet.add("yes"); + testSet.add("like"); + testSet.add("dog"); + + testSet.removeAll(null); + } + + //--------------test Myset toArray() T[] toArray(T[] a) + + + //------------test boolean equals(Object o) + /**Compares the specified object with this set for equality. + Returns true if the specified object is also a set, the two sets have the same size, + and every member of the specified set is contained in this set (or equivalently, every member + of this set is contained in the specified set). This definition ensures that the equals method + works properly across different implementations of the set interface. + Overrides: + equals in class Object + Parameters: + o - object to be compared for equality with this set + Returns: + true if the specified object is equal to this set + See Also: + Object.hashCode(), HashMap*/ + + + //------------test int hashCode() + /**Returns the hash code value for this set. The hash code of a set is defined to be the sum of the + hash codes of the elements in the set, where the hash code of a null element is defined to be zero. + This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as + required by the general contract of Object.hashCode(). + Overrides: + hashCode in class Object + Returns: + the hash code value for this set + See Also: + Object.equals(Object), equals(Object)*/ + +} + + + + +