Leon hace 6 años
commit
9292418923

+ 115
- 0
.gitignore Ver fichero

@@ -0,0 +1,115 @@
1
+# Created by .ignore support plugin (hsz.mobi)
2
+### Java template
3
+# Compiled class file
4
+*.class
5
+
6
+# Log file
7
+*.log
8
+
9
+# BlueJ files
10
+*.ctxt
11
+
12
+# Mobile Tools for Java (J2ME)
13
+.mtj.tmp/
14
+
15
+# Package Files #
16
+*.jar
17
+*.war
18
+*.ear
19
+*.zip
20
+*.tar.gz
21
+*.rar
22
+
23
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
24
+hs_err_pid*
25
+### Eclipse template
26
+
27
+.metadata
28
+bin/
29
+tmp/
30
+*.tmp
31
+*.bak
32
+*.swp
33
+*~.nib
34
+local.properties
35
+.settings/
36
+.loadpath
37
+.recommenders
38
+
39
+# Eclipse Core
40
+.project
41
+
42
+# External tool builders
43
+.externalToolBuilders/
44
+
45
+# Locally stored "Eclipse launch configurations"
46
+*.launch
47
+
48
+# PyDev specific (Python IDE for Eclipse)
49
+*.pydevproject
50
+
51
+# CDT-specific (C/C++ Development Tooling)
52
+.cproject
53
+
54
+# JDT-specific (Eclipse Java Development Tools)
55
+.classpath
56
+
57
+# Java annotation processor (APT)
58
+.factorypath
59
+
60
+# PDT-specific (PHP Development Tools)
61
+.buildpath
62
+
63
+# sbteclipse plugin
64
+.target
65
+
66
+# Tern plugin
67
+.tern-project
68
+
69
+# TeXlipse plugin
70
+.texlipse
71
+
72
+# STS (Spring Tool Suite)
73
+.springBeans
74
+
75
+# Code Recommenders
76
+.recommenders/
77
+
78
+# Scala IDE specific (Scala & Java development for Eclipse)
79
+.cache-main
80
+.scala_dependencies
81
+.worksheet
82
+### macOS template
83
+*.DS_Store
84
+.AppleDouble
85
+.LSOverride
86
+
87
+# Icon must end with two \r
88
+Icon
89
+
90
+
91
+# Thumbnails
92
+._*
93
+
94
+# Files that might appear in the root of a volume
95
+.DocumentRevisions-V100
96
+.fseventsd
97
+.Spotlight-V100
98
+.TemporaryItems
99
+.Trashes
100
+.VolumeIcon.icns
101
+.com.apple.timemachine.donotpresent
102
+
103
+# Directories potentially created on remote AFP share
104
+.AppleDB
105
+.AppleDesktop
106
+Network Trash Folder
107
+Temporary Items
108
+.apdisk
109
+
110
+#Intellij files
111
+.idea
112
+*.iml
113
+
114
+#maven build target
115
+target/

+ 233
- 0
README.md Ver fichero

@@ -0,0 +1,233 @@
1
+
2
+# Delete Duplicates 
3
+* **Objective**
4
+	* To write a method which removes duplicate elements from an array.
5
+* **Purpose**
6
+	* To demonstrate practical understanding of `while`, `for`, and `for each` loops.
7
+* **Instructions**
8
+    * Given an object, `DuplicateDeleter`, with a composite `Integer[]` object, write a method
9
+        * `removeDuplicatesExactly` which removes all values in the array which occur exactly the specified number of times.
10
+        * `removeDuplicates` which removes all values in the array which occur at least the specified number of times.
11
+
12
+* **Restrictions**
13
+    * No use of any built-in data structures, (`Collection`, `List`, `Map`)
14
+    * Operations should be [idempotent](https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation)
15
+        * The output of the methods should always be the same if the input is the same.
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+# Sample Behavior - DuplicateDeleter.removeDuplicateExactly(n)
35
+
36
+## Example 1
37
+* Sample Script
38
+    ```
39
+    // : Given
40
+    Integer[] array = new Integer[]{1,1,1,23,23,56,57,58};
41
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
42
+    
43
+    // : When
44
+    Integer[] actual = deleter.removeDuplicateExactly(3);
45
+    
46
+    // : Then
47
+    System.out.println(Arrays.toString(actual));
48
+    ```
49
+
50
+* Sample Output
51
+    ```
52
+    [23,23,56,57,58]
53
+    ```
54
+    
55
+
56
+## Example 2
57
+* Sample Script
58
+    ```
59
+    // : Given
60
+    Integer[] array = new Integer[]{1,1,1,23,23,56,57,58};
61
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
62
+    
63
+    // : When
64
+    Integer[] actual = deleter.removeDuplicateExactly(1);
65
+    
66
+    // : Then
67
+    System.out.println(Arrays.toString(actual));
68
+    ```
69
+
70
+* Sample Output
71
+    ```
72
+    [1,1,1,23,23]
73
+    ```
74
+    
75
+
76
+## Example 3
77
+* Sample Script
78
+    ```
79
+    // : Given
80
+    Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
81
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
82
+    
83
+    // : When
84
+    Integer[] actual = deleter.removeDuplicateExactly(3);
85
+    
86
+    // : Then
87
+    System.out.println(Arrays.toString(actual));
88
+    ```
89
+
90
+* Sample Output
91
+    ```
92
+    [1, 1, 2, 4, 4, 5, 5, 5, 5]
93
+    ```
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+# Sample Behavior - DuplicateDeleter.removeDuplicates(n)
115
+
116
+## Example 1
117
+* Sample Script
118
+    ```
119
+    // : Given
120
+    Integer[] array = new Integer[]{1,1,1,23,23,56,57,58};
121
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
122
+    
123
+    // : When
124
+    Integer[] actual = deleter.removeDuplicateExactly(1);
125
+    
126
+    // : Then
127
+    System.out.println(Arrays.toString(actual));
128
+    ```
129
+
130
+* Sample Output
131
+    ```
132
+    []
133
+    ```
134
+
135
+
136
+
137
+
138
+## Example 2
139
+* Sample Script
140
+    ```
141
+    // : Given
142
+    Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
143
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
144
+    
145
+    // : When
146
+    Integer[] actual = deleter.removeDuplicates(2);
147
+    
148
+    // : Then
149
+    System.out.println(Arrays.toString(actual));
150
+    ```
151
+
152
+* Sample Output
153
+    ```
154
+    [2]
155
+    ```
156
+
157
+
158
+
159
+## Example 3
160
+* Sample Script
161
+    ```
162
+    // : Given
163
+    Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
164
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
165
+    
166
+    // : When
167
+    Integer[] actual = deleter.removeDuplicates(3);
168
+    
169
+    // : Then
170
+    System.out.println(Arrays.toString(actual));
171
+    ```
172
+
173
+* Sample Output
174
+    ```
175
+    [1,1,2,4,4]
176
+    ```
177
+    
178
+    
179
+    
180
+    
181
+    
182
+    
183
+    
184
+    
185
+    
186
+    
187
+    
188
+# Sample Behavior - Idempotence
189
+
190
+## Example 1
191
+* Sample Script
192
+    ```
193
+    // : Given
194
+    Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
195
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
196
+    deleter.removeDuplicates(0);
197
+    deleter.removeDuplicates(1);
198
+    deleter.removeDuplicates(2);
199
+    
200
+    // : When
201
+    Integer[] actual = deleter.removeDuplicates(3);
202
+    
203
+    // : Then
204
+    System.out.println(Arrays.toString(actual));
205
+    ```
206
+
207
+* Sample Output
208
+    ```
209
+    [1,1,2,4,4]
210
+    ```
211
+    
212
+
213
+## Example 2
214
+* Sample Script
215
+    ```
216
+    // : Given
217
+    Integer[] array = new Integer[]{1,1,1,23,23,56,57,58};
218
+    DuplicateDeleter deleter = new DuplicateDeleter(array);
219
+    deleter.removeDuplicates(0);
220
+    deleter.removeDuplicates(1);
221
+    deleter.removeDuplicates(2);
222
+    
223
+    // : When
224
+    Integer[] actual = deleter.removeDuplicatesExactly(3);
225
+    
226
+    // : Then
227
+    System.out.println(Arrays.toString(actual));
228
+    ```
229
+
230
+* Sample Output
231
+    ```
232
+    [23,23,56,57,58]
233
+    ```

+ 22
- 0
pom.xml Ver fichero

@@ -0,0 +1,22 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>com.zipcodewilmington.looplabs</groupId>
8
+    <artifactId>deleteduplicates</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+        </dependency>
16
+    </dependencies>
17
+    <properties>
18
+        <maven.compiler.source>1.8</maven.compiler.source>
19
+        <maven.compiler.target>1.8</maven.compiler.target>
20
+    </properties>
21
+
22
+</project>

+ 15
- 0
src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleter.java Ver fichero

@@ -0,0 +1,15 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+/**
4
+ * Created by leon on 1/25/18.
5
+ */
6
+public abstract class DuplicateDeleter<T> implements DuplicateDeleterInterface<T> {
7
+    protected final T[] array;
8
+
9
+    public DuplicateDeleter(T[] intArray) {
10
+        this.array = intArray;
11
+    }
12
+
13
+    abstract public T[] removeDuplicates(int maxNumberOfDuplications);
14
+    abstract public T[] removeDuplicatesExactly(int exactNumberOfDuplications);
15
+}

+ 10
- 0
src/main/java/com/zipcodewilmington/looplabs/DuplicateDeleterInterface.java Ver fichero

@@ -0,0 +1,10 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+/**
4
+ * Created by leon on 1/28/18.
5
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
6
+ */
7
+public interface DuplicateDeleterInterface<T> {
8
+    T[] removeDuplicates(int maxNumberOfDuplications);
9
+    T[] removeDuplicatesExactly(int exactNumberOfDuplications);
10
+}

+ 8
- 0
src/main/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleter.java Ver fichero

@@ -0,0 +1,8 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+/**
4
+ * Created by leon on 1/29/18.
5
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6
+ */
7
+public final class IntegerDuplicateDeleter extends DuplicateDeleter<Integer> {
8
+}

+ 80
- 0
src/main/java/com/zipcodewilmington/looplabs/RandomNumberFactory.java Ver fichero

@@ -0,0 +1,80 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+import java.util.Random;
4
+
5
+/**
6
+ * @author leon.hunter
7
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
8
+ */
9
+public final class RandomNumberFactory {
10
+    private static volatile Random random = new Random();
11
+
12
+    private RandomNumberFactory() {
13
+    }
14
+    /**
15
+     * @param min
16
+     * @param max
17
+     * @return a random character between the specified min and max character range
18
+     */
19
+    public static Character createCharacter(char min, char max) {
20
+        return (char) createInteger((int) min, (int) max).intValue();
21
+    }
22
+
23
+    /**
24
+     * @param min
25
+     * @param max
26
+     * @return a random float between the specified minimum and maximum numeric range
27
+     */
28
+    public static synchronized Float createFloat(float min, float max) {
29
+        return random.nextFloat() * (max - min) + min;
30
+    }
31
+
32
+    /**
33
+     * @param min
34
+     * @param max
35
+     * @return a random integer between the specified minimum and maximum numeric range
36
+     */
37
+    public static Integer createInteger(Integer min, Integer max) {
38
+        return createFloat(min, max).intValue();
39
+    }
40
+
41
+    /**
42
+     * @param min
43
+     * @param max
44
+     * @return an array the specified length containing integers in the specified range
45
+     */
46
+    public static Integer[] createIntegers(int min, int max, int length) {
47
+        Integer[] integers = new Integer[length];
48
+        for(int i=0; i<length; i++) {
49
+            integers[i] = createInteger(min, max);
50
+        }
51
+        return integers;
52
+    }
53
+
54
+    /**
55
+     * @param min
56
+     * @param max
57
+     * @return a random string of the specified length containing characters in the specified range
58
+     */
59
+    public static String createString(char min, char max, int length) {
60
+        StringBuilder sb = new StringBuilder();
61
+        for (int i = 0; i < length; i++) {
62
+            sb.append(createCharacter(min, max));
63
+        }
64
+        return sb.toString();
65
+    }
66
+
67
+
68
+    /**
69
+     * @param min
70
+     * @param max
71
+     * @return an array containing arrays of specified length containing characters in the specified range
72
+     */
73
+    public static String[] createStrings(char min, char max, int stringLength, int arrayLength) {
74
+        String[] array = new String[arrayLength];
75
+        for(int i=0; i<arrayLength; i++) {
76
+            array[i] = createString(min, max, stringLength);
77
+        }
78
+        return array;
79
+    }
80
+}

+ 8
- 0
src/main/java/com/zipcodewilmington/looplabs/StringDuplicateDeleter.java Ver fichero

@@ -0,0 +1,8 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+/**
4
+ * Created by leon on 1/28/18.
5
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying the signature of this class.
6
+ */
7
+public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
8
+}

+ 183
- 0
src/test/java/com/zipcodewilmington/looplabs/IntegerDuplicateDeleterTest.java Ver fichero

@@ -0,0 +1,183 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+
6
+import java.util.Arrays;
7
+
8
+/**
9
+ * Created by leon on 1/25/18.
10
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
11
+ */
12
+public class IntegerDuplicateDeleterTest {
13
+
14
+    private static Integer[] intArray;
15
+    private static DuplicateDeleter<Integer> deleter;
16
+
17
+    @Before
18
+    public void setup() {
19
+        this.intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
20
+        this.deleter = new IntegerDuplicateDeleter(intArray);
21
+    }
22
+
23
+
24
+    @Test
25
+    public void testRemoveDuplicatesExactlyExactly() {
26
+        Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
27
+        Integer[] actual = deleter.removeDuplicatesExactly(3);
28
+        TestUtils.assertArrayEquality(expected, actual);
29
+    }
30
+
31
+
32
+    @Test
33
+    public void testRemoveDuplicatesExactly1() {
34
+        Integer[] expected = new Integer[]{0, 0, 0, 1, 5, 5, 5, 6, 9, 9, 9};
35
+        Integer[] actual = deleter.removeDuplicatesExactly(2);
36
+        TestUtils.assertArrayEquality(expected, actual);
37
+    }
38
+
39
+
40
+
41
+    @Test
42
+    public void testRemoveDuplicatesExactly2() {
43
+        Integer[] expected = new Integer[]{0, 0, 0, 2, 2, 4, 4, 5, 5, 5, 9, 9, 9};
44
+        Integer[] actual = deleter.removeDuplicatesExactly(1);
45
+        TestUtils.assertArrayEquality(expected, actual);
46
+    }
47
+
48
+
49
+    @Test
50
+    public void testRemoveDuplicatesExactly3() {
51
+        Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
52
+        deleter.removeDuplicates(3);
53
+        deleter.removeDuplicatesExactly(2);
54
+        deleter.removeDuplicatesExactly(1);
55
+
56
+        Integer[] actual = deleter.removeDuplicatesExactly(3);
57
+        TestUtils.assertArrayEquality(expected, actual);
58
+    }
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+    @Test
87
+    public void testRemoveDuplicates0() {
88
+        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
89
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
90
+        Integer[] expected = new Integer[]{};
91
+        Integer[] actual = deleter.removeDuplicates(0);
92
+        TestUtils.assertArrayEquality(expected, actual);
93
+    }
94
+
95
+
96
+    @Test
97
+    public void testRemoveDuplicates1() {
98
+        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
99
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
100
+        Integer[] expected = new Integer[]{};
101
+        Integer[] actual = deleter.removeDuplicates(1);
102
+        TestUtils.assertArrayEquality(expected, actual);
103
+    }
104
+
105
+
106
+    @Test
107
+    public void testRemoveDuplicates2() {
108
+        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
109
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
110
+        Integer[] expected = new Integer[]{2};
111
+        Integer[] actual = deleter.removeDuplicates(2);
112
+        TestUtils.assertArrayEquality(expected, actual);
113
+    }
114
+
115
+
116
+    @Test
117
+    public void testRemoveDuplicates3() {
118
+        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
119
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
120
+        Integer[] expected = new Integer[]{1,1,2,4,4};
121
+        Integer[] actual = deleter.removeDuplicates(3);
122
+        TestUtils.assertArrayEquality(expected, actual);
123
+    }
124
+
125
+
126
+    @Test
127
+    public void testRemoveDuplicates4() {
128
+        Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
129
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
130
+        Integer[] expected = new Integer[]{0,0,0,1,1,2,3,3,3,4,4};
131
+        Integer[] actual = deleter.removeDuplicates(4);
132
+        TestUtils.assertArrayEquality(expected, actual);
133
+    }
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+    @Test
150
+    public void testRemoveDuplicatesExactlyIdempotence() {
151
+        Integer[] input = RandomNumberFactory.createIntegers(0,50,150);
152
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
153
+        Integer[] expected = deleter.removeDuplicatesExactly(5);
154
+
155
+        for (int i = 0; i < input.length; i++) {
156
+            deleter.removeDuplicatesExactly(i);
157
+        }
158
+
159
+        Integer[] actual = deleter.removeDuplicatesExactly(5);
160
+
161
+        Arrays.sort(input);
162
+        System.out.println("Input:\n\t" + Arrays.toString(input));
163
+        TestUtils.assertArrayEquality(expected, actual);
164
+    }
165
+
166
+
167
+    @Test
168
+    public void testRemoveDuplicatesIdempotence() {
169
+        Integer[] input = RandomNumberFactory.createIntegers(0,50,150);
170
+        DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
171
+        Integer[] expected = deleter.removeDuplicates(5);
172
+
173
+        for (int i = 0; i < input.length; i++) {
174
+            deleter.removeDuplicates(i);
175
+        }
176
+
177
+        Integer[] actual = deleter.removeDuplicates(5);
178
+
179
+        Arrays.sort(input);
180
+        System.out.println("Input:\n\t" + Arrays.toString(input));
181
+        TestUtils.assertArrayEquality(expected, actual);
182
+    }
183
+}

+ 171
- 0
src/test/java/com/zipcodewilmington/looplabs/StringDuplicateDeleterTest.java Ver fichero

@@ -0,0 +1,171 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.Arrays;
8
+
9
+/**
10
+ * Created by leon on 1/28/18.
11
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
12
+ */
13
+public class StringDuplicateDeleterTest {
14
+    private static String[] stringArray;
15
+    private static volatile DuplicateDeleter<String> deleter;
16
+
17
+    @Before
18
+    public void setup() {
19
+        this.stringArray = new String[]{"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
20
+        this.deleter = new StringDuplicateDeleter(stringArray);
21
+    }
22
+
23
+
24
+    @Test
25
+    public void testRemoveDuplicatesExactly1() {
26
+        String[] expected = new String[]{"aba", "aba", "bba", "bba", "bba", "bba", "bbb", "bbb"};
27
+        String[] actual = deleter.removeDuplicatesExactly(1);
28
+        TestUtils.assertArrayEquality(expected, actual);
29
+    }
30
+
31
+
32
+    @Test
33
+    public void testRemoveDuplicatesExactly2() {
34
+        String[] expected = new String[]{"baa", "bab", "bba", "bba", "bba", "bba"};
35
+        String[] actual = deleter.removeDuplicatesExactly(2);
36
+        TestUtils.assertArrayEquality(expected, actual);
37
+    }
38
+
39
+
40
+    @Test
41
+    public void testRemoveDuplicatesExactly3() {
42
+        String[] expected = new String[]{"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
43
+        String[] actual = deleter.removeDuplicatesExactly(3);
44
+        TestUtils.assertArrayEquality(expected, actual);
45
+    }
46
+
47
+
48
+    @Test
49
+    public void testRemoveDuplicatesExactly4() {
50
+        String[] expected = new String[]{"aba", "aba", "baa", "bab", "bbb", "bbb"};
51
+        String[] actual = deleter.removeDuplicatesExactly(4);
52
+        TestUtils.assertArrayEquality(expected, actual);
53
+    }
54
+
55
+
56
+    @Test
57
+    public void testRemoveDuplicatesExactly5() {
58
+        String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"};
59
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
60
+        String[] expected = new String[]{"ab", "ba", "ba", "ba", "ba"};
61
+        String[] actual = deleter.removeDuplicatesExactly(5);
62
+        TestUtils.assertArrayEquality(expected, actual);
63
+    }
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+    @Test
79
+    public void testRemoveDuplicates0() {
80
+        String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"};
81
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
82
+        String[] expected = new String[]{};
83
+        String[] actual = deleter.removeDuplicates(0);
84
+        TestUtils.assertArrayEquality(expected, actual);
85
+    }
86
+
87
+
88
+
89
+    @Test
90
+    public void testRemoveDuplicates1() {
91
+        String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"};
92
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
93
+        String[] expected = new String[]{};
94
+        String[] actual = deleter.removeDuplicates(1);
95
+        TestUtils.assertArrayEquality(expected, actual);
96
+    }
97
+
98
+
99
+    @Test
100
+    public void testRemoveDuplicates2() {
101
+        String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"};
102
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
103
+        String[] expected = new String[]{"ab"};
104
+        String[] actual = deleter.removeDuplicates(2);
105
+        TestUtils.assertArrayEquality(expected, actual);
106
+    }
107
+
108
+    @Test
109
+    public void testRemoveDuplicates3() {
110
+        String[] input = new String[]{"aa", "aa", "aa", "aa", "aa", "ab", "ba", "ba", "ba", "ba", "bb", "bb", "bb", "bb", "bb"};
111
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
112
+        String[] expected = new String[]{"ab"};
113
+        String[] actual = deleter.removeDuplicates(3);
114
+        TestUtils.assertArrayEquality(expected, actual);
115
+    }
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+    @Test
137
+    public void testRemoveDuplicatesExactlyIdempotence() {
138
+        String[] input = RandomNumberFactory.createStrings('a', 'c', 2, 15);
139
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
140
+        String[] expected = deleter.removeDuplicatesExactly(5);
141
+
142
+        for (int i = 0; i < input.length; i++) {
143
+            deleter.removeDuplicatesExactly(i);
144
+        }
145
+
146
+        String[] actual = deleter.removeDuplicatesExactly(5);
147
+
148
+        Arrays.sort(input);
149
+        System.out.println("Input:\n\t" + Arrays.toString(input));
150
+        TestUtils.assertArrayEquality(expected, actual);
151
+    }
152
+
153
+
154
+    @Test
155
+    public void testRemoveDuplicatesIdempotence() {
156
+        String[] input = RandomNumberFactory.createStrings('a', 'c', 2, 15);
157
+        DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
158
+        String[] expected = deleter.removeDuplicates(5);
159
+
160
+        for (int i = 0; i < input.length; i++) {
161
+            deleter.removeDuplicates(i);
162
+        }
163
+
164
+        String[] actual = deleter.removeDuplicates(5);
165
+
166
+        Arrays.sort(input);
167
+        System.out.println("Input:\n\t" + Arrays.toString(input));
168
+        TestUtils.assertArrayEquality(expected, actual);
169
+    }
170
+
171
+}

+ 17
- 0
src/test/java/com/zipcodewilmington/looplabs/TestSuite.java Ver fichero

@@ -0,0 +1,17 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+import org.junit.runner.RunWith;
4
+import org.junit.runners.Suite;
5
+
6
+/**
7
+ * Created by leon on 1/28/18.
8
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
9
+ */
10
+@RunWith(Suite.class)
11
+
12
+@Suite.SuiteClasses({
13
+        IntegerDuplicateDeleterTest.class,
14
+        StringDuplicateDeleterTest.class
15
+})
16
+public class TestSuite {
17
+}

+ 26
- 0
src/test/java/com/zipcodewilmington/looplabs/TestUtils.java Ver fichero

@@ -0,0 +1,26 @@
1
+package com.zipcodewilmington.looplabs;
2
+
3
+import org.junit.Assert;
4
+
5
+import java.util.Arrays;
6
+
7
+/**
8
+ * Created by leon on 1/28/18.
9
+ * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
10
+ */
11
+public class TestUtils {
12
+    public synchronized static <T, E> void assertArrayEquality(T[] expected, E[] actual) {
13
+        Arrays.sort(expected);
14
+        Arrays.sort(actual);
15
+        String expectedString = Arrays.toString(expected);
16
+        String actualString = Arrays.toString(actual);
17
+        boolean equality = expectedString.equals(actualString);
18
+
19
+        System.out.println("\n\nExpected:\n\t" + expectedString);
20
+        System.out.println("\nActual:\n\t" + actualString);
21
+        System.out.println("\nEquivalence:\n\t" + equality);
22
+
23
+        Assert.assertEquals(expectedString, actualString);
24
+        Assert.assertEquals(expected, actual);
25
+    }
26
+}