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  package org.apache.commons.lang3.math;
18  
19  import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20  import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests {@link org.apache.commons.lang3.math.IEEE754rUtils}.
29   */
30  class IEEE754rUtilsTest extends AbstractLangTest {
31  
32      @Test
33      void testConstructorExists() {
34          new IEEE754rUtils();
35      }
36  
37      @Test
38      void testEnforceExceptions() {
39          assertNullPointerException(() -> IEEE754rUtils.min((float[]) null), "IllegalArgumentException expected for null input");
40          assertIllegalArgumentException(IEEE754rUtils::min, "IllegalArgumentException expected for empty input");
41          assertNullPointerException(() -> IEEE754rUtils.max((float[]) null), "IllegalArgumentException expected for null input");
42          assertIllegalArgumentException(IEEE754rUtils::max, "IllegalArgumentException expected for empty input");
43          assertNullPointerException(() -> IEEE754rUtils.min((double[]) null), "IllegalArgumentException expected for null input");
44          assertIllegalArgumentException(IEEE754rUtils::min, "IllegalArgumentException expected for empty input");
45          assertNullPointerException(() -> IEEE754rUtils.max((double[]) null), "IllegalArgumentException expected for null input");
46          assertIllegalArgumentException(IEEE754rUtils::max, "IllegalArgumentException expected for empty input");
47      }
48  
49      @Test
50      void testLang381() {
51          assertEquals(1.2, IEEE754rUtils.min(1.2, 2.5, Double.NaN), 0.01);
52          assertEquals(2.5, IEEE754rUtils.max(1.2, 2.5, Double.NaN), 0.01);
53          assertTrue(Double.isNaN(IEEE754rUtils.max(Double.NaN, Double.NaN, Double.NaN)));
54          assertEquals(1.2f, IEEE754rUtils.min(1.2f, 2.5f, Float.NaN), 0.01);
55          assertEquals(2.5f, IEEE754rUtils.max(1.2f, 2.5f, Float.NaN), 0.01);
56          assertTrue(Float.isNaN(IEEE754rUtils.max(Float.NaN, Float.NaN, Float.NaN)));
57  
58          final double[] a = { 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
59          assertEquals(42.0, IEEE754rUtils.max(a), 0.01);
60          assertEquals(1.2, IEEE754rUtils.min(a), 0.01);
61  
62          final double[] b = { Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN };
63          assertEquals(42.0, IEEE754rUtils.max(b), 0.01);
64          assertEquals(1.2, IEEE754rUtils.min(b), 0.01);
65  
66          final float[] aF = { 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
67          assertEquals(1.2f, IEEE754rUtils.min(aF), 0.01);
68          assertEquals(42.0f, IEEE754rUtils.max(aF), 0.01);
69  
70          final float[] bF = { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN };
71          assertEquals(1.2f, IEEE754rUtils.min(bF), 0.01);
72          assertEquals(42.0f, IEEE754rUtils.max(bF), 0.01);
73      }
74  
75  }