|
@@ -7,48 +7,429 @@ import org.junit.Test;
|
7
|
7
|
* Created by leon on 1/29/18.
|
8
|
8
|
*/
|
9
|
9
|
public class StringArrayUtilsTest {
|
10
|
|
- private static final String[] testArray = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
10
|
+
|
|
11
|
+ @Test
|
|
12
|
+ public void testGetFirstElement1() {
|
|
13
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
14
|
+ String expected = "the";
|
|
15
|
+ String actual = StringArrayUtils.getFirstElement(array);
|
|
16
|
+ Assert.assertEquals(expected, actual);
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ @Test
|
|
20
|
+ public void testGetFirstElement2() {
|
|
21
|
+ String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
22
|
+ String expected = "quick";
|
|
23
|
+ String actual = StringArrayUtils.getFirstElement(array);
|
|
24
|
+ Assert.assertEquals(expected, actual);
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+ @Test
|
|
29
|
+ public void testGetFirstElement3() {
|
|
30
|
+ String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
31
|
+ String expected = "brown";
|
|
32
|
+ String actual = StringArrayUtils.getFirstElement(array);
|
|
33
|
+ Assert.assertEquals(expected, actual);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+ @Test
|
|
50
|
+ public void testGetSecondElement1() {
|
|
51
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
52
|
+ String expected = "quick";
|
|
53
|
+ String actual = StringArrayUtils.getSecondElement(array);
|
|
54
|
+ Assert.assertEquals(expected, actual);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ @Test
|
|
58
|
+ public void testGetSecondElement2() {
|
|
59
|
+ String[] array = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
60
|
+ String expected = "brown";
|
|
61
|
+ String actual = StringArrayUtils.getSecondElement(array);
|
|
62
|
+ Assert.assertEquals(expected, actual);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ @Test
|
|
67
|
+ public void testGetSecondElement3() {
|
|
68
|
+ String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
69
|
+ String expected = "fox";
|
|
70
|
+ String actual = StringArrayUtils.getSecondElement(array);
|
|
71
|
+ Assert.assertEquals(expected, actual);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+ @Test
|
|
85
|
+ public void testGetLastElement1() {
|
|
86
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
87
|
+ String expected = "dog";
|
|
88
|
+ String actual = StringArrayUtils.getLastElement(array);
|
|
89
|
+ Assert.assertEquals(expected, actual);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ @Test
|
|
93
|
+ public void testGetLastElement2() {
|
|
94
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy"};
|
|
95
|
+ String expected = "lazy";
|
|
96
|
+ String actual = StringArrayUtils.getLastElement(array);
|
|
97
|
+ Assert.assertEquals(expected, actual);
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+ @Test
|
|
102
|
+ public void testGetLastElement3() {
|
|
103
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
|
|
104
|
+ String expected = "over";
|
|
105
|
+ String actual = StringArrayUtils.getLastElement(array);
|
|
106
|
+ Assert.assertEquals(expected, actual);
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+ @Test
|
|
130
|
+ public void testGetSecondToLastElement1() {
|
|
131
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
132
|
+ String expected = "lazy";
|
|
133
|
+ String actual = StringArrayUtils.getSecondToLastElement(array);
|
|
134
|
+ Assert.assertEquals(expected, actual);
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ @Test
|
|
138
|
+ public void testGetSecondToLastElement2() {
|
|
139
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "lazy"};
|
|
140
|
+ String expected = "over";
|
|
141
|
+ String actual = StringArrayUtils.getSecondToLastElement(array);
|
|
142
|
+ Assert.assertEquals(expected, actual);
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+ @Test
|
|
147
|
+ public void testGetSecondToLastElement3() {
|
|
148
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over"};
|
|
149
|
+ String expected = "jumps";
|
|
150
|
+ String actual = StringArrayUtils.getSecondToLastElement(array);
|
|
151
|
+ Assert.assertEquals(expected, actual);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
11
|
169
|
|
12
|
170
|
@Test
|
13
|
171
|
public void testContains() {
|
14
|
|
- for (String s : testArray) {
|
15
|
|
- boolean outcome = StringArrayUtils.contains(testArray, s);
|
|
172
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
173
|
+ for (String s : array) {
|
|
174
|
+ boolean outcome = StringArrayUtils.contains(array, s);
|
16
|
175
|
Assert.assertTrue(outcome);
|
17
|
176
|
}
|
18
|
177
|
}
|
19
|
178
|
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
20
|
191
|
@Test
|
21
|
|
- public void testRemoveValue() {
|
22
|
|
- String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
23
|
|
- String[] actual = StringArrayUtils.removeValue(expected, "The");
|
|
192
|
+ public void testReverse1() {
|
|
193
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
194
|
+ String[] expected = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
|
|
195
|
+ String[] actual = StringArrayUtils.reverse(array);
|
24
|
196
|
Assert.assertEquals(expected, actual);
|
25
|
197
|
}
|
26
|
198
|
|
|
199
|
+
|
27
|
200
|
@Test
|
28
|
|
- public void testRemoveValue1() {
|
29
|
|
- String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
30
|
|
- String[] actual = StringArrayUtils.removeValue(expected, "quick");
|
|
201
|
+ public void testReverse2() {
|
|
202
|
+ String[] array = {"dog", "lazy", "the", "over", "jumps", "fox", "brown", "quick", "the"};
|
|
203
|
+ String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
204
|
+ String[] actual = StringArrayUtils.reverse(array);
|
31
|
205
|
Assert.assertEquals(expected, actual);
|
32
|
206
|
}
|
33
|
207
|
|
|
208
|
+
|
34
|
209
|
@Test
|
35
|
|
- public void testRemoveValue2() {
|
36
|
|
- String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
|
37
|
|
- String[] actual = StringArrayUtils.removeValue(expected, "brown");
|
|
210
|
+ public void testReverse3() {
|
|
211
|
+ String[] expected = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
212
|
+ String[] actual = StringArrayUtils.reverse(StringArrayUtils.reverse(expected));
|
38
|
213
|
Assert.assertEquals(expected, actual);
|
39
|
214
|
}
|
40
|
215
|
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+ @Test
|
|
226
|
+ public void testIsPalindromic1() {
|
|
227
|
+ String[] array = {"a", "b", "c", "b", "a"};
|
|
228
|
+ boolean outcome = StringArrayUtils.isPalindromic(array);
|
|
229
|
+ Assert.assertTrue(outcome);
|
|
230
|
+ }
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+ @Test
|
|
235
|
+ public void testIsPalindromic2() {
|
|
236
|
+ String[] array = {"Is this a plaindrome?", "This is a plaindrome", "Is this a palindrome?"};
|
|
237
|
+ boolean outcome = StringArrayUtils.isPalindromic(array);
|
|
238
|
+ Assert.assertTrue(outcome);
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+ @Test
|
|
243
|
+ public void testIsPalindromic3() {
|
|
244
|
+ String[] array = {"Is this a plaindrome?", "This is not a plaindrome", "Is this a palindrome?", "This is not a palindrome"};
|
|
245
|
+ boolean outcome = StringArrayUtils.isPalindromic(array);
|
|
246
|
+ Assert.assertTrue(outcome);
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+ @Test
|
|
258
|
+ public void testIsPangramic1() {
|
|
259
|
+ String[] array = {"The quick brown", "Fox jumps over", "The lazy dog"};
|
|
260
|
+ boolean outcome = StringArrayUtils.isPangramic(array);
|
|
261
|
+ Assert.assertTrue(outcome);
|
|
262
|
+ }
|
|
263
|
+
|
|
264
|
+ @Test
|
|
265
|
+ public void testIsPangramic2() {
|
|
266
|
+ String[] array = {"The", "quick", "onyx", "goblin", "jumps", "over", "the", "lazy", "dwarf"};
|
|
267
|
+ boolean outcome = StringArrayUtils.isPangramic(array);
|
|
268
|
+ Assert.assertTrue(outcome);
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ @Test
|
|
272
|
+ public void testIsPangramic3() {
|
|
273
|
+ String[] array = {"Five quacking", "zephyrs", "jolt my", "wax bed"};
|
|
274
|
+ boolean outcome = StringArrayUtils.isPangramic(array);
|
|
275
|
+ Assert.assertTrue(outcome);
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
41
|
290
|
@Test
|
42
|
291
|
public void testGetNumberOfOccurrences1() {
|
|
292
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
293
|
+ int expected = 4;
|
|
294
|
+ int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
|
|
295
|
+ Assert.assertEquals(actual, expected);
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ @Test
|
|
299
|
+ public void testGetNumberOfOccurrences2() {
|
|
300
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
43
|
301
|
int expected = 2;
|
44
|
|
- int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "the");
|
|
302
|
+ int actual = StringArrayUtils.getNumberOfOccurrences(array, "bbb");
|
|
303
|
+ Assert.assertEquals(actual, expected);
|
|
304
|
+ }
|
|
305
|
+
|
|
306
|
+ @Test
|
|
307
|
+ public void testGetNumberOfOccurrences3() {
|
|
308
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
309
|
+ int expected = 4;
|
|
310
|
+ int actual = StringArrayUtils.getNumberOfOccurrences(array, "bba");
|
|
311
|
+ Assert.assertEquals(actual, expected);
|
|
312
|
+ }
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+ @Test
|
|
328
|
+ public void testRemoveConsecutiveDuplicates1() {
|
|
329
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "bba", "bba", "bba", "bbb", "bbb"};
|
|
330
|
+ String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
|
|
331
|
+ String[] expected = {"aba", "baa", "bab", "bba", "bbb"};
|
|
332
|
+ Assert.assertEquals(actual, expected);
|
|
333
|
+ }
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+ @Test
|
|
338
|
+ public void testRemoveConsecutiveDuplicates2() {
|
|
339
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
|
|
340
|
+ String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
|
|
341
|
+ String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "bbb"};
|
|
342
|
+ Assert.assertEquals(actual, expected);
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+ @Test
|
|
348
|
+ public void testRemoveConsecutiveDuplicates3() {
|
|
349
|
+ String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb", "aba", "bbb"};
|
|
350
|
+ String[] actual = StringArrayUtils.removeConsecutiveDuplicates(array);
|
|
351
|
+ String[] expected = {"aba", "baa", "bab", "bba", "zzz", "bba", "aba", "bbb"};
|
|
352
|
+ Assert.assertEquals(actual, expected);
|
|
353
|
+ }
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+ @Test
|
|
366
|
+ public void testRemovePackDuplicates1() {
|
|
367
|
+ String[] array = {"a", "a", "a", "a", "b", "c", "c", "a", "a", "d"};
|
|
368
|
+ String[] expected = {"aaa", "b", "cc", "aa", "d", "eee"};
|
|
369
|
+ String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
|
45
|
370
|
Assert.assertEquals(expected, actual);
|
46
|
371
|
}
|
47
|
372
|
|
|
373
|
+
|
|
374
|
+
|
48
|
375
|
@Test
|
49
|
|
- public void testGetNumberOfOccurrences2() {
|
50
|
|
- int expected = 1;
|
51
|
|
- int actual = StringArrayUtils.getNumberOfOccurrences(testArray, "quick");
|
|
376
|
+ public void testRemovePackDuplicates2() {
|
|
377
|
+ String[] array = {"t", "t", "q", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e"};
|
|
378
|
+ String[] expected = {"tt", "q", "aaa", "cc", "aa", "d", "eee"};
|
|
379
|
+ String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
|
|
380
|
+ Assert.assertEquals(expected, actual);
|
|
381
|
+ }
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+ @Test
|
|
386
|
+ public void testRemovePackDuplicates3() {
|
|
387
|
+ String[] array = {"m", "o", "o", "n", "m", "a", "n"};
|
|
388
|
+ String[] expected = {"m", "oo", "n", "m", "a", "n"};
|
|
389
|
+ String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
|
|
390
|
+ Assert.assertEquals(expected, actual);
|
|
391
|
+ }
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+ @Test
|
|
401
|
+ public void testRemoveValue() {
|
|
402
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
403
|
+ String[] expected = {"quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
404
|
+ String[] actual = StringArrayUtils.removeValue(array, "The");
|
|
405
|
+ Assert.assertEquals(expected, actual);
|
|
406
|
+ }
|
|
407
|
+
|
|
408
|
+ @Test
|
|
409
|
+ public void testRemoveValue1() {
|
|
410
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
411
|
+ String[] expected = {"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
412
|
+ String[] actual = StringArrayUtils.removeValue(array, "quick");
|
|
413
|
+ Assert.assertEquals(expected, actual);
|
|
414
|
+ }
|
|
415
|
+
|
|
416
|
+ @Test
|
|
417
|
+ public void testRemoveValue2() {
|
|
418
|
+ String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
419
|
+ String[] expected = {"the", "quick", "fox", "jumps", "over", "the", "lazy", "dog"};
|
|
420
|
+ String[] actual = StringArrayUtils.removeValue(array, "brown");
|
52
|
421
|
Assert.assertEquals(expected, actual);
|
53
|
422
|
}
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
54
|
435
|
}
|