| 1 | package simplelruscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/ |
| 2 | |
| 3 | import "testingscip-go gomod github.com/golang/go/src go1.12 testing/" |
| 4 | |
| 5 | func TestLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 6 | evictCounter := 0 |
| 7 | onEvicted := func(k interface{}, v interface{}) { |
| 8 | if k != v { |
| 9 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("Evict values not equal (%v!=%v)", k, v) |
| 10 | } |
| 11 | evictCounter++ |
| 12 | } |
| 13 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(128, onEvicted) |
| 14 | if err != nil { |
| 15 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 16 | } |
| 17 | |
| 18 | for i := 0; i < 256; i++ { |
| 19 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(i, i) |
| 20 | } |
| 21 | if l.Lenscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Len().() != 128 { |
| 22 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad len: %v", l.Lenscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Len().()) |
| 23 | } |
| 24 | |
| 25 | if evictCounter != 128 { |
| 26 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad evict count: %v", evictCounter) |
| 27 | } |
| 28 | |
| 29 | for i, k := range l.Keysscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Keys().() { |
| 30 | if v, ok := l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(k); !ok || v != k || v != i+128 { |
| 31 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad key: %v", k) |
| 32 | } |
| 33 | } |
| 34 | for i := 0; i < 128; i++ { |
| 35 | _, ok := l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(i) |
| 36 | if ok { |
| 37 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should be evicted") |
| 38 | } |
| 39 | } |
| 40 | for i := 128; i < 256; i++ { |
| 41 | _, ok := l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(i) |
| 42 | if !ok { |
| 43 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should not be evicted") |
| 44 | } |
| 45 | } |
| 46 | for i := 128; i < 192; i++ { |
| 47 | ok := l.Removescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Remove().(i) |
| 48 | if !ok { |
| 49 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should be contained") |
| 50 | } |
| 51 | ok = l.Removescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Remove().(i) |
| 52 | if ok { |
| 53 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should not be contained") |
| 54 | } |
| 55 | _, ok = l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(i) |
| 56 | if ok { |
| 57 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should be deleted") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(192) // expect 192 to be last key in l.Keys() |
| 62 | |
| 63 | for i, k := range l.Keysscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Keys().() { |
| 64 | if (i < 63 && k != i+193) || (i == 63 && k != 192) { |
| 65 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("out of order key: %v", k) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | l.Purgescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Purge().() |
| 70 | if l.Lenscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Len().() != 0 { |
| 71 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad len: %v", l.Lenscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Len().()) |
| 72 | } |
| 73 | if _, ok := l.Getscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Get().(200); ok { |
| 74 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("should contain nothing") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestLRU_GetOldest_RemoveOldestscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU_GetOldest_RemoveOldest().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 79 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(128, nil) |
| 80 | if err != nil { |
| 81 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 82 | } |
| 83 | for i := 0; i < 256; i++ { |
| 84 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(i, i) |
| 85 | } |
| 86 | k, _, ok := l.GetOldestscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#GetOldest().() |
| 87 | if !ok { |
| 88 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("missing") |
| 89 | } |
| 90 | if k.(int) != 128 { |
| 91 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad: %v", k) |
| 92 | } |
| 93 | |
| 94 | k, _, ok = l.RemoveOldestscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#RemoveOldest().() |
| 95 | if !ok { |
| 96 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("missing") |
| 97 | } |
| 98 | if k.(int) != 128 { |
| 99 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad: %v", k) |
| 100 | } |
| 101 | |
| 102 | k, _, ok = l.RemoveOldestscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#RemoveOldest().() |
| 103 | if !ok { |
| 104 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("missing") |
| 105 | } |
| 106 | if k.(int) != 129 { |
| 107 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("bad: %v", k) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Test that Add returns true/false if an eviction occurred |
| 112 | func TestLRU_Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU_Add().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 113 | evictCounter := 0 |
| 114 | onEvicted := func(k interface{}, v interface{}) { |
| 115 | evictCounter++ |
| 116 | } |
| 117 | |
| 118 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(1, onEvicted) |
| 119 | if err != nil { |
| 120 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 121 | } |
| 122 | |
| 123 | if l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(1, 1) == true || evictCounter != 0 { |
| 124 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("should not have an eviction") |
| 125 | } |
| 126 | if l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(2, 2) == false || evictCounter != 1 { |
| 127 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("should have an eviction") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Test that Contains doesn't update recent-ness |
| 132 | func TestLRU_Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU_Contains().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 133 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(2, nil) |
| 134 | if err != nil { |
| 135 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 136 | } |
| 137 | |
| 138 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(1, 1) |
| 139 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(2, 2) |
| 140 | if !l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(1) { |
| 141 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("1 should be contained") |
| 142 | } |
| 143 | |
| 144 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(3, 3) |
| 145 | if l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(1) { |
| 146 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("Contains should not have updated recent-ness of 1") |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Test that Peek doesn't update recent-ness |
| 151 | func TestLRU_Peekscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU_Peek().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 152 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(2, nil) |
| 153 | if err != nil { |
| 154 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 155 | } |
| 156 | |
| 157 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(1, 1) |
| 158 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(2, 2) |
| 159 | if v, ok := l.Peekscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Peek().(1); !ok || v != 1 { |
| 160 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("1 should be set to 1: %v, %v", v, ok) |
| 161 | } |
| 162 | |
| 163 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(3, 3) |
| 164 | if l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(1) { |
| 165 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("should not have updated recent-ness of 1") |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Test that Resize can upsize and downsize |
| 170 | func TestLRU_Resizescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/TestLRU_Resize().(t *testingscip-go gomod github.com/golang/go/src go1.12 testing/.Tscip-go gomod github.com/golang/go/src go1.12 testing/T#) { |
| 171 | onEvictCounter := 0 |
| 172 | onEvicted := func(k interface{}, v interface{}) { |
| 173 | onEvictCounter++ |
| 174 | } |
| 175 | l, err := NewLRUscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/NewLRU().(2, onEvicted) |
| 176 | if err != nil { |
| 177 | t.Fatalfscip-go gomod github.com/golang/go/src go1.12 testing/common#Fatalf().("err: %v", err) |
| 178 | } |
| 179 | |
| 180 | // Downsize |
| 181 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(1, 1) |
| 182 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(2, 2) |
| 183 | evicted := l.Resizescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Resize().(1); |
| 184 | if evicted != 1 { |
| 185 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("1 element should have been evicted: %v", evicted) |
| 186 | } |
| 187 | if onEvictCounter != 1 { |
| 188 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("onEvicted should have been called 1 time: %v", onEvictCounter) |
| 189 | } |
| 190 | |
| 191 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(3, 3) |
| 192 | if l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(1) { |
| 193 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("Element 1 should have been evicted") |
| 194 | } |
| 195 | |
| 196 | // Upsize |
| 197 | evicted = l.Resizescip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Resize().(2); |
| 198 | if evicted != 0 { |
| 199 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("0 elements should have been evicted: %v", evicted) |
| 200 | } |
| 201 | |
| 202 | l.Addscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Add().(4, 4) |
| 203 | if !l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(3) || !l.Containsscip-go gomod github.com/hashicorp/golang-lru . `github.com/hashicorp/golang-lru/simplelru`/LRU#Contains().(4) { |
| 204 | t.Errorfscip-go gomod github.com/golang/go/src go1.12 testing/common#Errorf().("Cache should have contained 2 elements") |
| 205 | } |
| 206 | } |