okami.mom/ultraviolet
git clone https://git.nkp.pet/okami.mom/ultraviolet
227cc8b
upstream
1package lru 2 3import ( 4"strconv" 5"testing" 6) 7 8func TestLRU (t * testing.T ) { 9const size = 20 10 11cache := New [int ,string ](size ) 12 13val := strconv .Itoa 14 15for i := range size { 16v := val (i ) 17 18if cache .Add (i ,v ) { 19t .Fatalf ("Cache.Add: value evicted before size limit at %d" ,i ) 20 } 21 22got ,ok := cache .Get (i ) 23if ! ok { 24t .Fatalf ("Cache.Get: value not found at key %d" ,i ) 25 } 26 27if v != got { 28t .Fatalf ("Cache.Get: value at key %d not equal: want %q, got %q" ,i ,v ,got ) 29 } 30 } 31 32if ! cache .Add (size ,val (size )) { 33t .Fatalf ("Cache.Add: value not evicted after limit at %d" ,size ) 34 } 35 36if _ ,ok := cache .Get (0 );ok { 37t .Fatalf ("Cache.Get: value at key %d not evicted" ,0 ) 38 } 39 40for i := 1 ;i <= size ;i ++ { 41got ,ok := cache .Get (i ) 42if ! ok { 43t .Fatalf ("Cache.Get: value not found at key %d" ,i ) 44 } 45 46want := val (i ) 47 48if want != got { 49t .Fatalf ("Cache.Get: value at key %d not equal: want %q, got %q" ,i ,want ,got ) 50 } 51 } 52}