| 16 | } |
| 17 | |
| 18 | func TestCache(t *testing.T) { |
| 19 | tc := New(DefaultExpiration, 0) |
| 20 | |
| 21 | a, found := tc.Get("a") |
| 22 | if found || a != nil { |
| 23 | t.Error("Getting A found value that shouldn't exist:", a) |
| 24 | } |
| 25 | |
| 26 | b, found := tc.Get("b") |
| 27 | if found || b != nil { |
| 28 | t.Error("Getting B found value that shouldn't exist:", b) |
| 29 | } |
| 30 | |
| 31 | c, found := tc.Get("c") |
| 32 | if found || c != nil { |
| 33 | t.Error("Getting C found value that shouldn't exist:", c) |
| 34 | } |
| 35 | |
| 36 | tc.Set("a", 1, DefaultExpiration) |
| 37 | tc.Set("b", "b", DefaultExpiration) |
| 38 | tc.Set("c", 3.5, DefaultExpiration) |
| 39 | |
| 40 | x, found := tc.Get("a") |
| 41 | if !found { |
| 42 | t.Error("a was not found while getting a2") |
| 43 | } |
| 44 | if x == nil { |
| 45 | t.Error("x for a is nil") |
| 46 | } else if a2 := x.(int); a2+2 != 3 { |
| 47 | t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2) |
| 48 | } |
| 49 | |
| 50 | x, found = tc.Get("b") |
| 51 | if !found { |
| 52 | t.Error("b was not found while getting b2") |
| 53 | } |
| 54 | if x == nil { |
| 55 | t.Error("x for b is nil") |
| 56 | } else if b2 := x.(string); b2+"B" != "bB" { |
| 57 | t.Error("b2 (which should be b) plus B does not equal bB; value:", b2) |
| 58 | } |
| 59 | |
| 60 | x, found = tc.Get("c") |
| 61 | if !found { |
| 62 | t.Error("c was not found while getting c2") |
| 63 | } |
| 64 | if x == nil { |
| 65 | t.Error("x for c is nil") |
| 66 | } else if c2 := x.(float64); c2+1.2 != 4.7 { |
| 67 | t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestCacheTimes(t *testing.T) { |
| 72 | var found bool |