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.numbers.arrays;
19  
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Test cases for the {@link SortInPlace} class.
25   *
26   */
27  class SortInPlaceTest {
28      @Test
29      void testAscending() {
30          final double[] x = {2, 5, -3, 1,  4};
31          final double[] y = {4, 25, 9, 1, 16};
32          final double[] z = {8, -125, 27, 1, 64};
33  
34          SortInPlace.ASCENDING.apply(x, y, z);
35  
36          final double[] xE = {-3, 1, 2, 4, 5};
37          final double[] yE = {9, 1, 4, 16, 25};
38          final double[] zE = {27, 1, 8, 64, -125};
39  
40          Assertions.assertArrayEquals(xE, x);
41          Assertions.assertArrayEquals(yE, y);
42          Assertions.assertArrayEquals(zE, z);
43      }
44  
45      @Test
46      void testDescending() {
47          final double[] x = {2, 5, -3, 1, 4};
48          final double[] y = {4, 25, 9, 1, 16};
49          final double[] z = {8, -125, 27, 1, 64};
50  
51          SortInPlace.DESCENDING.apply(x, y, z);
52  
53          final double[] xE = {5, 4, 2, 1, -3};
54          final double[] yE = {25, 16, 4, 1, 9};
55          final double[] zE = {-125, 64, 8, 1, 27};
56  
57          Assertions.assertArrayEquals(xE, x);
58          Assertions.assertArrayEquals(yE, y);
59          Assertions.assertArrayEquals(zE, z);
60      }
61  
62      // Example in Javadoc.
63      @Test
64      void testJavadocExample() {
65          final double[] x = {3, 1, 2};
66          final double[] y = {1, 2, 3};
67          final double[] z = {0, 5, 7};
68  
69          SortInPlace.ASCENDING.apply(x, y, z);
70  
71          final double[] xE = {1, 2, 3};
72          final double[] yE = {2, 3, 1};
73          final double[] zE = {5, 7, 0};
74  
75          Assertions.assertArrayEquals(xE, x);
76          Assertions.assertArrayEquals(yE, y);
77          Assertions.assertArrayEquals(zE, z);
78      }
79  
80      @Test
81      void testPreconditions() {
82          final double[] nullArray = null;
83          final double[] one = {1};
84          final double[] two = {1, 2};
85          final double[] onep = {2};
86  
87          Assertions.assertThrows(IllegalArgumentException.class, () -> SortInPlace.ASCENDING.apply(one, two));
88          Assertions.assertThrows(NullPointerException.class, () -> SortInPlace.ASCENDING.apply(one, nullArray));
89          Assertions.assertThrows(NullPointerException.class, () -> SortInPlace.ASCENDING.apply(one, onep, nullArray));
90      }
91  }