1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
89 @Test(expected=MathIllegalArgumentException.class)
90 public void testStrictOrdering() {
91 new Interval(0, -1);
92 }
93 }