summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/golang-lru/simplelru
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/golang-lru/simplelru')
-rw-r--r--vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go45
-rw-r--r--vendor/github.com/hashicorp/golang-lru/simplelru/lru_test.go167
2 files changed, 22 insertions, 190 deletions
diff --git a/vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go b/vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go
index 744cac0..74c7077 100644
--- a/vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go
+++ b/vendor/github.com/hashicorp/golang-lru/simplelru/lru_interface.go
@@ -1,37 +1,36 @@
package simplelru
-
// LRUCache is the interface for simple LRU cache.
type LRUCache interface {
- // Adds a value to the cache, returns true if an eviction occurred and
- // updates the "recently used"-ness of the key.
- Add(key, value interface{}) bool
+ // Adds a value to the cache, returns true if an eviction occurred and
+ // updates the "recently used"-ness of the key.
+ Add(key, value interface{}) bool
- // Returns key's value from the cache and
- // updates the "recently used"-ness of the key. #value, isFound
- Get(key interface{}) (value interface{}, ok bool)
+ // Returns key's value from the cache and
+ // updates the "recently used"-ness of the key. #value, isFound
+ Get(key interface{}) (value interface{}, ok bool)
- // Check if a key exsists in cache without updating the recent-ness.
- Contains(key interface{}) (ok bool)
+ // Check if a key exsists in cache without updating the recent-ness.
+ Contains(key interface{}) (ok bool)
- // Returns key's value without updating the "recently used"-ness of the key.
- Peek(key interface{}) (value interface{}, ok bool)
+ // Returns key's value without updating the "recently used"-ness of the key.
+ Peek(key interface{}) (value interface{}, ok bool)
- // Removes a key from the cache.
- Remove(key interface{}) bool
+ // Removes a key from the cache.
+ Remove(key interface{}) bool
- // Removes the oldest entry from cache.
- RemoveOldest() (interface{}, interface{}, bool)
+ // Removes the oldest entry from cache.
+ RemoveOldest() (interface{}, interface{}, bool)
- // Returns the oldest entry from the cache. #key, value, isFound
- GetOldest() (interface{}, interface{}, bool)
+ // Returns the oldest entry from the cache. #key, value, isFound
+ GetOldest() (interface{}, interface{}, bool)
- // Returns a slice of the keys in the cache, from oldest to newest.
- Keys() []interface{}
+ // Returns a slice of the keys in the cache, from oldest to newest.
+ Keys() []interface{}
- // Returns the number of items in the cache.
- Len() int
+ // Returns the number of items in the cache.
+ Len() int
- // Clear all cache entries
- Purge()
+ // Clear all cache entries
+ Purge()
}
diff --git a/vendor/github.com/hashicorp/golang-lru/simplelru/lru_test.go b/vendor/github.com/hashicorp/golang-lru/simplelru/lru_test.go
deleted file mode 100644
index ca5676e..0000000
--- a/vendor/github.com/hashicorp/golang-lru/simplelru/lru_test.go
+++ /dev/null
@@ -1,167 +0,0 @@
-package simplelru
-
-import "testing"
-
-func TestLRU(t *testing.T) {
- evictCounter := 0
- onEvicted := func(k interface{}, v interface{}) {
- if k != v {
- t.Fatalf("Evict values not equal (%v!=%v)", k, v)
- }
- evictCounter++
- }
- l, err := NewLRU(128, onEvicted)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
-
- for i := 0; i < 256; i++ {
- l.Add(i, i)
- }
- if l.Len() != 128 {
- t.Fatalf("bad len: %v", l.Len())
- }
-
- if evictCounter != 128 {
- t.Fatalf("bad evict count: %v", evictCounter)
- }
-
- for i, k := range l.Keys() {
- if v, ok := l.Get(k); !ok || v != k || v != i+128 {
- t.Fatalf("bad key: %v", k)
- }
- }
- for i := 0; i < 128; i++ {
- _, ok := l.Get(i)
- if ok {
- t.Fatalf("should be evicted")
- }
- }
- for i := 128; i < 256; i++ {
- _, ok := l.Get(i)
- if !ok {
- t.Fatalf("should not be evicted")
- }
- }
- for i := 128; i < 192; i++ {
- ok := l.Remove(i)
- if !ok {
- t.Fatalf("should be contained")
- }
- ok = l.Remove(i)
- if ok {
- t.Fatalf("should not be contained")
- }
- _, ok = l.Get(i)
- if ok {
- t.Fatalf("should be deleted")
- }
- }
-
- l.Get(192) // expect 192 to be last key in l.Keys()
-
- for i, k := range l.Keys() {
- if (i < 63 && k != i+193) || (i == 63 && k != 192) {
- t.Fatalf("out of order key: %v", k)
- }
- }
-
- l.Purge()
- if l.Len() != 0 {
- t.Fatalf("bad len: %v", l.Len())
- }
- if _, ok := l.Get(200); ok {
- t.Fatalf("should contain nothing")
- }
-}
-
-func TestLRU_GetOldest_RemoveOldest(t *testing.T) {
- l, err := NewLRU(128, nil)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
- for i := 0; i < 256; i++ {
- l.Add(i, i)
- }
- k, _, ok := l.GetOldest()
- if !ok {
- t.Fatalf("missing")
- }
- if k.(int) != 128 {
- t.Fatalf("bad: %v", k)
- }
-
- k, _, ok = l.RemoveOldest()
- if !ok {
- t.Fatalf("missing")
- }
- if k.(int) != 128 {
- t.Fatalf("bad: %v", k)
- }
-
- k, _, ok = l.RemoveOldest()
- if !ok {
- t.Fatalf("missing")
- }
- if k.(int) != 129 {
- t.Fatalf("bad: %v", k)
- }
-}
-
-// Test that Add returns true/false if an eviction occurred
-func TestLRU_Add(t *testing.T) {
- evictCounter := 0
- onEvicted := func(k interface{}, v interface{}) {
- evictCounter++
- }
-
- l, err := NewLRU(1, onEvicted)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
-
- if l.Add(1, 1) == true || evictCounter != 0 {
- t.Errorf("should not have an eviction")
- }
- if l.Add(2, 2) == false || evictCounter != 1 {
- t.Errorf("should have an eviction")
- }
-}
-
-// Test that Contains doesn't update recent-ness
-func TestLRU_Contains(t *testing.T) {
- l, err := NewLRU(2, nil)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
-
- l.Add(1, 1)
- l.Add(2, 2)
- if !l.Contains(1) {
- t.Errorf("1 should be contained")
- }
-
- l.Add(3, 3)
- if l.Contains(1) {
- t.Errorf("Contains should not have updated recent-ness of 1")
- }
-}
-
-// Test that Peek doesn't update recent-ness
-func TestLRU_Peek(t *testing.T) {
- l, err := NewLRU(2, nil)
- if err != nil {
- t.Fatalf("err: %v", err)
- }
-
- l.Add(1, 1)
- l.Add(2, 2)
- if v, ok := l.Peek(1); !ok || v != 1 {
- t.Errorf("1 should be set to 1: %v, %v", v, ok)
- }
-
- l.Add(3, 3)
- if l.Contains(1) {
- t.Errorf("should not have updated recent-ness of 1")
- }
-}