| 22 | ) |
| 23 | |
| 24 | func TestLRU(t *testing.T) { |
| 25 | c := New(2) |
| 26 | |
| 27 | expectMiss := func(k string) { |
| 28 | v, ok := c.Get(k) |
| 29 | if ok { |
| 30 | t.Fatalf("expected cache miss on key %q but hit value %v", k, v) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | expectHit := func(k string, ev interface{}) { |
| 35 | v, ok := c.Get(k) |
| 36 | if !ok { |
| 37 | t.Fatalf("expected cache(%q)=%v; but missed", k, ev) |
| 38 | } |
| 39 | if !reflect.DeepEqual(v, ev) { |
| 40 | t.Fatalf("expected cache(%q)=%v; but got %v", k, ev, v) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | expectMiss("1") |
| 45 | c.Add("1", "one") |
| 46 | expectHit("1", "one") |
| 47 | |
| 48 | c.Add("2", "two") |
| 49 | expectHit("1", "one") |
| 50 | expectHit("2", "two") |
| 51 | |
| 52 | c.Add("3", "three") |
| 53 | expectHit("3", "three") |
| 54 | expectHit("2", "two") |
| 55 | expectMiss("1") |
| 56 | } |
| 57 | |
| 58 | func TestRemoveOldest(t *testing.T) { |
| 59 | c := New(2) |