11package com .thealgorithms .datastructures .lists ;
22
3+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
34import static org .junit .jupiter .api .Assertions .assertEquals ;
45
56import org .junit .jupiter .api .BeforeEach ;
7+ import org .junit .jupiter .api .DisplayName ;
68import org .junit .jupiter .api .Test ;
79
810public 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}
0 commit comments