| 1 |
// Copyright 2012, Google Inc. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package mirrors |
| 6 |
|
| 7 |
import ( |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
type CacheValue struct { |
| 12 |
size int |
| 13 |
} |
| 14 |
|
| 15 |
func (cv *CacheValue) Size() int { |
| 16 |
return cv.size |
| 17 |
} |
| 18 |
|
| 19 |
func TestInitialState(t *testing.T) { |
| 20 |
cache := NewLRUCache(5) |
| 21 |
l, sz, c, _ := cache.Stats() |
| 22 |
if l != 0 { |
| 23 |
t.Errorf("length = %v, want 0", l) |
| 24 |
} |
| 25 |
if sz != 0 { |
| 26 |
t.Errorf("size = %v, want 0", sz) |
| 27 |
} |
| 28 |
if c != 5 { |
| 29 |
t.Errorf("capacity = %v, want 5", c) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestSetInsertsValue(t *testing.T) { |
| 34 |
cache := NewLRUCache(100) |
| 35 |
data := &CacheValue{0} |
| 36 |
key := "key" |
| 37 |
cache.Set(key, data) |
| 38 |
|
| 39 |
v, ok := cache.Get(key) |
| 40 |
if !ok || v.(*CacheValue) != data { |
| 41 |
t.Errorf("Cache has incorrect value: %v != %v", data, v) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestGetValueWithMultipleTypes(t *testing.T) { |
| 46 |
cache := NewLRUCache(100) |
| 47 |
data := &CacheValue{0} |
| 48 |
key := "key" |
| 49 |
cache.Set(key, data) |
| 50 |
|
| 51 |
v, ok := cache.Get("key") |
| 52 |
if !ok || v.(*CacheValue) != data { |
| 53 |
t.Errorf("Cache has incorrect value for \"key\": %v != %v", data, v) |
| 54 |
} |
| 55 |
|
| 56 |
v, ok = cache.Get(string([]byte{'k', 'e', 'y'})) |
| 57 |
if !ok || v.(*CacheValue) != data { |
| 58 |
t.Errorf("Cache has incorrect value for []byte {'k','e','y'}: %v != %v", data, v) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestSetUpdatesSize(t *testing.T) { |
| 63 |
cache := NewLRUCache(100) |
| 64 |
emptyValue := &CacheValue{0} |
| 65 |
key := "key1" |
| 66 |
cache.Set(key, emptyValue) |
| 67 |
if _, sz, _, _ := cache.Stats(); sz != 0 { |
| 68 |
t.Errorf("cache.Size() = %v, expected 0", sz) |
| 69 |
} |
| 70 |
someValue := &CacheValue{20} |
| 71 |
key = "key2" |
| 72 |
cache.Set(key, someValue) |
| 73 |
if _, sz, _, _ := cache.Stats(); sz != 20 { |
| 74 |
t.Errorf("cache.Size() = %v, expected 20", sz) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestSetWithOldKeyUpdatesValue(t *testing.T) { |
| 79 |
cache := NewLRUCache(100) |
| 80 |
emptyValue := &CacheValue{0} |
| 81 |
key := "key1" |
| 82 |
cache.Set(key, emptyValue) |
| 83 |
someValue := &CacheValue{20} |
| 84 |
cache.Set(key, someValue) |
| 85 |
|
| 86 |
v, ok := cache.Get(key) |
| 87 |
if !ok || v.(*CacheValue) != someValue { |
| 88 |
t.Errorf("Cache has incorrect value: %v != %v", someValue, v) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestSetWithOldKeyUpdatesSize(t *testing.T) { |
| 93 |
cache := NewLRUCache(100) |
| 94 |
emptyValue := &CacheValue{0} |
| 95 |
key := "key1" |
| 96 |
cache.Set(key, emptyValue) |
| 97 |
|
| 98 |
if _, sz, _, _ := cache.Stats(); sz != 0 { |
| 99 |
t.Errorf("cache.Size() = %v, expected %v", sz, 0) |
| 100 |
} |
| 101 |
|
| 102 |
someValue := &CacheValue{20} |
| 103 |
cache.Set(key, someValue) |
| 104 |
expected := uint64(someValue.size) |
| 105 |
if _, sz, _, _ := cache.Stats(); sz != expected { |
| 106 |
t.Errorf("cache.Size() = %v, expected %v", sz, expected) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestGetNonExistent(t *testing.T) { |
| 111 |
cache := NewLRUCache(100) |
| 112 |
|
| 113 |
if _, ok := cache.Get("crap"); ok { |
| 114 |
t.Error("Cache returned a crap value after no inserts.") |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestDelete(t *testing.T) { |
| 119 |
cache := NewLRUCache(100) |
| 120 |
value := &CacheValue{1} |
| 121 |
key := "key" |
| 122 |
|
| 123 |
if cache.Delete(key) { |
| 124 |
t.Error("Item unexpectedly already in cache.") |
| 125 |
} |
| 126 |
|
| 127 |
cache.Set(key, value) |
| 128 |
|
| 129 |
if !cache.Delete(key) { |
| 130 |
t.Error("Expected item to be in cache.") |
| 131 |
} |
| 132 |
|
| 133 |
if _, sz, _, _ := cache.Stats(); sz != 0 { |
| 134 |
t.Errorf("cache.Size() = %v, expected 0", sz) |
| 135 |
} |
| 136 |
|
| 137 |
if _, ok := cache.Get(key); ok { |
| 138 |
t.Error("Cache returned a value after deletion.") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestClear(t *testing.T) { |
| 143 |
cache := NewLRUCache(100) |
| 144 |
value := &CacheValue{1} |
| 145 |
key := "key" |
| 146 |
|
| 147 |
cache.Set(key, value) |
| 148 |
cache.Clear() |
| 149 |
|
| 150 |
if _, sz, _, _ := cache.Stats(); sz != 0 { |
| 151 |
t.Errorf("cache.Size() = %v, expected 0 after Clear()", sz) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestCapacityIsObeyed(t *testing.T) { |
| 156 |
size := uint64(3) |
| 157 |
cache := NewLRUCache(size) |
| 158 |
value := &CacheValue{1} |
| 159 |
|
| 160 |
// Insert up to the cache's capacity. |
| 161 |
cache.Set("key1", value) |
| 162 |
cache.Set("key2", value) |
| 163 |
cache.Set("key3", value) |
| 164 |
if _, sz, _, _ := cache.Stats(); sz != size { |
| 165 |
t.Errorf("cache.Size() = %v, expected %v", sz, size) |
| 166 |
} |
| 167 |
// Insert one more; something should be evicted to make room. |
| 168 |
cache.Set("key4", value) |
| 169 |
if _, sz, _, _ := cache.Stats(); sz != size { |
| 170 |
t.Errorf("post-evict cache.Size() = %v, expected %v", sz, size) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestLRUIsEvicted(t *testing.T) { |
| 175 |
size := uint64(3) |
| 176 |
cache := NewLRUCache(size) |
| 177 |
|
| 178 |
cache.Set("key1", &CacheValue{1}) |
| 179 |
cache.Set("key2", &CacheValue{1}) |
| 180 |
cache.Set("key3", &CacheValue{1}) |
| 181 |
// lru: [key3, key2, key1] |
| 182 |
|
| 183 |
// Look up the elements. This will rearrange the LRU ordering. |
| 184 |
cache.Get("key3") |
| 185 |
cache.Get("key2") |
| 186 |
cache.Get("key1") |
| 187 |
// lru: [key1, key2, key3] |
| 188 |
|
| 189 |
cache.Set("key0", &CacheValue{1}) |
| 190 |
// lru: [key0, key1, key2] |
| 191 |
|
| 192 |
// The least recently used one should have been evicted. |
| 193 |
if _, ok := cache.Get("key3"); ok { |
| 194 |
t.Error("Least recently used element was not evicted.") |
| 195 |
} |
| 196 |
} |