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.numbers.field;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  import org.junit.jupiter.params.ParameterizedTest;
22  import org.junit.jupiter.params.provider.ValueSource;
23  
24  /**
25   * Tests for {@link FP64}.
26   */
27  class FP64Test {
28      @ParameterizedTest
29      @ValueSource(doubles = {-5.67e89, -0.0, 0.0, Double.POSITIVE_INFINITY})
30      void testConsistencyWithDouble(double v) {
31          final Double a = Double.valueOf(v);
32          final FP64 b = FP64.of(v);
33  
34          Assertions.assertEquals(a.doubleValue(), b.doubleValue());
35          Assertions.assertEquals(a.floatValue(), b.floatValue());
36          Assertions.assertEquals(a.intValue(), b.intValue());
37          Assertions.assertEquals(a.longValue(), b.longValue());
38          Assertions.assertEquals(a.byteValue(), b.byteValue());
39          Assertions.assertEquals(a.toString(), b.toString());
40          // Hash code for -0.0 and 0.0 are the same
41          if (v == 0) {
42              Assertions.assertEquals(Double.valueOf(0.0).hashCode(), b.hashCode());
43          } else {
44              Assertions.assertEquals(a.hashCode(), b.hashCode());
45          }
46      }
47  
48      @Test
49      void testEquals() {
50          final FP64 a = FP64.of(1.23);
51          final FP64 b = FP64.of(4.56);
52  
53          // Same instance
54          Assertions.assertEquals(a, a);
55          // Same value
56          Assertions.assertEquals(a, FP64.of(a.doubleValue()));
57          // Different value
58          Assertions.assertNotEquals(a, b);
59          // Different object
60          Assertions.assertNotEquals(a, new Object());
61          Assertions.assertNotEquals(a, null);
62      }
63  
64      @ParameterizedTest
65      @ValueSource(doubles = {1.23, 0.0, -0.0})
66      void testEqualsCloseValues(double value) {
67          // Use very close values
68          final FP64 a = FP64.of(value);
69          final FP64 b = FP64.of(Math.nextUp(value));
70  
71          // Same value
72          Assertions.assertEquals(a, FP64.of(value));
73          // Different value
74          Assertions.assertNotEquals(a, b);
75      }
76  
77      @Test
78      void testEqualsZero() {
79          final FP64 a = FP64.of(-0.0);
80          final FP64 b = FP64.of(0.0);
81          Assertions.assertEquals(a, b);
82          Assertions.assertEquals(b, a);
83          Assertions.assertEquals(a.hashCode(), b.hashCode());
84      }
85  
86      @ParameterizedTest
87      @ValueSource(doubles = {-3.4, -0.0, 0.0, Double.POSITIVE_INFINITY})
88      void testOne(double value) {
89          final FP64 a = FP64.of(value);
90          Assertions.assertEquals(1d, a.one().doubleValue());
91          // Test definition
92          Assertions.assertEquals(a, a.one().multiply(a));
93      }
94  
95      @ParameterizedTest
96      @ValueSource(doubles = {-3.4, -0.0, 0.0, Double.POSITIVE_INFINITY})
97      void testZero(double value) {
98          final FP64 a = FP64.of(value);
99          Assertions.assertEquals(0d, a.zero().doubleValue());
100         // Test definition
101         Assertions.assertEquals(a, a.zero().add(a));
102     }
103 
104     @Test
105     void testIsOne() {
106         Assertions.assertTrue(FP64.of(1.0).isOne());
107         Assertions.assertTrue(FP64.of(0.5).add(FP64.of(0.5)).isOne());
108         FP64 value = FP64.of(1e300);
109         Assertions.assertTrue(value.divide(value).isOne());
110         // avoid weirdness
111         Assertions.assertFalse(FP64.of(3).isOne());
112         Assertions.assertFalse(FP64.of(0.0).isOne());
113     }
114 
115     @Test
116     void testIsZero() {
117         Assertions.assertTrue(FP64.of(0.0).isZero());
118         Assertions.assertTrue(FP64.of(0.5).subtract(FP64.of(0.5)).isZero());
119         FP64 value = FP64.of(1e300);
120         Assertions.assertTrue(value.multiply(value.zero()).isZero());
121         // avoid weirdness
122         Assertions.assertFalse(FP64.of(3).isZero());
123         Assertions.assertFalse(FP64.of(1.0).isZero());
124     }
125 
126     @Test
127     void testSubtract() {
128         final double a = 123.4;
129         final double b = 5678.9;
130 
131         Assertions.assertEquals(a - b, FP64.of(a).subtract(FP64.of(b)).doubleValue());
132     }
133     @Test
134     void testDivide() {
135         final double a = 123.4;
136         final double b = 5678.9;
137 
138         Assertions.assertEquals(a / b, FP64.of(a).divide(FP64.of(b)).doubleValue());
139     }
140 
141     @Test
142     void testMultiplyInt() {
143         final double a = 123.4;
144         final int n = 3456789;
145 
146         Assertions.assertEquals(n * a, FP64.of(a).multiply(n).doubleValue());
147     }
148 
149     @Test
150     void testPowInt() {
151         final double a = 123.4;
152         final int n = 5;
153 
154         Assertions.assertEquals(Math.pow(a, n), FP64.of(a).pow(n).doubleValue());
155     }
156     @Test
157     void testZeroPow() {
158         Assertions.assertSame(FP64.of(9876.5).one(), FP64.of(2.3456).pow(0));
159     }
160 
161     @Test
162     void testCompare() {
163         Assertions.assertTrue(FP64.of(0).compareTo(FP64.of(-1)) > 0);
164         Assertions.assertTrue(FP64.of(1).compareTo(FP64.of(2)) < 0);
165 
166         final double v = 123.45;
167         Assertions.assertEquals(0, FP64.of(v).compareTo(FP64.of(v)));
168     }
169 }