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.assertIllegalArgumentException;
21 import static org.apache.commons.lang3.LangAssertions.assertIndexOutOfBoundsException;
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertNotSame;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class ArrayUtilsAddTest extends AbstractLangTest {
34
35 @Test
36 void testAddFirstBoolean() {
37 boolean[] newArray;
38 newArray = ArrayUtils.addFirst(null, false);
39 assertArrayEquals(new boolean[] { false }, newArray);
40 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
41 newArray = ArrayUtils.addFirst(null, true);
42 assertArrayEquals(new boolean[] { true }, newArray);
43 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
44 final boolean[] array1 = { true, false, true };
45 newArray = ArrayUtils.addFirst(array1, false);
46 assertArrayEquals(new boolean[] { false, true, false, true }, newArray);
47 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
48 }
49
50 @Test
51 void testAddFirstByte() {
52 byte[] newArray;
53 newArray = ArrayUtils.addFirst((byte[]) null, (byte) 0);
54 assertArrayEquals(new byte[] { 0 }, newArray);
55 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
56 newArray = ArrayUtils.addFirst((byte[]) null, (byte) 1);
57 assertArrayEquals(new byte[] { 1 }, newArray);
58 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
59 final byte[] array1 = { 1, 2, 3 };
60 newArray = ArrayUtils.addFirst(array1, (byte) 0);
61 assertArrayEquals(new byte[] { 0, 1, 2, 3 }, newArray);
62 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
63 newArray = ArrayUtils.addFirst(array1, (byte) 4);
64 assertArrayEquals(new byte[] { 4, 1, 2, 3 }, newArray);
65 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
66 }
67
68 @Test
69 void testAddFirstChar() {
70 char[] newArray;
71 newArray = ArrayUtils.addFirst((char[]) null, (char) 0);
72 assertArrayEquals(new char[] { 0 }, newArray);
73 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
74 newArray = ArrayUtils.addFirst((char[]) null, (char) 1);
75 assertArrayEquals(new char[] { 1 }, newArray);
76 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
77 final char[] array1 = { 1, 2, 3 };
78 newArray = ArrayUtils.addFirst(array1, (char) 0);
79 assertArrayEquals(new char[] { 0, 1, 2, 3 }, newArray);
80 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
81 newArray = ArrayUtils.addFirst(array1, (char) 4);
82 assertArrayEquals(new char[] { 4, 1, 2, 3 }, newArray);
83 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
84 }
85
86 @Test
87 void testAddFirstDouble() {
88 double[] newArray;
89 newArray = ArrayUtils.addFirst((double[]) null, 0);
90 assertArrayEquals(new double[] { 0 }, newArray);
91 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
92 newArray = ArrayUtils.addFirst((double[]) null, 1);
93 assertArrayEquals(new double[] { 1 }, newArray);
94 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
95 final double[] array1 = { 1, 2, 3 };
96 newArray = ArrayUtils.addFirst(array1, 0);
97 assertArrayEquals(new double[] { 0, 1, 2, 3 }, newArray);
98 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
99 newArray = ArrayUtils.addFirst(array1, 4);
100 assertArrayEquals(new double[] { 4, 1, 2, 3 }, newArray);
101 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
102 }
103
104 @Test
105 void testAddFirstFloat() {
106 float[] newArray;
107 newArray = ArrayUtils.addFirst((float[]) null, 0);
108 assertArrayEquals(new float[] { 0 }, newArray);
109 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
110 newArray = ArrayUtils.addFirst((float[]) null, 1);
111 assertArrayEquals(new float[] { 1 }, newArray);
112 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
113 final float[] array1 = { 1, 2, 3 };
114 newArray = ArrayUtils.addFirst(array1, 0);
115 assertArrayEquals(new float[] { 0, 1, 2, 3 }, newArray);
116 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
117 newArray = ArrayUtils.addFirst(array1, 4);
118 assertArrayEquals(new float[] { 4, 1, 2, 3 }, newArray);
119 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
120 }
121
122 @Test
123 void testAddFirstInt() {
124 int[] newArray;
125 newArray = ArrayUtils.addFirst((int[]) null, 0);
126 assertArrayEquals(new int[] { 0 }, newArray);
127 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
128 newArray = ArrayUtils.addFirst((int[]) null, 1);
129 assertArrayEquals(new int[] { 1 }, newArray);
130 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
131 final int[] array1 = { 1, 2, 3 };
132 newArray = ArrayUtils.addFirst(array1, 0);
133 assertArrayEquals(new int[] { 0, 1, 2, 3 }, newArray);
134 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
135 newArray = ArrayUtils.addFirst(array1, 4);
136 assertArrayEquals(new int[] { 4, 1, 2, 3 }, newArray);
137 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
138 }
139
140 @Test
141 void testAddFirstLong() {
142 long[] newArray;
143 newArray = ArrayUtils.addFirst((long[]) null, 0);
144 assertArrayEquals(new long[] { 0 }, newArray);
145 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
146 newArray = ArrayUtils.addFirst((long[]) null, 1);
147 assertArrayEquals(new long[] { 1 }, newArray);
148 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
149 final long[] array1 = { 1, 2, 3 };
150 newArray = ArrayUtils.addFirst(array1, 0);
151 assertArrayEquals(new long[] { 0, 1, 2, 3 }, newArray);
152 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
153 newArray = ArrayUtils.addFirst(array1, 4);
154 assertArrayEquals(new long[] { 4, 1, 2, 3 }, newArray);
155 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
156 }
157
158 @Test
159 void testAddFirstObject() {
160 Object[] newArray;
161
162
163 newArray = ArrayUtils.add((Object[]) null, "a");
164 assertArrayEquals(new String[] { "a" }, newArray);
165 assertArrayEquals(new Object[] { "a" }, newArray);
166 assertEquals(String.class, newArray.getClass().getComponentType());
167
168
169 final String[] newStringArray = ArrayUtils.add(null, "a");
170 assertArrayEquals(new String[] { "a" }, newStringArray);
171 assertArrayEquals(new Object[] { "a" }, newStringArray);
172 assertEquals(String.class, newStringArray.getClass().getComponentType());
173
174 final String[] stringArray1 = { "a", "b", "c" };
175 newArray = ArrayUtils.addFirst(stringArray1, null);
176 assertArrayEquals(new String[] { null, "a", "b", "c" }, newArray);
177 assertEquals(String.class, newArray.getClass().getComponentType());
178
179 newArray = ArrayUtils.addFirst(stringArray1, "d");
180 assertArrayEquals(new String[] { "d", "a", "b", "c" }, newArray);
181 assertEquals(String.class, newArray.getClass().getComponentType());
182
183 Number[] numberArray1 = { Integer.valueOf(1), Double.valueOf(2) };
184 newArray = ArrayUtils.addFirst(numberArray1, Float.valueOf(3));
185 assertArrayEquals(new Number[] { Float.valueOf(3), Integer.valueOf(1), Double.valueOf(2) }, newArray);
186 assertEquals(Number.class, newArray.getClass().getComponentType());
187
188 numberArray1 = null;
189 newArray = ArrayUtils.addFirst(numberArray1, Float.valueOf(3));
190 assertArrayEquals(new Float[] { Float.valueOf(3) }, newArray);
191 assertEquals(Float.class, newArray.getClass().getComponentType());
192 }
193
194 @Test
195 void testAddFirstShort() {
196 short[] newArray;
197 newArray = ArrayUtils.addFirst((short[]) null, (short) 0);
198 assertArrayEquals(new short[] { 0 }, newArray);
199 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
200 newArray = ArrayUtils.addFirst((short[]) null, (short) 1);
201 assertArrayEquals(new short[] { 1 }, newArray);
202 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
203 final short[] array1 = { 1, 2, 3 };
204 newArray = ArrayUtils.addFirst(array1, (short) 0);
205 assertArrayEquals(new short[] { 0, 1, 2, 3 }, newArray);
206 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
207 newArray = ArrayUtils.addFirst(array1, (short) 4);
208 assertArrayEquals(new short[] { 4, 1, 2, 3 }, newArray);
209 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
210 }
211
212 @Test
213 void testAddObjectArrayBoolean() {
214 boolean[] newArray;
215 newArray = ArrayUtils.add(null, false);
216 assertArrayEquals(new boolean[] { false }, newArray);
217 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
218 newArray = ArrayUtils.add(null, true);
219 assertArrayEquals(new boolean[] { true }, newArray);
220 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
221 final boolean[] array1 = { true, false, true };
222 newArray = ArrayUtils.add(array1, false);
223 assertArrayEquals(new boolean[] { true, false, true, false }, newArray);
224 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType());
225 }
226
227 @Test
228 void testAddObjectArrayByte() {
229 byte[] newArray;
230 newArray = ArrayUtils.add((byte[]) null, (byte) 0);
231 assertArrayEquals(new byte[] { 0 }, newArray);
232 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
233 newArray = ArrayUtils.add((byte[]) null, (byte) 1);
234 assertArrayEquals(new byte[] { 1 }, newArray);
235 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
236 final byte[] array1 = { 1, 2, 3 };
237 newArray = ArrayUtils.add(array1, (byte) 0);
238 assertArrayEquals(new byte[] { 1, 2, 3, 0 }, newArray);
239 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
240 newArray = ArrayUtils.add(array1, (byte) 4);
241 assertArrayEquals(new byte[] { 1, 2, 3, 4 }, newArray);
242 assertEquals(Byte.TYPE, newArray.getClass().getComponentType());
243 }
244
245 @Test
246 void testAddObjectArrayChar() {
247 char[] newArray;
248 newArray = ArrayUtils.add((char[]) null, (char) 0);
249 assertArrayEquals(new char[] { 0 }, newArray);
250 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
251 newArray = ArrayUtils.add((char[]) null, (char) 1);
252 assertArrayEquals(new char[] { 1 }, newArray);
253 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
254 final char[] array1 = { 1, 2, 3 };
255 newArray = ArrayUtils.add(array1, (char) 0);
256 assertArrayEquals(new char[] { 1, 2, 3, 0 }, newArray);
257 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
258 newArray = ArrayUtils.add(array1, (char) 4);
259 assertArrayEquals(new char[] { 1, 2, 3, 4 }, newArray);
260 assertEquals(Character.TYPE, newArray.getClass().getComponentType());
261 }
262
263 @Test
264 void testAddObjectArrayDouble() {
265 double[] newArray;
266 newArray = ArrayUtils.add((double[]) null, 0);
267 assertArrayEquals(new double[] { 0 }, newArray);
268 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
269 newArray = ArrayUtils.add((double[]) null, 1);
270 assertArrayEquals(new double[] { 1 }, newArray);
271 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
272 final double[] array1 = { 1, 2, 3 };
273 newArray = ArrayUtils.add(array1, 0);
274 assertArrayEquals(new double[] { 1, 2, 3, 0 }, newArray);
275 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
276 newArray = ArrayUtils.add(array1, 4);
277 assertArrayEquals(new double[] { 1, 2, 3, 4 }, newArray);
278 assertEquals(Double.TYPE, newArray.getClass().getComponentType());
279 }
280
281 @Test
282 void testAddObjectArrayFloat() {
283 float[] newArray;
284 newArray = ArrayUtils.add((float[]) null, 0);
285 assertArrayEquals(new float[] { 0 }, newArray);
286 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
287 newArray = ArrayUtils.add((float[]) null, 1);
288 assertArrayEquals(new float[] { 1 }, newArray);
289 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
290 final float[] array1 = { 1, 2, 3 };
291 newArray = ArrayUtils.add(array1, 0);
292 assertArrayEquals(new float[] { 1, 2, 3, 0 }, newArray);
293 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
294 newArray = ArrayUtils.add(array1, 4);
295 assertArrayEquals(new float[] { 1, 2, 3, 4 }, newArray);
296 assertEquals(Float.TYPE, newArray.getClass().getComponentType());
297 }
298
299 @Test
300 void testAddObjectArrayInt() {
301 int[] newArray;
302 newArray = ArrayUtils.add((int[]) null, 0);
303 assertArrayEquals(new int[] { 0 }, newArray);
304 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
305 newArray = ArrayUtils.add((int[]) null, 1);
306 assertArrayEquals(new int[] { 1 }, newArray);
307 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
308 final int[] array1 = { 1, 2, 3 };
309 newArray = ArrayUtils.add(array1, 0);
310 assertArrayEquals(new int[] { 1, 2, 3, 0 }, newArray);
311 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
312 newArray = ArrayUtils.add(array1, 4);
313 assertArrayEquals(new int[] { 1, 2, 3, 4 }, newArray);
314 assertEquals(Integer.TYPE, newArray.getClass().getComponentType());
315 }
316
317 @Test
318 void testAddObjectArrayLong() {
319 long[] newArray;
320 newArray = ArrayUtils.add((long[]) null, 0);
321 assertArrayEquals(new long[] { 0 }, newArray);
322 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
323 newArray = ArrayUtils.add((long[]) null, 1);
324 assertArrayEquals(new long[] { 1 }, newArray);
325 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
326 final long[] array1 = { 1, 2, 3 };
327 newArray = ArrayUtils.add(array1, 0);
328 assertArrayEquals(new long[] { 1, 2, 3, 0 }, newArray);
329 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
330 newArray = ArrayUtils.add(array1, 4);
331 assertArrayEquals(new long[] { 1, 2, 3, 4 }, newArray);
332 assertEquals(Long.TYPE, newArray.getClass().getComponentType());
333 }
334
335 @Test
336 void testAddObjectArrayObject() {
337 Object[] newArray;
338
339
340 newArray = ArrayUtils.add((Object[]) null, "a");
341 assertArrayEquals(new String[] { "a" }, newArray);
342 assertArrayEquals(new Object[] { "a" }, newArray);
343 assertEquals(String.class, newArray.getClass().getComponentType());
344
345
346 final String[] newStringArray = ArrayUtils.add(null, "a");
347 assertArrayEquals(new String[] { "a" }, newStringArray);
348 assertArrayEquals(new Object[] { "a" }, newStringArray);
349 assertEquals(String.class, newStringArray.getClass().getComponentType());
350
351 final String[] stringArray1 = { "a", "b", "c" };
352 newArray = ArrayUtils.add(stringArray1, null);
353 assertArrayEquals(new String[] { "a", "b", "c", null }, newArray);
354 assertEquals(String.class, newArray.getClass().getComponentType());
355
356 newArray = ArrayUtils.add(stringArray1, "d");
357 assertArrayEquals(new String[] { "a", "b", "c", "d" }, newArray);
358 assertEquals(String.class, newArray.getClass().getComponentType());
359
360 Number[] numberArray1 = { Integer.valueOf(1), Double.valueOf(2) };
361 newArray = ArrayUtils.add(numberArray1, Float.valueOf(3));
362 assertArrayEquals(new Number[] { Integer.valueOf(1), Double.valueOf(2), Float.valueOf(3) }, newArray);
363 assertEquals(Number.class, newArray.getClass().getComponentType());
364
365 numberArray1 = null;
366 newArray = ArrayUtils.add(numberArray1, Float.valueOf(3));
367 assertArrayEquals(new Float[] { Float.valueOf(3) }, newArray);
368 assertEquals(Float.class, newArray.getClass().getComponentType());
369 }
370
371 @Test
372 void testAddObjectArrayShort() {
373 short[] newArray;
374 newArray = ArrayUtils.add((short[]) null, (short) 0);
375 assertArrayEquals(new short[] { 0 }, newArray);
376 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
377 newArray = ArrayUtils.add((short[]) null, (short) 1);
378 assertArrayEquals(new short[] { 1 }, newArray);
379 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
380 final short[] array1 = { 1, 2, 3 };
381 newArray = ArrayUtils.add(array1, (short) 0);
382 assertArrayEquals(new short[] { 1, 2, 3, 0 }, newArray);
383 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
384 newArray = ArrayUtils.add(array1, (short) 4);
385 assertArrayEquals(new short[] { 1, 2, 3, 4 }, newArray);
386 assertEquals(Short.TYPE, newArray.getClass().getComponentType());
387 }
388
389 @Test
390 void testAddObjectArrayToObjectArray() {
391 assertNull(ArrayUtils.addAll(null, (Object[]) null));
392 Object[] newArray;
393 final String[] stringArray1 = { "a", "b", "c" };
394 final String[] stringArray2 = { "1", "2", "3" };
395 newArray = ArrayUtils.addAll(stringArray1, (String[]) null);
396 assertNotSame(stringArray1, newArray);
397 assertArrayEquals(stringArray1, newArray);
398 assertArrayEquals(new String[] { "a", "b", "c" }, newArray);
399 assertEquals(String.class, newArray.getClass().getComponentType());
400 newArray = ArrayUtils.addAll(null, stringArray2);
401 assertNotSame(stringArray2, newArray);
402 assertArrayEquals(stringArray2, newArray);
403 assertArrayEquals(new String[] { "1", "2", "3" }, newArray);
404 assertEquals(String.class, newArray.getClass().getComponentType());
405 newArray = ArrayUtils.addAll(stringArray1, stringArray2);
406 assertArrayEquals(new String[] { "a", "b", "c", "1", "2", "3" }, newArray);
407 assertEquals(String.class, newArray.getClass().getComponentType());
408 newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, (String[]) null);
409 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, newArray);
410 assertArrayEquals(new String[] {}, newArray);
411 assertEquals(String.class, newArray.getClass().getComponentType());
412 newArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY);
413 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, newArray);
414 assertArrayEquals(new String[] {}, newArray);
415 assertEquals(String.class, newArray.getClass().getComponentType());
416 newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY);
417 assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, newArray);
418 assertArrayEquals(new String[] {}, newArray);
419 assertEquals(String.class, newArray.getClass().getComponentType());
420 final String[] stringArrayNull = { null };
421 newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull);
422 assertArrayEquals(new String[] { null, null }, newArray);
423 assertEquals(String.class, newArray.getClass().getComponentType());
424
425
426 assertArrayEquals(new boolean[] { true, false, false, true }, ArrayUtils.addAll(new boolean[] { true, false }, false, true));
427
428 assertArrayEquals(new boolean[] { false, true }, ArrayUtils.addAll(null, new boolean[] { false, true }));
429
430 assertArrayEquals(new boolean[] { true, false }, ArrayUtils.addAll(new boolean[] { true, false }, null));
431
432
433 assertArrayEquals(new char[] { 'a', 'b', 'c', 'd' }, ArrayUtils.addAll(new char[] { 'a', 'b' }, 'c', 'd'));
434
435 assertArrayEquals(new char[] { 'c', 'd' }, ArrayUtils.addAll(null, new char[] { 'c', 'd' }));
436
437 assertArrayEquals(new char[] { 'a', 'b' }, ArrayUtils.addAll(new char[] { 'a', 'b' }, null));
438
439
440 assertArrayEquals(new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3 }, ArrayUtils.addAll(new byte[] { (byte) 0, (byte) 1 }, (byte) 2, (byte) 3));
441
442 assertArrayEquals(new byte[] { (byte) 2, (byte) 3 }, ArrayUtils.addAll(null, new byte[] { (byte) 2, (byte) 3 }));
443
444 assertArrayEquals(new byte[] { (byte) 0, (byte) 1 }, ArrayUtils.addAll(new byte[] { (byte) 0, (byte) 1 }, null));
445
446
447 assertArrayEquals(new short[] { (short) 10, (short) 20, (short) 30, (short) 40 },
448 ArrayUtils.addAll(new short[] { (short) 10, (short) 20 }, (short) 30, (short) 40));
449
450 assertArrayEquals(new short[] { (short) 30, (short) 40 }, ArrayUtils.addAll(null, new short[] { (short) 30, (short) 40 }));
451
452 assertArrayEquals(new short[] { (short) 10, (short) 20 }, ArrayUtils.addAll(new short[] { (short) 10, (short) 20 }, null));
453
454
455 assertArrayEquals(new int[] { 1, 1000, -1000, -1 }, ArrayUtils.addAll(new int[] { 1, 1000 }, -1000, -1));
456
457 assertArrayEquals(new int[] { -1000, -1 }, ArrayUtils.addAll(null, new int[] { -1000, -1 }));
458
459 assertArrayEquals(new int[] { 1, 1000 }, ArrayUtils.addAll(new int[] { 1, 1000 }, null));
460
461
462 assertArrayEquals(new long[] { 1L, -1L, 1000L, -1000L }, ArrayUtils.addAll(new long[] { 1L, -1L }, 1000L, -1000L));
463
464 assertArrayEquals(new long[] { 1000L, -1000L }, ArrayUtils.addAll(null, new long[] { 1000L, -1000L }));
465
466 assertArrayEquals(new long[] { 1L, -1L }, ArrayUtils.addAll(new long[] { 1L, -1L }, null));
467
468
469 assertArrayEquals(new float[] { 10.5f, 10.1f, 1.6f, 0.01f }, ArrayUtils.addAll(new float[] { 10.5f, 10.1f }, 1.6f, 0.01f));
470
471 assertArrayEquals(new float[] { 1.6f, 0.01f }, ArrayUtils.addAll(null, new float[] { 1.6f, 0.01f }));
472
473 assertArrayEquals(new float[] { 10.5f, 10.1f }, ArrayUtils.addAll(new float[] { 10.5f, 10.1f }, null));
474
475
476 assertArrayEquals(new double[] { Math.PI, -Math.PI, 0, 9.99 }, ArrayUtils.addAll(new double[] { Math.PI, -Math.PI }, 0, 9.99));
477
478 assertArrayEquals(new double[] { 0, 9.99 }, ArrayUtils.addAll(null, new double[] { 0, 9.99 }));
479
480 assertArrayEquals(new double[] { Math.PI, -Math.PI }, ArrayUtils.addAll(new double[] { Math.PI, -Math.PI }, null));
481
482 }
483
484 @SuppressWarnings("deprecation")
485 @Test
486 void testAddObjectAtIndex() {
487 Object[] newArray;
488 newArray = ArrayUtils.add((Object[]) null, 0, "a");
489 assertArrayEquals(new String[] { "a" }, newArray);
490 assertArrayEquals(new Object[] { "a" }, newArray);
491 assertEquals(String.class, newArray.getClass().getComponentType());
492 final String[] stringArray1 = { "a", "b", "c" };
493 newArray = ArrayUtils.add(stringArray1, 0, null);
494 assertArrayEquals(new String[] { null, "a", "b", "c" }, newArray);
495 assertEquals(String.class, newArray.getClass().getComponentType());
496 newArray = ArrayUtils.add(stringArray1, 1, null);
497 assertArrayEquals(new String[] { "a", null, "b", "c" }, newArray);
498 assertEquals(String.class, newArray.getClass().getComponentType());
499 newArray = ArrayUtils.add(stringArray1, 3, null);
500 assertArrayEquals(new String[] { "a", "b", "c", null }, newArray);
501 assertEquals(String.class, newArray.getClass().getComponentType());
502 newArray = ArrayUtils.add(stringArray1, 3, "d");
503 assertArrayEquals(new String[] { "a", "b", "c", "d" }, newArray);
504 assertEquals(String.class, newArray.getClass().getComponentType());
505 assertEquals(String.class, newArray.getClass().getComponentType());
506
507 final Object[] o = { "1", "2", "4" };
508 final Object[] result = ArrayUtils.add(o, 2, "3");
509 final Object[] result2 = ArrayUtils.add(o, 3, "5");
510
511 assertNotNull(result);
512 assertEquals(4, result.length);
513 assertEquals("1", result[0]);
514 assertEquals("2", result[1]);
515 assertEquals("3", result[2]);
516 assertEquals("4", result[3]);
517 assertNotNull(result2);
518 assertEquals(4, result2.length);
519 assertEquals("1", result2[0]);
520 assertEquals("2", result2[1]);
521 assertEquals("4", result2[2]);
522 assertEquals("5", result2[3]);
523
524
525 boolean[] booleanArray = ArrayUtils.add(null, 0, true);
526 assertArrayEquals(new boolean[] { true }, booleanArray);
527 IndexOutOfBoundsException e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, true));
528 assertEquals("Index: -1, Length: 0", e.getMessage());
529 booleanArray = ArrayUtils.add(new boolean[] { true }, 0, false);
530 assertArrayEquals(new boolean[] { false, true }, booleanArray);
531 booleanArray = ArrayUtils.add(new boolean[] { false }, 1, true);
532 assertArrayEquals(new boolean[] { false, true }, booleanArray);
533 booleanArray = ArrayUtils.add(new boolean[] { true, false }, 1, true);
534 assertArrayEquals(new boolean[] { true, true, false }, booleanArray);
535 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, 4, true));
536 assertEquals("Index: 4, Length: 2", e.getMessage());
537 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new boolean[] { true, false }, -1, true));
538 assertEquals("Index: -1, Length: 2", e.getMessage());
539
540
541 char[] charArray = ArrayUtils.add((char[]) null, 0, 'a');
542 assertArrayEquals(new char[] { 'a' }, charArray);
543 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((char[]) null, -1, 'a'));
544 assertEquals("Index: -1, Length: 0", e.getMessage());
545 charArray = ArrayUtils.add(new char[] { 'a' }, 0, 'b');
546 assertArrayEquals(new char[] { 'b', 'a' }, charArray);
547 charArray = ArrayUtils.add(new char[] { 'a', 'b' }, 0, 'c');
548 assertArrayEquals(new char[] { 'c', 'a', 'b' }, charArray);
549 charArray = ArrayUtils.add(new char[] { 'a', 'b' }, 1, 'k');
550 assertArrayEquals(new char[] { 'a', 'k', 'b' }, charArray);
551 charArray = ArrayUtils.add(new char[] { 'a', 'b', 'c' }, 1, 't');
552 assertArrayEquals(new char[] { 'a', 't', 'b', 'c' }, charArray);
553 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, 4, 'c'));
554 assertEquals("Index: 4, Length: 2", e.getMessage());
555 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new char[] { 'a', 'b' }, -1, 'c'));
556 assertEquals("Index: -1, Length: 2", e.getMessage());
557
558
559 short[] shortArray = ArrayUtils.add(new short[] { 1 }, 0, (short) 2);
560 assertArrayEquals(new short[] { 2, 1 }, shortArray);
561 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((short[]) null, -1, (short) 2));
562 assertEquals("Index: -1, Length: 0", e.getMessage());
563 shortArray = ArrayUtils.add(new short[] { 2, 6 }, 2, (short) 10);
564 assertArrayEquals(new short[] { 2, 6, 10 }, shortArray);
565 shortArray = ArrayUtils.add(new short[] { 2, 6 }, 0, (short) -4);
566 assertArrayEquals(new short[] { -4, 2, 6 }, shortArray);
567 shortArray = ArrayUtils.add(new short[] { 2, 6, 3 }, 2, (short) 1);
568 assertArrayEquals(new short[] { 2, 6, 1, 3 }, shortArray);
569 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, 4, (short) 10));
570 assertEquals("Index: 4, Length: 2", e.getMessage());
571 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new short[] { 2, 6 }, -1, (short) 10));
572 assertEquals("Index: -1, Length: 2", e.getMessage());
573
574
575 byte[] byteArray = ArrayUtils.add(new byte[] { 1 }, 0, (byte) 2);
576 assertArrayEquals(new byte[] { 2, 1 }, byteArray);
577 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((byte[]) null, -1, (byte) 2));
578 assertEquals("Index: -1, Length: 0", e.getMessage());
579 byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 2, (byte) 3);
580 assertArrayEquals(new byte[] { 2, 6, 3 }, byteArray);
581 byteArray = ArrayUtils.add(new byte[] { 2, 6 }, 0, (byte) 1);
582 assertArrayEquals(new byte[] { 1, 2, 6 }, byteArray);
583 byteArray = ArrayUtils.add(new byte[] { 2, 6, 3 }, 2, (byte) 1);
584 assertArrayEquals(new byte[] { 2, 6, 1, 3 }, byteArray);
585 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, 4, (byte) 3));
586 assertEquals("Index: 4, Length: 2", e.getMessage());
587 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new byte[] { 2, 6 }, -1, (byte) 3));
588 assertEquals("Index: -1, Length: 2", e.getMessage());
589
590
591 int[] intArray = ArrayUtils.add(new int[] { 1 }, 0, 2);
592 assertArrayEquals(new int[] { 2, 1 }, intArray);
593 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((int[]) null, -1, 2));
594 assertEquals("Index: -1, Length: 0", e.getMessage());
595 intArray = ArrayUtils.add(new int[] { 2, 6 }, 2, 10);
596 assertArrayEquals(new int[] { 2, 6, 10 }, intArray);
597 intArray = ArrayUtils.add(new int[] { 2, 6 }, 0, -4);
598 assertArrayEquals(new int[] { -4, 2, 6 }, intArray);
599 intArray = ArrayUtils.add(new int[] { 2, 6, 3 }, 2, 1);
600 assertArrayEquals(new int[] { 2, 6, 1, 3 }, intArray);
601 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, 4, 10));
602 assertEquals("Index: 4, Length: 2", e.getMessage());
603 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new int[] { 2, 6 }, -1, 10));
604 assertEquals("Index: -1, Length: 2", e.getMessage());
605
606
607 long[] longArray = ArrayUtils.add(new long[] { 1L }, 0, 2L);
608 assertArrayEquals(new long[] { 2L, 1L }, longArray);
609 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((long[]) null, -1, 2L));
610 assertEquals("Index: -1, Length: 0", e.getMessage());
611 longArray = ArrayUtils.add(new long[] { 2L, 6L }, 2, 10L);
612 assertArrayEquals(new long[] { 2L, 6L, 10L }, longArray);
613 longArray = ArrayUtils.add(new long[] { 2L, 6L }, 0, -4L);
614 assertArrayEquals(new long[] { -4L, 2L, 6L }, longArray);
615 longArray = ArrayUtils.add(new long[] { 2L, 6L, 3L }, 2, 1L);
616 assertArrayEquals(new long[] { 2L, 6L, 1L, 3L }, longArray);
617 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, 4, 10L));
618 assertEquals("Index: 4, Length: 2", e.getMessage());
619 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new long[] { 2L, 6L }, -1, 10L));
620 assertEquals("Index: -1, Length: 2", e.getMessage());
621
622
623 float[] floatArray = ArrayUtils.add(new float[] { 1.1f }, 0, 2.2f);
624 assertArrayEquals(new float[] { 2.2f, 1.1f }, floatArray);
625 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add((float[]) null, -1, 2.2f));
626 assertEquals("Index: -1, Length: 0", e.getMessage());
627 floatArray = ArrayUtils.add(new float[] { 2.3f, 6.4f }, 2, 10.5f);
628 assertArrayEquals(new float[] { 2.3f, 6.4f, 10.5f }, floatArray);
629 floatArray = ArrayUtils.add(new float[] { 2.6f, 6.7f }, 0, -4.8f);
630 assertArrayEquals(new float[] { -4.8f, 2.6f, 6.7f }, floatArray);
631 floatArray = ArrayUtils.add(new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f);
632 assertArrayEquals(new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray);
633 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, 4, 10.5f));
634 assertEquals("Index: 4, Length: 2", e.getMessage());
635 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new float[] { 2.3f, 6.4f }, -1, 10.5f));
636 assertEquals("Index: -1, Length: 2", e.getMessage());
637
638
639 double[] doubleArray = ArrayUtils.add(new double[] { 1.1 }, 0, 2.2);
640 assertArrayEquals(new double[] { 2.2, 1.1 }, doubleArray);
641 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(null, -1, 2.2));
642 assertEquals("Index: -1, Length: 0", e.getMessage());
643 doubleArray = ArrayUtils.add(new double[] { 2.3, 6.4 }, 2, 10.5);
644 assertArrayEquals(new double[] { 2.3, 6.4, 10.5 }, doubleArray);
645 doubleArray = ArrayUtils.add(new double[] { 2.6, 6.7 }, 0, -4.8);
646 assertArrayEquals(new double[] { -4.8, 2.6, 6.7 }, doubleArray);
647 doubleArray = ArrayUtils.add(new double[] { 2.9, 6.0, 0.3 }, 2, 1.0);
648 assertArrayEquals(new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray);
649 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, 4, 10.5));
650 assertEquals("Index: 4, Length: 2", e.getMessage());
651 e = assertIndexOutOfBoundsException(() -> ArrayUtils.add(new double[] { 2.3, 6.4 }, -1, 10.5));
652 assertEquals("Index: -1, Length: 2", e.getMessage());
653 }
654
655 @Test
656 void testJira567() {
657 final Number[] n;
658
659 n = ArrayUtils.addAll(new Number[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) });
660 assertEquals(2, n.length);
661 assertEquals(Number.class, n.getClass().getComponentType());
662
663 assertIllegalArgumentException(() -> ArrayUtils.addAll(new Integer[] { Integer.valueOf(1) }, new Long[] { Long.valueOf(2) }));
664 }
665
666 @Test
667 @SuppressWarnings("deprecation")
668 void testLANG571() {
669 final String[] stringArray = null;
670 final String aString = null;
671 assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, aString));
672 assertIllegalArgumentException(() -> ArrayUtils.add(stringArray, 0, aString));
673 }
674
675 }