@@ -3,20 +3,22 @@ package cache
33import (
44 "container/list"
55 "testing"
6+
7+ "github.com/stretchr/testify/assert"
68)
79
810func TestEvictionPolicy (t * testing.T ) {
911 c := & cache {keyList : list .New ()}
1012 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 )
1416
1517 c = & cache {keyList : list .New ()}
1618 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 )
2022}
2123
2224func TestNew (t * testing.T ) {
@@ -27,24 +29,15 @@ func TestNew(t *testing.T) {
2729
2830 c := New (314159 , option ).(* cache )
2931
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 )
4841}
4942
5043type testItem uint64
@@ -123,18 +116,7 @@ func TestPutGetRemoveSize(t *testing.T) {
123116 for _ , testCase := range testCases {
124117 t .Log (testCase .label )
125118 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 ... ))
139121 }
140122}
0 commit comments