Skip to content

Commit e2023a8

Browse files
author
Aaron
committed
test: added Tests for Point.java
1 parent 25aaa6e commit e2023a8

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.thealgorithms.geometry;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class PointTest {
8+
9+
@Test
10+
void testCompareTo() {
11+
Point p1 = new Point (1, 2);
12+
Point p2 = new Point(5, -1);
13+
Point p3 = new Point(3, 9);
14+
Point p4 = new Point(3, 9);
15+
assertEquals(1,p1.compareTo(p2));
16+
assertEquals(-1,p2.compareTo(p3));
17+
assertEquals(0,p3.compareTo(p4));
18+
}
19+
20+
@Test
21+
void testToString() {
22+
Point p1 = new Point (-3, 5);
23+
assertEquals("(-3, 5)",p1.toString());
24+
}
25+
26+
27+
@Test
28+
void testPolarOrder() {
29+
Point p = new Point(0,0);
30+
assertNotNull(p.polarOrder());
31+
}
32+
33+
@Test
34+
void testOrientation() {
35+
// setup points
36+
Point pA = new Point(0, 0);
37+
Point pB = new Point(1, 0);
38+
Point pC = new Point(1, 1);
39+
40+
// test for left curve
41+
assertEquals(1, Point.orientation(pA, pB, pC));
42+
43+
//test for right curve
44+
pB = new Point(0, 1);
45+
assertEquals(-1, Point.orientation(pA, pB, pC));
46+
47+
//test for left curve
48+
pC = new Point(-1, 1);
49+
assertEquals(1, Point.orientation(pA, pB, pC));
50+
51+
//test for right curve
52+
pB = new Point(1,0);
53+
pC = new Point(1,-1);
54+
assertEquals(-1, Point.orientation(pA, pB, pC));
55+
56+
//test for collinearity
57+
pB = new Point(1, 1);
58+
pC = new Point(2, 2);
59+
assertEquals(0, Point.orientation(pA, pB, pC));
60+
}
61+
62+
@Test
63+
void testPolarOrderCompare() {
64+
Point ref = new Point(0,0);
65+
66+
Point p1 = new Point(1,1);
67+
Point p2 = new Point(1,-1);
68+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
69+
70+
p1 = new Point(3,0);
71+
p2 = new Point(2,0);
72+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
73+
74+
p1 = new Point(0,1);
75+
p2 = new Point(-1,1);
76+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
77+
78+
p1 = new Point(1,1);
79+
p2 = new Point(2,2);
80+
assertEquals(0, ref.polarOrder().compare(p1, p2));
81+
82+
p1 = new Point(1,2);
83+
p2 = new Point(2,1);
84+
assertTrue(ref.polarOrder().compare(p1, p2) > 0);
85+
86+
p1 = new Point(2,1);
87+
p2 = new Point(1,2);
88+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
89+
90+
p1 = new Point(-1,0);
91+
p2 = new Point(-2,0);
92+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
93+
94+
p1 = new Point(2,3);
95+
p2 = new Point(2,3);
96+
assertEquals(0, ref.polarOrder().compare(p1, p2));
97+
98+
p1 = new Point(0,1);
99+
p2 = new Point(0,-1);
100+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
101+
102+
ref = new Point(1,1);
103+
104+
p1 = new Point(1,2);
105+
p2 = new Point(2,2);
106+
assertTrue(ref.polarOrder().compare(p1, p2) > 0);
107+
108+
p1 = new Point(2,1);
109+
p2 = new Point(2,0);
110+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
111+
112+
p1 = new Point(0,1);
113+
p2 = new Point(1,0);
114+
assertTrue(ref.polarOrder().compare(p1, p2) < 0);
115+
}
116+
}

0 commit comments

Comments
 (0)