1
1
package com .thealgorithms .datastructures .lists ;
2
2
3
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
3
4
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
5
5
6
import org .junit .jupiter .api .BeforeEach ;
7
+ import org .junit .jupiter .api .DisplayName ;
6
8
import org .junit .jupiter .api .Test ;
7
9
8
10
public class CountSinglyLinkedListRecursionTest {
@@ -15,70 +17,112 @@ public void setUp() {
15
17
}
16
18
17
19
@ Test
20
+ @ DisplayName ("Count of an empty list should be 0" )
18
21
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 ());
21
23
}
22
24
23
25
@ Test
26
+ @ DisplayName ("Count after inserting a single element should be 1" )
24
27
public void testCountSingleElementList () {
25
- // Insert a single element and check the count
26
28
list .insert (1 );
27
- assertEquals (1 , list .count (), "Count of a single-element list should be 1." );
29
+ assertEquals (1 , list .count ());
28
30
}
29
31
30
32
@ Test
33
+ @ DisplayName ("Count after inserting multiple distinct elements" )
31
34
public void testCountMultipleElements () {
32
- // Insert multiple elements and check the count
33
35
for (int i = 1 ; i <= 5 ; i ++) {
34
36
list .insert (i );
35
37
}
36
- assertEquals (5 , list .count (), "Count of a list with 5 elements should be 5." );
38
+ assertEquals (5 , list .count ());
37
39
}
38
40
39
41
@ Test
42
+ @ DisplayName ("Count should reflect total number of nodes with duplicate values" )
40
43
public void testCountWithDuplicateElements () {
41
- // Insert duplicate elements and verify the count is correct
42
- list .insert (1 );
43
44
list .insert (2 );
44
45
list .insert (2 );
45
46
list .insert (3 );
46
47
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 ());
48
50
}
49
51
50
52
@ Test
53
+ @ DisplayName ("Count should return 0 after clearing the list" )
51
54
public void testCountAfterClearingList () {
52
55
for (int i = 1 ; i <= 4 ; i ++) {
53
56
list .insert (i );
54
57
}
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 ());
57
60
}
58
61
59
62
@ Test
63
+ @ DisplayName ("Count on a very large list should be accurate" )
60
64
public void testCountOnVeryLargeList () {
61
65
int n = 1000 ;
62
66
for (int i = 0 ; i < n ; i ++) {
63
67
list .insert (i );
64
68
}
65
- assertEquals (n , list .count (), "Count should correctly return for large list sizes." );
69
+ assertEquals (n , list .count ());
66
70
}
67
71
68
72
@ Test
73
+ @ DisplayName ("Count should work correctly with negative values" )
69
74
public void testCountOnListWithNegativeNumbers () {
70
75
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 ());
74
79
}
75
80
76
81
@ Test
82
+ @ DisplayName ("Calling count multiple times should return the same value if list is unchanged" )
77
83
public void testCountIsConsistentWithoutModification () {
78
84
list .insert (1 );
79
85
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 ());
83
127
}
84
128
}
0 commit comments