Skip to content

Commit 157d6c8

Browse files
authored
Merge branch 'master' into my_algorithm
2 parents 49d7f98 + b45fd2a commit 157d6c8

File tree

3 files changed

+180
-9
lines changed

3 files changed

+180
-9
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@
10421042
- 📄 [GrahamScanTest](src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
10431043
- 📄 [MidpointCircleTest](src/test/java/com/thealgorithms/geometry/MidpointCircleTest.java)
10441044
- 📄 [MidpointEllipseTest](src/test/java/com/thealgorithms/geometry/MidpointEllipseTest.java)
1045+
- 📄 [PointTest](src/test/java/com/thealgorithms/geometry/PointTest.java)
10451046
- 📁 **graph**
10461047
- 📄 [ConstrainedShortestPathTest](src/test/java/com/thealgorithms/graph/ConstrainedShortestPathTest.java)
10471048
- 📄 [StronglyConnectedComponentOptimizedTest](src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,82 @@
1-
21
package com.thealgorithms.others;
32

43
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import org.junit.jupiter.api.Test;
77

88
public class CRCAlgorithmTest {
99

1010
@Test
11-
void test1() {
11+
void testNoErrorsWithZeroBER() {
1212
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 0.0);
13-
14-
// A bit-error rate of 0.0 should not provide any wrong messages
15-
c.changeMess();
13+
c.generateRandomMess();
1614
c.divideMessageWithP(false);
17-
assertEquals(c.getWrongMess(), 0);
15+
c.changeMess();
16+
c.divideMessageWithP(true);
17+
assertEquals(0, c.getWrongMess(), "BER=0 should produce no wrong messages");
18+
assertEquals(0, c.getWrongMessCaught(), "No errors, so no caught wrong messages");
19+
assertEquals(0, c.getWrongMessNotCaught(), "No errors, so no uncaught wrong messages");
20+
assertTrue(c.getCorrectMess() > 0, "Should have some correct messages");
1821
}
1922

2023
@Test
21-
void test2() {
24+
void testAllErrorsWithBEROne() {
2225
CRCAlgorithm c = new CRCAlgorithm("10010101010100101010010000001010010101010", 10, 1.0);
26+
c.generateRandomMess();
27+
c.divideMessageWithP(false);
28+
c.changeMess();
29+
c.divideMessageWithP(true);
30+
assertTrue(c.getWrongMess() > 0, "BER=1 should produce wrong messages");
31+
assertEquals(0, c.getCorrectMess(), "BER=1 should produce no correct messages");
32+
}
2333

24-
// A bit error rate of 1.0 should not provide any correct messages
34+
@Test
35+
void testIntermediateBER() {
36+
CRCAlgorithm c = new CRCAlgorithm("1101", 4, 0.5);
37+
c.generateRandomMess();
38+
for (int i = 0; i < 1000; i++) {
39+
c.refactor();
40+
c.generateRandomMess();
41+
c.divideMessageWithP(false);
42+
c.changeMess();
43+
c.divideMessageWithP(true);
44+
}
45+
assertTrue(c.getWrongMess() > 0, "Some wrong messages expected with BER=0.5");
46+
assertTrue(c.getWrongMessCaught() >= 0, "Wrong messages caught counter >= 0");
47+
assertTrue(c.getWrongMessNotCaught() >= 0, "Wrong messages not caught counter >= 0");
48+
assertTrue(c.getCorrectMess() >= 0, "Correct messages counter >= 0");
49+
assertEquals(c.getWrongMess(), c.getWrongMessCaught() + c.getWrongMessNotCaught(), "Sum of caught and not caught wrong messages should equal total wrong messages");
50+
}
51+
52+
@Test
53+
void testMessageChangedFlag() {
54+
CRCAlgorithm c = new CRCAlgorithm("1010", 4, 1.0);
55+
c.generateRandomMess();
56+
c.divideMessageWithP(false);
57+
c.changeMess();
58+
assertTrue(c.getWrongMess() > 0, "Message should be marked as changed with BER=1");
59+
}
60+
61+
@Test
62+
void testSmallMessageSize() {
63+
CRCAlgorithm c = new CRCAlgorithm("11", 2, 0.0);
64+
c.generateRandomMess();
65+
c.divideMessageWithP(false);
2566
c.changeMess();
67+
c.divideMessageWithP(true);
68+
assertEquals(0, c.getWrongMess(), "No errors expected for BER=0 with small message");
69+
}
70+
71+
@Test
72+
void testLargeMessageSize() {
73+
CRCAlgorithm c = new CRCAlgorithm("1101", 1000, 0.01);
74+
c.generateRandomMess();
2675
c.divideMessageWithP(false);
27-
assertEquals(c.getCorrectMess(), 0);
76+
c.changeMess();
77+
c.divideMessageWithP(true);
78+
// Just ensure counters are updated, no exceptions
79+
assertTrue(c.getWrongMess() >= 0);
80+
assertTrue(c.getCorrectMess() >= 0);
2881
}
2982
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy