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.Assert;
29  import org.junit.Test;
30  
31  public class IntervalTest {
32  
33      @Test
34      public void testInterval() {
35          Interval interval = new Interval(2.3, 5.7);
36          Assert.assertEquals(3.4, interval.getSize(), 1.0e-10);
37          Assert.assertEquals(4.0, interval.getBarycenter(), 1.0e-10);
38          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(2.3, 1.0e-10));
39          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(5.7, 1.0e-10));
40          Assert.assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(1.2, 1.0e-10));
41          Assert.assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.7, 1.0e-10));
42          Assert.assertEquals(Region.Location.INSIDE,   interval.checkPoint(3.0, 1.0e-10));
43          Assert.assertEquals(2.3, interval.getInf(), 1.0e-10);
44          Assert.assertEquals(5.7, interval.getSup(), 1.0e-10);
45      }
46  
47      @Test
48      public void testTolerance() {
49          Interval interval = new Interval(2.3, 5.7);
50          Assert.assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(1.2, 1.0));
51          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(1.2, 1.2));
52          Assert.assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.7, 2.9));
53          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(8.7, 3.1));
54          Assert.assertEquals(Region.Location.INSIDE,   interval.checkPoint(3.0, 0.6));
55          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(3.0, 0.8));
56      }
57  
58      @Test
59      public void testInfinite() {
60          Interval interval = new Interval(9.0, Double.POSITIVE_INFINITY);
61          Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(9.0, 1.0e-10));
62          Assert.assertEquals(Region.Location.OUTSIDE,  interval.checkPoint(8.4, 1.0e-10));
63          for (double e = 1.0; e <= 6.0; e += 1.0) {
64              Assert.assertEquals(Region.Location.INSIDE,
65                                  interval.checkPoint(FastMath.pow(10.0, e), 1.0e-10));
66          }
67          Assert.assertTrue(Double.isInfinite(interval.getSize()));
68          Assert.assertEquals(9.0, interval.getInf(), 1.0e-10);
69          Assert.assertTrue(Double.isInfinite(interval.getSup()));
70  
71      }
72  
73      @Test
74      public void testWholeLine() {
75          Interval interval = new Interval(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
76          Assert.assertTrue(Double.isInfinite(interval.getSize()));
77          Assert.assertEquals(Region.Location.INSIDE, interval.checkPoint(-Double.MAX_VALUE, 1.0e-10));
78          Assert.assertEquals(Region.Location.INSIDE, interval.checkPoint(+Double.MAX_VALUE, 1.0e-10));
79      }
80  
81      @Test
82      public void testSinglePoint() {
83          Interval interval = new Interval(1.0, 1.0);
84          Assert.assertEquals(0.0, interval.getSize(), Precision.SAFE_MIN);
85          Assert.assertEquals(1.0, interval.getBarycenter(), Precision.EPSILON);
86      }
87  
88      // MATH-1256
89      @Test(expected=MathIllegalArgumentException.class)
90      public void testStrictOrdering() {
91          new Interval(0, -1);
92      }
93  }