github.com/fufuok/freelru@v0.13.3/helper_test.go (about)

     1  package freelru
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestDefaultLRUAddGet(t *testing.T) {
     8  	const CAP = 1_000_000
     9  
    10  	cache, _ := NewDefault[int, int](CAP * 2)
    11  	m := make(map[int]int, CAP)
    12  
    13  	for i := 0; i < CAP; i++ {
    14  		x := int(runtimeFastrand())
    15  		cache.Add(i, x)
    16  		m[i] = x
    17  	}
    18  
    19  	cache.PrintStats()
    20  
    21  	for i := 0; i < CAP; i++ {
    22  		v, ok := cache.Get(i)
    23  		if !ok {
    24  			t.Fatalf("key: %d not found", i)
    25  		}
    26  		if v != m[i] {
    27  			t.Fatalf("key: %d, value: %d != %d", i, v, m[i])
    28  		}
    29  	}
    30  }
    31  
    32  func TestSyncedDefaultLRUAddGet(t *testing.T) {
    33  	const CAP = 1_000_000
    34  
    35  	cache, _ := NewSyncedDefault[int, int](CAP * 2)
    36  	m := make(map[int]int, CAP)
    37  
    38  	for i := 0; i < CAP; i++ {
    39  		x := int(runtimeFastrand())
    40  		cache.Add(i, x)
    41  		m[i] = x
    42  	}
    43  
    44  	cache.PrintStats()
    45  
    46  	for i := 0; i < CAP; i++ {
    47  		v, ok := cache.Get(i)
    48  		if !ok {
    49  			t.Fatalf("key: %d not found", i)
    50  		}
    51  		if v != m[i] {
    52  			t.Fatalf("key: %d, value: %d != %d", i, v, m[i])
    53  		}
    54  	}
    55  }
    56  
    57  func TestShardedDefaultLRUAddGet(t *testing.T) {
    58  	const CAP = 1_000_000
    59  
    60  	cache, _ := NewShardedDefault[int, int](CAP * 2)
    61  	m := make(map[int]int, CAP)
    62  
    63  	for i := 0; i < CAP; i++ {
    64  		x := int(runtimeFastrand())
    65  		cache.Add(i, x)
    66  		m[i] = x
    67  	}
    68  
    69  	cache.PrintStats()
    70  
    71  	for i := 0; i < CAP; i++ {
    72  		v, ok := cache.Get(i)
    73  		if !ok {
    74  			t.Fatalf("key: %d not found", i)
    75  		}
    76  		if v != m[i] {
    77  			t.Fatalf("key: %d, value: %d != %d", i, v, m[i])
    78  		}
    79  	}
    80  }