Files
gocryptotrader/common/cache/cache_test.go
Adrian Gallagher 9a4eb9de84 CI: Fix golangci-lint linter issues, add prealloc linter and bump version depends for Go 1.18 (#915)
* Bump CI versions

* Specifically set go version as 1.17.x bumps it to 1.18

* Another

* Adjust AppVeyor

* Part 1 of linter issues

* Part 2

* Fix various linters and improvements

* Part 3

* Finishing touches

* Tests and EqualFold

* Fix nitterinos plus bonus requester jobs bump for exchanges with large number of tests

* Fix nitterinos and bump golangci-lint timeout for AppVeyor

* Address nits, ensure all books are returned on err due to syncer regression

* Fix the wiggins

* Fix duplication

* Fix nitterinos
2022-04-20 13:45:15 +10:00

145 lines
3.3 KiB
Go

package cache
import (
"testing"
"github.com/thrasher-corp/gocryptotrader/common/convert"
)
func TestCache(t *testing.T) {
lruCache := New(5)
lruCache.Add("hello", "world")
c := lruCache.Contains("hello")
if !c {
t.Fatal("expected cache to contain \"hello\" key")
}
v := lruCache.Get("hello")
if v == nil {
t.Fatal("expected cache to contain \"hello\" key")
}
if convert.InterfaceToStringOrZeroValue(v) != "world" {
t.Fatal("expected \"hello\" key to contain value \"world\"")
}
if r := lruCache.Remove("hello"); !r {
t.Fatal("expected \"hello\" key to be removed from cache")
}
v = lruCache.Get("hello")
if v != nil {
t.Fatal("expected cache to not contain \"hello\" key")
}
}
func TestContainsOrAdd(t *testing.T) {
lruCache := New(5)
if lruCache.ContainsOrAdd("hello", "world") {
t.Fatal("expected ContainsOrAdd() to add new key when not found")
}
if !lruCache.ContainsOrAdd("hello", "world") {
t.Fatal("expected ContainsOrAdd() to return true when key found")
}
}
func TestClear(t *testing.T) {
lruCache := New(5)
for x := 0; x < 5; x++ {
lruCache.Add(x, x)
}
if lruCache.Len() != 5 {
t.Fatal("expected cache to have 5 entries")
}
lruCache.Clear()
if lruCache.Len() != 0 {
t.Fatal("expected cache to have 0 entries")
}
}
func TestAdd(t *testing.T) {
lruCache := New(2)
lruCache.Add(1, 1)
lruCache.Add(2, 2)
if lruCache.Len() != 2 {
t.Fatal("expected cache to have 2 entries")
}
lruCache.Add(3, 3)
if lruCache.Len() != 2 {
t.Fatal("expected cache to have 2 entries")
}
v := lruCache.Get(1)
if v != nil {
t.Fatal("expected cache to no longer contain \"1\" key")
}
v = lruCache.Get(2)
if v == nil {
t.Fatal("expected cache to contain \"2\" key")
}
if convert.InterfaceToIntOrZeroValue(v) != 2 {
t.Fatal("expected \"2\" key to contain value \"2\"")
}
k, v := lruCache.getNewest()
if convert.InterfaceToIntOrZeroValue(k) != 2 {
t.Fatal("expected latest key to be 2")
}
if convert.InterfaceToIntOrZeroValue(v) != 2 {
t.Fatal("expected latest value to be 2")
}
lruCache.Add(3, 3)
k, _ = lruCache.getNewest()
if convert.InterfaceToIntOrZeroValue(k) != 3 {
t.Fatal("expected latest key to be 3")
}
k, _ = lruCache.getOldest()
if convert.InterfaceToIntOrZeroValue(k) != 2 {
t.Fatal("expected oldest key to be 2")
}
k, v = lruCache.getOldest()
if convert.InterfaceToIntOrZeroValue(k) != 2 {
t.Fatal("expected oldest key to be 2")
}
if convert.InterfaceToIntOrZeroValue(v) != 2 {
t.Fatal("expected latest value to be 2")
}
lruCache.Add(2, 2)
k, _ = lruCache.getNewest()
if convert.InterfaceToIntOrZeroValue(k) != 2 {
t.Fatal("expected latest key to be 2")
}
k, _ = lruCache.getOldest()
if convert.InterfaceToIntOrZeroValue(k) != 3 {
t.Fatal("expected oldest key to be 3")
}
}
func TestRemove(t *testing.T) {
lruCache := New(2)
lruCache.Add(1, 1)
v := lruCache.Remove(1)
if !v {
t.Fatal("expected remove on valid key to return true")
}
v = lruCache.Remove(2)
if v {
t.Fatal("expected remove on invalid key to return false")
}
}
func TestGetNewest(t *testing.T) {
lruCache := New(2)
if k, _ := lruCache.getNewest(); k != nil {
t.Fatal("expected GetNewest() on empty cache to return nil")
}
}
func TestGetOldest(t *testing.T) {
lruCache := New(2)
if k, _ := lruCache.getOldest(); k != nil {
t.Fatal("expected GetOldest() on empty cache to return nil")
}
}