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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.spherical.oned;
23  
24  import org.hipparchus.exception.MathRuntimeException;
25  import org.hipparchus.util.FastMath;
26  import org.hipparchus.util.MathUtils;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
32  import static org.junit.jupiter.api.Assertions.assertNotEquals;
33  import static org.junit.jupiter.api.Assertions.assertNotSame;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  class S1PointTest {
38  
39      @Test
40      void testS1Point() {
41          for (int k = -2; k < 3; ++k) {
42              S1Point p = new S1Point(1.0 + k * MathUtils.TWO_PI);
43              assertEquals(FastMath.cos(1.0), p.getVector().getX(), 1.0e-10);
44              assertEquals(FastMath.sin(1.0), p.getVector().getY(), 1.0e-10);
45              assertFalse(p.isNaN());
46          }
47      }
48  
49      @Test
50      void testNaN() {
51          assertTrue(S1Point.NaN.isNaN());
52          assertEquals(S1Point.NaN, new S1Point(Double.NaN));
53          assertNotEquals(S1Point.NaN, new S1Point(1.0));
54      }
55  
56      @SuppressWarnings("unlikely-arg-type")
57      @Test
58      void testEquals() {
59          S1Point a = new S1Point(1.0);
60          S1Point b = new S1Point(1.0);
61          assertEquals(a.hashCode(), b.hashCode());
62          assertNotSame(a, b);
63          assertEquals(a, b);
64          assertEquals(a, a);
65          assertNotEquals('a', a);
66          assertEquals(S1Point.NaN, S1Point.NaN);
67          assertEquals(S1Point.NaN, new S1Point(Double.NaN));
68      }
69  
70      @Test
71      void testEqualsIeee754() {
72          S1Point a = new S1Point(1.0);
73          S1Point b = new S1Point(1.0);
74          assertEquals(a.hashCode(), b.hashCode());
75          assertNotSame(a, b);
76          assertTrue(a.equalsIeee754(b));
77          assertTrue(a.equalsIeee754(a));
78          assertFalse(a.equalsIeee754('a'));
79          assertFalse(S1Point.NaN.equalsIeee754(S1Point.NaN));
80          assertFalse(S1Point.NaN.equalsIeee754(new S1Point(Double.NaN)));
81      }
82  
83      @Test
84      void testDistance() {
85          S1Point a = new S1Point(1.0);
86          S1Point b = new S1Point(a.getAlpha() + MathUtils.SEMI_PI);
87          assertEquals(MathUtils.SEMI_PI, a.distance(b), 1.0e-10);
88      }
89  
90      @Test
91      void testSpace() {
92          S1Point a = new S1Point(1.0);
93          assertInstanceOf(Sphere1D.class, a.getSpace());
94          assertEquals(1, a.getSpace().getDimension());
95          try {
96              a.getSpace().getSubSpace();
97              fail("an exception should have been thrown");
98          } catch (MathRuntimeException muoe) {
99              // expected
100         }
101     }
102 
103     @Test
104     void testMoveTowards() {
105         final S1Point s1 = new S1Point(2.0);
106         final S1Point s2 = new S1Point(4.0);
107         for (double r = 0.0; r <= 1.0; r += FastMath.scalb(1.0, -10)) {
108             // motion should be linear according to angles
109             assertEquals(r * s1.distance(s2), s1.distance(s1.moveTowards(s2, r)), 1.0e-14);
110         }
111     }
112 
113 }