package ch.epfl.alpano; import static org.junit.Assert.*; import java.util.HashMap; import org.junit.Test; public class Interval2DTest { @Test(expected= NullPointerException.class) public void testInterval2D() { new Interval2D(null, null); } @Test public void testContains() { HashMap testValues = new HashMap(); testValues.put(new int[][] { {0,4}, {0,4} }, new int[] {2,4}); testValues.put(new int[][] { {0,0}, {0,0} }, new int[] {0,0}); for (int[][] values : testValues.keySet()){ assertEquals ( true , new Interval2D( new Interval1D(values[0][0], values[0][1]), new Interval1D(values[1][0], values[1][1])).contains(testValues.get(values)[0], testValues.get(values)[1]) ); } testValues = new HashMap(); testValues.put(new int[][] { {0,4}, {0,4} }, new int[] {-1,-1}); testValues.put(new int[][] { {0,0}, {0,0} }, new int[] {0,1}); for (int[][] values : testValues.keySet()){ assertEquals ( false, new Interval2D( new Interval1D(values[0][0], values[0][1]), new Interval1D(values[1][0], values[1][1])).contains(testValues.get(values)[0], testValues.get(values)[1]) ); } } @Test public void testSize() { HashMap testValues = new HashMap(); testValues.put(new int[][] { {0,4}, {0,4} }, 25); testValues.put(new int[][] { {0,0}, {0,0} }, 1); for (int[][] values : testValues.keySet()){ assertEquals ( (int)testValues.get(values), new Interval2D( new Interval1D(values[0][0], values[0][1]), new Interval1D(values[1][0], values[1][1])).size() ); } } @Test public void testSizeOfIntersectionWith() { HashMap testValues = new HashMap(); testValues.put( new int[][][] { {{0,5}, {0,5}} , {{4,6},{4,6}} } , 4); testValues.put( new int[][][] { {{0,0}, {0,0}} , {{-40,40},{-40,40}} } , 1); testValues.put( new int[][][] { {{0,5}, {0,5}} , {{10,15},{10,15}} } , 0); for (int[][][] values : testValues.keySet()){ assertEquals ( (int)testValues.get(values), new Interval2D( new Interval1D(values[0][0][0], values[0][0][1]), new Interval1D(values[0][1][0], values[0][1][1])).sizeOfIntersectionWith( new Interval2D( new Interval1D(values[1][0][0], values[1][0][1]), new Interval1D(values[1][1][0], values[1][1][1]))) ); }; } @Test public void testBoundingUnion() { HashMap testValues = new HashMap(); testValues.put( new int[][][] {{{0,5}, {0,5}} , {{4,6},{4,6}} } , new int[][] {{0,6},{0,6}} ); testValues.put( new int[][][] {{{0,0}, {0,0}} , {{4,6},{4,6}} } , new int[][] {{0,6},{0,6}} ); for (int[][][] values : testValues.keySet()){ assertEquals (true, new Interval2D( new Interval1D(values[0][0][0], values[0][0][1]), new Interval1D(values[0][1][0], values[0][1][1])).boundingUnion( new Interval2D( new Interval1D(values[1][0][0], values[1][0][1]), new Interval1D(values[1][1][0], values[1][1][1]))).equals( new Interval2D( new Interval1D(testValues.get(values)[0][0], testValues.get(values)[0][1]), new Interval1D(testValues.get(values)[1][0], testValues.get(values)[1][1])) )); }; } @Test public void testIsUnionableWith() { HashMap testValues = new HashMap(); testValues.put( new int[][][] {{{0,5}, {0,5}} , {{4,6},{4,6}} } ,false); testValues.put( new int[][][] {{{0,0}, {0,0}} , {{1,6},{1,6}} } , false ); for (int[][][] values : testValues.keySet()){ assertEquals ( true, testValues.get(values) == new Interval2D( new Interval1D(values[0][0][0], values[0][0][1]), new Interval1D(values[0][1][0], values[0][1][1])).isUnionableWith( new Interval2D( new Interval1D(values[1][0][0], values[1][0][1]), new Interval1D(values[1][1][0], values[1][1][1])))); }; } @Test(expected = IllegalArgumentException.class) public void testUnion() { HashMap testValues = new HashMap(); testValues.put( new int[][][] {{{0,5}, {0,5}} , {{4,6},{4,6}} } , new int[][] {{4,5},{4,5}} ); testValues.put( new int[][][] {{{-100,-100}, {0,0}} , {{0,0},{100,100}} } , new int[][] {{0,0},{0,0}} ); testValues.put( new int[][][] {{{-100,100}, {0,0}} , {{0,0},{-100,100}} } , new int[][] {{0,0},{0,0}} ); for (int[][][] values : testValues.keySet()){ assertEquals (false, new Interval2D( new Interval1D(values[0][0][0], values[0][0][1]), new Interval1D(values[0][1][0], values[0][1][1])).union( new Interval2D( new Interval1D(values[1][0][0], values[1][0][1]), new Interval1D(values[1][1][0], values[1][1][1]))).equals( new Interval2D( new Interval1D(testValues.get(values)[0][0], testValues.get(values)[0][1]), new Interval1D(testValues.get(values)[1][0], testValues.get(values)[1][1])) )); }; new Interval2D( new Interval1D(0, 0), new Interval1D(0, 0)).union( new Interval2D( new Interval1D(1, 1), new Interval1D(1, 1))); } @Test public void testEqualsObject() { HashMap testValues = new HashMap(); testValues.put( new int[][][] {{{0,5}, {0,5}} , {{0,5},{0,5}} } ,true ); testValues.put( new int[][][] {{{0,5}, {0,5}} , {{0,5},{0,6}} } , false ); for (int[][][] values : testValues.keySet()){ assertEquals ( testValues.get(values) , new Interval2D( new Interval1D(values[0][0][0], values[0][0][1]), new Interval1D(values[0][1][0], values[0][1][1])).equals( new Interval2D( new Interval1D(values[1][0][0], values[1][0][1]), new Interval1D(values[1][1][0], values[1][1][1]))) ); }; } @Test public void testToString() { HashMap testValues = new HashMap(); testValues.put(new int[][] { {0,4}, {0,4} }, "[0..4]x[0..4]"); for (int[][] values : testValues.keySet()){ assertEquals (testValues.get(values), new Interval2D( new Interval1D(values[0][0], values[0][1]), new Interval1D(values[1][0], values[1][1])).toString() ); } } }