github.com/kelleygo/clashcore@v1.0.2/common/arc/arc_test.go (about) 1 package arc 2 3 import ( 4 "testing" 5 ) 6 7 func TestInsertion(t *testing.T) { 8 cache := New[string, string](WithSize[string, string](3)) 9 if got, want := cache.Len(), 0; got != want { 10 t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want) 11 } 12 13 const ( 14 k1 = "Hello" 15 k2 = "Hallo" 16 k3 = "Ciao" 17 k4 = "Salut" 18 19 v1 = "World" 20 v2 = "Worlds" 21 v3 = "Welt" 22 ) 23 24 // Insert the first value 25 cache.Set(k1, v1) 26 if got, want := cache.Len(), 1; got != want { 27 t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want) 28 } 29 if got, ok := cache.Get(k1); !ok || got != v1 { 30 t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v1) 31 } 32 33 // Replace existing value for a given key 34 cache.Set(k1, v2) 35 if got, want := cache.Len(), 1; got != want { 36 t.Errorf("re-insertion: cache.Len(): got %d want %d", cache.Len(), want) 37 } 38 if got, ok := cache.Get(k1); !ok || got != v2 { 39 t.Errorf("re-insertion: cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2) 40 } 41 42 // Add a second different key 43 cache.Set(k2, v3) 44 if got, want := cache.Len(), 2; got != want { 45 t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want) 46 } 47 if got, ok := cache.Get(k1); !ok || got != v2 { 48 t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2) 49 } 50 if got, ok := cache.Get(k2); !ok || got != v3 { 51 t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k2, got, ok, v3) 52 } 53 54 // Fill cache 55 cache.Set(k3, v1) 56 if got, want := cache.Len(), 3; got != want { 57 t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want) 58 } 59 60 // Exceed size, this should not exceed size: 61 cache.Set(k4, v1) 62 if got, want := cache.Len(), 3; got != want { 63 t.Errorf("insertion of key out of size: cache.Len(): got %d want %d", cache.Len(), want) 64 } 65 } 66 67 func TestEviction(t *testing.T) { 68 size := 3 69 cache := New[string, string](WithSize[string, string](size)) 70 if got, want := cache.Len(), 0; got != want { 71 t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want) 72 } 73 74 tests := []struct { 75 k, v string 76 }{ 77 {"k1", "v1"}, 78 {"k2", "v2"}, 79 {"k3", "v3"}, 80 {"k4", "v4"}, 81 } 82 for i, tt := range tests[:size] { 83 cache.Set(tt.k, tt.v) 84 if got, want := cache.Len(), i+1; got != want { 85 t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want) 86 } 87 } 88 89 // Exceed size and check we don't outgrow it: 90 cache.Set(tests[size].k, tests[size].v) 91 if got := cache.Len(); got != size { 92 t.Errorf("insertion of overflow key #%d: cache.Len(): got %d want %d", 4, cache.Len(), size) 93 } 94 95 // Check that LRU got evicted: 96 if got, ok := cache.Get(tests[0].k); ok || got != "" { 97 t.Errorf("cache.Get(%v): got (%v,%t) want (<nil>,true)", tests[0].k, got, ok) 98 } 99 100 for _, tt := range tests[1:] { 101 if got, ok := cache.Get(tt.k); !ok || got != tt.v { 102 t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", tt.k, got, ok, tt.v) 103 } 104 } 105 }