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.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  
25  import org.apache.commons.lang3.function.FailableIntFunction;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link ArrayFill}.
30   */
31  class ArrayFillTest extends AbstractLangTest {
32  
33      @Test
34      void testFillBooleanArray() {
35          final boolean[] array = new boolean[3];
36          final boolean val = true;
37          final boolean[] actual = ArrayFill.fill(array, val);
38          assertSame(array, actual);
39          for (final boolean v : actual) {
40              assertEquals(val, v);
41          }
42      }
43  
44      @Test
45      void testFillBooleanArrayNull() {
46          final boolean[] array = null;
47          final boolean val = true;
48          final boolean[] actual = ArrayFill.fill(array, val);
49          assertSame(array, actual);
50      }
51  
52      @Test
53      void testFillByteArray() {
54          final byte[] array = new byte[3];
55          final byte val = (byte) 1;
56          final byte[] actual = ArrayFill.fill(array, val);
57          assertSame(array, actual);
58          for (final byte v : actual) {
59              assertEquals(val, v);
60          }
61      }
62  
63      @Test
64      void testFillByteArrayNull() {
65          final byte[] array = null;
66          final byte val = (byte) 1;
67          final byte[] actual = ArrayFill.fill(array, val);
68          assertSame(array, actual);
69      }
70  
71      @Test
72      void testFillCharArray() {
73          final char[] array = new char[3];
74          final char val = 1;
75          final char[] actual = ArrayFill.fill(array, val);
76          assertSame(array, actual);
77          for (final char v : actual) {
78              assertEquals(val, v);
79          }
80      }
81  
82      @Test
83      void testFillCharArrayNull() {
84          final char[] array = null;
85          final char val = 1;
86          final char[] actual = ArrayFill.fill(array, val);
87          assertSame(array, actual);
88      }
89  
90      @Test
91      void testFillDoubleArray() {
92          final double[] array = new double[3];
93          final double val = 1;
94          final double[] actual = ArrayFill.fill(array, val);
95          assertSame(array, actual);
96          for (final double v : actual) {
97              assertEquals(val, v);
98          }
99      }
100 
101     @Test
102     void testFillDoubleArrayNull() {
103         final double[] array = null;
104         final double val = 1;
105         final double[] actual = ArrayFill.fill(array, val);
106         assertSame(array, actual);
107     }
108 
109     @Test
110     void testFillFloatArray() {
111         final float[] array = new float[3];
112         final float val = 1;
113         final float[] actual = ArrayFill.fill(array, val);
114         assertSame(array, actual);
115         for (final float v : actual) {
116             assertEquals(val, v);
117         }
118     }
119 
120     @Test
121     void testFillFloatArrayNull() {
122         final float[] array = null;
123         final float val = 1;
124         final float[] actual = ArrayFill.fill(array, val);
125         assertSame(array, actual);
126     }
127 
128     @Test
129     void testFillFunction() throws Exception {
130         final FailableIntFunction<?, Exception> nullIntFunction = null;
131         assertNull(ArrayFill.fill(null, nullIntFunction));
132         assertArrayEquals(null, ArrayFill.fill(null, nullIntFunction));
133         assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayFill.fill(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, nullIntFunction));
134         assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, ArrayFill.fill(ArrayUtils.EMPTY_OBJECT_ARRAY, nullIntFunction));
135         final Integer[] array = new Integer[10];
136         final Integer[] array2 = ArrayFill.fill(array, Integer::valueOf);
137         assertSame(array, array2);
138         for (int i = 0; i < array.length; i++) {
139             assertEquals(i, array[i].intValue());
140         }
141     }
142 
143     @Test
144     void testFillIntArray() {
145         final int[] array = new int[3];
146         final int val = 1;
147         final int[] actual = ArrayFill.fill(array, val);
148         assertSame(array, actual);
149         for (final int v : actual) {
150             assertEquals(val, v);
151         }
152     }
153 
154     @Test
155     void testFillIntArrayNull() {
156         final int[] array = null;
157         final int val = 1;
158         final int[] actual = ArrayFill.fill(array, val);
159         assertSame(array, actual);
160     }
161 
162     @Test
163     void testFillLongArray() {
164         final long[] array = new long[3];
165         final long val = 1;
166         final long[] actual = ArrayFill.fill(array, val);
167         assertSame(array, actual);
168         for (final long v : actual) {
169             assertEquals(val, v);
170         }
171     }
172 
173     @Test
174     void testFillLongArrayNull() {
175         final long[] array = null;
176         final long val = 1;
177         final long[] actual = ArrayFill.fill(array, val);
178         assertSame(array, actual);
179     }
180 
181     @Test
182     void testFillObjectArray() {
183         final String[] array = new String[3];
184         final String val = "A";
185         final String[] actual = ArrayFill.fill(array, val);
186         assertSame(array, actual);
187         for (final String v : actual) {
188             assertEquals(val, v);
189         }
190     }
191 
192     @Test
193     void testFillObjectArrayNull() {
194         final Object[] array = null;
195         final Object val = 1;
196         final Object[] actual = ArrayFill.fill(array, val);
197         assertSame(array, actual);
198     }
199 
200     @Test
201     void testFillShortArray() {
202         final short[] array = new short[3];
203         final short val = (byte) 1;
204         final short[] actual = ArrayFill.fill(array, val);
205         assertSame(array, actual);
206         for (final short v : actual) {
207             assertEquals(val, v);
208         }
209     }
210 
211     @Test
212     void testFillShortArrayNull() {
213         final short[] array = null;
214         final short val = 1;
215         final short[] actual = ArrayFill.fill(array, val);
216         assertSame(array, actual);
217     }
218 }