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