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    *      http://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.assertNull;
22  
23  import java.util.Arrays;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests {@link ArraySorter}.
29   */
30  public class ArraySorterTest extends AbstractLangTest {
31  
32      @Test
33      public void testSortByteArray() {
34          final byte[] array1 = {2, 1};
35          final byte[] array2 = array1.clone();
36          Arrays.sort(array1);
37          assertArrayEquals(array1, ArraySorter.sort(array2));
38          assertNull(ArraySorter.sort((byte[]) null));
39      }
40  
41      @Test
42      public void testSortCharArray() {
43          final char[] array1 = {2, 1};
44          final char[] array2 = array1.clone();
45          Arrays.sort(array1);
46          assertArrayEquals(array1, ArraySorter.sort(array2));
47          assertNull(ArraySorter.sort((char[]) null));
48      }
49  
50      @Test
51      public void testSortComparable() {
52          final String[] array1 = ArrayUtils.toArray("foo", "bar");
53          final String[] array2 = array1.clone();
54          Arrays.sort(array1);
55          assertArrayEquals(array1, ArraySorter.sort(array2, String::compareTo));
56          assertNull(ArraySorter.sort((String[]) null));
57      }
58  
59      @Test
60      public void testSortDoubleArray() {
61          final double[] array1 = {2, 1};
62          final double[] array2 = array1.clone();
63          Arrays.sort(array1);
64          assertArrayEquals(array1, ArraySorter.sort(array2));
65          assertNull(ArraySorter.sort((double[]) null));
66      }
67  
68      @Test
69      public void testSortFloatArray() {
70          final float[] array1 = {2, 1};
71          final float[] array2 = array1.clone();
72          Arrays.sort(array1);
73          assertArrayEquals(array1, ArraySorter.sort(array2));
74          assertNull(ArraySorter.sort((float[]) null));
75      }
76  
77      @Test
78      public void testSortIntArray() {
79          final int[] array1 = {2, 1};
80          final int[] array2 = array1.clone();
81          Arrays.sort(array1);
82          assertArrayEquals(array1, ArraySorter.sort(array2));
83          assertNull(ArraySorter.sort((int[]) null));
84      }
85  
86      @Test
87      public void testSortLongArray() {
88          final long[] array1 = {2, 1};
89          final long[] array2 = array1.clone();
90          Arrays.sort(array1);
91          assertArrayEquals(array1, ArraySorter.sort(array2));
92          assertNull(ArraySorter.sort((long[]) null));
93      }
94  
95      @Test
96      public void testSortObjects() {
97          final String[] array1 = ArrayUtils.toArray("foo", "bar");
98          final String[] array2 = array1.clone();
99          Arrays.sort(array1);
100         assertArrayEquals(array1, ArraySorter.sort(array2));
101         assertNull(ArraySorter.sort((String[]) null));
102     }
103 
104     @Test
105     public void testSortShortArray() {
106         final short[] array1 = {2, 1};
107         final short[] array2 = array1.clone();
108         Arrays.sort(array1);
109         assertArrayEquals(array1, ArraySorter.sort(array2));
110         assertNull(ArraySorter.sort((short[]) null));
111     }
112 
113 }