summaryrefslogtreecommitdiff
path: root/tests/com/google/common/geometry/R1IntervalTest.java
blob: 0fe87ea8bf28d07d493f677bfe05a7e80e974eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2005 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.common.geometry;

public strictfp class R1IntervalTest extends GeometryTestCase {


  /**
   * Test all of the interval operations on the given pair of intervals.
   * "expected_relation" is a sequence of "T" and "F" characters corresponding
   * to the expected results of contains(), interiorContains(), Intersects(),
   * and InteriorIntersects() respectively.
   */
  public void testIntervalOps(R1Interval x, R1Interval y, String expectedRelation) {
    assertEquals(x.contains(y), expectedRelation.charAt(0) == 'T');
    assertEquals(x.interiorContains(y), expectedRelation.charAt(1) == 'T');
    assertEquals(x.intersects(y), expectedRelation.charAt(2) == 'T');
    assertEquals(x.interiorIntersects(y), expectedRelation.charAt(3) == 'T');

    assertEquals(x.contains(y), x.union(y).equals(x));
    assertEquals(x.intersects(y), !x.intersection(y).isEmpty());
  }

  public void testBasic() {
    // Constructors and accessors.
    R1Interval unit = new R1Interval(0, 1);
    R1Interval negunit = new R1Interval(-1, 0);
    assertEquals(unit.lo(), 0.0);
    assertEquals(unit.hi(), 1.0);
    assertEquals(negunit.bound(0), -1.0);
    assertEquals(negunit.bound(1), 0.0);
    R1Interval ten = new R1Interval(0, 0);
    ten.setHi(10);
    assertEquals(ten.hi(), 10.0);

    // is_empty()
    R1Interval half = new R1Interval(0.5, 0.5);
    assertTrue(!unit.isEmpty());
    assertTrue(!half.isEmpty());
    R1Interval empty = R1Interval.empty();
    assertTrue(empty.isEmpty());

    // GetCenter(), GetLength()
    assertEquals(unit.getCenter(), 0.5);
    assertEquals(half.getCenter(), 0.5);
    assertEquals(negunit.getLength(), 1.0);
    assertEquals(half.getLength(), 0.0);
    assertTrue(empty.getLength() < 0);

    // contains(double), interiorContains(double)
    assertTrue(unit.contains(0.5));
    assertTrue(unit.interiorContains(0.5));
    assertTrue(unit.contains(0));
    assertTrue(!unit.interiorContains(0));
    assertTrue(unit.contains(1));
    assertTrue(!unit.interiorContains(1));

    // contains(R1Interval), interiorContains(R1Interval)
    // Intersects(R1Interval), InteriorIntersects(R1Interval)
    testIntervalOps(empty, empty, "TTFF");
    testIntervalOps(empty, unit, "FFFF");
    testIntervalOps(unit, half, "TTTT");
    testIntervalOps(unit, unit, "TFTT");
    testIntervalOps(unit, empty, "TTFF");
    testIntervalOps(unit, negunit, "FFTF");
    testIntervalOps(unit, new R1Interval(0, 0.5), "TFTT");
    testIntervalOps(half, new R1Interval(0, 0.5), "FFTF");

    // addPoint()
    R1Interval r;
    r = empty.addPoint(5);
    assertTrue(r.lo() == 5.0 && r.hi() == 5.0);
    r = r.addPoint(-1);
    assertTrue(r.lo() == -1.0 && r.hi() == 5.0);
    r = r.addPoint(0);
    assertTrue(r.lo() == -1.0 && r.hi() == 5.0);

    // fromPointPair()
    assertEquals(R1Interval.fromPointPair(4, 4), new R1Interval(4, 4));
    assertEquals(R1Interval.fromPointPair(-1, -2), new R1Interval(-2, -1));
    assertEquals(R1Interval.fromPointPair(-5, 3), new R1Interval(-5, 3));

    // expanded()
    assertEquals(empty.expanded(0.45), empty);
    assertEquals(unit.expanded(0.5), new R1Interval(-0.5, 1.5));

    // union(), intersection()
    assertTrue(new R1Interval(99, 100).union(empty).equals(new R1Interval(99, 100)));
    assertTrue(empty.union(new R1Interval(99, 100)).equals(new R1Interval(99, 100)));
    assertTrue(new R1Interval(5, 3).union(new R1Interval(0, -2)).isEmpty());
    assertTrue(new R1Interval(0, -2).union(new R1Interval(5, 3)).isEmpty());
    assertTrue(unit.union(unit).equals(unit));
    assertTrue(unit.union(negunit).equals(new R1Interval(-1, 1)));
    assertTrue(negunit.union(unit).equals(new R1Interval(-1, 1)));
    assertTrue(half.union(unit).equals(unit));
    assertTrue(unit.intersection(half).equals(half));
    assertTrue(unit.intersection(negunit).equals(new R1Interval(0, 0)));
    assertTrue(negunit.intersection(half).isEmpty());
    assertTrue(unit.intersection(empty).isEmpty());
    assertTrue(empty.intersection(unit).isEmpty());
  }
}