blob: c8d5a5906efdbeef296ecec405e3706577e876dd [file] [log] [blame]
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lru
import (
"reflect"
"testing"
)
func TestLRU(t *testing.T) {
c := New(2)
expectMiss := func(k string) {
v, ok := c.Get(k)
if ok {
t.Fatalf("expected cache miss on key %q but hit value %v", k, v)
}
}
expectHit := func(k string, ev interface{}) {
v, ok := c.Get(k)
if !ok {
t.Fatalf("expected cache(%q)=%v; but missed", k, ev)
}
if !reflect.DeepEqual(v, ev) {
t.Fatalf("expected cache(%q)=%v; but got %v", k, ev, v)
}
}
expectMiss("1")
c.Add("1", "one")
expectHit("1", "one")
c.Add("2", "two")
expectHit("1", "one")
expectHit("2", "two")
c.Add("3", "three")
expectHit("3", "three")
expectHit("2", "two")
expectMiss("1")
}
func TestRemoveOldest(t *testing.T) {
c := New(2)
c.Add("1", "one")
c.Add("2", "two")
if k, v := c.RemoveOldest(); k != "1" || v != "one" {
t.Fatalf("oldest = %q, %q; want 1, one", k, v)
}
if k, v := c.RemoveOldest(); k != "2" || v != "two" {
t.Fatalf("oldest = %q, %q; want 2, two", k, v)
}
if k, v := c.RemoveOldest(); k != nil || v != nil {
t.Fatalf("oldest = %v, %v; want \"\", nil", k, v)
}
}