github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/cache/lru_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/v2fly/v2ray-core/v5/common/cache"
     7  )
     8  
     9  func TestLruReplaceValue(t *testing.T) {
    10  	lru := NewLru(2)
    11  	lru.Put(2, 6)
    12  	lru.Put(1, 5)
    13  	lru.Put(1, 2)
    14  	v, _ := lru.Get(1)
    15  	if v != 2 {
    16  		t.Error("should get 2", v)
    17  	}
    18  	v, _ = lru.Get(2)
    19  	if v != 6 {
    20  		t.Error("should get 6", v)
    21  	}
    22  }
    23  
    24  func TestLruRemoveOld(t *testing.T) {
    25  	lru := NewLru(2)
    26  	v, ok := lru.Get(2)
    27  	if ok {
    28  		t.Error("should get nil", v)
    29  	}
    30  	lru.Put(1, 1)
    31  	lru.Put(2, 2)
    32  	v, _ = lru.Get(1)
    33  	if v != 1 {
    34  		t.Error("should get 1", v)
    35  	}
    36  	lru.Put(3, 3)
    37  	v, ok = lru.Get(2)
    38  	if ok {
    39  		t.Error("should get nil", v)
    40  	}
    41  	lru.Put(4, 4)
    42  	v, ok = lru.Get(1)
    43  	if ok {
    44  		t.Error("should get nil", v)
    45  	}
    46  	v, _ = lru.Get(3)
    47  	if v != 3 {
    48  		t.Error("should get 3", v)
    49  	}
    50  	v, _ = lru.Get(4)
    51  	if v != 4 {
    52  		t.Error("should get 4", v)
    53  	}
    54  }
    55  
    56  func TestGetKeyFromValue(t *testing.T) {
    57  	lru := NewLru(2)
    58  	lru.Put(3, 3)
    59  	lru.Put(2, 2)
    60  	lru.Put(1, 1)
    61  	v, ok := lru.GetKeyFromValue(3)
    62  	if ok {
    63  		t.Error("should get nil", v)
    64  	}
    65  	v, _ = lru.GetKeyFromValue(2)
    66  	if v != 2 {
    67  		t.Error("should get 2", v)
    68  	}
    69  }