Skip to content

Commit 115a7db

Browse files
Merge branch 'master' into test/CountingInversionsTest
2 parents d863cb6 + 44c572b commit 115a7db

File tree

7 files changed

+271
-11
lines changed

7 files changed

+271
-11
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
<plugin>
129129
<groupId>com.mebigfatguy.fb-contrib</groupId>
130130
<artifactId>fb-contrib</artifactId>
131-
<version>7.6.11</version>
131+
<version>7.6.12</version>
132132
</plugin>
133133
<plugin>
134134
<groupId>com.h3xstream.findsecbugs</groupId>

src/test/java/com/thealgorithms/bitmanipulation/ParityCheckTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,30 @@
66
import org.junit.jupiter.api.Test;
77

88
public class ParityCheckTest {
9+
@Test
10+
public void testIsEvenParity() {
11+
assertTrue(ParityCheck.checkParity(0)); // 0 -> 0 ones
12+
assertTrue(ParityCheck.checkParity(3)); // 11 -> 2 ones
13+
assertTrue(ParityCheck.checkParity(5)); // 101 -> 2 ones
14+
assertTrue(ParityCheck.checkParity(10)); // 1010 -> 2 ones
15+
assertTrue(ParityCheck.checkParity(15)); // 1111 -> 4 ones
16+
assertTrue(ParityCheck.checkParity(1023)); // 10 ones
17+
}
18+
919
@Test
1020
public void testIsOddParity() {
11-
assertTrue(ParityCheck.checkParity(5)); // 101 has 2 ones (even parity)
12-
assertFalse(ParityCheck.checkParity(7)); // 111 has 3 ones (odd parity)
13-
assertFalse(ParityCheck.checkParity(8)); // 1000 has 1 one (odd parity)
21+
assertFalse(ParityCheck.checkParity(1)); // 1 -> 1 one
22+
assertFalse(ParityCheck.checkParity(2)); // 10 -> 1 one
23+
assertFalse(ParityCheck.checkParity(7)); // 111 -> 3 ones
24+
assertFalse(ParityCheck.checkParity(8)); // 1000 -> 1 one
25+
assertFalse(ParityCheck.checkParity(11)); // 1011 -> 3 ones
26+
assertFalse(ParityCheck.checkParity(31)); // 11111 -> 5 ones
27+
}
28+
29+
@Test
30+
public void testLargeNumbers() {
31+
assertTrue(ParityCheck.checkParity(0b10101010)); // 4 ones
32+
assertFalse(ParityCheck.checkParity(0b100000000)); // 1 one
33+
assertTrue(ParityCheck.checkParity(0xAAAAAAAA)); // Alternating bits, 16 ones
1434
}
1535
}

src/test/java/com/thealgorithms/datastructures/lists/CountSinglyLinkedListRecursionTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,39 @@ public void testCountWithDuplicateElements() {
4646
list.insert(3);
4747
assertEquals(5, list.count(), "Count of a list with duplicate elements should match total node count.");
4848
}
49+
50+
@Test
51+
public void testCountAfterClearingList() {
52+
for (int i = 1; i <= 4; i++) {
53+
list.insert(i);
54+
}
55+
list.clear(); // assuming you have a clear method; if not, skip this
56+
assertEquals(0, list.count(), "Count after clearing the list should be 0.");
57+
}
58+
59+
@Test
60+
public void testCountOnVeryLargeList() {
61+
int n = 1000;
62+
for (int i = 0; i < n; i++) {
63+
list.insert(i);
64+
}
65+
assertEquals(n, list.count(), "Count should correctly return for large list sizes.");
66+
}
67+
68+
@Test
69+
public void testCountOnListWithNegativeNumbers() {
70+
list.insert(-1);
71+
list.insert(-5);
72+
list.insert(-10);
73+
assertEquals(3, list.count(), "Count should correctly handle negative values.");
74+
}
75+
76+
@Test
77+
public void testCountIsConsistentWithoutModification() {
78+
list.insert(1);
79+
list.insert(2);
80+
int firstCount = list.count();
81+
int secondCount = list.count();
82+
assertEquals(firstCount, secondCount, "Repeated count calls should return consistent values.");
83+
}
4984
}

src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,81 @@ public void testIsEmptyAfterDeletion() {
128128
list.delete(10);
129129
assertTrue(list.isEmpty());
130130
}
131+
132+
@Test
133+
public void testInsertNegativeNumbers() {
134+
list.insert(-10);
135+
list.insert(-5);
136+
list.insert(-20);
137+
assertEquals("[-20, -10, -5]", list.toString());
138+
}
139+
140+
@Test
141+
public void testInsertMixedPositiveAndNegativeNumbers() {
142+
list.insert(0);
143+
list.insert(-1);
144+
list.insert(1);
145+
assertEquals("[-1, 0, 1]", list.toString());
146+
}
147+
148+
@Test
149+
public void testMultipleDeletesUntilEmpty() {
150+
list.insert(2);
151+
list.insert(4);
152+
list.insert(6);
153+
assertTrue(list.delete(4));
154+
assertTrue(list.delete(2));
155+
assertTrue(list.delete(6));
156+
assertTrue(list.isEmpty());
157+
assertEquals("[]", list.toString());
158+
}
159+
160+
@Test
161+
public void testDeleteDuplicateValuesOnlyDeletesOneInstance() {
162+
list.insert(5);
163+
list.insert(5);
164+
list.insert(5);
165+
assertTrue(list.delete(5));
166+
assertEquals("[5, 5]", list.toString());
167+
assertTrue(list.delete(5));
168+
assertEquals("[5]", list.toString());
169+
assertTrue(list.delete(5));
170+
assertEquals("[]", list.toString());
171+
}
172+
173+
@Test
174+
public void testSearchOnListWithDuplicates() {
175+
list.insert(7);
176+
list.insert(7);
177+
list.insert(7);
178+
assertTrue(list.search(7));
179+
assertFalse(list.search(10));
180+
}
181+
182+
@Test
183+
public void testToStringOnEmptyList() {
184+
assertEquals("[]", list.toString());
185+
}
186+
187+
@Test
188+
public void testDeleteAllDuplicates() {
189+
list.insert(4);
190+
list.insert(4);
191+
list.insert(4);
192+
assertTrue(list.delete(4));
193+
assertTrue(list.delete(4));
194+
assertTrue(list.delete(4));
195+
assertFalse(list.delete(4)); // nothing left to delete
196+
assertEquals("[]", list.toString());
197+
}
198+
199+
@Test
200+
public void testInsertAfterDeletion() {
201+
list.insert(1);
202+
list.insert(3);
203+
list.insert(5);
204+
assertTrue(list.delete(3));
205+
list.insert(2);
206+
assertEquals("[1, 2, 5]", list.toString());
207+
}
131208
}

src/test/java/com/thealgorithms/maths/AbsoluteValueTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,28 @@ public class AbsoluteValueTest {
1212
void testGetAbsValue() {
1313
Stream.generate(() -> ThreadLocalRandom.current().nextInt()).limit(1000).forEach(number -> assertEquals(Math.abs(number), AbsoluteValue.getAbsValue(number)));
1414
}
15+
16+
@Test
17+
void testZero() {
18+
assertEquals(0, AbsoluteValue.getAbsValue(0));
19+
}
20+
21+
@Test
22+
void testPositiveNumbers() {
23+
assertEquals(5, AbsoluteValue.getAbsValue(5));
24+
assertEquals(123456, AbsoluteValue.getAbsValue(123456));
25+
assertEquals(Integer.MAX_VALUE, AbsoluteValue.getAbsValue(Integer.MAX_VALUE));
26+
}
27+
28+
@Test
29+
void testNegativeNumbers() {
30+
assertEquals(5, AbsoluteValue.getAbsValue(-5));
31+
assertEquals(123456, AbsoluteValue.getAbsValue(-123456));
32+
assertEquals(Integer.MAX_VALUE, AbsoluteValue.getAbsValue(-Integer.MAX_VALUE));
33+
}
34+
35+
@Test
36+
void testMinIntEdgeCase() {
37+
assertEquals(Integer.MIN_VALUE, AbsoluteValue.getAbsValue(Integer.MIN_VALUE));
38+
}
1539
}

src/test/java/com/thealgorithms/maths/BinaryPowTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,34 @@ void testBinPow() {
1313
assertEquals(729, BinaryPow.binPow(9, 3));
1414
assertEquals(262144, BinaryPow.binPow(8, 6));
1515
}
16+
17+
@Test
18+
void testZeroExponent() {
19+
assertEquals(1, BinaryPow.binPow(2, 0));
20+
assertEquals(1, BinaryPow.binPow(100, 0));
21+
assertEquals(1, BinaryPow.binPow(-5, 0));
22+
}
23+
24+
@Test
25+
void testZeroBase() {
26+
assertEquals(0, BinaryPow.binPow(0, 5));
27+
assertEquals(1, BinaryPow.binPow(0, 0));
28+
}
29+
30+
@Test
31+
void testOneBase() {
32+
assertEquals(1, BinaryPow.binPow(1, 100));
33+
assertEquals(1, BinaryPow.binPow(1, 0));
34+
}
35+
36+
@Test
37+
void testNegativeBase() {
38+
assertEquals(-8, BinaryPow.binPow(-2, 3));
39+
assertEquals(16, BinaryPow.binPow(-2, 4));
40+
}
41+
42+
@Test
43+
void testLargeExponent() {
44+
assertEquals(1073741824, BinaryPow.binPow(2, 30));
45+
}
1646
}
Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,112 @@
11
package com.thealgorithms.stacks;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

6+
import java.util.EmptyStackException;
57
import org.junit.jupiter.api.Test;
68

79
public class MinStackUsingTwoStacksTest {
810

911
@Test
10-
public void testMinStackOperations() {
12+
public void testBasicOperations() {
1113
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
1214
minStack.push(3);
1315
minStack.push(5);
14-
assertEquals(3, minStack.getMin());
16+
assertEquals(3, minStack.getMin(), "Min should be 3");
1517

1618
minStack.push(2);
1719
minStack.push(1);
18-
assertEquals(1, minStack.getMin());
20+
assertEquals(1, minStack.getMin(), "Min should be 1");
1921

2022
minStack.pop();
21-
assertEquals(2, minStack.getMin());
23+
assertEquals(2, minStack.getMin(), "Min should be 2 after popping 1");
24+
25+
assertEquals(2, minStack.top(), "Top should be 2");
26+
}
27+
28+
@Test
29+
public void testPushDuplicateMins() {
30+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
31+
minStack.push(2);
32+
minStack.push(2);
33+
minStack.push(1);
34+
minStack.push(1);
35+
assertEquals(1, minStack.getMin(), "Min should be 1");
36+
37+
minStack.pop();
38+
assertEquals(1, minStack.getMin(), "Min should still be 1 after popping one 1");
39+
40+
minStack.pop();
41+
assertEquals(2, minStack.getMin(), "Min should be 2 after popping both 1s");
42+
43+
minStack.pop();
44+
assertEquals(2, minStack.getMin(), "Min should still be 2 after popping one 2");
45+
46+
minStack.pop();
47+
// Now stack is empty, expect exception on getMin
48+
assertThrows(EmptyStackException.class, minStack::getMin);
2249
}
2350

2451
@Test
25-
public void testMinStackOperations2() {
52+
public void testPopOnEmptyStack() {
2653
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
54+
assertThrows(EmptyStackException.class, minStack::pop);
55+
}
56+
57+
@Test
58+
public void testTopOnEmptyStack() {
59+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
60+
assertThrows(EmptyStackException.class, minStack::top);
61+
}
62+
63+
@Test
64+
public void testGetMinOnEmptyStack() {
65+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
66+
assertThrows(EmptyStackException.class, minStack::getMin);
67+
}
68+
69+
@Test
70+
public void testSingleElementStack() {
71+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
72+
minStack.push(10);
73+
assertEquals(10, minStack.getMin());
74+
assertEquals(10, minStack.top());
75+
76+
minStack.pop();
77+
assertThrows(EmptyStackException.class, minStack::getMin);
78+
}
79+
80+
@Test
81+
public void testIncreasingSequence() {
82+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
83+
minStack.push(1);
84+
minStack.push(2);
2785
minStack.push(3);
28-
minStack.push(5);
29-
assertEquals(3, minStack.getMin());
86+
minStack.push(4);
87+
88+
assertEquals(1, minStack.getMin());
89+
assertEquals(4, minStack.top());
90+
91+
minStack.pop();
92+
minStack.pop();
93+
assertEquals(1, minStack.getMin());
94+
assertEquals(2, minStack.top());
95+
}
3096

97+
@Test
98+
public void testDecreasingSequence() {
99+
MinStackUsingTwoStacks minStack = new MinStackUsingTwoStacks();
100+
minStack.push(4);
101+
minStack.push(3);
31102
minStack.push(2);
32103
minStack.push(1);
104+
33105
assertEquals(1, minStack.getMin());
106+
assertEquals(1, minStack.top());
34107

35108
minStack.pop();
36109
assertEquals(2, minStack.getMin());
110+
assertEquals(2, minStack.top());
37111
}
38112
}

0 commit comments

Comments
 (0)