Skip to content

Commit 222663c

Browse files
authored
Merge branch 'master' into SAI
2 parents 72ce8a2 + bbbc1dd commit 222663c

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed
Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45

56
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.DisplayName;
68
import org.junit.jupiter.api.Test;
79

810
public class CountSinglyLinkedListRecursionTest {
@@ -15,70 +17,112 @@ public void setUp() {
1517
}
1618

1719
@Test
20+
@DisplayName("Count of an empty list should be 0")
1821
public void testCountEmptyList() {
19-
// An empty list should have a count of 0
20-
assertEquals(0, list.count(), "Count of an empty list should be 0.");
22+
assertEquals(0, list.count());
2123
}
2224

2325
@Test
26+
@DisplayName("Count after inserting a single element should be 1")
2427
public void testCountSingleElementList() {
25-
// Insert a single element and check the count
2628
list.insert(1);
27-
assertEquals(1, list.count(), "Count of a single-element list should be 1.");
29+
assertEquals(1, list.count());
2830
}
2931

3032
@Test
33+
@DisplayName("Count after inserting multiple distinct elements")
3134
public void testCountMultipleElements() {
32-
// Insert multiple elements and check the count
3335
for (int i = 1; i <= 5; i++) {
3436
list.insert(i);
3537
}
36-
assertEquals(5, list.count(), "Count of a list with 5 elements should be 5.");
38+
assertEquals(5, list.count());
3739
}
3840

3941
@Test
42+
@DisplayName("Count should reflect total number of nodes with duplicate values")
4043
public void testCountWithDuplicateElements() {
41-
// Insert duplicate elements and verify the count is correct
42-
list.insert(1);
4344
list.insert(2);
4445
list.insert(2);
4546
list.insert(3);
4647
list.insert(3);
47-
assertEquals(5, list.count(), "Count of a list with duplicate elements should match total node count.");
48+
list.insert(1);
49+
assertEquals(5, list.count());
4850
}
4951

5052
@Test
53+
@DisplayName("Count should return 0 after clearing the list")
5154
public void testCountAfterClearingList() {
5255
for (int i = 1; i <= 4; i++) {
5356
list.insert(i);
5457
}
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.");
58+
list.clear(); // assumed to exist
59+
assertEquals(0, list.count());
5760
}
5861

5962
@Test
63+
@DisplayName("Count on a very large list should be accurate")
6064
public void testCountOnVeryLargeList() {
6165
int n = 1000;
6266
for (int i = 0; i < n; i++) {
6367
list.insert(i);
6468
}
65-
assertEquals(n, list.count(), "Count should correctly return for large list sizes.");
69+
assertEquals(n, list.count());
6670
}
6771

6872
@Test
73+
@DisplayName("Count should work correctly with negative values")
6974
public void testCountOnListWithNegativeNumbers() {
7075
list.insert(-1);
71-
list.insert(-5);
72-
list.insert(-10);
73-
assertEquals(3, list.count(), "Count should correctly handle negative values.");
76+
list.insert(-2);
77+
list.insert(-3);
78+
assertEquals(3, list.count());
7479
}
7580

7681
@Test
82+
@DisplayName("Calling count multiple times should return the same value if list is unchanged")
7783
public void testCountIsConsistentWithoutModification() {
7884
list.insert(1);
7985
list.insert(2);
80-
int firstCount = list.count();
81-
int secondCount = list.count();
82-
assertEquals(firstCount, secondCount, "Repeated count calls should return consistent values.");
86+
int count1 = list.count();
87+
int count2 = list.count();
88+
assertEquals(count1, count2);
89+
}
90+
91+
@Test
92+
@DisplayName("Count should reflect total even if all values are the same")
93+
public void testCountAllSameValues() {
94+
for (int i = 0; i < 5; i++) {
95+
list.insert(42);
96+
}
97+
assertEquals(5, list.count());
98+
}
99+
100+
@Test
101+
@DisplayName("Count should remain correct after multiple interleaved insert and count operations")
102+
public void testCountAfterEachInsert() {
103+
assertEquals(0, list.count());
104+
list.insert(1);
105+
assertEquals(1, list.count());
106+
list.insert(2);
107+
assertEquals(2, list.count());
108+
list.insert(3);
109+
assertEquals(3, list.count());
110+
}
111+
112+
@Test
113+
@DisplayName("List should not throw on edge count (0 nodes)")
114+
public void testEdgeCaseNoElements() {
115+
assertDoesNotThrow(() -> list.count());
116+
}
117+
118+
@Test
119+
@DisplayName("Should count accurately after inserting then removing all elements")
120+
public void testCountAfterInsertAndClear() {
121+
for (int i = 0; i < 10; i++) {
122+
list.insert(i);
123+
}
124+
assertEquals(10, list.count());
125+
list.clear();
126+
assertEquals(0, list.count());
83127
}
84128
}
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
package com.thealgorithms.recursion;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
44

5+
import java.util.Arrays;
56
import java.util.List;
7+
import org.junit.jupiter.api.DisplayName;
68
import org.junit.jupiter.api.Test;
79

810
public final class GenerateSubsetsTest {
911

1012
@Test
11-
void subsetRecursionTestOne() {
12-
String str = "abc";
13-
String[] expected = new String[] {"abc", "ab", "ac", "a", "bc", "b", "c", ""};
14-
15-
List<String> ans = GenerateSubsets.subsetRecursion(str);
16-
assertArrayEquals(ans.toArray(), expected);
13+
@DisplayName("Subsets of 'abc'")
14+
void testSubsetsOfABC() {
15+
assertSubsets("abc", Arrays.asList("abc", "ab", "ac", "a", "bc", "b", "c", ""));
1716
}
1817

1918
@Test
20-
void subsetRecursionTestTwo() {
21-
String str = "cbf";
22-
String[] expected = new String[] {"cbf", "cb", "cf", "c", "bf", "b", "f", ""};
19+
@DisplayName("Subsets of 'cbf'")
20+
void testSubsetsOfCBF() {
21+
assertSubsets("cbf", Arrays.asList("cbf", "cb", "cf", "c", "bf", "b", "f", ""));
22+
}
2323

24-
List<String> ans = GenerateSubsets.subsetRecursion(str);
25-
assertArrayEquals(ans.toArray(), expected);
24+
@Test
25+
@DisplayName("Subsets of 'aba' with duplicates")
26+
void testSubsetsWithDuplicateChars() {
27+
assertSubsets("aba", Arrays.asList("aba", "ab", "aa", "a", "ba", "b", "a", ""));
2628
}
2729

2830
@Test
29-
void subsetRecursionTestThree() {
30-
String str = "aba";
31-
String[] expected = new String[] {"aba", "ab", "aa", "a", "ba", "b", "a", ""};
31+
@DisplayName("Subsets of empty string")
32+
void testEmptyInput() {
33+
assertSubsets("", List.of(""));
34+
}
3235

33-
List<String> ans = GenerateSubsets.subsetRecursion(str);
34-
assertArrayEquals(ans.toArray(), expected);
36+
private void assertSubsets(String input, Iterable<String> expected) {
37+
List<String> actual = GenerateSubsets.subsetRecursion(input);
38+
assertIterableEquals(expected, actual, "Subsets do not match for input: " + input);
3539
}
3640
}

0 commit comments

Comments
 (0)