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