-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangleTest.java
More file actions
82 lines (70 loc) · 3.53 KB
/
RectangleTest.java
File metadata and controls
82 lines (70 loc) · 3.53 KB
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
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class RectangleTest {
Rectangle r1;
Rectangle r2;
Rectangle r3;
Rectangle r4;
Rectangle r5;
@Before
public void setUp() throws Exception {
r1 = new Rectangle(new Point(0,0), new Point(0,8), new Point(6,8), new Point(6,0));
r2 = new Rectangle(new Point(2,2), new Point(2,4), new Point(4,4), new Point(4,2));
r3 = new Rectangle(new Point(1,0), new Point(0,2), new Point(4,4), new Point(5,2));
r4 = new Rectangle(new Point(4,1), new Point(1,4), new Point(4,7), new Point(7,4));
r5 = new Rectangle(new Point(0,0), new Point(7,8));
}
@Test
public void testPointInside() throws Exception {
assertTrue(r1.containsPoint(new Point(1, 1)));
assertFalse(r1.containsPoint(new Point(-1, 1)));
assertTrue(r1.containsPoint(new Point(6, 8)));
assertTrue(r4.containsPoint(new Point(6, 4)));
}
@Test
public void testToString() throws Exception{
assertEquals(r1.toString(),"[ ( 0.0, 0.0 ) -> ( 0.0, 8.0 ) ][ ( 0.0, 8.0 ) -> ( 6.0, 8.0 ) ][ ( 6.0, 8.0 ) -> ( 6.0, 0.0 ) ][ ( 6.0, 0.0 ) -> ( 0.0, 0.0 ) ]");
}
@Test
public void testTwoPointInitializer() throws Exception{
assertEquals(r1.toString(), new Rectangle(new Point(0,0), new Point(6,8)).toString());
}
@Test
public void testContainsRectangle() throws Exception {
assertTrue(r1.containsRectangle(r2));
assertFalse(r2.containsRectangle(r4));
assertFalse(r1.containsRectangle(r4));
assertTrue(r1.containsRectangle(r1));
}
@Test
public void testIsAdjacent() throws Exception{
assertTrue(r1.isAdjacent(new Rectangle(new Point(1,0), new Point(2,2))));
assertFalse(r1.isAdjacent(new Rectangle(new Point(6,10), new Point(12,23))));
assertTrue(r1.isAdjacent(new Rectangle(new Point(6,8), new Point(12,23))));
assertTrue(r1.isAdjacent(r1));
assertTrue(r1.isAdjacent(new Rectangle(new Point(-2,0), new Point(3,4))));
assertTrue(r1.isAdjacent(new Rectangle(new Point(6,2), new Point(6,23))));
assertTrue(r1.isAdjacent(new Rectangle(new Point(6,2), new Point(6,-23))));
assertTrue(r1.isAdjacent(new Rectangle(new Point(6,4), new Point(6,5))));
}
public boolean intersectionHelper(Rectangle r1, Rectangle r2, Point... expected){
List<Point> points = Arrays.asList(expected);
List<Point> result = r1.getIntersectionPoints(r2);
return points.containsAll(result) && result.containsAll(points);
}
@Test
public void testGetIntersectionPoints() throws Exception{
assertTrue(intersectionHelper(r1, new Rectangle(new Point(2,-1), new Point(4,2)), new Point(2,0), new Point(4,0)));
assertTrue(intersectionHelper(r1,r2));
assertTrue(intersectionHelper(r1,r4,new Point(6,3), new Point(6,5)));
assertTrue(intersectionHelper(r1,r4,new Point(6,3), new Point(6,5)));
assertTrue(intersectionHelper(r1,r3,new Point(1,0), new Point(0,2)));
assertTrue(intersectionHelper(r3,r4, new Point(5,2), new Point(2,3), new Point(11.0/3, 4.0/3)));
assertTrue(intersectionHelper(r3,r2, new Point(2,3), new Point(4,4)));
assertTrue(intersectionHelper(r4,r2, new Point(3,2), new Point(2,3)));
assertTrue(intersectionHelper(r1,r5, new Point(0,8), new Point(0,0), new Point(6,8), new Point(6,0)));
}
}