package ch.epfl.alpano; import static ch.epfl.test.TestRandomizer.RANDOM_ITERATIONS; import static ch.epfl.test.TestRandomizer.newRandom; import static org.junit.Assert.*; import java.util.Random; import org.junit.Test; public class Interval1DTest { @Test(expected = IllegalArgumentException.class) public void testRandomIncludedFromAndToFails() { Random rng = newRandom(); int a,b; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(); b = rng.nextInt(); if(a>b){ new Interval1D(a,b); } } } @Test public void testRandomIncludedFromAndTo() { Random rng = newRandom(); Interval1D interval; int a,b; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(); b = rng.nextInt(); if(a<=b){ interval = new Interval1D(a,b); assertEquals(interval.includedFrom(),a,0); assertEquals(interval.includedTo(),b,0); assertEquals(interval.size(),(b-a+1),0); } } } @Test public void testLimitsIncludedFromAndTo() { Interval1D interval = new Interval1D(0,0); assertEquals(interval.includedFrom(),0,0); assertEquals(interval.includedTo(),0,0); assertEquals(interval.size(),1,0); } @Test public void testContainsTrue() { Random rng = newRandom(); Interval1D interval; int a,b,m; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(500); b = rng.nextInt(500); if(a<=b){ m = (b+a)/2; interval = new Interval1D(a,b); assertTrue(interval.contains(m)); } } } @Test public void testContainsFails() { Random rng = newRandom(); Interval1D interval; int a,b,c; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(500); b = rng.nextInt(500); c = rng.nextInt(500); if(a<=b && (cb)){ interval = new Interval1D(a,b); assertFalse(interval.contains(c)); } } } @Test public void testSizeOfIntersectionWith() { Interval1D interval1; Interval1D interval2; int num[] = { 1,1,0,2,1, 1,2,1,5,2, 1,4,0,2,2, 1,4,0,1,1, 2,4,0,1,0 }; for (int i = 0; i < num.length; i += 5) { interval1 = new Interval1D(num[i],num[i+1]); interval2 = new Interval1D(num[i+2],num[i+3]); assertEquals(interval1.sizeOfIntersectionWith(interval2), interval2.sizeOfIntersectionWith(interval1)); assertEquals(interval1.sizeOfIntersectionWith(interval2), num[i+4], 0); } } @Test public void testBoundingUnion() { Interval1D interval1; Interval1D interval2; Interval1D interval3; int num[] = { 1,1,0,2,0,2, 1,1,1,1,1,1, 0,0,0,0,0,0, 0,0,1,1,0,1, 0,1,0,1,0,1, 0,5,1,1,0,5, 0,4,1,5,0,5, 1,5,0,4,0,5, 1,4,0,5,0,5, 0,0,9,9,0,9 }; for (int i = 0; i < num.length; i += 6) { interval1 = new Interval1D(num[i],num[i+1]); interval2 = new Interval1D(num[i+2],num[i+3]); interval3 = new Interval1D(num[i+4],num[i+5]); assertTrue(interval1.boundingUnion(interval2).equals(interval3)); } } @Test public void testIsUnionableWithTrue() { Interval1D interval1; Interval1D interval2; int num[] = { 1,1,0,2, 1,2,1,5, 1,4,0,2, 1,4,0,1, 2,4,0,2, 0,0,0,1, 0,5,5,5, 0,1,1,1, -1,1,0,2 }; for (int i = 0; i < num.length; i += 4) { interval1 = new Interval1D(num[i],num[i+1]); interval2 = new Interval1D(num[i+2],num[i+3]); assertEquals(interval1.isUnionableWith(interval2), interval2.isUnionableWith(interval1)); assertTrue(interval1.isUnionableWith(interval2)); } } @Test public void testIsUnionableWithFalse() { Interval1D interval1; Interval1D interval2; int num[] = { 0,0,2,2, 0,0,2,3, 0,0,4,4, 0,4,6,8, 2,2,0,0, }; for (int i = 0; i < num.length; i += 4) { interval1 = new Interval1D(num[i],num[i+1]); interval2 = new Interval1D(num[i+2],num[i+3]); assertEquals(interval1.isUnionableWith(interval2), interval2.isUnionableWith(interval1)); assertFalse(interval1.isUnionableWith(interval2)); } } @Test public void testUnion() { Interval1D interval1; Interval1D interval2; String str; int num[] = { 1,1,0,2,0,2, 1,1,1,1,1,1, 0,0,0,0,0,0, 0,1,0,1,0,1, 0,1,0,1,0,1, 0,5,1,1,0,5, 0,4,1,5,0,5, 1,5,0,4,0,5, 1,4,0,5,0,5, }; for (int i = 0; i < num.length; i += 6) { interval1 = new Interval1D(num[i],num[i+1]); interval2 = new Interval1D(num[i+2],num[i+3]); str =("["+num[i+4]+".."+num[i+5]+"]"); assertEquals(str,interval1.union(interval2).toString()); } } @Test(expected = IllegalArgumentException.class) public void testUnionFails() { Interval1D interval1 = new Interval1D(0,0); Interval1D interval2 = new Interval1D(2,2); interval1.union(interval2); } @Test public void testEqualsObjectTrue() { Random rng = newRandom(); Interval1D interval1; Interval1D interval2; int a,b; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(500); b = rng.nextInt(500); if(a<=b){ interval1 = new Interval1D(a,b); interval2 = new Interval1D(a,b); assertTrue(interval1.equals(interval2)); assertEquals(interval1.equals(interval2), interval2.equals(interval1)); } } } @Test public void testEqualsObjectFalse() { Random rng = newRandom(); Interval1D interval1; String str = ""; int a,b; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(500); b = rng.nextInt(500); if(a<=b){ interval1 = new Interval1D(a,b); assertFalse(interval1.equals(str)); assertEquals(interval1.equals(str), str.equals(interval1)); } } } @Test public void testToString() { Random rng = newRandom(); Interval1D interval1; int a,b; for (int i = 0; i < RANDOM_ITERATIONS; ++i){ a = rng.nextInt(500); b = rng.nextInt(500); if(a<=b){ interval1 = new Interval1D(a,b); assertEquals(interval1.toString(),"["+a+".."+b+"]"); } } } }