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