View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.assertThrows;
22  import static org.mockito.ArgumentMatchers.anyInt;
23  import static org.mockito.Mockito.mockStatic;
24  
25  import org.junit.jupiter.api.Test;
26  import org.mockito.MockedStatic;
27  
28  /**
29   * Tests {@link ArrayUtils} concat methods.
30   */
31  class ArrayUtilsConcatTest extends AbstractLangTest {
32  
33      @Test
34      void testBooleanArraysConcat() {
35          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((boolean[][]) null));
36          assertArrayEquals(new boolean[] {}, ArrayUtils.concat((boolean[]) null, null));
37          assertArrayEquals(new boolean[] { true, false, true }, ArrayUtils.concat(new boolean[] { true, false, true }, null));
38          assertArrayEquals(new boolean[] { false, true, false }, ArrayUtils.concat(null, new boolean[] { false, true, false }));
39          assertArrayEquals(new boolean[] { false, true, false, false, true },
40                  ArrayUtils.concat(new boolean[] { false, true, false }, new boolean[] { false, true }));
41      }
42  
43      @Test
44      void testByteArraysConcat() {
45          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((byte[][]) null));
46          assertArrayEquals(new byte[] {}, ArrayUtils.concat((byte[]) null, null));
47          assertArrayEquals(new byte[] { 1, 2, 3 }, ArrayUtils.concat(new byte[] { 1, 2, 3 }, null));
48          assertArrayEquals(new byte[] { -3, 2, 1 }, ArrayUtils.concat(null, new byte[] { -3, 2, 1 }));
49          assertArrayEquals(new byte[] { 1, 3, 2, 100, 7 }, ArrayUtils.concat(new byte[] { 1, 3, 2 }, new byte[] { 100, 7 }));
50      }
51  
52      @Test
53      void testCharArraysConcat() {
54          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((char[][]) null));
55          assertArrayEquals(new char[] {}, ArrayUtils.concat((char[]) null, null));
56          assertArrayEquals(new char[] { 'a', 'b', 'c' }, ArrayUtils.concat(new char[] { 'a', 'b', 'c' }, null));
57          assertArrayEquals(new char[] { 'b', 'a', 'c' }, ArrayUtils.concat(null, new char[] { 'b', 'a', 'c' }));
58          assertArrayEquals(new char[] { 'a', 'b', 'c', 'q', 'w' }, ArrayUtils.concat(new char[] { 'a', 'b', 'c' }, new char[] { 'q', 'w' }));
59      }
60  
61      @Test
62      void testDoubleArraysConcat() {
63          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((double[][]) null));
64          assertArrayEquals(new double[] {}, ArrayUtils.concat((double[]) null, null));
65          assertArrayEquals(new double[] { 1e-300, .2e-300, 3.0e-300 }, ArrayUtils.concat(new double[] { 1e-300, .2e-300, 3.0e-300 }, null));
66          assertArrayEquals(new double[] { 3.0e-300, 1e-300, .2e-300 }, ArrayUtils.concat(null, new double[] { 3.0e-300, 1e-300, .2e-300 }));
67          assertArrayEquals(new double[] { 1e-300, .2e-300, 3.0e-300, 10.01e-300, 0.001e-300 },
68                  ArrayUtils.concat(new double[] { 1e-300, .2e-300, 3.0e-300 }, new double[] { 10.01e-300, 0.001e-300 }));
69      }
70  
71      @Test
72      void testExceedSafeMaxArraySize() {
73          try (MockedStatic<ArrayUtils.MathBridge> mockedStatic = mockStatic(ArrayUtils.MathBridge.class)) {
74              mockedStatic.when(() -> ArrayUtils.MathBridge.addExact(anyInt(), anyInt())).thenThrow(ArithmeticException.class);
75              assertThrows(IllegalArgumentException.class, () -> ArrayUtils.concat(new int[] { 0 }, new int[] { 1 }));
76          }
77      }
78  
79      @Test
80      void testFloatArraysConcat() {
81          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((float[][]) null));
82          assertArrayEquals(new float[] {}, ArrayUtils.concat((float[]) null, null));
83          assertArrayEquals(new float[] { 1f, .2f, 3.0f }, ArrayUtils.concat(new float[] { 1f, .2f, 3.0f }, null));
84          assertArrayEquals(new float[] { 3.0f, 1f, .2f }, ArrayUtils.concat(null, new float[] { 3.0f, 1f, .2f }));
85          assertArrayEquals(new float[] { 1f, .2f, 3.0f, 10.01f, 0.001f }, ArrayUtils.concat(new float[] { 1f, .2f, 3.0f }, new float[] { 10.01f, 0.001f }));
86      }
87  
88      @Test
89      void testIntArraysConcat() {
90          assertThrows(NullPointerException.class, () -> ArrayUtils.concat((int[][]) null));
91          assertArrayEquals(new int[] {}, ArrayUtils.concat((int[]) null, null));
92          assertArrayEquals(new int[] { 10000000, 20000000, 30000000 }, ArrayUtils.concat(new int[] { 10000000, 20000000, 30000000 }, null));
93          assertArrayEquals(new int[] { -30000000, 20000000, 10000000 }, ArrayUtils.concat(null, new int[] { -30000000, 20000000, 10000000 }));
94          assertArrayEquals(new int[] { 10000000, 30000000, 20000000, 100000000, 70000000 },
95                  ArrayUtils.concat(new int[] { 10000000, 30000000, 20000000 }, new int[] { 100000000, 70000000 }));
96      }
97  
98      @Test
99      void testLongArraysConcat() {
100         assertThrows(NullPointerException.class, () -> ArrayUtils.concat((long[][]) null));
101         assertArrayEquals(new long[] {}, ArrayUtils.concat((long[]) null, null));
102         assertArrayEquals(new long[] { 10000000000L, 20000000000L, 30000000000L },
103                 ArrayUtils.concat(new long[] { 10000000000L, 20000000000L, 30000000000L }, null));
104         assertArrayEquals(new long[] { -30000000000L, 20000000000L, 10000000000L },
105                 ArrayUtils.concat(null, new long[] { -30000000000L, 20000000000L, 10000000000L }));
106         assertArrayEquals(new long[] { 10000000000L, 30000000000L, 20000000000L, 100000000000L, 70000000000L },
107                 ArrayUtils.concat(new long[] { 10000000000L, 30000000000L, 20000000000L }, new long[] { 100000000000L, 70000000000L }));
108     }
109 
110     @Test
111     void testShortArraysConcat() {
112         assertThrows(NullPointerException.class, () -> ArrayUtils.concat((short[][]) null));
113         assertArrayEquals(new short[] {}, ArrayUtils.concat((short[]) null, null));
114         assertArrayEquals(new short[] { 1000, 2000, 3000 }, ArrayUtils.concat(new short[] { 1000, 2000, 3000 }, null));
115         assertArrayEquals(new short[] { -3000, 2000, 1000 }, ArrayUtils.concat(null, new short[] { -3000, 2000, 1000 }));
116         assertArrayEquals(new short[] { 1000, 3000, 2000, 10000, 7000 }, ArrayUtils.concat(new short[] { 1000, 3000, 2000 }, new short[] { 10000, 7000 }));
117     }
118 }