Back to Repositories

Testing Linked List Cycle Detection in JCSprout

This test suite evaluates the LinkLoop class’s capability to detect cycles in linked list structures. It implements comprehensive test cases to verify both cyclic and non-cyclic linked list scenarios using JUnit testing framework.

Test Coverage Overview

The test suite provides thorough coverage of linked list cycle detection functionality.

Key areas tested include:
  • Non-cyclic linked lists with multiple nodes
  • Cyclic linked lists with complete loops
  • Edge case testing with minimal node count
  • Validation of both positive and negative scenarios

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate loop detection logic. Tests are structured using individual test methods with clear naming conventions and documentation. Each test case constructs specific linked list configurations to verify the isLoop() method’s accuracy in detecting cycles.

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Custom Node class implementation
  • Assert class for result validation
  • Java exception handling for error cases

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method naming convention
  • Comprehensive documentation using JavaDoc
  • Isolated test cases for specific scenarios
  • Proper test setup and initialization
  • Explicit expected vs actual result comparison

crossoverjie/jcsprout

src/test/java/com/crossoverjie/algorithm/LinkLoopTest.java

            
package com.crossoverjie.algorithm;

import org.junit.Assert;
import org.junit.Test;

public class LinkLoopTest {

    /**
     * 无环
     * @throws Exception
     */
    @Test
    public void isLoop() throws Exception {
        LinkLoop.Node node3 = new LinkLoop.Node("3");
        LinkLoop.Node node2 = new LinkLoop.Node("2") ;
        LinkLoop.Node node1 = new LinkLoop.Node("1") ;

        node1.next = node2 ;
        node2.next = node3 ;

        LinkLoop linkLoop = new LinkLoop() ;
        boolean loop = linkLoop.isLoop(node1);
        Assert.assertEquals(loop,false);
    }

    /**
     * 有环
     * @throws Exception
     */
    @Test
    public void isLoop2() throws Exception {
        LinkLoop.Node node3 = new LinkLoop.Node("3");
        LinkLoop.Node node2 = new LinkLoop.Node("2") ;
        LinkLoop.Node node1 = new LinkLoop.Node("1") ;

        node1.next = node2 ;
        node2.next = node3 ;
        node3.next = node1 ;

        LinkLoop linkLoop = new LinkLoop() ;
        boolean loop = linkLoop.isLoop(node1);
        Assert.assertEquals(loop,true);
    }

    /**
     * 无环
     * @throws Exception
     */
    @Test
    public void isLoop3() throws Exception {
        LinkLoop.Node node2 = new LinkLoop.Node("2") ;
        LinkLoop.Node node1 = new LinkLoop.Node("1") ;

        node1.next = node2 ;


        LinkLoop linkLoop = new LinkLoop() ;
        boolean loop = linkLoop.isLoop(node1);
        Assert.assertEquals(loop,false);
    }

}