epfl-archive/Alpano/tests/ch/epfl/alpano/Interval2DTest.java

228 lines
6.4 KiB
Java
Raw Normal View History

2022-04-07 18:43:21 +02:00
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<int[][], int[]> testValues = new HashMap<int[][], int[]>();
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<int[][], int[]>();
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<int[][], Integer> testValues = new HashMap<int[][], Integer>();
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<int[][][], Integer> testValues = new HashMap<int[][][], Integer>();
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<int[][][], int[][]> testValues = new HashMap<int[][][], int[][]>();
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<int[][][], Boolean> testValues = new HashMap<int[][][], Boolean>();
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<int[][][], int[][]> testValues = new HashMap<int[][][], int[][]>();
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<int[][][], Boolean> testValues = new HashMap<int[][][], Boolean>();
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<int[][], String> testValues = new HashMap<int[][], String>();
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()
);
}
}
}