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.geometry.core.precision;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  public class DoublePrecisionContextTest {
23  
24      private final StubContext ctx = new StubContext();
25  
26      @Test
27      public void testEq() {
28          // act/assert
29          Assert.assertTrue(ctx.eq(0.0, 0.0));
30          Assert.assertTrue(ctx.eq(1.0, 1.0));
31          Assert.assertTrue(ctx.eq(-1.0, -1.0));
32  
33          Assert.assertFalse(ctx.eq(1.0, -1.0));
34          Assert.assertFalse(ctx.eq(1.0, Math.nextUp(1.0)));
35          Assert.assertFalse(ctx.eq(-1.0, Math.nextDown(1.0)));
36      }
37  
38      @Test
39      public void testEqZero() {
40          // act/assert
41          Assert.assertTrue(ctx.eqZero(0.0));
42  
43          Assert.assertFalse(ctx.eqZero(Math.nextUp(0.0)));
44          Assert.assertFalse(ctx.eqZero(Math.nextDown(-0.0)));
45      }
46  
47      @Test
48      public void testLt() {
49          // act/assert
50          Assert.assertTrue(ctx.lt(1, 2));
51          Assert.assertTrue(ctx.lt(-2, -1));
52  
53          Assert.assertFalse(ctx.lt(1, 1));
54          Assert.assertFalse(ctx.lt(-1, -1));
55          Assert.assertFalse(ctx.lt(2, 1));
56          Assert.assertFalse(ctx.lt(-1, -2));
57      }
58  
59      @Test
60      public void testLte() {
61          // act/assert
62          Assert.assertTrue(ctx.lte(1, 2));
63          Assert.assertTrue(ctx.lte(-2, -1));
64          Assert.assertTrue(ctx.lte(1, 1));
65          Assert.assertTrue(ctx.lte(-1, -1));
66  
67          Assert.assertFalse(ctx.lte(2, 1));
68          Assert.assertFalse(ctx.lte(-1, -2));
69      }
70  
71      @Test
72      public void testGt() {
73          // act/assert
74          Assert.assertTrue(ctx.gt(2, 1));
75          Assert.assertTrue(ctx.gt(-1, -2));
76  
77          Assert.assertFalse(ctx.gt(1, 1));
78          Assert.assertFalse(ctx.gt(-1, -1));
79          Assert.assertFalse(ctx.gt(1, 2));
80          Assert.assertFalse(ctx.gt(-2, -1));
81      }
82  
83      @Test
84      public void testGte() {
85          // act/assert
86          Assert.assertTrue(ctx.gte(2, 1));
87          Assert.assertTrue(ctx.gte(-1, -2));
88          Assert.assertTrue(ctx.gte(1, 1));
89          Assert.assertTrue(ctx.gte(-1, -1));
90  
91          Assert.assertFalse(ctx.gte(1, 2));
92          Assert.assertFalse(ctx.gte(-2, -1));
93      }
94  
95      @Test
96      public void testSign() {
97          // act/assert
98          Assert.assertEquals(0, ctx.sign(0.0));
99  
100         Assert.assertEquals(1, ctx.sign(1e-3));
101         Assert.assertEquals(-1, ctx.sign(-1e-3));
102 
103         Assert.assertEquals(1, ctx.sign(Double.NaN));
104         Assert.assertEquals(1, ctx.sign(Double.POSITIVE_INFINITY));
105         Assert.assertEquals(-1, ctx.sign(Double.NEGATIVE_INFINITY));
106     }
107 
108     @Test
109     public void testCompare() {
110         // act/assert
111         Assert.assertEquals(0, ctx.compare(1, 1));
112         Assert.assertEquals(-1, ctx.compare(1, 2));
113         Assert.assertEquals(1, ctx.compare(2, 1));
114 
115         Assert.assertEquals(0, ctx.compare(-1, -1));
116         Assert.assertEquals(1, ctx.compare(-1, -2));
117         Assert.assertEquals(-1, ctx.compare(-2, -1));
118     }
119 
120     @Test
121     public void testCompare_wrapper() {
122         // act/assert
123         Assert.assertEquals(0, ctx.compare(new Double(1), new Double(1)));
124         Assert.assertEquals(-1, ctx.compare(new Double(1), new Double(2)));
125         Assert.assertEquals(1, ctx.compare(new Double(2), new Double(1)));
126 
127         Assert.assertEquals(0, ctx.compare(new Double(-1), new Double(-1)));
128         Assert.assertEquals(1, ctx.compare(new Double(-1), new Double(-2)));
129         Assert.assertEquals(-1, ctx.compare(new Double(-2), new Double(-1)));
130     }
131 
132     private static class StubContext extends DoublePrecisionContext {
133 
134         @Override
135         public double getMaxZero() {
136             return 0.0;
137         }
138 
139         @Override
140         public int compare(final double a, final double b) {
141             return Double.compare(a, b);
142         }
143     }
144 }