View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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.hipparchus.util;
18  
19  import org.hipparchus.CalculusFieldElementAbstractTest;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  public class TupleTest extends CalculusFieldElementAbstractTest<Tuple> {
24      public static final double X = 1.2345;
25  
26      public static final Tuple PLUS_X = new Tuple(X, X);
27  
28      public static final Tuple MINUS_X = new Tuple(-X, -X);
29  
30      public static final double Y = 6.789;
31  
32      public static final Tuple PLUS_Y = new Tuple(Y, Y);
33  
34      public static final Tuple MINUS_Y = new Tuple(-Y, -Y);
35  
36      public static final Tuple PLUS_ZERO = new Tuple(0.0, 0.0);
37  
38      public static final Tuple MINUS_ZERO = new Tuple(-0.0, -0.0);
39  
40      @Override
41      protected Tuple build(final double x) {
42          return new Tuple(x, x);
43      }
44  
45      @Test
46      public void testComponents() {
47          Assert.assertEquals(2, PLUS_ZERO.getDimension());
48          final Tuple oneToFive = new Tuple(1, 2, 3, 4, 5);
49          Assert.assertEquals(5, oneToFive.getDimension());
50          Assert.assertArrayEquals(new double[] { 1,  2,  3, 4, 5 }, oneToFive.getComponents(), 1.0e-10);
51          Assert.assertEquals(1, oneToFive.getComponent(0), 1.0e-10);
52          Assert.assertEquals(2, oneToFive.getComponent(1), 1.0e-10);
53          Assert.assertEquals(3, oneToFive.getComponent(2), 1.0e-10);
54          Assert.assertEquals(4, oneToFive.getComponent(3), 1.0e-10);
55          Assert.assertEquals(5, oneToFive.getComponent(4), 1.0e-10);
56      }
57  
58      @Test
59      public void testEquals() {
60          Assert.assertNotEquals(PLUS_ZERO, null);
61          Assert.assertEquals(PLUS_ZERO, PLUS_ZERO);
62          Assert.assertEquals(PLUS_X,    PLUS_X);
63          Assert.assertEquals(PLUS_Y,    PLUS_Y);
64          Assert.assertEquals(MINUS_X,   MINUS_X);
65          Assert.assertEquals(MINUS_Y,   MINUS_Y);
66          Assert.assertNotEquals(PLUS_X,  new Tuple(1, 2, 3, 4, 5));
67          Assert.assertNotEquals(PLUS_X,  new Tuple(PLUS_X.getComponent(0), 999.999));
68          Assert.assertNotEquals(PLUS_ZERO.getField(), null);
69          Assert.assertNotEquals(PLUS_X.getField(), new Tuple(1, 2, 3, 4, 5).getField());
70          Assert.assertEquals(PLUS_ZERO.getField(), MINUS_Y.getField());
71      }
72  
73      @Test
74      public void testHashcode() {
75          Assert.assertEquals(1718765887, PLUS_ZERO.getField().hashCode());
76          Assert.assertEquals(884058117, PLUS_ZERO.hashCode());
77          Assert.assertEquals(884058117, MINUS_ZERO.hashCode());
78          Assert.assertEquals(-396588603, PLUS_X.hashCode());
79          Assert.assertEquals(-396588603, MINUS_X.hashCode());
80          Assert.assertEquals(851552261, new Tuple(1, 2).hashCode());
81          Assert.assertEquals(883009541, new Tuple(2, 1).hashCode());
82      }
83  
84      @Test
85      public void testAdd() {
86          Tuple expected, actual;
87  
88          expected = new Tuple(X + Y, X + Y);
89          actual = PLUS_X.add(PLUS_Y);
90          Assert.assertEquals(expected, actual);
91          actual = PLUS_Y.add(PLUS_X);
92          Assert.assertEquals(expected, actual);
93  
94          expected = new Tuple(X + (-Y), X + (-Y));
95          actual = PLUS_X.add(MINUS_Y);
96          Assert.assertEquals(expected, actual);
97          actual = MINUS_Y.add(PLUS_X);
98          Assert.assertEquals(expected, actual);
99  
100         expected = new Tuple((-X) + (-Y), (-X) + (-Y));
101         actual = MINUS_X.add(MINUS_Y);
102         Assert.assertEquals(expected, actual);
103         actual = MINUS_Y.add(MINUS_X);
104         Assert.assertEquals(expected, actual);
105 
106     }
107 
108     @Test
109     public void testSubtract() {
110         Tuple expected, actual;
111 
112         expected = new Tuple(X - Y, X - Y);
113         actual = PLUS_X.subtract(PLUS_Y);
114         Assert.assertEquals(expected, actual);
115 
116         expected = new Tuple(X - (-Y), X - (-Y));
117         actual = PLUS_X.subtract(MINUS_Y);
118         Assert.assertEquals(expected, actual);
119 
120         expected = new Tuple((-X) - Y, (-X) - Y);
121         actual = MINUS_X.subtract(PLUS_Y);
122         Assert.assertEquals(expected, actual);
123 
124         expected = new Tuple((-X) - (-Y), (-X) - (-Y));
125         actual = MINUS_X.subtract(MINUS_Y);
126         Assert.assertEquals(expected, actual);
127 
128     }
129 
130     @Test
131     public void testNegate() {
132         Tuple expected, actual;
133 
134         expected = MINUS_X;
135         actual = PLUS_X.negate();
136         Assert.assertEquals(expected, actual);
137 
138         expected = PLUS_X;
139         actual = MINUS_X.negate();
140         Assert.assertEquals(expected, actual);
141 
142         expected = MINUS_ZERO;
143         actual = PLUS_ZERO.negate();
144         Assert.assertEquals(expected, actual);
145 
146         expected = PLUS_ZERO;
147         actual = MINUS_ZERO.negate();
148         Assert.assertEquals(expected, actual);
149 
150     }
151 
152     @Test
153     public void testMultiply() {
154         Tuple expected, actual;
155 
156         expected = new Tuple(X * Y, X * Y);
157         actual = PLUS_X.multiply(PLUS_Y);
158         Assert.assertEquals(expected, actual);
159         actual = PLUS_Y.multiply(PLUS_X);
160         Assert.assertEquals(expected, actual);
161 
162         expected = new Tuple(X * (-Y), X * (-Y));
163         actual = PLUS_X.multiply(MINUS_Y);
164         Assert.assertEquals(expected, actual);
165         actual = MINUS_Y.multiply(PLUS_X);
166         Assert.assertEquals(expected, actual);
167 
168         expected = new Tuple((-X) * (-Y), (-X) * (-Y));
169         actual = MINUS_X.multiply(MINUS_Y);
170         Assert.assertEquals(expected, actual);
171         actual = MINUS_Y.multiply(MINUS_X);
172         Assert.assertEquals(expected, actual);
173 
174     }
175 
176     @Test
177     public void testDivide() {
178         Tuple expected, actual;
179 
180         expected = new Tuple(X / Y, X / Y);
181         actual = PLUS_X.divide(PLUS_Y);
182         Assert.assertEquals(expected, actual);
183 
184         expected = new Tuple(X / (-Y), X / (-Y));
185         actual = PLUS_X.divide(MINUS_Y);
186         Assert.assertEquals(expected, actual);
187 
188         expected = new Tuple((-X) / Y, (-X) / Y);
189         actual = MINUS_X.divide(PLUS_Y);
190         Assert.assertEquals(expected, actual);
191 
192         expected = new Tuple((-X) / (-Y), (-X) / (-Y));
193         actual = MINUS_X.divide(MINUS_Y);
194         Assert.assertEquals(expected, actual);
195 
196     }
197 
198     @Test
199     public void testReciprocal() {
200         Tuple expected, actual;
201 
202         expected = new Tuple(1.0 / X, 1.0 / X);
203         actual = PLUS_X.reciprocal();
204         Assert.assertEquals(expected, actual);
205 
206         expected = new Tuple(1.0 / (-X), 1.0 / (-X));
207         actual = MINUS_X.reciprocal();
208         Assert.assertEquals(expected, actual);
209 
210         expected = PLUS_ZERO;
211         actual = new Tuple(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY).reciprocal();
212         Assert.assertEquals(expected, actual);
213 
214         expected = MINUS_ZERO;
215         actual = new Tuple(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY).reciprocal();
216         Assert.assertEquals(expected, actual);
217     }
218 
219     @Test
220     public void testToDegreesDefinition() {
221         double epsilon = 3.0e-16;
222         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
223             for (double x = 0.1; x < 1.2; x += 0.001) {
224                 Tuple value = new Tuple(x, x);
225                 Assert.assertEquals(FastMath.toDegrees(x), value.toDegrees().getReal(), epsilon);
226             }
227         }
228     }
229 
230     @Test
231     public void testToRadiansDefinition() {
232         double epsilon = 3.0e-16;
233         for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
234             for (double x = 0.1; x < 1.2; x += 0.001) {
235                 Tuple value = new Tuple(x, x);
236                 Assert.assertEquals(FastMath.toRadians(x), value.toRadians().getReal(), epsilon);
237             }
238         }
239     }
240 
241     @Test
242     public void testDegRad() {
243         for (double x = 0.1; x < 1.2; x += 0.001) {
244             Tuple value = new Tuple(x, x);
245             Tuple rebuilt = value.toDegrees().toRadians();
246             Tuple zero = rebuilt.subtract(value);
247             Assert.assertEquals(zero.getReal(), 0, 3.0e-16);
248         }
249     }
250 
251 }