View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) 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 ASF 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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.euclidean.oned;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.geometry.partitioning.Region;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.Precision;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  class IntervalTest {
35  
36      @Test
37      void testInterval() {
38          Interval interval = new Interval(2.3, 5.7);
39          assertEquals(3.4, interval.getSize(), 1.0e-10);
40          assertEquals(4.0, interval.getBarycenter(), 1.0e-10);
41          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(2.3, 1.0e-10));
42          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(5.7, 1.0e-10));
43          assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(1.2, 1.0e-10));
44          assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.7, 1.0e-10));
45          assertEquals(Region.Location.INSIDE,   interval.checkPoint(3.0, 1.0e-10));
46          assertEquals(2.3, interval.getInf(), 1.0e-10);
47          assertEquals(5.7, interval.getSup(), 1.0e-10);
48      }
49  
50      @Test
51      void testTolerance() {
52          Interval interval = new Interval(2.3, 5.7);
53          assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(1.2, 1.0));
54          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(1.2, 1.2));
55          assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.7, 2.9));
56          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(8.7, 3.1));
57          assertEquals(Region.Location.INSIDE,   interval.checkPoint(3.0, 0.6));
58          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(3.0, 0.8));
59      }
60  
61      @Test
62      void testInfinite() {
63          Interval interval = new Interval(9.0, Double.POSITIVE_INFINITY);
64          assertEquals(Region.Location.BOUNDARY, interval.checkPoint(9.0, 1.0e-10));
65          assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.4, 1.0e-10));
66          for (double e = 1.0; e <= 6.0; e += 1.0) {
67              assertEquals(Region.Location.INSIDE,
68                                  interval.checkPoint(FastMath.pow(10.0, e), 1.0e-10));
69          }
70          assertTrue(Double.isInfinite(interval.getSize()));
71          assertEquals(9.0, interval.getInf(), 1.0e-10);
72          assertTrue(Double.isInfinite(interval.getSup()));
73  
74      }
75  
76      @Test
77      void testWholeLine() {
78          Interval interval = new Interval(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
79          assertTrue(Double.isInfinite(interval.getSize()));
80          assertEquals(Region.Location.INSIDE, interval.checkPoint(-Double.MAX_VALUE, 1.0e-10));
81          assertEquals(Region.Location.INSIDE, interval.checkPoint(+Double.MAX_VALUE, 1.0e-10));
82      }
83  
84      @Test
85      void testSinglePoint() {
86          Interval interval = new Interval(1.0, 1.0);
87          assertEquals(0.0, interval.getSize(), Precision.SAFE_MIN);
88          assertEquals(1.0, interval.getBarycenter(), Precision.EPSILON);
89      }
90  
91      // MATH-1256
92      @Test
93      void testStrictOrdering() {
94          assertThrows(MathIllegalArgumentException.class, () -> {
95              new Interval(0, -1);
96          });
97      }
98  }