forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointTest.java
More file actions
117 lines (91 loc) · 3.15 KB
/
PointTest.java
File metadata and controls
117 lines (91 loc) · 3.15 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.thealgorithms.geometry;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class PointTest {
@Test
void testCompareTo() {
Point p1 = new Point(1, 2);
Point p2 = new Point(5, -1);
Point p3 = new Point(3, 9);
Point p4 = new Point(3, 9);
assertEquals(1, p1.compareTo(p2));
assertEquals(-1, p2.compareTo(p3));
assertEquals(0, p3.compareTo(p4));
}
@Test
void testToString() {
Point p = new Point(-3, 5);
assertEquals("(-3, 5)", p.toString());
}
@Test
void testPolarOrder() {
Point p = new Point(0, 0);
assertNotNull(p.polarOrder());
}
@Test
void testOrientation() {
// setup points
Point pA = new Point(0, 0);
Point pB = new Point(1, 0);
Point pC = new Point(1, 1);
// test for left curve
assertEquals(1, Point.orientation(pA, pB, pC));
// test for right curve
pB = new Point(0, 1);
assertEquals(-1, Point.orientation(pA, pB, pC));
// test for left curve
pC = new Point(-1, 1);
assertEquals(1, Point.orientation(pA, pB, pC));
// test for right curve
pB = new Point(1, 0);
pC = new Point(1, -1);
assertEquals(-1, Point.orientation(pA, pB, pC));
// test for collinearity
pB = new Point(1, 1);
pC = new Point(2, 2);
assertEquals(0, Point.orientation(pA, pB, pC));
}
@Test
void testPolarOrderCompare() {
Point ref = new Point(0, 0);
Point pA = new Point(1, 1);
Point pB = new Point(1, -1);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(3, 0);
pB = new Point(2, 0);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(0, 1);
pB = new Point(-1, 1);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(1, 1);
pB = new Point(2, 2);
assertEquals(0, ref.polarOrder().compare(pA, pB));
pA = new Point(1, 2);
pB = new Point(2, 1);
assertTrue(ref.polarOrder().compare(pA, pB) > 0);
pA = new Point(2, 1);
pB = new Point(1, 2);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(-1, 0);
pB = new Point(-2, 0);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(2, 3);
pB = new Point(2, 3);
assertEquals(0, ref.polarOrder().compare(pA, pB));
pA = new Point(0, 1);
pB = new Point(0, -1);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
ref = new Point(1, 1);
pA = new Point(1, 2);
pB = new Point(2, 2);
assertTrue(ref.polarOrder().compare(pA, pB) > 0);
pA = new Point(2, 1);
pB = new Point(2, 0);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
pA = new Point(0, 1);
pB = new Point(1, 0);
assertTrue(ref.polarOrder().compare(pA, pB) < 0);
}
}