@@ -3,20 +3,22 @@ package cache
3
3
import (
4
4
"container/list"
5
5
"testing"
6
+
7
+ "github.com/stretchr/testify/assert"
6
8
)
7
9
8
10
func TestEvictionPolicy (t * testing.T ) {
9
11
c := & cache {keyList : list .New ()}
10
12
EvictionPolicy (LeastRecentlyUsed )(c )
11
- if accessed , added := c .recordAccess ("foo" ), c .recordAdd ("foo" ); accessed == nil || added != nil {
12
- t . Errorf ( "EvictionPolicy failed to set LRU policy" )
13
- }
13
+ accessed , added := c .recordAccess ("foo" ), c .recordAdd ("foo" )
14
+ assert . NotNil ( t , accessed )
15
+ assert . Nil ( t , added )
14
16
15
17
c = & cache {keyList : list .New ()}
16
18
EvictionPolicy (LeastRecentlyAdded )(c )
17
- if accessed , added : = c .recordAccess ("foo" ), c .recordAdd ("foo" ); accessed != nil || added == nil {
18
- t . Errorf ( "EvictionPolicy failed to set LRU policy" )
19
- }
19
+ accessed , added = c .recordAccess ("foo" ), c .recordAdd ("foo" )
20
+ assert . Nil ( t , accessed )
21
+ assert . NotNil ( t , added )
20
22
}
21
23
22
24
func TestNew (t * testing.T ) {
@@ -27,24 +29,15 @@ func TestNew(t *testing.T) {
27
29
28
30
c := New (314159 , option ).(* cache )
29
31
30
- if c .cap != 314159 {
31
- t .Errorf ("Expected cache capacity of %d" , 314159 )
32
- }
33
- if c .size != 0 {
34
- t .Errorf ("Expected initial size of zero" )
35
- }
36
- if c .items == nil {
37
- t .Errorf ("Expected items to be initialized" )
38
- }
39
- if c .keyList == nil {
40
- t .Errorf ("Expected keyList to be initialized" )
41
- }
42
- if ! optionApplied {
43
- t .Errorf ("New did not apply its provided option" )
44
- }
45
- if accessed , added := c .recordAccess ("foo" ), c .recordAdd ("foo" ); accessed == nil || added != nil {
46
- t .Errorf ("Expected default LRU policy" )
47
- }
32
+ assert .Equal (t , uint64 (314159 ), c .cap )
33
+ assert .Equal (t , uint64 (0 ), c .size )
34
+ assert .NotNil (t , c .items )
35
+ assert .NotNil (t , c .keyList )
36
+ assert .True (t , optionApplied )
37
+
38
+ accessed , added := c .recordAccess ("foo" ), c .recordAdd ("foo" )
39
+ assert .NotNil (t , accessed )
40
+ assert .Nil (t , added )
48
41
}
49
42
50
43
type testItem uint64
@@ -123,18 +116,7 @@ func TestPutGetRemoveSize(t *testing.T) {
123
116
for _ , testCase := range testCases {
124
117
t .Log (testCase .label )
125
118
testCase .useCache (testCase .cache )
126
- if testCase .cache .Size () != testCase .expectedSize {
127
- t .Errorf ("Expected size of %d, got %d" , testCase .expectedSize , testCase .cache .Size ())
128
- }
129
- actual := testCase .cache .Get (keys ... )
130
- if len (actual ) != len (testCase .expectedItems ) {
131
- t .Errorf ("Expected to get %d items, got %d" , len (testCase .expectedItems ), len (actual ))
132
- } else {
133
- for i , expectedItem := range testCase .expectedItems {
134
- if actual [i ] != expectedItem {
135
- t .Errorf ("Expected Get to return %v in position %d, got %v" , expectedItem , i , actual [i ])
136
- }
137
- }
138
- }
119
+ assert .Equal (t , testCase .expectedSize , testCase .cache .Size ())
120
+ assert .Equal (t , testCase .expectedItems , testCase .cache .Get (keys ... ))
139
121
}
140
122
}
0 commit comments