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.propagation.events;
18  
19  import java.util.Collections;
20  import java.util.NoSuchElementException;
21  
22  import org.hipparchus.ode.events.Action;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.mockito.Mockito;
27  import org.orekit.propagation.SpacecraftState;
28  import org.orekit.propagation.events.handlers.EventHandler;
29  import org.orekit.time.AbsoluteDate;
30  
31  /**
32   * Unit tests for {@link BooleanDetector#orCombine(EventDetector...)}.
33   *
34   * @author Evan Ward
35   */
36  public class OrDetectorTest {
37  
38      /** first operand. */
39      private MockDetector a;
40      /** second operand. */
41      private MockDetector b;
42      /** null */
43      private SpacecraftState s;
44      /** subject under test */
45      private BooleanDetector or;
46  
47      /** create subject under test and dependencies. */
48      @Before
49      public void setUp() {
50          a = new MockDetector();
51          b = new MockDetector();
52          s = null;
53          or = BooleanDetector.orCombine(a, b);
54      }
55  
56      /**
57       * check {@link BooleanDetector#g(SpacecraftState)}.
58       */
59      @Test
60      public void testG() {
61          // test zero cases
62          a.g = b.g = 0.0;
63          Assert.assertEquals(0.0, or.g(s), 0);
64          a.g = -1;
65          b.g = 0;
66          Assert.assertEquals(0.0, or.g(s), 0);
67          a.g = 0;
68          b.g = -1;
69          Assert.assertEquals(0.0, or.g(s), 0);
70  
71          // test negative cases
72          a.g = -1;
73          b.g = -1;
74          Assert.assertTrue("negative", or.g(s) < 0);
75  
76          // test positive cases
77          a.g = 0;
78          b.g = 1;
79          Assert.assertTrue("positive", or.g(s) > 0);
80          a.g = 1;
81          b.g = -1;
82          Assert.assertTrue("positive", or.g(s) > 0);
83          a.g = 1;
84          b.g = 0;
85          Assert.assertTrue("positive", or.g(s) > 0);
86          a.g = -1;
87          b.g = 1;
88          Assert.assertTrue("positive", or.g(s) > 0);
89          a.g = 1;
90          b.g = 1;
91          Assert.assertTrue("positive", or.g(s) > 0);
92  
93      }
94  
95      /**
96       * check when there is numeric cancellation between the two g values.
97       */
98      @Test
99      public void testCancellation() {
100         a.g = -1e-10;
101         b.g = -1e10;
102         Assert.assertTrue("negative", or.g(s) < 0);
103         a.g = -1e10;
104         b.g = -1e-10;
105         Assert.assertTrue("negative", or.g(s) < 0);
106         a.g = -1e10;
107         b.g = 1e-10;
108         Assert.assertTrue("positive", or.g(s) > 0);
109         a.g = 1e-10;
110         b.g = -1e10;
111         Assert.assertTrue("positive", or.g(s) > 0);
112         a.g = 1e10;
113         b.g = -1e-10;
114         Assert.assertTrue("positive", or.g(s) > 0);
115         a.g = -1e-10;
116         b.g = 1e10;
117         Assert.assertTrue("positive", or.g(s) > 0);
118     }
119 
120     /**
121      * Check wrapped detectors are initialized.
122      */
123     @Test
124     public void testInit() {
125         // setup
126         EventDetector a = Mockito.mock(EventDetector.class);
127         EventDetector b = Mockito.mock(EventDetector.class);
128         @SuppressWarnings("unchecked")
129         EventHandler<EventDetector> c = Mockito.mock(EventHandler.class);
130         BooleanDetector or = BooleanDetector.orCombine(a, b).withHandler(c);
131         AbsoluteDate t = AbsoluteDate.CCSDS_EPOCH;
132         s = Mockito.mock(SpacecraftState.class);
133         Mockito.when(s.getDate()).thenReturn(t.shiftedBy(60.0));
134 
135         // action
136         or.init(s, t);
137 
138         // verify
139         Assert.assertEquals(2, or.getDetectors().size());
140         Mockito.verify(a).init(s, t);
141         Mockito.verify(b).init(s, t);
142         Mockito.verify(c).init(s, t, or);
143     }
144 
145     /** check when no operands are passed to the constructor. */
146     @Test
147     public void testZeroDetectors() {
148         // action
149         try {
150             BooleanDetector.orCombine(Collections.emptyList());
151             Assert.fail("Expected Exception");
152         } catch (NoSuchElementException e) {
153             // expected
154         }
155     }
156 
157     /** Mock detector to set the g function to arbitrary values. */
158     private static class MockDetector implements EventDetector {
159 
160         /** value to return from {@link #g(SpacecraftState)}. */
161         public double g = 0;
162 
163         @Override
164         public void init(SpacecraftState s0, AbsoluteDate t) {
165 
166         }
167 
168         @Override
169         public double g(SpacecraftState s) {
170             return this.g;
171         }
172 
173         @Override
174         public double getThreshold() {
175             return 0;
176         }
177 
178         @Override
179         public double getMaxCheckInterval() {
180             return 0;
181         }
182 
183         @Override
184         public int getMaxIterationCount() {
185             return 0;
186         }
187 
188         @Override
189         public Action eventOccurred(SpacecraftState s, boolean increasing) {
190             return null;
191         }
192 
193         @Override
194         public SpacecraftState resetState(SpacecraftState oldState) {
195             return null;
196         }
197     }
198 }