1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS 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.orekit.estimation.measurements;
18  
19  import org.hipparchus.geometry.euclidean.threed.Vector3D;
20  import org.hipparchus.util.FastMath;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.orekit.Utils;
24  import org.orekit.bodies.GeodeticPoint;
25  import org.orekit.bodies.OneAxisEllipsoid;
26  import org.orekit.frames.FramesFactory;
27  import org.orekit.frames.TopocentricFrame;
28  import org.orekit.time.AbsoluteDate;
29  import org.orekit.utils.Constants;
30  import org.orekit.utils.IERSConventions;
31  
32  /** This test class is only used to test the <i>compareTo</i> default method of interface <i>ComparableMeasurement</i>.<br>
33   * This is done to test the resolution of  <a href="https://gitlab.orekit.org/orekit/orekit/issues/538"> issue #538</a> on Orekit forge.
34   */
35  public class ComparableMeasurementTest {
36  
37      /** Test default method compareTo, see <a href="https://gitlab.orekit.org/orekit/orekit/issues/538"> issue #538</a> on Orekit forge. */
38      @Test
39      public void testDefaultCompareToIssue538() {
40  
41          // Print on console ?
42          boolean print = false;
43  
44          // Setup data
45          Utils.setDataRoot("regular-data");
46          OneAxisEllipsoid earth = new OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS, Constants.WGS84_EARTH_FLATTENING,
47                                                        FramesFactory.getITRF(IERSConventions.IERS_2010, false));
48          TopocentricFrame stationFrame = new TopocentricFrame(earth, new GeodeticPoint(FastMath.toRadians(45.0), FastMath.toRadians(0.0), 0.0), "station");
49          GroundStation station = new GroundStation(stationFrame);
50          AbsoluteDate date = AbsoluteDate.J2000_EPOCH;
51  
52          // Create a Range and Azel at same date,
53          // A 2 identical PV before date
54          // And 1 PV measurement before date with one value bigger
55          ObservableSatellite satellite = new ObservableSatellite(0);
56          Range range = new Range(station, false, date, 400e3, 1.0, 1.0, satellite);
57          AngularAzEl azel = new AngularAzEl(station, date, new double[] {1.0, 0.5}, new double[] {1.0e-2, 1.0e-2}, new double[] {1.0, 1.0}, satellite);
58          PV pv = new PV(date.shiftedBy(-1.), new Vector3D(7e3,7e3,7e3), new Vector3D(7,7,7), 1., 1e-3, 1., satellite);
59          PV pv2 = new PV(date.shiftedBy(-1.), new Vector3D(7e3,7e3,7e3), new Vector3D(7,7,7), 1., 1e-3, 1., satellite);
60          PV pv3 = new PV(date.shiftedBy(-1.), new Vector3D(7e3,7e3,7e3), new Vector3D(7,8,7), 1., 1e-3, 1., satellite);
61  
62          // Print out results
63          if (print) {
64              System.out.println("azel.compareTo(azel)  = " + azel.compareTo(azel));
65              System.out.println("pv.compareTo(azel)    = " + pv.compareTo(azel));
66              System.out.println("range.compareTo(pv2)  = " + range.compareTo(pv2));
67              System.out.println("range.compareTo(azel) = " + range.compareTo(azel));
68              System.out.println("azel.compareTo(range) = " + azel.compareTo(range));
69              System.out.println("pv.compareTo(pv3)     = " + pv.compareTo(pv3));
70              System.out.println("pv3.compareTo(pv)     = " + pv3.compareTo(pv));
71              System.out.println("pv.compareTo(pv2)     = " + pv.compareTo(pv2));
72              System.out.println("pv2.compareTo(pv)     = " + pv.compareTo(pv2));
73          }
74  
75          // Same object, only case when compareTo returns 0
76          Assertions.assertEquals(azel.compareTo(azel), 0);
77  
78          // Sorted by date by default
79          Assertions.assertEquals(-1, pv.compareTo(azel));
80          Assertions.assertEquals(+1, azel.compareTo(pv));
81          Assertions.assertEquals(+1, range.compareTo(pv2));
82          Assertions.assertEquals(-1, pv2.compareTo(range));
83  
84          // Same date but different measurement - "bigger" measurement after "smaller" one
85          Assertions.assertEquals(-1, range.compareTo(azel));
86          Assertions.assertEquals(+1, azel.compareTo(range));
87  
88          // Same date, same size, but different values, "bigger" measurement after "smaller" one
89          Assertions.assertEquals(-1, pv.compareTo(pv3));
90          Assertions.assertEquals(+1, pv3.compareTo(pv));
91  
92          // Same date, same size, same values. Likely non-zero, but may be zero.
93          Assertions.assertEquals(pv.compareTo(pv2), -pv2.compareTo(pv));
94      }
95  
96      /**
97       * Check {@link ComparableMeasurement#compareTo(ComparableMeasurement)} still works if
98       * hashcode is implemented poorly.
99       */
100     @Test
101     public void testComparePoorlyImplementedHashCode() {
102         ComparableMeasurement a = new BadHashCode(), b = new BadHashCode();
103 
104         // Same date, same size, same values. Likely non-zero, but may be zero.
105         Assertions.assertEquals(a.compareTo(b), -b.compareTo(a));
106         Assertions.assertEquals(a.compareTo(a), 0);
107         Assertions.assertEquals(b.compareTo(b), 0);
108     }
109 
110     /** Implementation of {@link ComparableMeasurement} with a bad hash code. */
111     private static class BadHashCode implements ComparableMeasurement {
112 
113         @Override
114         public double[] getObservedValue() {
115             return new double[0];
116         }
117 
118         @Override
119         public void setObservedValue(double[] newObserved) {
120         }
121 
122         @Override
123         public AbsoluteDate getDate() {
124             return AbsoluteDate.ARBITRARY_EPOCH;
125         }
126 
127         @Override
128         public int hashCode() {
129             // test comparison w/ poor hashcode implementation
130             return 1;
131         }
132 
133     }
134 
135 }