From 9b989bbe39028dfcd2dadd1ccd0dbe94458fbe83 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sat, 10 Mar 2018 17:57:11 -0500 Subject: [PATCH 01/12] Arraylist methods throwing/testing exceptions, testing methods done --- .gitignore | 2 + .idea/compiler.xml | 16 + .idea/libraries/Maven__junit_junit_4_12.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + generics.iml | 16 + src/main/java/MyArrayList.java | 150 +++++++ src/test/java/TestMyArrayList.java | 418 ++++++++++++++++++ 10 files changed, 655 insertions(+) create mode 100644 .idea/compiler.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 generics.iml create mode 100644 src/main/java/MyArrayList.java create mode 100644 src/test/java/TestMyArrayList.java 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..3acc9a0 --- /dev/null +++ b/src/main/java/MyArrayList.java @@ -0,0 +1,150 @@ +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]; + } + + 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/test/java/TestMyArrayList.java b/src/test/java/TestMyArrayList.java new file mode 100644 index 0000000..614e162 --- /dev/null +++ b/src/test/java/TestMyArrayList.java @@ -0,0 +1,418 @@ +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<>(); + genericUtility.add("this"); + genericUtility.add("that"); + genericUtility.add("AndtheOtherThing"); + genericUtility.remove(1); + Object actual = genericUtility.get(1); + Object expected = "AndtheOtherThing"; + 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 testRemmoveAtIndexUpperBound() { + 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"); + + String actual = 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"); + + String actual = 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); + + } + +} + From fdd96114ef7c5b9b834e6c86f710a72f4d080aed Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sat, 10 Mar 2018 22:02:59 -0500 Subject: [PATCH 02/12] TDD got a little sidetracked last round, adding only tests this round --- src/test/java/TestMyArrayList.java | 1 - src/test/java/TestMySet.java | 404 +++++++++++++++++++++++++++++ 2 files changed, 404 insertions(+), 1 deletion(-) create mode 100644 src/test/java/TestMySet.java diff --git a/src/test/java/TestMyArrayList.java b/src/test/java/TestMyArrayList.java index 614e162..9e9a23e 100644 --- a/src/test/java/TestMyArrayList.java +++ b/src/test/java/TestMyArrayList.java @@ -2,7 +2,6 @@ import org.junit.Test; public class TestMyArrayList { - //---------------Test Constructor @Test public void testConstructorDefaultSize() { diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java new file mode 100644 index 0000000..3106297 --- /dev/null +++ b/src/test/java/TestMySet.java @@ -0,0 +1,404 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestMySet { + + private MySet testObject; + + @Before + public void setup() { + testObject = new MySet(); + } + + //---------------test MySet constructor + @Test + public void testConstructorIntialArrayCapacity() { + MySet testOb = new MySet<>(3); + Integer[] expected = {null, null, null}; + Integer[] actual = testOb.toArray(new Integer[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testConstructorIntegerArray() { + Integer[] expected = {50, 60, 70}; + MySet testOb = new MySet<>(expected); + Integer[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testConstructorLongArray() { + Long[] expected = {50L, 60L, 70L}; + MySet testOb = new MySet<>(expected); + Long[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testConstructorStringArray() { + String[] expected = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(expected); + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testConstructorObjectArray() { + Object[] expected = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(expected); + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testConstructorIgnoresDups() { + Object[] input = {"this", "that", "this", "andTheOtherThing", "this", "this"}; + MySet testOb = new MySet<>(input); + Object[] expected = {"this", "that", "andTheOtherThing"}; + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + //---------------test MySet size() + + /** + * Returns: + * the number of elements in this set (its cardinality) + */ + @Test + public void testSizeReturnsDefaultElementCount() { + MySet testOb = new MySet<>(); + int expected = 0; + int actual = testOb.size(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSizeReturnsElementCountIfLessThanCapacity() { + MySet testOb = new MySet<>(20); + testOb.add("this"); + testOb.add("that"); + testOb.add("andTheOtherThing"); + int expected = 3; + int actual = testOb.size(); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSizeReturnsElementCountIfMaxCapacity() { + MySet testOb = new MySet<>(5); + testOb.add("this"); + testOb.add("that"); + testOb.add("andTheOtherThing"); + testOb.add("allTheThings"); + testOb.add("SomeThings"); + int expected = 5; + int actual = testOb.size(); + Assert.assertEquals(expected, actual); + } + + + //---------------test MySet isEmpty() + + /** + * isEmpty + * returns true if this set contains no elements + */ + @Test + public void testIsSizeEmptyReturnsFalse() { + testObject.add("this"); + testObject.add("that"); + testObject.add("andTheOtherThing"); + Boolean actualElements = testObject.isEmpty(); + Assert.assertEquals(false, actualElements); + } + + @Test + public void testIsSizeEmptyReturnsTrue() { + Boolean actualElements = testObject.isEmpty(); + Assert.assertEquals(true, actualElements); + } + + //---------------test MySet contains() + + /** + * boolean contains(Object o) + * Returns true if this set contains the specified element. + * Returns: + * true if this set contains the specified element + * Throws: + * ClassCastException - if the type of the specified element is incompatible with this set (optional) + * NullPointerException - if the specified element is null and this set does not permit null elements (optional) + */ + @Test + public void testContainsTrue() { + MySet testOb = new MySet<>(20); + testObject.add("thisThing"); + testObject.add("thatThing"); + testObject.add("theOtherThing"); + + Boolean actual = testObject.contains("something"); + + Assert.assertEquals(true, actual); + + } + + @Test + public void testContainsFalse() { + testObject.add("thisThing"); + testObject.add("thatThing"); + testObject.add("theOtherThing"); + + Boolean actual = testObject.contains("SoManyThings"); + + Assert.assertEquals(false, actual); + + } + + @Test(expected = NullPointerException.class) + public void testContainsThrowsExceptionIfNullFound() { + testObject.add("thisThing"); + testObject.add("thatThing"); + testObject.add("theOtherThing"); + testObject.add("null"); + testObject.add("null"); + + Boolean actual = testObject.contains(null); + } + + //---------------test MySet toArray() Object[] toArray() + /** + * Returns an array containing all of the elements in this set + * the returned array will be "safe" in that no references to it are maintained by this set + * meaning this method must allocate a new array if if this set is backed by an array + * user should be free to modify the returned array + */ + + //--------------test Myset toArray() T[] toArray(T[] a) + + //------------test Myset Bool add(E e) + + /** + * Parameters: + * e - element to be added to this set + * Returns: + * true if this set did not already contain the specified element + * If this set already contains the element, the call leaves the set + * unchanged and returns false. In combination with the restriction on constructors, this ensures that sets + * never contain duplicate elements. + * NullPointerException - if the specified element is null and this set does not permit null elements + */ + @Test + public void testAdd_SetContainsElementAfterAdd_True() { + String[] input = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(input); + testOb.add("So many things"); + String[] expected = {"this", "that", "andTheOtherThing", "So many things"}; + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testAdd_DoesNotContainElement_True() { + String[] expected = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(expected); + Boolean actual = testOb.add("something"); + Assert.assertEquals(true, actual); + } + + @Test + public void testAdd_AlreadyContainsElement_False() { + String[] input = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(input); + Boolean actual = testOb.add("thatThing"); + Assert.assertEquals(false, actual); + } + + @Test + public void testAdd_MultipleDuplicates_False() { + MySet testOb = new MySet<>(5); + testOb.add(10); + testOb.add(10); + testOb.add(20); + testOb.add(10); + testOb.add(20); + Integer[] expected = {10, 20, null, null, null}; + Integer[] actual = testOb.toArray(new Integer[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testAdd_AlreadyContainsElement_AddDoesNotAlterSet_True() { + String[] expected = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(expected); + testObject.add("that"); + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + @Test(expected = NullPointerException.class) + public void testAdd_Null_Exception() { + String[] expected = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(10); + testObject.add(null); + } + + + //------------test Myset Bool remove(Object o) + /** + * Removes the specified element from this set if it is present (optional operation). + * o - object to be removed from this set, if present + Returns: + true if this set contained the specified element + NullPointerException - if the specified element is null and this set does not permit null elements (optional)*/ + + @Test + public void testRemove_SetDoesNotContainElementAfterRemove_True() { + String[] input = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(input); + testObject.remove("that"); + String[] expected = {"this","andTheOtherThing", null}; + String[] actual = testOb.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + + } + + @Test + public void testRemove_ContainsElementAndRemoves_True() { + String[] input = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(input); + Boolean actual = testObject.remove("that"); + Assert.assertEquals(true, actual); + + } + + @Test + public void testRemove_DoesNotContainElementAndRemoves_False() { + String[] input = {"this", "that", "andTheOtherThing"}; + MySet testOb = new MySet<>(input); + Boolean actual = testObject.remove("stuff"); + Assert.assertEquals(false, actual); + } + + + + //------------test Myset Bool containsAll(Collection c) + /** Returns 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. + c - collection to be checked for containment in this set + Returns: + true if this set contains all of the elements of the specified collection + NullPointerException - if the specified collection contains one or more null elements and + this set does not permit null elements (optional), or if the specified collection is null*/ + + + + + //------------test Myset Bool addAll(Collection c) + /**Adds all of the elements in the specified collection to + this set if they're not already present (optional operation). + 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. The behavior of this + operation is undefined if the specified collection is modified while the operation is in progress. + Parameters: + c - collection containing elements to be added to this set + Returns: + true if this set changed as a result of the call + NullPointerException - if the specified collection contains one or more null elements + and this set does not permit null elements, or if the specified collection is null*/ + @Test + public void testAddAll_Union2Sets() { + Integer[] array1 = {10, 20, 30, 40}; + Integer[] array2 = {50, 60, 70, 80}; + MySet testOb = new MySet<>(); + testOb.addAll(array1); + testOb.addAll(array2); + + Integer[] expected = {10, 20, 30, 40, 50, 60, 70, 80}; + Integer[] actual = testOb.toArray(new Integer[0]); + Assert.assertArrayEquals(expected, actual); + } + + + //------------test Myset boolean retainAll(Collection c) + /**Retains only the elements in this set that are contained in the specified collection + (optional operation). In other words, removes from this set all of its elements that are not + contained in the specified collection. If the specified collection is also a set, this operation + effectively modifies this set so that its value is the intersection of the two sets. + Parameters: + c - collection containing elements to be retained in 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 + */ + + + + //------------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 Myset void clear() + /** Removes all of the elements from this set (optional operation). + * The set will be empty after this call returns. */ + + @Test + public void testClear_SetEmpty() { + Integer[] input = {50, 60, 70}; + MySet testOb = new MySet<>(input); + testOb.clear(); + String[] expected = {null,null,null,null}; + String [] actual = test.toArray(new String[0]); + Assert.assertArrayEquals(expected, actual); + } + + + //------------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)*/ + +} + + + + + From e4433122e615beecdd56421c807b0fa047a22b34 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 16:22:59 -0400 Subject: [PATCH 03/12] wrote methods to pass tests --- src/main/java/MyArrayList.java | 6 +- src/main/java/MySet.java | 206 +++++++++++++ src/test/java/TestMyArrayList.java | 54 +++- src/test/java/TestMySet.java | 472 +++++++++++++++-------------- 4 files changed, 505 insertions(+), 233 deletions(-) create mode 100644 src/main/java/MySet.java diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java index 3acc9a0..651f58f 100644 --- a/src/main/java/MyArrayList.java +++ b/src/main/java/MyArrayList.java @@ -69,7 +69,7 @@ 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; + int length = size() - index; ensureCapacity(); System.arraycopy(this.inputArray, index, this.inputArray, index + 1, length); this.inputArray[index] = addMe; @@ -111,6 +111,10 @@ public E get(Integer index) { 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; diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java new file mode 100644 index 0000000..62892c4 --- /dev/null +++ b/src/main/java/MySet.java @@ -0,0 +1,206 @@ +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(Object 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(Object 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(Object 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].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 (Object o : container) { + if (!this.add(o)) { + allAdded = false; + } + } + return allAdded; + } + + /** + * 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 + * @return + */ + public Object[] toArray() { + return trimTrailingNull(this.inputArray); + } + + /** + * NullPointerException - if the specified collection contains one or more null elements + * and this set does not permit null elements, or if the specified collection is null + * Trim the null values from an array. + * @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 + */ + 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 + */ + 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 index 9e9a23e..700974f 100644 --- a/src/test/java/TestMyArrayList.java +++ b/src/test/java/TestMyArrayList.java @@ -7,7 +7,9 @@ public class TestMyArrayList { public void testConstructorDefaultSize() { int expectedElementCount = 0; MyArrayList genericUtility = new MyArrayList<>(); + int actualLength = genericUtility.size(); + Assert.assertEquals(expectedElementCount, actualLength); } @@ -18,7 +20,9 @@ public void testConstructorSizeIncrease() { genericUtility.add("this"); genericUtility.add("that"); genericUtility.add("andTheOtherThing"); + int actualLength = genericUtility.size(); + Assert.assertEquals(expectedSize, actualLength); } @@ -30,14 +34,18 @@ public void testIsSizeEmptyReturnsFalse() { 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); } @@ -47,7 +55,9 @@ public void testIsSizeEmptyReturnsTrue() { public void testAddIntegerExpectTrue() { Integer addMe = 25; MyArrayList genericUtility = new MyArrayList<>(); + Boolean actual = genericUtility.add(addMe); + Assert.assertTrue(actual); } @@ -55,7 +65,9 @@ public void testAddIntegerExpectTrue() { public void testAddLongExpectTrue() { Long addMe = 25L; MyArrayList genericUtility = new MyArrayList<>(); + Boolean actual = genericUtility.add(addMe); + Assert.assertTrue(actual); } @@ -63,7 +75,9 @@ public void testAddLongExpectTrue() { public void testAddStringExpectTrue() { String addMe = "last"; MyArrayList genericUtility = new MyArrayList<>(); + Boolean actual = genericUtility.add(addMe); + Assert.assertTrue(actual); } @@ -71,26 +85,28 @@ public void testAddStringExpectTrue() { 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); @@ -105,7 +121,6 @@ public void testAddInsertsAtIndex() { @Test public void testAddShiftsIndexToRightByOne() { - MyArrayList genericUtility = new MyArrayList<>(); Integer expected = 10; genericUtility.add(25); @@ -125,7 +140,9 @@ public void testAddInsertLongAtIndexReturnTrue() { genericUtility.add(25L); genericUtility.add(10L); genericUtility.add(1L); + Boolean actual = genericUtility.add(2, addMe); + Assert.assertTrue(actual); } @@ -136,7 +153,9 @@ public void testAddInsertStringAtIndexReturnTrue() { genericUtility.add("this"); genericUtility.add("that"); genericUtility.add("AndtheOtherThing"); - Boolean actual = genericUtility.add(1,addMe); + + Boolean actual = genericUtility.add(1, addMe); + Assert.assertTrue(actual); } @@ -147,7 +166,9 @@ public void testAddInsertObjectAtIndexReturnTrue() { genericUtility.add("this"); genericUtility.add("that"); genericUtility.add("AndtheOtherThing"); - Boolean actual = genericUtility.add(2,addMe); + + Boolean actual = genericUtility.add(2, addMe); + Assert.assertTrue(actual); } @@ -183,7 +204,9 @@ public void testRemoveReturnsElementRemoved() { genericUtility.add("this"); genericUtility.add(expected); genericUtility.add("stuff"); + Object actual = genericUtility.remove(1); + Assert.assertEquals(expected, actual); } @@ -193,20 +216,24 @@ public void testRemoveNoLongerContainsElement() { 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); - Object expected = "AndtheOtherThing"; + Assert.assertEquals(expected, actual); } @@ -216,13 +243,15 @@ public void testRemoveNullAtLastIndex() { 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 testRemmoveAtIndexUpperBound() { + public void testRemoveAtIndexUpperBound() { MyArrayList genericUtility = new MyArrayList<>(); genericUtility.add(0, 1); genericUtility.add(1, 3); @@ -296,8 +325,8 @@ public void testClearSetsSizeToZero() { genericUtility.clear(); int actual = genericUtility.size(); - Assert.assertEquals(0, actual); + Assert.assertEquals(0, actual); } @@ -314,6 +343,7 @@ public void testSetOverwritesPreviousElement() { genericUtility.set(1, "stuff"); String actual = genericUtility.get(1); + Assert.assertEquals(expected, actual); } @@ -328,6 +358,7 @@ public void testSetReturnsPreviousElement() { genericUtility.add("allTheThings"); String actual = genericUtility.set(1, "stuff"); + Assert.assertEquals(expected, actual); } @@ -340,8 +371,10 @@ public void testSetArraySizeUnchanged() { genericUtility.add("everyThing"); genericUtility.add("allTheThings"); int expected = 5; + genericUtility.set(1, "stuff"); int actual = genericUtility.size(); + Assert.assertEquals(expected, actual); } @@ -355,6 +388,7 @@ public void testSetThrowsExceptionWhenEmpty() { genericUtility.add("allTheThings"); genericUtility.clear(); + String actual = genericUtility.set(0, "stuff"); } @@ -367,7 +401,7 @@ public void testSetThrowsExceptionLowerBound() { genericUtility.add("everyThing"); genericUtility.add("allTheThings"); - String actual = genericUtility.set(-1, "stuff"); + genericUtility.set(-1, "stuff"); } @Test(expected = IndexOutOfBoundsException.class) @@ -379,7 +413,7 @@ public void testSetThrowsExceptionUpperBound() { genericUtility.add("everyThing"); genericUtility.add("allTheThings"); - String actual = genericUtility.set(5, "stuff"); + genericUtility.set(5, "stuff"); } //---------------Test contains diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 3106297..a94e4d0 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -4,142 +4,165 @@ public class TestMySet { - private MySet testObject; + //---------------test MySet constructor + + @Test + public void testConstructorDefaultSize() { + int expectedElementCount = 0; + MySet testSet = new MySet<>(); + + int actualElementCount = testSet.size(); - @Before - public void setup() { - testObject = new MySet(); + Assert.assertEquals(expectedElementCount, actualElementCount); } - //---------------test MySet constructor @Test - public void testConstructorIntialArrayCapacity() { - MySet testOb = new MySet<>(3); - Integer[] expected = {null, null, null}; - Integer[] actual = testOb.toArray(new Integer[0]); - Assert.assertArrayEquals(expected, actual); + 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 testConstructorIntegerArray() { - Integer[] expected = {50, 60, 70}; - MySet testOb = new MySet<>(expected); - Integer[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + 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 testConstructorLongArray() { - Long[] expected = {50L, 60L, 70L}; - MySet testOb = new MySet<>(expected); - Long[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + 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 testConstructorStringArray() { - String[] expected = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(expected); - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + 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 testConstructorObjectArray() { - Object[] expected = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(expected); - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + 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 testConstructorIgnoresDups() { - Object[] input = {"this", "that", "this", "andTheOtherThing", "this", "this"}; - MySet testOb = new MySet<>(input); - Object[] expected = {"this", "that", "andTheOtherThing"}; - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); - } + public void testConstructorObjectArray() { + MySet testSet = new MySet<>(); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); + int expected = 3; - //---------------test MySet size() + int actual = testSet.size(); - /** - * Returns: - * the number of elements in this set (its cardinality) - */ - @Test - public void testSizeReturnsDefaultElementCount() { - MySet testOb = new MySet<>(); - int expected = 0; - int actual = testOb.size(); Assert.assertEquals(expected, actual); } + + //---------------test MySet size() + @Test public void testSizeReturnsElementCountIfLessThanCapacity() { - MySet testOb = new MySet<>(20); - testOb.add("this"); - testOb.add("that"); - testOb.add("andTheOtherThing"); + MySet testSet = new MySet<>(20); + testSet.add("this"); + testSet.add("that"); + testSet.add("andTheOtherThing"); int expected = 3; - int actual = testOb.size(); + + int actual = testSet.size(); + Assert.assertEquals(expected, actual); } @Test public void testSizeReturnsElementCountIfMaxCapacity() { - MySet testOb = new MySet<>(5); - testOb.add("this"); - testOb.add("that"); - testOb.add("andTheOtherThing"); - testOb.add("allTheThings"); - testOb.add("SomeThings"); + 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 = testOb.size(); + + int actual = testSet.size(); + Assert.assertEquals(expected, actual); } //---------------test MySet isEmpty() - - /** - * isEmpty - * returns true if this set contains no elements - */ @Test - public void testIsSizeEmptyReturnsFalse() { - testObject.add("this"); - testObject.add("that"); - testObject.add("andTheOtherThing"); - Boolean actualElements = testObject.isEmpty(); + 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 testIsSizeEmptyReturnsTrue() { - Boolean actualElements = testObject.isEmpty(); + public void testIsEmptyReturnsTrue() { + MySet testSet = new MySet<>(0); + + Boolean actualElements = testSet.isEmpty(); + Assert.assertEquals(true, actualElements); } //---------------test MySet contains() - - /** - * boolean contains(Object o) - * Returns true if this set contains the specified element. - * Returns: - * true if this set contains the specified element - * Throws: - * ClassCastException - if the type of the specified element is incompatible with this set (optional) - * NullPointerException - if the specified element is null and this set does not permit null elements (optional) - */ @Test public void testContainsTrue() { - MySet testOb = new MySet<>(20); - testObject.add("thisThing"); - testObject.add("thatThing"); - testObject.add("theOtherThing"); + MySet testSet = new MySet<>(5); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("something"); + testSet.add("theOtherThing"); - Boolean actual = testObject.contains("something"); + Boolean actual = testSet.contains("something"); Assert.assertEquals(true, actual); @@ -147,141 +170,177 @@ public void testContainsTrue() { @Test public void testContainsFalse() { - testObject.add("thisThing"); - testObject.add("thatThing"); - testObject.add("theOtherThing"); + MySet testSet = new MySet<>(5); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); - Boolean actual = testObject.contains("SoManyThings"); + Boolean actual = testSet.contains("SoManyThings"); Assert.assertEquals(false, actual); } + @Test(expected = NullPointerException.class) - public void testContainsThrowsExceptionIfNullFound() { - testObject.add("thisThing"); - testObject.add("thatThing"); - testObject.add("theOtherThing"); - testObject.add("null"); - testObject.add("null"); - - Boolean actual = testObject.contains(null); + public void testContainsThrowsExceptionIfElementSpecifiedNull() { + MySet testSet = new MySet<>(); + testSet.contains(null); } - //---------------test MySet toArray() Object[] toArray() - /** - * Returns an array containing all of the elements in this set - * the returned array will be "safe" in that no references to it are maintained by this set - * meaning this method must allocate a new array if if this set is backed by an array - * user should be free to modify the returned array - */ - - //--------------test Myset toArray() T[] toArray(T[] a) - //------------test Myset Bool add(E e) - - /** - * Parameters: - * e - element to be added to this set - * Returns: - * true if this set did not already contain the specified element - * If this set already contains the element, the call leaves the set - * unchanged and returns false. In combination with the restriction on constructors, this ensures that sets - * never contain duplicate elements. - * NullPointerException - if the specified element is null and this set does not permit null elements - */ - @Test + @Test //Check that element is in list after adding public void testAdd_SetContainsElementAfterAdd_True() { - String[] input = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(input); - testOb.add("So many things"); - String[] expected = {"this", "that", "andTheOtherThing", "So many things"}; - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + MySet testSet = new MySet<>(); + testSet.add(10); + + Boolean actual = testSet.contains(10); + + Assert.assertEquals(true, actual); } - @Test +// @Test +// public void testAddAppendsAtEndOfList() { +// MySet testSet = new MySet<>(); +// String expected = "andTheOtherThing"; +// testSet.add("this"); +// testSet.add("that"); +// testSet.add(expected); +// String actual = testSet.get(2); +// Assert.assertEquals(expected, actual); +// } + + @Test //return true if set did not contain the specified element public void testAdd_DoesNotContainElement_True() { - String[] expected = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(expected); - Boolean actual = testOb.add("something"); + MySet testSet = new MySet<>(); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.add("something"); + Assert.assertEquals(true, actual); } - @Test + @Test//return false if set already contains element public void testAdd_AlreadyContainsElement_False() { - String[] input = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(input); - Boolean actual = testOb.add("thatThing"); + MySet testSet = new MySet<>(); + testSet.add("thisThing"); + testSet.add("thatThing"); + testSet.add("theOtherThing"); + + Boolean actual = testSet.add("thatThing"); + Assert.assertEquals(false, actual); } - @Test +// @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); +// Boolean actual = testSet.add(20); +// Assert.assertEquals(false, actual); +// } + + @Test //make sure you can't add duplicates public void testAdd_MultipleDuplicates_False() { - MySet testOb = new MySet<>(5); - testOb.add(10); - testOb.add(10); - testOb.add(20); - testOb.add(10); - testOb.add(20); - Integer[] expected = {10, 20, null, null, null}; - Integer[] actual = testOb.toArray(new Integer[0]); - Assert.assertArrayEquals(expected, actual); - } - - @Test - public void testAdd_AlreadyContainsElement_AddDoesNotAlterSet_True() { - String[] expected = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(expected); - testObject.add("that"); - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + 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_Null_Exception() { - String[] expected = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(10); + public void testAdd_ThrowsExceptionIfElementSpecifiedNull() { + MySet testObject = new MySet<>(); testObject.add(null); } //------------test Myset Bool remove(Object o) - /** - * Removes the specified element from this set if it is present (optional operation). - * o - object to be removed from this set, if present - Returns: - true if this set contained the specified element - NullPointerException - if the specified element is null and this set does not permit null elements (optional)*/ - @Test - public void testRemove_SetDoesNotContainElementAfterRemove_True() { - String[] input = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(input); - testObject.remove("that"); - String[] expected = {"this","andTheOtherThing", null}; - String[] actual = testOb.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); + @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 + @Test //removes element from list and returns true public void testRemove_ContainsElementAndRemoves_True() { - String[] input = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(input); - Boolean actual = testObject.remove("that"); + MySet testObject = new MySet<>(); + testObject.add(10); + testObject.add(20); + testObject.add(30); + + Boolean actual = testObject.remove(20); + Assert.assertEquals(true, actual); } - @Test - public void testRemove_DoesNotContainElementAndRemoves_False() { - String[] input = {"this", "that", "andTheOtherThing"}; - MySet testOb = new MySet<>(input); - Boolean actual = testObject.remove("stuff"); + @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() + /** + * Returns an array containing all of the elements in this set + * the returned array will be "safe" in that no references to it are maintained by this set + * meaning this method must allocate a new array if if this set is backed by an array + * user should be free to modify the returned array + */ + + //--------------test Myset toArray() T[] toArray(T[] a) //------------test Myset Bool containsAll(Collection c) @@ -294,32 +353,19 @@ public void testRemove_DoesNotContainElementAndRemoves_False() { this set does not permit null elements (optional), or if the specified collection is null*/ - - //------------test Myset Bool addAll(Collection c) - /**Adds all of the elements in the specified collection to - this set if they're not already present (optional operation). - 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. The behavior of this - operation is undefined if the specified collection is modified while the operation is in progress. - Parameters: - c - collection containing elements to be added to this set - Returns: - true if this set changed as a result of the call - NullPointerException - if the specified collection contains one or more null elements - and this set does not permit null elements, or if the specified collection is null*/ - @Test - public void testAddAll_Union2Sets() { - Integer[] array1 = {10, 20, 30, 40}; - Integer[] array2 = {50, 60, 70, 80}; - MySet testOb = new MySet<>(); - testOb.addAll(array1); - testOb.addAll(array2); - - Integer[] expected = {10, 20, 30, 40, 50, 60, 70, 80}; - Integer[] actual = testOb.toArray(new Integer[0]); - Assert.assertArrayEquals(expected, actual); - } +// @Test +// public void testAddAll_Union2Sets() { +// Integer[] array1 = {10, 20, 30, 40}; +// Integer[] array2 = {50, 60, 70, 80}; +// MySet testOb = new MySet<>(); +// testOb.addAll(array1); +// testOb.addAll(array2); +// +// Integer[] expected = {10, 20, 30, 40, 50, 60, 70, 80}; +// Integer[] actual = testOb.toArray(new Integer[0]); +// Assert.assertArrayEquals(expected, actual); +// } //------------test Myset boolean retainAll(Collection c) @@ -336,7 +382,6 @@ collection does not permit null elements (optional), or if the specified collect */ - //------------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 @@ -351,22 +396,6 @@ collection does not permit null elements (optional), or if the specified collect remove(Object), contains(Object)*/ - - //------------test Myset void clear() - /** Removes all of the elements from this set (optional operation). - * The set will be empty after this call returns. */ - - @Test - public void testClear_SetEmpty() { - Integer[] input = {50, 60, 70}; - MySet testOb = new MySet<>(input); - testOb.clear(); - String[] expected = {null,null,null,null}; - String [] actual = test.toArray(new String[0]); - Assert.assertArrayEquals(expected, actual); - } - - //------------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, @@ -383,7 +412,6 @@ equals in class Object 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. From 20230e92dbafeda47f34b1c5108190a53664e631 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 18:21:42 -0400 Subject: [PATCH 04/12] cleaned up tests and applied generics to methods, added some descriptions --- src/main/java/MySet.java | 24 ++++++++------ src/test/java/TestMySet.java | 62 +++++++++++++++++------------------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index 62892c4..53878ae 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -33,7 +33,7 @@ public int size() { * @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(Object element) { + public boolean add(E element) { if (element == null) { throw new NullPointerException("Specified element can't be null"); } @@ -59,7 +59,7 @@ public boolean isEmpty() { * @param element to be checked * @return true if this set contains the specified element */ - public Boolean contains(Object element) { + public Boolean contains(E element) { if (element == null) { throw new NullPointerException("Specified element can't be null"); } @@ -77,14 +77,14 @@ public Boolean contains(Object element) { * @param element - element to be removed from this set, if present * @return true if this set contained the specified element */ - public boolean remove(Object 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].equals(element)) { + if (this.inputArray[i] != null && this.inputArray[i].equals(element)) { indexOf = i; canRemove = true; break; @@ -108,10 +108,10 @@ public boolean remove(Object element) { * @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) { + public boolean addAll(Collection container) { boolean allAdded = container.size() > 0; - for (Object o : container) { - if (!this.add(o)) { + for (E obj : container) { + if (!this.add(obj)) { allAdded = false; } } @@ -129,10 +129,12 @@ public void clear() { /** * Get array representation of set - * @return + * makes a copy of input array and removes all the nulls. + * @return array with only elements */ - public Object[] toArray() { - return trimTrailingNull(this.inputArray); + @SuppressWarnings("unchecked") + public E[] toArray() { + return (E[]) trimTrailingNull(this.inputArray); } /** @@ -141,6 +143,7 @@ public Object[] toArray() { * Trim the null values from an array. * @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, */ private static Object[] trimTrailingNull(Object[] arr) { int lastNotNull = 0; @@ -167,6 +170,7 @@ private static Object[] trimTrailingNull(Object[] arr) { * 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; diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index a94e4d0..11e6dd7 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -105,9 +105,9 @@ public void testConstructorObjectArray() { @Test public void testSizeReturnsElementCountIfLessThanCapacity() { MySet testSet = new MySet<>(20); - testSet.add("this"); - testSet.add("that"); - testSet.add("andTheOtherThing"); + testSet.add(10); + testSet.add(11); + testSet.add(12); int expected = 3; int actual = testSet.size(); @@ -117,7 +117,7 @@ public void testSizeReturnsElementCountIfLessThanCapacity() { @Test public void testSizeReturnsElementCountIfMaxCapacity() { - MySet testSet = new MySet<>(5); + MySet testSet = new MySet<>(5); testSet.add("this"); testSet.add("that"); testSet.add("andTheOtherThing"); @@ -134,7 +134,7 @@ public void testSizeReturnsElementCountIfMaxCapacity() { //---------------test MySet isEmpty() @Test public void testIsEmptyReturnsFalse() { - MySet testSet = new MySet<>(5); + MySet testSet = new MySet<>(5); testSet.add("this"); testSet.add("that"); testSet.add("andTheOtherThing"); @@ -156,7 +156,7 @@ public void testIsEmptyReturnsTrue() { //---------------test MySet contains() @Test public void testContainsTrue() { - MySet testSet = new MySet<>(5); + MySet testSet = new MySet<>(5); testSet.add("thisThing"); testSet.add("thatThing"); testSet.add("something"); @@ -170,7 +170,7 @@ public void testContainsTrue() { @Test public void testContainsFalse() { - MySet testSet = new MySet<>(5); + MySet testSet = new MySet<>(5); testSet.add("thisThing"); testSet.add("thatThing"); testSet.add("theOtherThing"); @@ -199,16 +199,6 @@ public void testAdd_SetContainsElementAfterAdd_True() { Assert.assertEquals(true, actual); } -// @Test -// public void testAddAppendsAtEndOfList() { -// MySet testSet = new MySet<>(); -// String expected = "andTheOtherThing"; -// testSet.add("this"); -// testSet.add("that"); -// testSet.add(expected); -// String actual = testSet.get(2); -// Assert.assertEquals(expected, actual); -// } @Test //return true if set did not contain the specified element public void testAdd_DoesNotContainElement_True() { @@ -241,6 +231,7 @@ public void testAdd_AlreadyContainsElement_False() { // testSet.add(20); // testSet.add(30); // Boolean actual = testSet.add(20); + //check size before and after // Assert.assertEquals(false, actual); // } @@ -344,27 +335,32 @@ public void testClearSetsSizeToZero() { //------------test Myset Bool containsAll(Collection c) - /** Returns 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. - c - collection to be checked for containment in this set - Returns: - true if this set contains all of the elements of the specified collection - NullPointerException - if the specified collection contains one or more null elements and - this set does not permit null elements (optional), or if the specified collection is null*/ + /** + * Returns 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. + * c - collection to be checked for containment in this set + * Returns: + * true if this set contains all of the elements of the specified collection + * NullPointerException - if the specified collection contains one or more null elements and + * this set does not permit null elements (optional), or if the specified collection is null + */ - //------------test Myset Bool addAll(Collection c) + +// ------------test Myset Bool addAll(Collection c) // @Test // public void testAddAll_Union2Sets() { -// Integer[] array1 = {10, 20, 30, 40}; -// Integer[] array2 = {50, 60, 70, 80}; -// MySet testOb = new MySet<>(); -// testOb.addAll(array1); -// testOb.addAll(array2); +// MySet set1 = new MySet<>(); +// set1.add("yes"); +// set1.add("is"); +// set1.add("fun"); +// MySet set2 = new MySet<>(); +// set2.add("this"); +// set2.add("is"); +// set2.add("fun"); +// +// set1.addAll(set2); // -// Integer[] expected = {10, 20, 30, 40, 50, 60, 70, 80}; -// Integer[] actual = testOb.toArray(new Integer[0]); -// Assert.assertArrayEquals(expected, actual); // } From d195e2d89af18c0a9e54eddd270b449dc3191895 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 20:47:52 -0400 Subject: [PATCH 05/12] added tests for toArray and addAll completed those methods, debugged toArray --- src/main/java/MySet.java | 11 +-- src/test/java/TestMySet.java | 133 +++++++++++++++++++++++++++++------ 2 files changed, 118 insertions(+), 26 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index 53878ae..3f2c634 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -1,3 +1,4 @@ +import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collection; @@ -133,8 +134,9 @@ public void clear() { * @return array with only elements */ @SuppressWarnings("unchecked") - public E[] toArray() { - return (E[]) trimTrailingNull(this.inputArray); + public E[] toArray(E[] in) { + Object[] t = trimTrailingNull(this.inputArray); + return (E[]) Arrays.copyOf(t, t.length, in.getClass()); } /** @@ -145,7 +147,8 @@ public E[] toArray() { * @return array with with trailing nulls removed * Do not make generic, internal to class, */ - private static Object[] trimTrailingNull(Object[] arr) { + @SuppressWarnings("unchecked") + private static Object[] trimTrailingNull(Object[] arr) { int lastNotNull = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] != null) { @@ -159,7 +162,7 @@ private static Object[] trimTrailingNull(Object[] arr) { } Object[] trimmed = new Object[lastNotNull]; - for (int i = 0; i <= lastNotNull; i++) { + for (int i = 0; i < lastNotNull; i++) { trimmed[i] = arr[i]; } diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 11e6dd7..7d174bc 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -1,7 +1,10 @@ + import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.ArrayList; + public class TestMySet { //---------------test MySet constructor @@ -323,13 +326,79 @@ public void testClearSetsSizeToZero() { Assert.assertEquals(0, actual); } -//---------------test MySet toArray() Object[] toArray() - /** - * Returns an array containing all of the elements in this set - * the returned array will be "safe" in that no references to it are maintained by this set - * meaning this method must allocate a new array if if this set is backed by an array - * user should be free to modify the returned array - */ + + //---------------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 toArray() T[] toArray(T[] a) @@ -347,22 +416,42 @@ public void testClearSetsSizeToZero() { */ -// ------------test Myset Bool addAll(Collection c) -// @Test -// public void testAddAll_Union2Sets() { -// MySet set1 = new MySet<>(); -// set1.add("yes"); -// set1.add("is"); -// set1.add("fun"); -// MySet set2 = new MySet<>(); -// set2.add("this"); -// set2.add("is"); -// set2.add("fun"); -// -// set1.addAll(set2); -// -// } + //------------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) /**Retains only the elements in this set that are contained in the specified collection From 059878e6819da4e6e3ad10e12ab38a4e07df7a29 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 21:15:11 -0400 Subject: [PATCH 06/12] completed tests for containsAll --- src/main/java/MySet.java | 57 +++++++++++++++++++++++++++++-- src/test/java/TestMySet.java | 65 ++++++++++++++++++++++++++++++------ 2 files changed, 110 insertions(+), 12 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index 3f2c634..ac05e9b 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -201,8 +201,61 @@ private void ensureCapacity() { } - - +///**/** +// * Remove all elements that are also in c +// * returns true if ANY elements are removed +// * @param c +// * @return +// */ +//public boolean removeAll(Collection c) { +// boolean anyRemoved = false; +// for (Object o : c) { +// if (this.remove(o)) { +// anyRemoved = true; +// } +// } +// return anyRemoved; +// } +// +///** +// * Remove all elements from set except those in c +// * @param c +// * @return +// */ +//public boolean retainAll(Collection c) { +// for (int i = 0; i < this.internal.length; i++) { +// if (!c.contains(this.internal[i])) { +// this.internal[i] = null; +// this.size--; +// } +// } +// +// this.internal = makeContiguous(this.internal); +// +// return true; +// } +// +///** +// * Set contains all elements in c +// * @param c +// * @return +// */ +//public boolean containsAll(Collection c) { +// for (Object o : c) { +// if (!this.contains(o)) { +// return false; +// } +// } +// return true; +// } +// } +// +// +// Add CommentCollapse  +// Message Input +// +// Message #general */ +// diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 7d174bc..dc7341d 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -227,16 +227,17 @@ public void testAdd_AlreadyContainsElement_False() { 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); -// Boolean actual = testSet.add(20); - //check size before and after -// 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() { @@ -415,6 +416,50 @@ public void testToArray_String() { * this set does not permit null elements (optional), or if the specified collection is null */ + @Test + public void testContainsAll_True() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + Integer[] actual = testSet.containsAll({1, 2, 3, 4}); + + Assert.assertArrayEquals(true, actual); + } + + @Test + public void testContainsAll_False() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + Integer[] actual = testSet.containsAll({5,6,7,8}); + + Assert.assertArrayEquals(false, actual); + } + + @Test + public void testContainsAll_subsetOfSet_True() { + MySet testSet = new MySet<>(); + testSet.add(1); + testSet.add(2); + testSet.add(3); + testSet.add(4); + + Integer[] actual = testSet.containsAll({1,2}); + + Assert.assertArrayEquals(true, actual); + } + + @Test(expected = NullPointerException.class) + public void testContainsAll_ThrowsExceptionIfElementSpecifiedNull() { + MySet testObject = new MySet<>(); + testObject.containsAll(null); + } //------------test Myset Bool addAll(Collection c) @Test From 123113398e8c34c95bf55ccefe3a255f17e299d5 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 21:16:44 -0400 Subject: [PATCH 07/12] removed --- src/main/java/MySet.java | 63 ---------------------------------------- 1 file changed, 63 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index ac05e9b..b362f7c 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -201,66 +201,3 @@ private void ensureCapacity() { } -///**/** -// * Remove all elements that are also in c -// * returns true if ANY elements are removed -// * @param c -// * @return -// */ -//public boolean removeAll(Collection c) { -// boolean anyRemoved = false; -// for (Object o : c) { -// if (this.remove(o)) { -// anyRemoved = true; -// } -// } -// return anyRemoved; -// } -// -///** -// * Remove all elements from set except those in c -// * @param c -// * @return -// */ -//public boolean retainAll(Collection c) { -// for (int i = 0; i < this.internal.length; i++) { -// if (!c.contains(this.internal[i])) { -// this.internal[i] = null; -// this.size--; -// } -// } -// -// this.internal = makeContiguous(this.internal); -// -// return true; -// } -// -///** -// * Set contains all elements in c -// * @param c -// * @return -// */ -//public boolean containsAll(Collection c) { -// for (Object o : c) { -// if (!this.contains(o)) { -// return false; -// } -// } -// return true; -// } -// } -// -// -// Add CommentCollapse  -// Message Input -// -// Message #general */ -// - - - - - - - - From 1b690c5d971548d24cb129aa2ac7c9ff81926276 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 21:50:01 -0400 Subject: [PATCH 08/12] completed containsAll method, refactored shitty tests --- src/main/java/MySet.java | 26 +++++++++++++++++++++++- src/test/java/TestMySet.java | 39 ++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index b362f7c..f5b74b8 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -22,6 +22,7 @@ public MySet(int capacity) { /** * Get set size + * * @return current number of elements in this list */ public int size() { @@ -30,6 +31,7 @@ public int 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 @@ -50,6 +52,7 @@ public boolean add(E element) { /** * Check if set is empty + * * @return true if this set contains no elements */ public boolean isEmpty() { @@ -75,6 +78,7 @@ public Boolean contains(E element) { /** * 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 */ @@ -119,6 +123,23 @@ public boolean addAll(Collection container) { return allAdded; } + /** + * NullPointerException - if the specified collection contains one or more null elements and + * this set does not permit null elements (optional), or if the specified collection is null + * + * @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; + } + /** * Removes all of the elements from this set * The set will be empty after this call returns. @@ -131,6 +152,7 @@ public void clear() { /** * Get array representation of set * makes a copy of input array and removes all the nulls. + * * @return array with only elements */ @SuppressWarnings("unchecked") @@ -143,12 +165,13 @@ public E[] toArray(E[] in) { * NullPointerException - if the specified collection contains one or more null elements * and this set does not permit null elements, or if the specified collection is null * Trim the null values from an array. + * * @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) { + private static Object[] trimTrailingNull(Object[] arr) { int lastNotNull = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] != null) { @@ -171,6 +194,7 @@ private static Object[] trimTrailingNull(Object[] arr) { /** * 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 diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index dc7341d..6415b6a 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -405,17 +405,6 @@ public void testToArray_String() { //------------test Myset Bool containsAll(Collection c) - - /** - * Returns 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. - * c - collection to be checked for containment in this set - * Returns: - * true if this set contains all of the elements of the specified collection - * NullPointerException - if the specified collection contains one or more null elements and - * this set does not permit null elements (optional), or if the specified collection is null - */ - @Test public void testContainsAll_True() { MySet testSet = new MySet<>(); @@ -424,9 +413,15 @@ public void testContainsAll_True() { testSet.add(3); testSet.add(4); - Integer[] actual = testSet.containsAll({1, 2, 3, 4}); + ArrayList check = new ArrayList(); + check.add(1); + check.add(2); + check.add(3); + check.add(4); - Assert.assertArrayEquals(true, actual); + boolean actual = testSet.containsAll(check); + + Assert.assertTrue(actual); } @Test @@ -437,9 +432,15 @@ public void testContainsAll_False() { testSet.add(3); testSet.add(4); - Integer[] actual = testSet.containsAll({5,6,7,8}); + ArrayList check = new ArrayList(); + check.add(5); + check.add(6); + check.add(7); + check.add(8); + + boolean actual = testSet.containsAll(check); - Assert.assertArrayEquals(false, actual); + Assert.assertFalse(actual); } @Test @@ -450,9 +451,13 @@ public void testContainsAll_subsetOfSet_True() { testSet.add(3); testSet.add(4); - Integer[] actual = testSet.containsAll({1,2}); + ArrayList check = new ArrayList(); + check.add(1); + check.add(2); + + boolean actual = testSet.containsAll(check); - Assert.assertArrayEquals(true, actual); + Assert.assertTrue(actual); } @Test(expected = NullPointerException.class) From bea0aa1d023fe9ba6b80378f7ec33b3ec1a76fa5 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 22:16:31 -0400 Subject: [PATCH 09/12] added tests for retainAll() --- src/test/java/TestMySet.java | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 6415b6a..d79c2da 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -514,9 +514,52 @@ public void testAddAll_Union2Sets_WithOnlyUniqueValues() { 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 - */ + *///if dup retain it + @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_RetainOnlyDups() { + 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 testContainsAll_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 From 84d5bac3e9f4f68b338916706e1e064d7b1b4ac7 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 22:29:30 -0400 Subject: [PATCH 10/12] retain all method complete and all tests wrote passing --- src/main/java/MySet.java | 16 ++++++++++++++++ src/test/java/TestMySet.java | 16 +++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index f5b74b8..6c54b1e 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -140,6 +140,22 @@ public boolean containsAll(Collection container) { 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; + } + /** * Removes all of the elements from this set * The set will be empty after this call returns. diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index d79c2da..23dec8e 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -504,17 +504,7 @@ public void testAddAll_Union2Sets_WithOnlyUniqueValues() { } //------------test Myset boolean retainAll(Collection c) - /**Retains only the elements in this set that are contained in the specified collection - (optional operation). In other words, removes from this set all of its elements that are not - contained in the specified collection. If the specified collection is also a set, this operation - effectively modifies this set so that its value is the intersection of the two sets. - Parameters: - c - collection containing elements to be retained in 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 - *///if dup retain it + @Test //returning matching elements public void testRetainAll_RetainOnlyDups() { MySet testSet = new MySet<>(); @@ -534,7 +524,7 @@ public void testRetainAll_RetainOnlyDups() { } @Test //if no matching elements then return empty array - public void testRetainAll_RetainOnlyDups() { + public void testRetainAll_NoMatches_ReturnEmptyArray() { MySet testSet = new MySet<>(); testSet.add("yes"); testSet.add("like"); @@ -551,7 +541,7 @@ public void testRetainAll_RetainOnlyDups() { Assert.assertArrayEquals(expected, actual); } @Test(expected = NullPointerException.class) - public void testContainsAll_ThrowsExceptionIfElementSpecifiedNull() { + public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { MySet testSet = new MySet<>(); testSet.add("yes"); testSet.add("like"); From 9603cef0baf8a7be3b777e5a65cc1b9c1e962792 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Sun, 11 Mar 2018 22:47:54 -0400 Subject: [PATCH 11/12] cleaned up formatting --- src/main/java/MySet.java | 9 +-------- src/test/java/TestMyArrayList.java | 1 - src/test/java/TestMySet.java | 12 ++++++++++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index 6c54b1e..c2334e5 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -75,7 +75,6 @@ public Boolean contains(E element) { return false; } - /** * Removes the specified element from this set if it is present * @@ -124,9 +123,6 @@ public boolean addAll(Collection container) { } /** - * NullPointerException - if the specified collection contains one or more null elements and - * this set does not permit null elements (optional), or if the specified collection is null - * * @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. @@ -142,6 +138,7 @@ public boolean containsAll(Collection container) { /** * 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. */ @@ -178,10 +175,6 @@ public E[] toArray(E[] in) { } /** - * NullPointerException - if the specified collection contains one or more null elements - * and this set does not permit null elements, or if the specified collection is null - * Trim the null values from an array. - * * @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, diff --git a/src/test/java/TestMyArrayList.java b/src/test/java/TestMyArrayList.java index 700974f..94f991a 100644 --- a/src/test/java/TestMyArrayList.java +++ b/src/test/java/TestMyArrayList.java @@ -26,7 +26,6 @@ public void testConstructorSizeIncrease() { Assert.assertEquals(expectedSize, actualLength); } - //---------------Test isEmpty @Test public void testIsSizeEmptyReturnsFalse() { diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 23dec8e..79803cc 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -157,6 +157,7 @@ public void testIsEmptyReturnsTrue() { } //---------------test MySet contains() + @Test public void testContainsTrue() { MySet testSet = new MySet<>(5); @@ -192,6 +193,7 @@ public void testContainsThrowsExceptionIfElementSpecifiedNull() { } //------------test Myset Bool add(E e) + @Test //Check that element is in list after adding public void testAdd_SetContainsElementAfterAdd_True() { MySet testSet = new MySet<>(); @@ -312,6 +314,7 @@ public void testRemove_ThrowsExceptionIfElementSpecifiedNull() { } //------------test Myset void clear() + @Test public void testClearSetsSizeToZero() { MySet testSet = new MySet<>(); @@ -329,6 +332,7 @@ public void testClearSetsSizeToZero() { } //---------------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<>(); @@ -401,10 +405,9 @@ public void testToArray_String() { Assert.assertArrayEquals(expected, actual); } - //--------------test Myset toArray() T[] toArray(T[] a) - //------------test Myset Bool containsAll(Collection c) + @Test public void testContainsAll_True() { MySet testSet = new MySet<>(); @@ -467,6 +470,7 @@ public void testContainsAll_ThrowsExceptionIfElementSpecifiedNull() { } //------------test Myset Bool addAll(Collection c) + @Test public void testAddAll_Union2Sets() { MySet testSet = new MySet<>(); @@ -540,6 +544,7 @@ public void testRetainAll_NoMatches_ReturnEmptyArray() { Assert.assertArrayEquals(expected, actual); } + @Test(expected = NullPointerException.class) public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { MySet testSet = new MySet<>(); @@ -550,6 +555,9 @@ public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { testSet.retainAll(null); } + + //--------------test Myset toArray() T[] toArray(T[] a) + //------------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 From 2c8c6238eb53b552ef93e9773fd500b30e6cb4f4 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Mon, 12 Mar 2018 10:26:23 -0400 Subject: [PATCH 12/12] completed removeAll tests and method --- src/main/java/MySet.java | 17 +++++++++ src/test/java/TestMySet.java | 67 +++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index c2334e5..8db4dde 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -153,6 +153,21 @@ public boolean retainAll(Collection container) { 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. @@ -234,3 +249,5 @@ private void ensureCapacity() { } + + diff --git a/src/test/java/TestMySet.java b/src/test/java/TestMySet.java index 79803cc..c95e6fa 100644 --- a/src/test/java/TestMySet.java +++ b/src/test/java/TestMySet.java @@ -555,10 +555,7 @@ public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { testSet.retainAll(null); } - - //--------------test Myset toArray() T[] toArray(T[] a) - - //------------test Myset boolean removeAll(Collection c) +//------------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. @@ -571,6 +568,68 @@ public void testRetainAll_ThrowsExceptionIfElementSpecifiedNull() { 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.