Content-Length: 343064 | pFad | http://github.com/TheAlgorithms/Java/pull/6413/commits/01609bd57f070bb3a8b303fa34499a6e413bbc4a

80 testing: improving CountSinglyLinkedListRecursionTest by alxkm · Pull Request #6413 · TheAlgorithms/Java · GitHub
Skip to content

testing: improving CountSinglyLinkedListRecursionTest #6413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
testing: improving CountSinglyLinkedListRecursionTest
  • Loading branch information
alxkm committed Jul 18, 2025
commit 01609bd57f070bb3a8b303fa34499a6e413bbc4a
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.thealgorithms.datastructures.lists;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class CountSinglyLinkedListRecursionTest {
Expand All @@ -15,70 +17,112 @@ public void setUp() {
}

@Test
@DisplayName("Count of an empty list should be 0")
public void testCountEmptyList() {
// An empty list should have a count of 0
assertEquals(0, list.count(), "Count of an empty list should be 0.");
assertEquals(0, list.count());
}

@Test
@DisplayName("Count after inserting a single element should be 1")
public void testCountSingleElementList() {
// Insert a single element and check the count
list.insert(1);
assertEquals(1, list.count(), "Count of a single-element list should be 1.");
assertEquals(1, list.count());
}

@Test
@DisplayName("Count after inserting multiple distinct elements")
public void testCountMultipleElements() {
// Insert multiple elements and check the count
for (int i = 1; i <= 5; i++) {
list.insert(i);
}
assertEquals(5, list.count(), "Count of a list with 5 elements should be 5.");
assertEquals(5, list.count());
}

@Test
@DisplayName("Count should reflect total number of nodes with duplicate values")
public void testCountWithDuplicateElements() {
// Insert duplicate elements and verify the count is correct
list.insert(1);
list.insert(2);
list.insert(2);
list.insert(3);
list.insert(3);
assertEquals(5, list.count(), "Count of a list with duplicate elements should match total node count.");
list.insert(1);
assertEquals(5, list.count());
}

@Test
@DisplayName("Count should return 0 after clearing the list")
public void testCountAfterClearingList() {
for (int i = 1; i <= 4; i++) {
list.insert(i);
}
list.clear(); // assuming you have a clear method; if not, skip this
assertEquals(0, list.count(), "Count after clearing the list should be 0.");
list.clear(); // assumed to exist
assertEquals(0, list.count());
}

@Test
@DisplayName("Count on a very large list should be accurate")
public void testCountOnVeryLargeList() {
int n = 1000;
for (int i = 0; i < n; i++) {
list.insert(i);
}
assertEquals(n, list.count(), "Count should correctly return for large list sizes.");
assertEquals(n, list.count());
}

@Test
@DisplayName("Count should work correctly with negative values")
public void testCountOnListWithNegativeNumbers() {
list.insert(-1);
list.insert(-5);
list.insert(-10);
assertEquals(3, list.count(), "Count should correctly handle negative values.");
list.insert(-2);
list.insert(-3);
assertEquals(3, list.count());
}

@Test
@DisplayName("Calling count multiple times should return the same value if list is unchanged")
public void testCountIsConsistentWithoutModification() {
list.insert(1);
list.insert(2);
int firstCount = list.count();
int secondCount = list.count();
assertEquals(firstCount, secondCount, "Repeated count calls should return consistent values.");
int count1 = list.count();
int count2 = list.count();
assertEquals(count1, count2);
}

@Test
@DisplayName("Count should reflect total even if all values are the same")
public void testCountAllSameValues() {
for (int i = 0; i < 5; i++) {
list.insert(42);
}
assertEquals(5, list.count());
}

@Test
@DisplayName("Count should remain correct after multiple interleaved insert and count operations")
public void testCountAfterEachInsert() {
assertEquals(0, list.count());
list.insert(1);
assertEquals(1, list.count());
list.insert(2);
assertEquals(2, list.count());
list.insert(3);
assertEquals(3, list.count());
}

@Test
@DisplayName("List should not throw on edge count (0 nodes)")
public void testEdgeCaseNoElements() {
assertDoesNotThrow(() -> list.count());
}

@Test
@DisplayName("Should count accurately after inserting then removing all elements")
public void testCountAfterInsertAndClear() {
for (int i = 0; i < 10; i++) {
list.insert(i);
}
assertEquals(10, list.count());
list.clear();
assertEquals(0, list.count());
}
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/Java/pull/6413/commits/01609bd57f070bb3a8b303fa34499a6e413bbc4a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy