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.propagation.events;
18  
19  import org.hamcrest.CoreMatchers;
20  import org.hamcrest.MatcherAssert;
21  import org.hipparchus.CalculusFieldElement;
22  import org.hipparchus.Field;
23  import org.hipparchus.util.Binary64;
24  import org.hipparchus.util.Binary64Field;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  import org.mockito.Mockito;
28  import org.orekit.propagation.FieldSpacecraftState;
29  import org.orekit.time.FieldAbsoluteDate;
30  
31  /**
32   * Unit tests for {@link FieldNegateDetector}.
33   *
34   * @author Evan Ward
35   */
36  class FieldNegateDetectorTest {
37  
38      @Test
39      void testGetDetector() {
40          // GIVEN
41          final FieldDateDetector<Binary64> expectedDetector = new FieldDateDetector<>(FieldAbsoluteDate.getArbitraryEpoch(Binary64Field.getInstance()));
42          
43          // WHEN
44          final FieldNegateDetector<Binary64> negateDetector = new FieldNegateDetector<>(expectedDetector);
45          
46          // THEN
47          Assertions.assertEquals(expectedDetector, negateDetector.getDetector());
48          Assertions.assertEquals(expectedDetector, negateDetector.getOriginal());
49      }
50  
51      /**
52       * check g function is negated.
53       */
54      @Test
55      void testG() {
56          doTestG(Binary64Field.getInstance());
57      }
58  
59      private <T extends CalculusFieldElement<T>> void doTestG(final Field<T> field) {
60          //setup
61          @SuppressWarnings("unchecked")
62          FieldEventDetector<T> a = Mockito.mock(FieldEventDetector.class);
63          Mockito.when(a.getDetectionSettings()).thenReturn(new FieldEventDetectionSettings<>(field,
64                  EventDetectionSettings.getDefaultEventDetectionSettings()));
65          FieldNegateDetector<T> detector = new FieldNegateDetector<>(a);
66          @SuppressWarnings("unchecked")
67          FieldSpacecraftState<T> s = Mockito.mock(FieldSpacecraftState.class);
68  
69          // verify + to -
70          Mockito.when(a.g(s)).thenReturn(field.getZero().newInstance(1.0));
71          MatcherAssert.assertThat(detector.g(s).getReal(), CoreMatchers.is(-1.0));
72          // verify - to +
73          Mockito.when(a.g(s)).thenReturn(field.getZero().newInstance(-1.0));
74          MatcherAssert.assertThat(detector.g(s).getReal(), CoreMatchers.is(1.0));
75      }
76  
77      /** Check a with___ method. */
78      @Test
79      void testCreate() {
80          doTestCreate(Binary64Field.getInstance());
81      }
82  
83      private <T extends CalculusFieldElement<T>> void doTestCreate(final Field<T> field) {
84          //setup
85          @SuppressWarnings("unchecked")
86          FieldEventDetector<T> a = Mockito.mock(FieldEventDetector.class);
87          Mockito.when(a.getDetectionSettings()).thenReturn(new FieldEventDetectionSettings<>(field,
88                  EventDetectionSettings.getDefaultEventDetectionSettings()));
89          FieldNegateDetector<T> detector = new FieldNegateDetector<>(a);
90  
91          // action
92          FieldNegateDetector<T> actual = detector.withMaxCheck(100);
93  
94          //verify
95          MatcherAssert.assertThat(actual.getMaxCheckInterval().currentInterval(null, true), CoreMatchers.is(100.0));
96          Assertions.assertTrue(actual.getOriginal() == a);
97      }
98  }