2 Copyright 2013 Google Inc.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
23 type simpleStruct struct {
28 type complexStruct struct {
33 var getTests = []struct {
39 {"string_hit", "myKey", "myKey", true},
40 {"string_miss", "myKey", "nonsense", false},
41 {"simple_struct_hit", simpleStruct{1, "two"}, simpleStruct{1, "two"}, true},
42 {"simeple_struct_miss", simpleStruct{1, "two"}, simpleStruct{0, "noway"}, false},
43 {"complex_struct_hit", complexStruct{1, simpleStruct{2, "three"}},
44 complexStruct{1, simpleStruct{2, "three"}}, true},
47 func TestGet(t *testing.T) {
48 for _, tt := range getTests {
50 lru.Add(tt.keyToAdd, 1234)
51 val, ok := lru.Get(tt.keyToGet)
52 if ok != tt.expectedOk {
53 t.Fatalf("%s: cache hit = %v; want %v", tt.name, ok, !ok)
54 } else if ok && val != 1234 {
55 t.Fatalf("%s expected get to return 1234 but got %v", tt.name, val)
60 func TestRemove(t *testing.T) {
62 lru.Add("myKey", 1234)
63 if val, ok := lru.Get("myKey"); !ok {
64 t.Fatal("TestRemove returned no match")
65 } else if val != 1234 {
66 t.Fatalf("TestRemove failed. Expected %d, got %v", 1234, val)
70 if _, ok := lru.Get("myKey"); ok {
71 t.Fatal("TestRemove returned a removed entry")