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