1   /* Copyright 2002-2022 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.time;
18  
19  
20  
21  import java.util.List;
22  
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.orekit.Utils;
27  
28  public class FixedStepSelectorTest {
29  
30      @Test
31      public void testNoAlign() {
32          final TimeScale utc = TimeScalesFactory.getUTC();
33          final DatesSelector selector = new FixedStepSelector(10.0, null);
34          final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
35          final AbsoluteDate t1 = t0.shiftedBy(91);
36          final List<AbsoluteDate> list = selector.selectDates(t0, t1);
37          Assert.assertEquals(10, list.size());
38          Assert.assertEquals( 27.0, list.get(0).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
39          Assert.assertEquals( 37.0, list.get(1).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
40          Assert.assertEquals( 47.0, list.get(2).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
41          Assert.assertEquals( 57.0, list.get(3).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
42          Assert.assertEquals( 67.0, list.get(4).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
43          Assert.assertEquals( 77.0, list.get(5).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
44          Assert.assertEquals( 87.0, list.get(6).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
45          Assert.assertEquals( 97.0, list.get(7).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
46          Assert.assertEquals(107.0, list.get(8).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
47          Assert.assertEquals(117.0, list.get(9).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
48      }
49  
50      @Test
51      public void testAlignUTCBeforeForward() {
52          final TimeScale utc = TimeScalesFactory.getUTC();
53          final DatesSelector selector = new FixedStepSelector(5.0, utc);
54          final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
55          final AbsoluteDate t1 = t0.shiftedBy(17);
56          final List<AbsoluteDate> list = selector.selectDates(t0, t1);
57          Assert.assertEquals(3, list.size());
58          Assert.assertEquals( 30.0, list.get(0).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
59          Assert.assertEquals( 35.0, list.get(1).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
60          Assert.assertEquals( 40.0, list.get(2).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
61      }
62  
63      @Test
64      public void testAlignUTCBeforeBackward() {
65          final TimeScale utc = TimeScalesFactory.getUTC();
66          final DatesSelector selector = new FixedStepSelector(5.0, utc);
67          final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
68          final AbsoluteDate t1 = t0.shiftedBy(17);
69          final List<AbsoluteDate> list = selector.selectDates(t1, t0);
70          Assert.assertEquals(3, list.size());
71          Assert.assertEquals( 40.0, list.get(0).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
72          Assert.assertEquals( 35.0, list.get(1).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
73          Assert.assertEquals( 30.0, list.get(2).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
74      }
75  
76      @Test
77      public void testAlignUTCAfterForward() {
78          final TimeScale utc = TimeScalesFactory.getUTC();
79          final DatesSelector selector = new FixedStepSelector(10.0, utc);
80          final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
81          final AbsoluteDate t1 = t0.shiftedBy(2);
82          final AbsoluteDate t2 = t1.shiftedBy(89);
83          final List<AbsoluteDate> list1 = selector.selectDates(t0, t1);
84          Assert.assertEquals(0, list1.size());
85          final List<AbsoluteDate> list2 = selector.selectDates(t1, t2);
86          Assert.assertEquals(9, list2.size());
87          Assert.assertEquals( 30.0, list2.get(0).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
88          Assert.assertEquals( 40.0, list2.get(1).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
89          Assert.assertEquals( 50.0, list2.get(2).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
90          Assert.assertEquals( 60.0, list2.get(3).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
91          Assert.assertEquals( 70.0, list2.get(4).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
92          Assert.assertEquals( 80.0, list2.get(5).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
93          Assert.assertEquals( 90.0, list2.get(6).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
94          Assert.assertEquals(100.0, list2.get(7).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
95          Assert.assertEquals(110.0, list2.get(8).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
96      }
97  
98      @Test
99      public void testAlignUTCAfterBackward() {
100         final TimeScale utc = TimeScalesFactory.getUTC();
101         final DatesSelector selector = new FixedStepSelector(10.0, utc);
102         final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
103         final AbsoluteDate t1 = t0.shiftedBy(2);
104         final AbsoluteDate t2 = t1.shiftedBy(89);
105         final List<AbsoluteDate> list1 = selector.selectDates(t2, t1);
106         Assert.assertEquals(9, list1.size());
107         Assert.assertEquals(110.0, list1.get(0).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
108         Assert.assertEquals(100.0, list1.get(1).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
109         Assert.assertEquals( 90.0, list1.get(2).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
110         Assert.assertEquals( 80.0, list1.get(3).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
111         Assert.assertEquals( 70.0, list1.get(4).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
112         Assert.assertEquals( 60.0, list1.get(5).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
113         Assert.assertEquals( 50.0, list1.get(6).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
114         Assert.assertEquals( 40.0, list1.get(7).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
115         Assert.assertEquals( 30.0, list1.get(8).getComponents(utc).getTime().getSecondsInLocalDay(), 1.0e-15);
116         final List<AbsoluteDate> list2 = selector.selectDates(t1, t0);
117         Assert.assertEquals(0, list2.size());
118     }
119 
120     @Test
121     public void testInterruptedStream() {
122         final TimeScale utc = TimeScalesFactory.getUTC();
123         final DatesSelector selector = new FixedStepSelector(10.0, null);
124         final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
125         final AbsoluteDate t1 = t0.shiftedBy(91);
126         final AbsoluteDate t2 = t1.shiftedBy(30);
127         final List<AbsoluteDate> list1 = selector.selectDates(t0, t1);
128         Assert.assertEquals(10, list1.size());
129         final List<AbsoluteDate> list2 = selector.selectDates(t1, t2);
130         Assert.assertEquals(3, list2.size());
131         Assert.assertEquals(10.0, list2.get(0).durationFrom(list1.get(list1.size() - 1)), 1.0e-15);
132     }
133 
134     @Test
135     public void testResetStream() {
136         final TimeScale utc = TimeScalesFactory.getUTC();
137         final DatesSelector selector = new FixedStepSelector(10.0, null);
138         final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
139         final AbsoluteDate t1 = t0.shiftedBy(91);
140         final AbsoluteDate t2 = t1.shiftedBy(30);
141         final AbsoluteDate t3 = t2.shiftedBy(15);
142         final List<AbsoluteDate> list1 = selector.selectDates(t0, t1);
143         Assert.assertEquals(10, list1.size());
144         final List<AbsoluteDate> list2 = selector.selectDates(t2, t3);
145         Assert.assertEquals(2, list2.size());
146         Assert.assertEquals( 0.0, list2.get(0).durationFrom(t2), 1.0e-15);
147         Assert.assertEquals(10.0, list2.get(1).durationFrom(t2), 1.0e-15);
148     }
149 
150     @Test
151     public void testShortInterval() {
152         final TimeScale utc = TimeScalesFactory.getUTC();
153         final DatesSelector selector = new FixedStepSelector(10.0, null);
154         final AbsoluteDate t0 = new AbsoluteDate("2003-02-25T00:00:27.0", utc);
155         final AbsoluteDate t1 = t0.shiftedBy(91);
156         final AbsoluteDate t2 = t1.shiftedBy(8);
157         final List<AbsoluteDate> list1 = selector.selectDates(t0, t1);
158         Assert.assertEquals(10, list1.size());
159         final List<AbsoluteDate> list2 = selector.selectDates(t1, t2);
160         Assert.assertEquals(0, list2.size());
161     }
162 
163     @Before
164     public void setUp() {
165         Utils.setDataRoot("regular-data");
166     }
167 
168 }