From f20834a79f686f811095d71be869e0031136987d Mon Sep 17 00:00:00 2001 From: vvmk Date: Thu, 8 Mar 2018 22:10:49 -0500 Subject: [PATCH 1/8] Write ArrayList clone --- .idea/.name | 1 + .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 + .idea/workspace.xml | 942 ++++++++++++++++++ .notes | 7 + generics.iml | 16 + pom.xml | 4 +- src/main/java/.deleteme | 0 src/main/java/MyArrayList.java | 124 +++ src/main/java/MyMap.java | 8 + src/main/java/MySet.java | 8 + src/test/java/.deleteme | 0 src/test/java/MyArrayListTest.java | 133 +++ 17 files changed, 1310 insertions(+), 2 deletions(-) create mode 100644 .idea/.name 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 .idea/workspace.xml create mode 100644 .notes create mode 100644 generics.iml delete mode 100644 src/main/java/.deleteme create mode 100644 src/main/java/MyArrayList.java create mode 100644 src/main/java/MyMap.java create mode 100644 src/main/java/MySet.java delete mode 100644 src/test/java/.deleteme create mode 100644 src/test/java/MyArrayListTest.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..1f58a0f --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +generics \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ce1cf9f --- /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/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..97ad195 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,942 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + arraylist( + remove( + toArray + contains( + to + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -263,7 +271,7 @@ - + + + + + + + + @@ -385,14 +413,16 @@ - + + - + - + + @@ -409,7 +439,8 @@ @@ -417,8 +448,17 @@ - - + + + + + + + + + + + @@ -429,15 +469,6 @@ - - - - - - - - - @@ -446,41 +477,41 @@ - - + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - + + + + - + + + + @@ -491,24 +522,97 @@ - + + - + - - - + + + + + + + + + + + + + + + + + + + + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -516,7 +620,6 @@ - @@ -524,7 +627,6 @@ - @@ -540,7 +642,6 @@ - @@ -548,7 +649,6 @@ - @@ -556,166 +656,115 @@ - - + - - + + - + - - + + - + - - - - - - + + - + - - - + + - + - - - + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -725,8 +774,8 @@ - - + + @@ -751,8 +800,8 @@ - - + + @@ -760,35 +809,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/java/GreatValueSet.java b/src/main/java/GreatValueSet.java index 26c0ecb..c826ab2 100644 --- a/src/main/java/GreatValueSet.java +++ b/src/main/java/GreatValueSet.java @@ -1,3 +1,4 @@ +import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -9,6 +10,7 @@ */ public class GreatValueSet { GreatValueArrayList arrayList; + ArrayList i = null; public GreatValueSet() { arrayList = new GreatValueArrayList<>(); @@ -23,7 +25,7 @@ public GreatValueSet(Collection c) { } public void add(E e) { - + } public void addAll(Collection c) { diff --git a/src/test/java/GreatValueArrayListTest.java b/src/test/java/GreatValueArrayListTest.java index 02c68f7..c3a2523 100644 --- a/src/test/java/GreatValueArrayListTest.java +++ b/src/test/java/GreatValueArrayListTest.java @@ -130,4 +130,14 @@ public void containsFail() { Assert.assertFalse(greatValueArrayList.contains(notInArray)); } + + @Test + public void testOOB() { + try { + greatValueArrayList.get(75); + Assert.fail(); + } catch (IndexOutOfBoundsException oobe) { + oobe.printStackTrace(); + } + } } \ No newline at end of file diff --git a/src/test/java/GreatValueSetTest.java b/src/test/java/GreatValueSetTest.java index aa76f26..9689e68 100644 --- a/src/test/java/GreatValueSetTest.java +++ b/src/test/java/GreatValueSetTest.java @@ -48,11 +48,42 @@ public void addDuplicateElement() { @Test public void addAll() { + Integer[] input = {1, 2, 3}; + Integer[] addMe = {4, 5, 6}; + Integer[] expectedArray = {1, 2, 3, 4, 5, 6}; + String expected = Arrays.toString(expectedArray); + + set = new GreatValueSet<>(Arrays.asList(input)); + set.addAll(Arrays.asList(addMe)); + + Integer[] actualArray = set.toArray(new Integer[0]); + + Arrays.sort(actualArray); + Arrays.sort(expectedArray); + + String actual = Arrays.toString(actualArray); + + Assert.assertEquals(expected, actual); + } + @Test + public void containsAllYes() { + Integer[] input = {1, 2, 3}; + Integer[] checkMe = {1, 2}; + + set = new GreatValueSet<>(Arrays.asList(input)); + + Assert.assertTrue(set.containsAll(Arrays.asList(checkMe))); } @Test - public void containsAll() { + public void containsAllNope() { + Integer[] input = {1, 2, 3, 4, 5}; + Integer[] checkMe = {1, 2, 3, 4, 6}; + + set = new GreatValueSet<>(Arrays.asList(input)); + + Assert.assertFalse(set.containsAll(Arrays.asList(checkMe))); } @Test @@ -61,6 +92,7 @@ public void iterator() { @Test public void removeAll() { + } @Test From 38c0680a45b6fc0485b5ab37d7008411c6e0d24f Mon Sep 17 00:00:00 2001 From: vvmk Date: Fri, 9 Mar 2018 15:33:33 -0500 Subject: [PATCH 8/8] GreatValueSet tests pass GreatValueHashMap to come, game of life more fun --- .idea/workspace.xml | 567 ++++++++++++++++--------- pom.xml | 1 - src/main/java/GreatValueArrayList.java | 25 +- src/main/java/GreatValueSet.java | 41 +- src/main/java/MyMap.java | 8 - src/test/java/GreatValueSetTest.java | 16 +- src/test/java/ProjectTestSuite.java | 16 + 7 files changed, 426 insertions(+), 248 deletions(-) delete mode 100644 src/main/java/MyMap.java create mode 100644 src/test/java/ProjectTestSuite.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9ac8912..2b9c9ba 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,8 +3,10 @@ + + - + @@ -21,11 +23,11 @@ - + - - + + @@ -38,12 +40,13 @@ - - - - - - + + + + + + + @@ -52,11 +55,32 @@ - - + + + + + + + + + + + + - - + + + + + + + + + + + + + @@ -65,40 +89,41 @@ - - + + - - - - - - - - - - + + + + + + + + - - - - - - - - - + + + + + + + + + + + + - + - - + + @@ -112,8 +137,8 @@ @@ -139,15 +164,16 @@ @@ -167,7 +193,7 @@ @@ -198,11 +224,6 @@ - - - - - @@ -271,7 +292,7 @@ - + - + + + + + + + + - + + + + + + + + @@ -413,16 +474,20 @@ - + - - + + + + - - - - + + + + + + @@ -440,44 +505,44 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - @@ -485,13 +550,14 @@ + - + @@ -499,7 +565,7 @@ - + @@ -508,7 +574,6 @@ - @@ -522,7 +587,15 @@ - @@ -547,12 +620,13 @@ - - - - - - + + + + + + + @@ -563,26 +637,27 @@ - - - - - - - - - - + + + + + + + + - - - - - - - - - + + + + + + + + + + + + @@ -592,8 +667,8 @@ - - + + @@ -620,13 +695,7 @@ - - - - - - - + @@ -638,17 +707,11 @@ - - - - - - - + @@ -667,115 +730,227 @@ - + - - + + - + - - + + + + + + - + - - + + + - + - - + + + - + - - + + + - + - - + + + - + - - + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + - + - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -788,23 +963,13 @@ - - - - - - - - - - - - - - - - - + + + + + + + diff --git a/pom.xml b/pom.xml index 0ca9830..1a88bb5 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,6 @@ - junit junit diff --git a/src/main/java/GreatValueArrayList.java b/src/main/java/GreatValueArrayList.java index 2a8af6d..a41e182 100644 --- a/src/main/java/GreatValueArrayList.java +++ b/src/main/java/GreatValueArrayList.java @@ -68,9 +68,9 @@ public E get(int index) { return (E) array[index]; } - public void remove(E e) { + public void remove(Object o) { for (int i = 0; i < numberOfElements; i++) { - if (array[i].equals(e)) + if (array[i].equals(o)) remove(i); } } @@ -79,6 +79,8 @@ public void remove(E e) { private void remove(int index) { Objects.checkIndex(index, numberOfElements); + assert (index < numberOfElements); + int shifts = numberOfElements - index - 1; System.arraycopy(array, index + 1, array, index, shifts); @@ -94,17 +96,25 @@ public boolean isEmpty() { return numberOfElements == 0; } - public boolean contains(Object o) { + public boolean contains(E e) { if (isEmpty()) return false; - for (Object candidate : array) { - if (candidate.equals(o)) + for (int i = 0; i < numberOfElements; i++) { + if (array[i].equals(e)) return true; } return false; } + public boolean containsAll(Collection c) { + for (E e : c) { + if (!contains(e)) + return false; + } + return true; + } + @Override public String toString() { return Arrays.toString(array); @@ -113,7 +123,7 @@ public String toString() { @SuppressWarnings("unchecked") public T[] toArray(T[] a) { if (a.length < numberOfElements) - return (T[]) Arrays.copyOf(array, 0, a.getClass()); + return (T[]) Arrays.copyOf(array, numberOfElements, a.getClass()); // this is here not because I have ever needed it, but because we're cloning ArrayList and it does this. System.arraycopy(array, 0, a, 0, numberOfElements); @@ -122,7 +132,6 @@ public T[] toArray(T[] a) { } public Object[] toArray() { - return array; + return Arrays.copyOf(array, numberOfElements); } - } diff --git a/src/main/java/GreatValueSet.java b/src/main/java/GreatValueSet.java index c826ab2..31f52f8 100644 --- a/src/main/java/GreatValueSet.java +++ b/src/main/java/GreatValueSet.java @@ -1,6 +1,5 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; /** * filename: @@ -9,8 +8,7 @@ * date: 3/8/18 */ public class GreatValueSet { - GreatValueArrayList arrayList; - ArrayList i = null; + private GreatValueArrayList arrayList; public GreatValueSet() { arrayList = new GreatValueArrayList<>(); @@ -25,43 +23,41 @@ public GreatValueSet(Collection c) { } public void add(E e) { + if (arrayList.isEmpty()) + arrayList.add(e); + if (!arrayList.contains(e)) + arrayList.add(e); } public void addAll(Collection c) { - + for (E e : c) + add(e); } public void clear() { - + arrayList.clear(); } - public boolean contains(Object o) { - return false; + public boolean contains(E e) { + return arrayList.contains(e); } - public boolean containsAll(Collection c) { - return false; + public boolean containsAll(Collection c) { + return arrayList.containsAll(c); } public boolean isEmpty() { - return false; + return arrayList.isEmpty(); } - public Iterator iterator() { - return null; - } - - public void remove(Object o) { - + public void remove(E e) { + arrayList.remove(e); } public void removeAll(Collection c) { - - } - - public void retainAll(Collection c) { - + for (Object o : c) + arrayList.remove(o); } public int size() { @@ -73,7 +69,6 @@ public Object[] toArray() { } public T[] toArray(T[] a) { - return null; + return arrayList.toArray(a); } - } diff --git a/src/main/java/MyMap.java b/src/main/java/MyMap.java deleted file mode 100644 index dd40a8a..0000000 --- a/src/main/java/MyMap.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * filename: - * project: generics - * author: https://github.com/vvmk - * date: 3/8/18 - */ -public class MyMap { -} diff --git a/src/test/java/GreatValueSetTest.java b/src/test/java/GreatValueSetTest.java index 9689e68..dc44f61 100644 --- a/src/test/java/GreatValueSetTest.java +++ b/src/test/java/GreatValueSetTest.java @@ -86,16 +86,18 @@ public void containsAllNope() { Assert.assertFalse(set.containsAll(Arrays.asList(checkMe))); } - @Test - public void iterator() { - } - @Test public void removeAll() { + Integer[] input = {1, 2, 3}; + Integer[] removeUs = {2, 3}; + Integer[] expectedArray = {1}; - } + set = new GreatValueSet<>(Arrays.asList(input)); + set.removeAll(Arrays.asList(removeUs)); - @Test - public void retainAll() { + String expected = Arrays.toString(expectedArray); + String actual = Arrays.toString(set.toArray(new Integer[0])); + + Assert.assertEquals(expected, actual); } } \ No newline at end of file diff --git a/src/test/java/ProjectTestSuite.java b/src/test/java/ProjectTestSuite.java new file mode 100644 index 0000000..0ee9b21 --- /dev/null +++ b/src/test/java/ProjectTestSuite.java @@ -0,0 +1,16 @@ +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * filename: + * project: generics + * author: https://github.com/vvmk + * date: 3/9/18 + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + GreatValueArrayListTest.class, + GreatValueSetTest.class +}) +public class ProjectTestSuite { +}