1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3;
19
20 import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException;
21 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24
25 import org.junit.jupiter.api.Test;
26
27
28
29
30 class ArrayUtilsRemoveTest extends AbstractLangTest {
31
32 @Test
33 void testRemoveAllBooleanOccurences() {
34 boolean[] a = null;
35 assertNull(ArrayUtils.removeAllOccurences(a, true));
36
37 a = new boolean[0];
38 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a, true));
39
40 a = new boolean[] { true };
41 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a, true));
42
43 a = new boolean[] { true, true };
44 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurences(a, true));
45
46 a = new boolean[] { false, true, true, false, true };
47 assertArrayEquals(new boolean[]{false, false}, ArrayUtils.removeAllOccurences(a, true));
48
49 a = new boolean[] { false, true, true, false, true };
50 assertArrayEquals(new boolean[]{true, true, true}, ArrayUtils.removeAllOccurences(a, false));
51 }
52
53 @Test
54 void testRemoveAllBooleanOccurrences() {
55 boolean[] a = null;
56 assertNull(ArrayUtils.removeAllOccurrences(a, true));
57
58 a = new boolean[0];
59 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurrences(a, true));
60
61 a = new boolean[] { true };
62 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurrences(a, true));
63
64 a = new boolean[] { true, true };
65 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.removeAllOccurrences(a, true));
66
67 a = new boolean[] { false, true, true, false, true };
68 assertArrayEquals(new boolean[]{false, false}, ArrayUtils.removeAllOccurrences(a, true));
69
70 a = new boolean[] { false, true, true, false, true };
71 assertArrayEquals(new boolean[]{true, true, true}, ArrayUtils.removeAllOccurrences(a, false));
72 }
73
74 @Test
75 void testRemoveAllByteOccurences() {
76 byte[] a = null;
77 assertNull(ArrayUtils.removeAllOccurences(a, (byte) 2));
78
79 a = new byte[0];
80 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a, (byte) 2));
81
82 a = new byte[] { 2 };
83 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a, (byte) 2));
84
85 a = new byte[] { 2, 2 };
86 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurences(a, (byte) 2));
87
88 a = new byte[] { 1, 2, 2, 3, 2 };
89 assertArrayEquals(new byte[]{1, 3}, ArrayUtils.removeAllOccurences(a, (byte) 2));
90
91 a = new byte[] { 1, 2, 2, 3, 2 };
92 assertArrayEquals(new byte[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, (byte) 4));
93 }
94
95 @Test
96 void testRemoveAllByteOccurrences() {
97 byte[] a = null;
98 assertNull(ArrayUtils.removeAllOccurrences(a, (byte) 2));
99
100 a = new byte[0];
101 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurrences(a, (byte) 2));
102
103 a = new byte[] { 2 };
104 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurrences(a, (byte) 2));
105
106 a = new byte[] { 2, 2 };
107 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.removeAllOccurrences(a, (byte) 2));
108
109 a = new byte[] { 1, 2, 2, 3, 2 };
110 assertArrayEquals(new byte[]{1, 3}, ArrayUtils.removeAllOccurrences(a, (byte) 2));
111
112 a = new byte[] { 1, 2, 2, 3, 2 };
113 assertArrayEquals(new byte[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, (byte) 4));
114 }
115
116 @Test
117 void testRemoveAllCharOccurences() {
118 char[] a = null;
119 assertNull(ArrayUtils.removeAllOccurences(a, '2'));
120
121 a = new char[0];
122 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a, '2'));
123
124 a = new char[] { '2' };
125 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a, '2'));
126
127 a = new char[] { '2', '2' };
128 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurences(a, '2'));
129
130 a = new char[] { '1', '2', '2', '3', '2' };
131 assertArrayEquals(new char[]{'1', '3'}, ArrayUtils.removeAllOccurences(a, '2'));
132
133 a = new char[] { '1', '2', '2', '3', '2' };
134 assertArrayEquals(new char[]{'1', '2', '2', '3', '2'}, ArrayUtils.removeAllOccurences(a, '4'));
135 }
136
137 @Test
138 void testRemoveAllCharOccurrences() {
139 char[] a = null;
140 assertNull(ArrayUtils.removeAllOccurrences(a, '2'));
141
142 a = new char[0];
143 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurrences(a, '2'));
144
145 a = new char[] { '2' };
146 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurrences(a, '2'));
147
148 a = new char[] { '2', '2' };
149 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.removeAllOccurrences(a, '2'));
150
151 a = new char[] { '1', '2', '2', '3', '2' };
152 assertArrayEquals(new char[]{'1', '3'}, ArrayUtils.removeAllOccurrences(a, '2'));
153
154 a = new char[] { '1', '2', '2', '3', '2' };
155 assertArrayEquals(new char[]{'1', '2', '2', '3', '2'}, ArrayUtils.removeAllOccurrences(a, '4'));
156 }
157
158 @Test
159 void testRemoveAllDoubleOccurences() {
160 double[] a = null;
161 assertNull(ArrayUtils.removeAllOccurences(a, 2));
162
163 a = new double[0];
164 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
165
166 a = new double[] { 2 };
167 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
168
169 a = new double[] { 2, 2 };
170 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
171
172 a = new double[] { 1, 2, 2, 3, 2 };
173 assertArrayEquals(new double[]{1, 3}, ArrayUtils.removeAllOccurences(a, 2));
174
175 a = new double[] { 1, 2, 2, 3, 2 };
176 assertArrayEquals(new double[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, 4));
177 }
178
179 @Test
180 void testRemoveAllDoubleOccurrences() {
181 double[] a = null;
182 assertNull(ArrayUtils.removeAllOccurrences(a, 2));
183
184 a = new double[0];
185 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
186
187 a = new double[] { 2 };
188 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
189
190 a = new double[] { 2, 2 };
191 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
192
193 a = new double[] { 1, 2, 2, 3, 2 };
194 assertArrayEquals(new double[]{1, 3}, ArrayUtils.removeAllOccurrences(a, 2));
195
196 a = new double[] { 1, 2, 2, 3, 2 };
197 assertArrayEquals(new double[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, 4));
198 }
199
200 @Test
201 void testRemoveAllFloatOccurences() {
202 float[] a = null;
203 assertNull(ArrayUtils.removeAllOccurences(a, 2));
204
205 a = new float[0];
206 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
207
208 a = new float[] { 2 };
209 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
210
211 a = new float[] { 2, 2 };
212 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
213
214 a = new float[] { 1, 2, 2, 3, 2 };
215 assertArrayEquals(new float[]{1, 3}, ArrayUtils.removeAllOccurences(a, 2));
216
217 a = new float[] { 1, 2, 2, 3, 2 };
218 assertArrayEquals(new float[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, 4));
219 }
220
221 @Test
222 void testRemoveAllFloatOccurrences() {
223 float[] a = null;
224 assertNull(ArrayUtils.removeAllOccurrences(a, 2));
225
226 a = new float[0];
227 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
228
229 a = new float[] { 2 };
230 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
231
232 a = new float[] { 2, 2 };
233 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
234
235 a = new float[] { 1, 2, 2, 3, 2 };
236 assertArrayEquals(new float[]{1, 3}, ArrayUtils.removeAllOccurrences(a, 2));
237
238 a = new float[] { 1, 2, 2, 3, 2 };
239 assertArrayEquals(new float[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, 4));
240 }
241
242 @Test
243 void testRemoveAllIntOccurences() {
244 int[] a = null;
245 assertNull(ArrayUtils.removeAllOccurences(a, 2));
246
247 a = new int[0];
248 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
249
250 a = new int[] { 2 };
251 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
252
253 a = new int[] { 2, 2 };
254 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
255
256 a = new int[] { 1, 2, 2, 3, 2 };
257 assertArrayEquals(new int[]{1, 3}, ArrayUtils.removeAllOccurences(a, 2));
258
259 a = new int[] { 1, 2, 2, 3, 2 };
260 assertArrayEquals(new int[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, 4));
261 }
262
263 @Test
264 void testRemoveAllIntOccurrences() {
265 int[] a = null;
266 assertNull(ArrayUtils.removeAllOccurrences(a, 2));
267
268 a = new int[0];
269 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
270
271 a = new int[] { 2 };
272 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
273
274 a = new int[] { 2, 2 };
275 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
276
277 a = new int[] { 1, 2, 2, 3, 2 };
278 assertArrayEquals(new int[]{1, 3}, ArrayUtils.removeAllOccurrences(a, 2));
279
280 a = new int[] { 1, 2, 2, 3, 2 };
281 assertArrayEquals(new int[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, 4));
282 }
283
284 @Test
285 void testRemoveAllLongOccurences() {
286 long[] a = null;
287 assertNull(ArrayUtils.removeAllOccurences(a, 2));
288
289 a = new long[0];
290 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
291
292 a = new long[] { 2 };
293 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
294
295 a = new long[] { 2, 2 };
296 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurences(a, 2));
297
298 a = new long[] { 1, 2, 2, 3, 2 };
299 assertArrayEquals(new long[]{1, 3}, ArrayUtils.removeAllOccurences(a, 2));
300
301 a = new long[] { 1, 2, 2, 3, 2 };
302 assertArrayEquals(new long[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, 4));
303 }
304
305 @Test
306 void testRemoveAllLongOccurrences() {
307 long[] a = null;
308 assertNull(ArrayUtils.removeAllOccurrences(a, 2));
309
310 a = new long[0];
311 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
312
313 a = new long[] { 2 };
314 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
315
316 a = new long[] { 2, 2 };
317 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.removeAllOccurrences(a, 2));
318
319 a = new long[] { 1, 2, 2, 3, 2 };
320 assertArrayEquals(new long[]{1, 3}, ArrayUtils.removeAllOccurrences(a, 2));
321
322 a = new long[] { 1, 2, 2, 3, 2 };
323 assertArrayEquals(new long[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, 4));
324 }
325
326 @Test
327 void testRemoveAllObjectOccurences() {
328 String[] a = null;
329 assertNull(ArrayUtils.removeAllOccurences(a, "2"));
330
331 a = new String[0];
332 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a, "2"));
333
334 a = new String[] { "2" };
335 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a, "2"));
336
337 a = new String[] { "2", "2" };
338 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurences(a, "2"));
339
340 a = new String[] { "1", "2", "2", "3", "2" };
341 assertArrayEquals(new String[]{"1", "3"}, ArrayUtils.removeAllOccurences(a, "2"));
342
343 a = new String[] { "1", "2", "2", "3", "2" };
344 assertArrayEquals(new String[]{"1", "2", "2", "3", "2"}, ArrayUtils.removeAllOccurences(a, "4"));
345 }
346
347 @Test
348 void testRemoveAllObjectOccurrences() {
349 String[] a = null;
350 assertNull(ArrayUtils.removeAllOccurrences(a, "2"));
351
352 a = new String[0];
353 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurrences(a, "2"));
354
355 a = new String[] { "2" };
356 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurrences(a, "2"));
357
358 a = new String[] { "2", "2" };
359 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.removeAllOccurrences(a, "2"));
360
361 a = new String[] { "1", "2", "2", "3", "2" };
362 assertArrayEquals(new String[]{"1", "3"}, ArrayUtils.removeAllOccurrences(a, "2"));
363
364 a = new String[] { "1", "2", "2", "3", "2" };
365 assertArrayEquals(new String[]{"1", "2", "2", "3", "2"}, ArrayUtils.removeAllOccurrences(a, "4"));
366 }
367
368 @Test
369 void testRemoveAllShortOccurences() {
370 short[] a = null;
371 assertNull(ArrayUtils.removeAllOccurences(a, (short) 2));
372
373 a = new short[0];
374 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a, (short) 2));
375
376 a = new short[] { 2 };
377 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a, (short) 2));
378
379 a = new short[] { 2, 2 };
380 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurences(a, (short) 2));
381
382 a = new short[] { 1, 2, 2, 3, 2 };
383 assertArrayEquals(new short[]{1, 3}, ArrayUtils.removeAllOccurences(a, (short) 2));
384
385 a = new short[] { 1, 2, 2, 3, 2 };
386 assertArrayEquals(new short[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurences(a, (short) 4));
387 }
388
389 @Test
390 void testRemoveAllShortOccurrences() {
391 short[] a = null;
392 assertNull(ArrayUtils.removeAllOccurrences(a, (short) 2));
393
394 a = new short[0];
395 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurrences(a, (short) 2));
396
397 a = new short[] { 2 };
398 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurrences(a, (short) 2));
399
400 a = new short[] { 2, 2 };
401 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.removeAllOccurrences(a, (short) 2));
402
403 a = new short[] { 1, 2, 2, 3, 2 };
404 assertArrayEquals(new short[]{1, 3}, ArrayUtils.removeAllOccurrences(a, (short) 2));
405
406 a = new short[] { 1, 2, 2, 3, 2 };
407 assertArrayEquals(new short[]{1, 2, 2, 3, 2}, ArrayUtils.removeAllOccurrences(a, (short) 4));
408 }
409
410 @Test
411 void testRemoveBooleanArray() {
412 boolean[] array;
413 array = ArrayUtils.remove(new boolean[] {true}, 0);
414 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array);
415 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
416 array = ArrayUtils.remove(new boolean[] {true, false}, 0);
417 assertArrayEquals(new boolean[]{false}, array);
418 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
419 array = ArrayUtils.remove(new boolean[] {true, false}, 1);
420 assertArrayEquals(new boolean[]{true}, array);
421 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
422 array = ArrayUtils.remove(new boolean[] {true, false, true}, 1);
423 assertArrayEquals(new boolean[]{true, true}, array);
424 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
425 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new boolean[] {true, false}, -1));
426 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new boolean[] {true, false}, 2));
427 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((boolean[]) null, 0));
428 }
429
430 @Test
431 void testRemoveByteArray() {
432 byte[] array;
433 array = ArrayUtils.remove(new byte[] {1}, 0);
434 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, array);
435 assertEquals(Byte.TYPE, array.getClass().getComponentType());
436 array = ArrayUtils.remove(new byte[] {1, 2}, 0);
437 assertArrayEquals(new byte[]{2}, array);
438 assertEquals(Byte.TYPE, array.getClass().getComponentType());
439 array = ArrayUtils.remove(new byte[] {1, 2}, 1);
440 assertArrayEquals(new byte[]{1}, array);
441 assertEquals(Byte.TYPE, array.getClass().getComponentType());
442 array = ArrayUtils.remove(new byte[] {1, 2, 1}, 1);
443 assertArrayEquals(new byte[]{1, 1}, array);
444 assertEquals(Byte.TYPE, array.getClass().getComponentType());
445 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new byte[] {1, 2}, -1));
446 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new byte[] {1, 2}, 2));
447 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((byte[]) null, 0));
448 }
449
450 @Test
451 void testRemoveCharArray() {
452 char[] array;
453 array = ArrayUtils.remove(new char[] {'a'}, 0);
454 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, array);
455 assertEquals(Character.TYPE, array.getClass().getComponentType());
456 array = ArrayUtils.remove(new char[] {'a', 'b'}, 0);
457 assertArrayEquals(new char[]{'b'}, array);
458 assertEquals(Character.TYPE, array.getClass().getComponentType());
459 array = ArrayUtils.remove(new char[] {'a', 'b'}, 1);
460 assertArrayEquals(new char[]{'a'}, array);
461 assertEquals(Character.TYPE, array.getClass().getComponentType());
462 array = ArrayUtils.remove(new char[] {'a', 'b', 'c'}, 1);
463 assertArrayEquals(new char[]{'a', 'c'}, array);
464 assertEquals(Character.TYPE, array.getClass().getComponentType());
465 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new char[] {'a', 'b'}, -1));
466 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new char[] {'a', 'b'}, 2));
467 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((char[]) null, 0));
468 }
469
470 @Test
471 void testRemoveDoubleArray() {
472 double[] array;
473 array = ArrayUtils.remove(new double[] {1}, 0);
474 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array);
475 assertEquals(Double.TYPE, array.getClass().getComponentType());
476 array = ArrayUtils.remove(new double[] {1, 2}, 0);
477 assertArrayEquals(new double[]{2}, array);
478 assertEquals(Double.TYPE, array.getClass().getComponentType());
479 array = ArrayUtils.remove(new double[] {1, 2}, 1);
480 assertArrayEquals(new double[]{1}, array);
481 assertEquals(Double.TYPE, array.getClass().getComponentType());
482 array = ArrayUtils.remove(new double[] {1, 2, 1}, 1);
483 assertArrayEquals(new double[]{1, 1}, array);
484 assertEquals(Double.TYPE, array.getClass().getComponentType());
485 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new double[] {1, 2}, -1));
486 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new double[] {1, 2}, 2));
487 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((double[]) null, 0));
488 }
489
490 @Test
491 void testRemoveElementBooleanArray() {
492 boolean[] array;
493 array = ArrayUtils.removeElement(null, true);
494 assertNull(array);
495 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_BOOLEAN_ARRAY, true);
496 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array);
497 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
498 array = ArrayUtils.removeElement(new boolean[] {true}, true);
499 assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, array);
500 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
501 array = ArrayUtils.removeElement(new boolean[] {true, false}, true);
502 assertArrayEquals(new boolean[]{false}, array);
503 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
504 array = ArrayUtils.removeElement(new boolean[] {true, false, true}, true);
505 assertArrayEquals(new boolean[]{false, true}, array);
506 assertEquals(Boolean.TYPE, array.getClass().getComponentType());
507 }
508
509 @Test
510 void testRemoveElementByteArray() {
511 byte[] array;
512 array = ArrayUtils.removeElement((byte[]) null, (byte) 1);
513 assertNull(array);
514 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_BYTE_ARRAY, (byte) 1);
515 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, array);
516 assertEquals(Byte.TYPE, array.getClass().getComponentType());
517 array = ArrayUtils.removeElement(new byte[] {1}, (byte) 1);
518 assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, array);
519 assertEquals(Byte.TYPE, array.getClass().getComponentType());
520 array = ArrayUtils.removeElement(new byte[] {1, 2}, (byte) 1);
521 assertArrayEquals(new byte[]{2}, array);
522 assertEquals(Byte.TYPE, array.getClass().getComponentType());
523 array = ArrayUtils.removeElement(new byte[] {1, 2, 1}, (byte) 1);
524 assertArrayEquals(new byte[]{2, 1}, array);
525 assertEquals(Byte.TYPE, array.getClass().getComponentType());
526 }
527
528 @Test
529 void testRemoveElementCharArray() {
530 char[] array;
531 array = ArrayUtils.removeElement((char[]) null, 'a');
532 assertNull(array);
533 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_CHAR_ARRAY, 'a');
534 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, array);
535 assertEquals(Character.TYPE, array.getClass().getComponentType());
536 array = ArrayUtils.removeElement(new char[] {'a'}, 'a');
537 assertArrayEquals(ArrayUtils.EMPTY_CHAR_ARRAY, array);
538 assertEquals(Character.TYPE, array.getClass().getComponentType());
539 array = ArrayUtils.removeElement(new char[] {'a', 'b'}, 'a');
540 assertArrayEquals(new char[]{'b'}, array);
541 assertEquals(Character.TYPE, array.getClass().getComponentType());
542 array = ArrayUtils.removeElement(new char[] {'a', 'b', 'a'}, 'a');
543 assertArrayEquals(new char[]{'b', 'a'}, array);
544 assertEquals(Character.TYPE, array.getClass().getComponentType());
545 }
546
547 @Test
548 void testRemoveElementDoubleArray() {
549 double[] array;
550 array = ArrayUtils.removeElement(null, (double) 1);
551 assertNull(array);
552 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_DOUBLE_ARRAY, 1);
553 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array);
554 assertEquals(Double.TYPE, array.getClass().getComponentType());
555 array = ArrayUtils.removeElement(new double[] {1}, 1);
556 assertArrayEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, array);
557 assertEquals(Double.TYPE, array.getClass().getComponentType());
558 array = ArrayUtils.removeElement(new double[] {1, 2}, 1);
559 assertArrayEquals(new double[]{2}, array);
560 assertEquals(Double.TYPE, array.getClass().getComponentType());
561 array = ArrayUtils.removeElement(new double[] {1, 2, 1}, 1);
562 assertArrayEquals(new double[]{2, 1}, array);
563 assertEquals(Double.TYPE, array.getClass().getComponentType());
564 }
565
566 @Test
567 void testRemoveElementFloatArray() {
568 float[] array;
569 array = ArrayUtils.removeElement((float[]) null, 1);
570 assertNull(array);
571 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_FLOAT_ARRAY, 1);
572 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, array);
573 assertEquals(Float.TYPE, array.getClass().getComponentType());
574 array = ArrayUtils.removeElement(new float[] {1}, 1);
575 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, array);
576 assertEquals(Float.TYPE, array.getClass().getComponentType());
577 array = ArrayUtils.removeElement(new float[] {1, 2}, 1);
578 assertArrayEquals(new float[]{2}, array);
579 assertEquals(Float.TYPE, array.getClass().getComponentType());
580 array = ArrayUtils.removeElement(new float[] {1, 2, 1}, 1);
581 assertArrayEquals(new float[]{2, 1}, array);
582 assertEquals(Float.TYPE, array.getClass().getComponentType());
583 }
584
585 @Test
586 void testRemoveElementIntArray() {
587 int[] array;
588 array = ArrayUtils.removeElement((int[]) null, 1);
589 assertNull(array);
590 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_INT_ARRAY, 1);
591 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, array);
592 assertEquals(Integer.TYPE, array.getClass().getComponentType());
593 array = ArrayUtils.removeElement(new int[] {1}, 1);
594 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, array);
595 assertEquals(Integer.TYPE, array.getClass().getComponentType());
596 array = ArrayUtils.removeElement(new int[] {1, 2}, 1);
597 assertArrayEquals(new int[]{2}, array);
598 assertEquals(Integer.TYPE, array.getClass().getComponentType());
599 array = ArrayUtils.removeElement(new int[] {1, 2, 1}, 1);
600 assertArrayEquals(new int[]{2, 1}, array);
601 assertEquals(Integer.TYPE, array.getClass().getComponentType());
602 }
603
604 @Test
605 void testRemoveElementLongArray() {
606 long[] array;
607 array = ArrayUtils.removeElement((long[]) null, 1L);
608 assertNull(array);
609 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_LONG_ARRAY, 1L);
610 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, array);
611 assertEquals(Long.TYPE, array.getClass().getComponentType());
612 array = ArrayUtils.removeElement(new long[] {1}, 1L);
613 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, array);
614 assertEquals(Long.TYPE, array.getClass().getComponentType());
615 array = ArrayUtils.removeElement(new long[] {1, 2}, 1L);
616 assertArrayEquals(new long[]{2}, array);
617 assertEquals(Long.TYPE, array.getClass().getComponentType());
618 array = ArrayUtils.removeElement(new long[] {1, 2, 1}, 1L);
619 assertArrayEquals(new long[]{2, 1}, array);
620 assertEquals(Long.TYPE, array.getClass().getComponentType());
621 }
622
623 @Test
624 void testRemoveElementObjectArray() {
625 Object[] array;
626 array = ArrayUtils.removeElement(null, "a");
627 assertNull(array);
628 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_OBJECT_ARRAY, "a");
629 assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, array);
630 assertEquals(Object.class, array.getClass().getComponentType());
631 array = ArrayUtils.removeElement(new Object[] {"a"}, "a");
632 assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, array);
633 assertEquals(Object.class, array.getClass().getComponentType());
634 array = ArrayUtils.removeElement(new Object[] {"a", "b"}, "a");
635 assertArrayEquals(new Object[]{"b"}, array);
636 assertEquals(Object.class, array.getClass().getComponentType());
637 array = ArrayUtils.removeElement(new Object[] {"a", "b", "a"}, "a");
638 assertArrayEquals(new Object[]{"b", "a"}, array);
639 assertEquals(Object.class, array.getClass().getComponentType());
640 }
641
642 @Test
643 void testRemoveElementShortArray() {
644 short[] array;
645 array = ArrayUtils.removeElement((short[]) null, (short) 1);
646 assertNull(array);
647 array = ArrayUtils.removeElement(ArrayUtils.EMPTY_SHORT_ARRAY, (short) 1);
648 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, array);
649 assertEquals(Short.TYPE, array.getClass().getComponentType());
650 array = ArrayUtils.removeElement(new short[] {1}, (short) 1);
651 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, array);
652 assertEquals(Short.TYPE, array.getClass().getComponentType());
653 array = ArrayUtils.removeElement(new short[] {1, 2}, (short) 1);
654 assertArrayEquals(new short[]{2}, array);
655 assertEquals(Short.TYPE, array.getClass().getComponentType());
656 array = ArrayUtils.removeElement(new short[] {1, 2, 1}, (short) 1);
657 assertArrayEquals(new short[]{2, 1}, array);
658 assertEquals(Short.TYPE, array.getClass().getComponentType());
659 }
660
661 @Test
662 void testRemoveFloatArray() {
663 float[] array;
664 array = ArrayUtils.remove(new float[] {1}, 0);
665 assertArrayEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, array);
666 assertEquals(Float.TYPE, array.getClass().getComponentType());
667 array = ArrayUtils.remove(new float[] {1, 2}, 0);
668 assertArrayEquals(new float[]{2}, array);
669 assertEquals(Float.TYPE, array.getClass().getComponentType());
670 array = ArrayUtils.remove(new float[] {1, 2}, 1);
671 assertArrayEquals(new float[]{1}, array);
672 assertEquals(Float.TYPE, array.getClass().getComponentType());
673 array = ArrayUtils.remove(new float[] {1, 2, 1}, 1);
674 assertArrayEquals(new float[]{1, 1}, array);
675 assertEquals(Float.TYPE, array.getClass().getComponentType());
676 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new float[] {1, 2}, -1));
677 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new float[] {1, 2}, 2));
678 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((float[]) null, 0));
679 }
680
681 @Test
682 void testRemoveIntArray() {
683 int[] array;
684 array = ArrayUtils.remove(new int[] {1}, 0);
685 assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, array);
686 assertEquals(Integer.TYPE, array.getClass().getComponentType());
687 array = ArrayUtils.remove(new int[] {1, 2}, 0);
688 assertArrayEquals(new int[]{2}, array);
689 assertEquals(Integer.TYPE, array.getClass().getComponentType());
690 array = ArrayUtils.remove(new int[] {1, 2}, 1);
691 assertArrayEquals(new int[]{1}, array);
692 assertEquals(Integer.TYPE, array.getClass().getComponentType());
693 array = ArrayUtils.remove(new int[] {1, 2, 1}, 1);
694 assertArrayEquals(new int[]{1, 1}, array);
695 assertEquals(Integer.TYPE, array.getClass().getComponentType());
696 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new int[] {1, 2}, -1));
697 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new int[] {1, 2}, 2));
698 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((int[]) null, 0));
699 }
700
701 @Test
702 void testRemoveLongArray() {
703 long[] array;
704 array = ArrayUtils.remove(new long[] {1}, 0);
705 assertArrayEquals(ArrayUtils.EMPTY_LONG_ARRAY, array);
706 assertEquals(Long.TYPE, array.getClass().getComponentType());
707 array = ArrayUtils.remove(new long[] {1, 2}, 0);
708 assertArrayEquals(new long[]{2}, array);
709 assertEquals(Long.TYPE, array.getClass().getComponentType());
710 array = ArrayUtils.remove(new long[] {1, 2}, 1);
711 assertArrayEquals(new long[]{1}, array);
712 assertEquals(Long.TYPE, array.getClass().getComponentType());
713 array = ArrayUtils.remove(new long[] {1, 2, 1}, 1);
714 assertArrayEquals(new long[]{1, 1}, array);
715 assertEquals(Long.TYPE, array.getClass().getComponentType());
716 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new long[] {1, 2}, -1));
717 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new long[] {1, 2}, 2));
718 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((long[]) null, 0));
719 }
720
721 @Test
722 void testRemoveNumberArray() {
723 final Number[] inarray = {Integer.valueOf(1), Long.valueOf(2), Byte.valueOf((byte) 3)};
724 assertEquals(3, inarray.length);
725 Number[] outarray;
726 outarray = ArrayUtils.remove(inarray, 1);
727 assertEquals(2, outarray.length);
728 assertEquals(Number.class, outarray.getClass().getComponentType());
729 outarray = ArrayUtils.remove(outarray, 1);
730 assertEquals(1, outarray.length);
731 assertEquals(Number.class, outarray.getClass().getComponentType());
732 outarray = ArrayUtils.remove(outarray, 0);
733 assertEquals(0, outarray.length);
734 assertEquals(Number.class, outarray.getClass().getComponentType());
735 }
736
737 @Test
738 void testRemoveObjectArray() {
739 Object[] array;
740 array = ArrayUtils.remove(new Object[] {"a"}, 0);
741 assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, array);
742 assertEquals(Object.class, array.getClass().getComponentType());
743 array = ArrayUtils.remove(new Object[] {"a", "b"}, 0);
744 assertArrayEquals(new Object[]{"b"}, array);
745 assertEquals(Object.class, array.getClass().getComponentType());
746 array = ArrayUtils.remove(new Object[] {"a", "b"}, 1);
747 assertArrayEquals(new Object[]{"a"}, array);
748 assertEquals(Object.class, array.getClass().getComponentType());
749 array = ArrayUtils.remove(new Object[] {"a", "b", "c"}, 1);
750 assertArrayEquals(new Object[]{"a", "c"}, array);
751 assertEquals(Object.class, array.getClass().getComponentType());
752 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new Object[] {"a", "b"}, -1));
753 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new Object[] {"a", "b"}, 2));
754 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((Object[]) null, 0));
755 }
756
757 @Test
758 void testRemoveShortArray() {
759 short[] array;
760 array = ArrayUtils.remove(new short[] {1}, 0);
761 assertArrayEquals(ArrayUtils.EMPTY_SHORT_ARRAY, array);
762 assertEquals(Short.TYPE, array.getClass().getComponentType());
763 array = ArrayUtils.remove(new short[] {1, 2}, 0);
764 assertArrayEquals(new short[]{2}, array);
765 assertEquals(Short.TYPE, array.getClass().getComponentType());
766 array = ArrayUtils.remove(new short[] {1, 2}, 1);
767 assertArrayEquals(new short[]{1}, array);
768 assertEquals(Short.TYPE, array.getClass().getComponentType());
769 array = ArrayUtils.remove(new short[] {1, 2, 1}, 1);
770 assertArrayEquals(new short[]{1, 1}, array);
771 assertEquals(Short.TYPE, array.getClass().getComponentType());
772 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new short[] {1, 2}, -1));
773 assertIndexOutOfBoundsException(() -> ArrayUtils.remove(new short[] {1, 2}, 2));
774 assertIndexOutOfBoundsException(() -> ArrayUtils.remove((short[]) null, 0));
775 }
776 }