1 /* 2 * Copyright 2005 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.common.geometry; 17 18 public strictfp class R1IntervalTest extends GeometryTestCase { 19 20 21 /** 22 * Test all of the interval operations on the given pair of intervals. 23 * "expected_relation" is a sequence of "T" and "F" characters corresponding 24 * to the expected results of contains(), interiorContains(), Intersects(), 25 * and InteriorIntersects() respectively. 26 */ testIntervalOps(R1Interval x, R1Interval y, String expectedRelation)27 public void testIntervalOps(R1Interval x, R1Interval y, String expectedRelation) { 28 assertEquals(x.contains(y), expectedRelation.charAt(0) == 'T'); 29 assertEquals(x.interiorContains(y), expectedRelation.charAt(1) == 'T'); 30 assertEquals(x.intersects(y), expectedRelation.charAt(2) == 'T'); 31 assertEquals(x.interiorIntersects(y), expectedRelation.charAt(3) == 'T'); 32 33 assertEquals(x.contains(y), x.union(y).equals(x)); 34 assertEquals(x.intersects(y), !x.intersection(y).isEmpty()); 35 } 36 testBasic()37 public void testBasic() { 38 // Constructors and accessors. 39 R1Interval unit = new R1Interval(0, 1); 40 R1Interval negunit = new R1Interval(-1, 0); 41 assertEquals(unit.lo(), 0.0); 42 assertEquals(unit.hi(), 1.0); 43 assertEquals(negunit.lo(), -1.0); 44 assertEquals(negunit.hi(), 0.0); 45 46 // is_empty() 47 R1Interval half = new R1Interval(0.5, 0.5); 48 assertTrue(!unit.isEmpty()); 49 assertTrue(!half.isEmpty()); 50 R1Interval empty = R1Interval.empty(); 51 assertTrue(empty.isEmpty()); 52 53 // GetCenter(), GetLength() 54 assertEquals(unit.getCenter(), 0.5); 55 assertEquals(half.getCenter(), 0.5); 56 assertEquals(negunit.getLength(), 1.0); 57 assertEquals(half.getLength(), 0.0); 58 assertTrue(empty.getLength() < 0); 59 60 // contains(double), interiorContains(double) 61 assertTrue(unit.contains(0.5)); 62 assertTrue(unit.interiorContains(0.5)); 63 assertTrue(unit.contains(0)); 64 assertTrue(!unit.interiorContains(0)); 65 assertTrue(unit.contains(1)); 66 assertTrue(!unit.interiorContains(1)); 67 68 // contains(R1Interval), interiorContains(R1Interval) 69 // Intersects(R1Interval), InteriorIntersects(R1Interval) 70 testIntervalOps(empty, empty, "TTFF"); 71 testIntervalOps(empty, unit, "FFFF"); 72 testIntervalOps(unit, half, "TTTT"); 73 testIntervalOps(unit, unit, "TFTT"); 74 testIntervalOps(unit, empty, "TTFF"); 75 testIntervalOps(unit, negunit, "FFTF"); 76 testIntervalOps(unit, new R1Interval(0, 0.5), "TFTT"); 77 testIntervalOps(half, new R1Interval(0, 0.5), "FFTF"); 78 79 // addPoint() 80 R1Interval r; 81 r = empty.addPoint(5); 82 assertTrue(r.lo() == 5.0 && r.hi() == 5.0); 83 r = r.addPoint(-1); 84 assertTrue(r.lo() == -1.0 && r.hi() == 5.0); 85 r = r.addPoint(0); 86 assertTrue(r.lo() == -1.0 && r.hi() == 5.0); 87 88 // fromPointPair() 89 assertEquals(R1Interval.fromPointPair(4, 4), new R1Interval(4, 4)); 90 assertEquals(R1Interval.fromPointPair(-1, -2), new R1Interval(-2, -1)); 91 assertEquals(R1Interval.fromPointPair(-5, 3), new R1Interval(-5, 3)); 92 93 // expanded() 94 assertEquals(empty.expanded(0.45), empty); 95 assertEquals(unit.expanded(0.5), new R1Interval(-0.5, 1.5)); 96 97 // union(), intersection() 98 assertTrue(new R1Interval(99, 100).union(empty).equals(new R1Interval(99, 100))); 99 assertTrue(empty.union(new R1Interval(99, 100)).equals(new R1Interval(99, 100))); 100 assertTrue(new R1Interval(5, 3).union(new R1Interval(0, -2)).isEmpty()); 101 assertTrue(new R1Interval(0, -2).union(new R1Interval(5, 3)).isEmpty()); 102 assertTrue(unit.union(unit).equals(unit)); 103 assertTrue(unit.union(negunit).equals(new R1Interval(-1, 1))); 104 assertTrue(negunit.union(unit).equals(new R1Interval(-1, 1))); 105 assertTrue(half.union(unit).equals(unit)); 106 assertTrue(unit.intersection(half).equals(half)); 107 assertTrue(unit.intersection(negunit).equals(new R1Interval(0, 0))); 108 assertTrue(negunit.intersection(half).isEmpty()); 109 assertTrue(unit.intersection(empty).isEmpty()); 110 assertTrue(empty.intersection(unit).isEmpty()); 111 } 112 } 113