First pass at implementing generics

This commit is contained in:
Luke Massa 2021-12-30 12:32:04 -05:00
parent 46f4078530
commit d798983798
3 changed files with 114 additions and 784 deletions

832
cache.go

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
package cache
import (
"bytes"
"io/ioutil"
"runtime"
"strconv"
"sync"
//"bytes"
//"io/ioutil"
//"runtime"
//"strconv"
//"sync"
"testing"
"time"
//"time"
)
type TestStruct struct {
@ -16,58 +16,61 @@ type TestStruct struct {
}
func TestCache(t *testing.T) {
tc := New(DefaultExpiration, 0)
a, found := tc.Get("a")
if found || a != nil {
tcInt := New[int](DefaultExpiration, 0)
tcString := New[string](DefaultExpiration, 0)
tcFloat := New[float64](DefaultExpiration, 0)
a, found := tcInt.Get("a")
if found || a != 0 {
t.Error("Getting A found value that shouldn't exist:", a)
}
b, found := tc.Get("b")
if found || b != nil {
b, found := tcString.Get("b")
if found || b != "" {
t.Error("Getting B found value that shouldn't exist:", b)
}
c, found := tc.Get("c")
if found || c != nil {
c, found := tcFloat.Get("c")
if found || c != 0.0 {
t.Error("Getting C found value that shouldn't exist:", c)
}
tc.Set("a", 1, DefaultExpiration)
tc.Set("b", "b", DefaultExpiration)
tc.Set("c", 3.5, DefaultExpiration)
tcInt.Set("a", 1, DefaultExpiration)
tcString.Set("b", "b", DefaultExpiration)
tcFloat.Set("c", 3.5, DefaultExpiration)
x, found := tc.Get("a")
a2, found := tcInt.Get("a")
if !found {
t.Error("a was not found while getting a2")
}
if x == nil {
t.Error("x for a is nil")
} else if a2 := x.(int); a2+2 != 3 {
if a2 == 0 {
t.Error("x for a is 0 (zero value)")
} else if a2+2 != 3 {
t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
}
x, found = tc.Get("b")
b2, found := tcString.Get("b")
if !found {
t.Error("b was not found while getting b2")
}
if x == nil {
t.Error("x for b is nil")
} else if b2 := x.(string); b2+"B" != "bB" {
if b2 == "" {
t.Error("x for b is \"\" (zero value)")
} else if b2+"B" != "bB" {
t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
}
x, found = tc.Get("c")
c2, found := tcFloat.Get("c")
if !found {
t.Error("c was not found while getting c2")
}
if x == nil {
t.Error("x for c is nil")
} else if c2 := x.(float64); c2+1.2 != 4.7 {
if c2 == 0.0 {
t.Error("x for c is 0.0 (zero value)")
} else if c2+1.2 != 4.7 {
t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
}
}
/*
func TestCacheTimes(t *testing.T) {
var found bool
@ -1769,3 +1772,5 @@ func TestGetWithExpiration(t *testing.T) {
t.Error("expiration for e is in the past")
}
}
*/

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/patrickmn/go-cache
go 1.18