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  package org.apache.commons.lang3.math;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Unit tests {@link org.apache.commons.lang3.math.IEEE754rUtils}.
28   */
29  public class IEEE754rUtilsTest extends AbstractLangTest {
30  
31      @Test
32      public void testConstructorExists() {
33          new IEEE754rUtils();
34      }
35  
36      @Test
37      public void testEnforceExceptions() {
38          assertThrows(
39                  NullPointerException.class,
40                  () -> IEEE754rUtils.min( (float[]) null),
41                  "IllegalArgumentException expected for null input");
42  
43          assertThrows(
44                  IllegalArgumentException.class,
45                  IEEE754rUtils::min,
46                  "IllegalArgumentException expected for empty input");
47  
48          assertThrows(
49                  NullPointerException.class,
50                  () -> IEEE754rUtils.max( (float[]) null),
51                  "IllegalArgumentException expected for null input");
52  
53          assertThrows(
54                  IllegalArgumentException.class,
55                  IEEE754rUtils::max,
56                  "IllegalArgumentException expected for empty input");
57  
58          assertThrows(
59                  NullPointerException.class,
60                  () -> IEEE754rUtils.min( (double[]) null),
61                  "IllegalArgumentException expected for null input");
62  
63          assertThrows(
64                  IllegalArgumentException.class,
65                  IEEE754rUtils::min,
66                  "IllegalArgumentException expected for empty input");
67  
68          assertThrows(
69                  NullPointerException.class,
70                  () -> IEEE754rUtils.max( (double[]) null),
71                  "IllegalArgumentException expected for null input");
72  
73          assertThrows(
74                  IllegalArgumentException.class,
75                  IEEE754rUtils::max,
76                  "IllegalArgumentException expected for empty input");
77      }
78  
79      @Test
80      public void testLang381() {
81          assertEquals(1.2, IEEE754rUtils.min(1.2, 2.5, Double.NaN), 0.01);
82          assertEquals(2.5, IEEE754rUtils.max(1.2, 2.5, Double.NaN), 0.01);
83          assertTrue(Double.isNaN(IEEE754rUtils.max(Double.NaN, Double.NaN, Double.NaN)));
84          assertEquals(1.2f, IEEE754rUtils.min(1.2f, 2.5f, Float.NaN), 0.01);
85          assertEquals(2.5f, IEEE754rUtils.max(1.2f, 2.5f, Float.NaN), 0.01);
86          assertTrue(Float.isNaN(IEEE754rUtils.max(Float.NaN, Float.NaN, Float.NaN)));
87  
88          final double[] a = { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
89          assertEquals(42.0, IEEE754rUtils.max(a), 0.01);
90          assertEquals(1.2, IEEE754rUtils.min(a), 0.01);
91  
92          final double[] b = { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
93          assertEquals(42.0, IEEE754rUtils.max(b), 0.01);
94          assertEquals(1.2, IEEE754rUtils.min(b), 0.01);
95  
96          final float[] aF = { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
97          assertEquals(1.2f, IEEE754rUtils.min(aF), 0.01);
98          assertEquals(42.0f, IEEE754rUtils.max(aF), 0.01);
99  
100         final float[] bF = { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
101         assertEquals(1.2f, IEEE754rUtils.min(bF), 0.01);
102         assertEquals(42.0f, IEEE754rUtils.max(bF), 0.01);
103     }
104 
105 }