Back to Repositories

Testing LRU Cache Implementation with LinkedMap in JCSprout

This test suite validates the functionality of a custom LRU (Least Recently Used) cache implementation using LinkedMap in Java. The tests verify the core operations of putting elements into the cache, handling capacity limits, and accessing elements with proper LRU ordering.

Test Coverage Overview

The test suite provides comprehensive coverage of LRU cache operations:

  • Cache initialization with different capacities
  • Element insertion and overflow handling
  • Access pattern verification
  • LRU eviction policy validation

Implementation Analysis

The testing approach uses JUnit to verify the LRULinkedMap implementation through multiple test cases. Each test method focuses on specific aspects of the cache behavior, using assertion-based validation and visual output verification.

The tests employ systematic patterns of inserting and retrieving elements to ensure proper LRU functionality.

Technical Details

Testing tools and configuration:

  • JUnit 4 testing framework
  • Java Map interface implementation
  • System.out for visual verification
  • Custom LRULinkedMap implementation

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Isolated test methods for specific functionality
  • Clear test method naming conventions
  • Boundary testing with different cache sizes
  • Sequential operation verification

crossoverjie/jcsprout

src/test/java/com/crossoverjie/actual/LRULinkedMapTest.java

            
package com.crossoverjie.actual;

import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.*;

public class LRULinkedMapTest {
    @Test
    public void put() throws Exception {
        LRULinkedMap<String,Integer> map = new LRULinkedMap(3) ;
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);

        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }

        System.out.println("");
        map.put("4",4);
        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }
    }

    @Test
    public void put2() throws Exception {
        LRULinkedMap<String,Integer> map = new LRULinkedMap(4) ;
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        map.put("4",4);

        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }

        System.out.println("");
        map.put("5",5);
        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }
    }
    @Test
    public void get() throws Exception {
        LRULinkedMap<String,Integer> map = new LRULinkedMap(4) ;
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        map.put("4",4);

        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }

        System.out.println("");
        map.get("1") ;
        for (Map.Entry<String, Integer> e : map.getAll()){
            System.out.print(e.getKey() + " : " + e.getValue() + "\t");
        }
    }

}