From 6c7b140b721adb23d21f37dcd6c70af379e3af86 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Fri, 26 Jul 2024 17:12:00 +0200 Subject: [PATCH 01/10] write FIFO cache unit tests --- cache/fifo_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 cache/fifo_test.go diff --git a/cache/fifo_test.go b/cache/fifo_test.go new file mode 100644 index 0000000000..ff2e27c403 --- /dev/null +++ b/cache/fifo_test.go @@ -0,0 +1,86 @@ +// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package cache + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFIFOCache(t *testing.T) { + limit := 10 + + tests := []struct { + name string + maxVal int + check func(t *testing.T, cache *FIFO[int, int]) + }{ + { + name: "inserting up to limit works", + maxVal: limit - 1, + }, + { + name: "inserting after limit cleans first", + maxVal: limit, + check: func(t *testing.T, cache *FIFO[int, int]) { + require := require.New(t) + _, ok := cache.Get(0) + require.False(ok) + v, ok := cache.Get(1) + require.True(ok) + require.Equal(v, 1) + }, + }, + { + name: "inserting before limit can insert again", + maxVal: limit - 2, + check: func(t *testing.T, cache *FIFO[int, int]) { + require := require.New(t) + exists := cache.Put(limit-1, limit-1) + require.False(exists) + exists = cache.Put(limit, limit) + require.False(exists) + _, ok := cache.Get(0) + require.False(ok) + }, + }, + { + name: "inserting existing value doesn't free value", + maxVal: limit - 1, + check: func(t *testing.T, cache *FIFO[int, int]) { + require := require.New(t) + v, ok := cache.Get(0) + require.True(ok) + require.Equal(v, 0) + exists := cache.Put(0, 10) + require.True(exists) + v, ok = cache.Get(0) + require.True(ok) + require.Equal(v, 10) + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require := require.New(t) + + cache, err := NewFIFO[int, int](limit) + require.NoError(err) + + for i := 0; i <= tt.maxVal; i++ { + exists := cache.Put(i, i) + require.False(exists) + v, ok := cache.Get(i) + require.True(ok) + require.Equal(v, i) + } + + if tt.check != nil { + tt.check(t, cache) + } + }) + } +} From 56d492189f64f7b069bad6c84ddca8204a6dee54 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Fri, 26 Jul 2024 17:21:11 +0200 Subject: [PATCH 02/10] invert expected / actual --- cache/fifo_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index ff2e27c403..f60dce0091 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -30,7 +30,7 @@ func TestFIFOCache(t *testing.T) { require.False(ok) v, ok := cache.Get(1) require.True(ok) - require.Equal(v, 1) + require.Equal(1, v) }, }, { @@ -53,12 +53,12 @@ func TestFIFOCache(t *testing.T) { require := require.New(t) v, ok := cache.Get(0) require.True(ok) - require.Equal(v, 0) + require.Equal(0, v) exists := cache.Put(0, 10) require.True(exists) v, ok = cache.Get(0) require.True(ok) - require.Equal(v, 10) + require.Equal(10, v) }, }, } @@ -75,7 +75,7 @@ func TestFIFOCache(t *testing.T) { require.False(exists) v, ok := cache.Get(i) require.True(ok) - require.Equal(v, i) + require.Equal(i, v) } if tt.check != nil { From f81c9ebbd64715d6c4dcf92381bbf4821e1b1422 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Fri, 26 Jul 2024 20:16:31 +0200 Subject: [PATCH 03/10] add limit parameter and cache fail on 0 size --- cache/fifo_test.go | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index f60dce0091..f385804b80 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -4,26 +4,34 @@ package cache import ( + "errors" "testing" "github.com/stretchr/testify/require" ) func TestFIFOCache(t *testing.T) { - limit := 10 - tests := []struct { - name string - maxVal int - check func(t *testing.T, cache *FIFO[int, int]) + name string + limit int + maxVal int + expected error + check func(t *testing.T, cache *FIFO[int, int]) }{ + { + name: "empty cache fails", + limit: 0, + expected: errors.New("maxSize must be greater than 0"), + }, { name: "inserting up to limit works", - maxVal: limit - 1, + limit: 10, + maxVal: 9, }, { name: "inserting after limit cleans first", - maxVal: limit, + limit: 10, + maxVal: 10, check: func(t *testing.T, cache *FIFO[int, int]) { require := require.New(t) _, ok := cache.Get(0) @@ -35,12 +43,13 @@ func TestFIFOCache(t *testing.T) { }, { name: "inserting before limit can insert again", - maxVal: limit - 2, + limit: 10, + maxVal: 8, check: func(t *testing.T, cache *FIFO[int, int]) { require := require.New(t) - exists := cache.Put(limit-1, limit-1) + exists := cache.Put(9, 9) require.False(exists) - exists = cache.Put(limit, limit) + exists = cache.Put(10, 10) require.False(exists) _, ok := cache.Get(0) require.False(ok) @@ -48,7 +57,8 @@ func TestFIFOCache(t *testing.T) { }, { name: "inserting existing value doesn't free value", - maxVal: limit - 1, + limit: 10, + maxVal: 9, check: func(t *testing.T, cache *FIFO[int, int]) { require := require.New(t) v, ok := cache.Get(0) @@ -67,8 +77,11 @@ func TestFIFOCache(t *testing.T) { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - cache, err := NewFIFO[int, int](limit) - require.NoError(err) + cache, err := NewFIFO[int, int](tt.limit) + require.Equal(tt.expected, err) + if err != nil { + return + } for i := 0; i <= tt.maxVal; i++ { exists := cache.Put(i, i) From 7df4e00c5c06e7010c4ac3943814cd7a1edddb58 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 12:24:45 +0200 Subject: [PATCH 04/10] simplify test table and create new test for empty cache --- cache/fifo_test.go | 236 +++++++++++++++++++++++++++++++++------------ 1 file changed, 172 insertions(+), 64 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index f385804b80..904016b62c 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -10,66 +10,169 @@ import ( "github.com/stretchr/testify/require" ) -func TestFIFOCache(t *testing.T) { +func TestFIFOCacheInsertion(t *testing.T) { + limit := 10 + + type put struct { + i int + exists bool + } + + type get struct { + i int + ok bool + } + tests := []struct { - name string - limit int - maxVal int - expected error - check func(t *testing.T, cache *FIFO[int, int]) + name string + ops []interface{} }{ { - name: "empty cache fails", - limit: 0, - expected: errors.New("maxSize must be greater than 0"), + name: "inserting 10 elements works", + ops: func() []interface{} { + ops := make([]interface{}, 20) + for i := 0; i < limit; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + return ops + }(), + }, + { + name: "inserting after limit cleans first", + ops: func() []interface{} { + ops := make([]interface{}, 23) + for i := 0; i < limit; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + ops[2*limit] = put{ + i: 10, + exists: false, + } + ops[2*limit+1] = get{ + i: 0, + ok: false, + } + ops[2*limit+2] = get{ + i: 1, + ok: true, + } + return ops + }(), }, { - name: "inserting up to limit works", - limit: 10, - maxVal: 9, + name: "no elements removed when cache is exactly full", + ops: func() []interface{} { + ops := make([]interface{}, 20) + for i := 0; i < 9; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + ops[2*9] = put{ + i: 9, + exists: false, + } + ops[2*9+1] = get{ + i: 9, + ok: true, + } + return ops + }(), }, { - name: "inserting after limit cleans first", - limit: 10, - maxVal: 10, - check: func(t *testing.T, cache *FIFO[int, int]) { - require := require.New(t) - _, ok := cache.Get(0) - require.False(ok) - v, ok := cache.Get(1) - require.True(ok) - require.Equal(1, v) - }, + name: "no elements removed when the cache is less than full", + ops: func() []interface{} { + ops := make([]interface{}, 12) + for i := 0; i < 5; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + ops[2*5] = put{ + i: 5, + exists: false, + } + ops[2*5+1] = get{ + i: 0, + ok: true, + } + return ops + }(), }, { - name: "inserting before limit can insert again", - limit: 10, - maxVal: 8, - check: func(t *testing.T, cache *FIFO[int, int]) { - require := require.New(t) - exists := cache.Put(9, 9) - require.False(exists) - exists = cache.Put(10, 10) - require.False(exists) - _, ok := cache.Get(0) - require.False(ok) - }, + name: "inserting existing value when full doesn't free value", + ops: func() []interface{} { + ops := make([]interface{}, 20) + for i := 0; i < limit; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + ops[2*9] = put{ + i: 0, + exists: true, + } + ops[2*9+1] = get{ + i: 0, + ok: true, + } + return ops + }(), }, { - name: "inserting existing value doesn't free value", - limit: 10, - maxVal: 9, - check: func(t *testing.T, cache *FIFO[int, int]) { - require := require.New(t) - v, ok := cache.Get(0) - require.True(ok) - require.Equal(0, v) - exists := cache.Put(0, 10) - require.True(exists) - v, ok = cache.Get(0) - require.True(ok) - require.Equal(10, v) - }, + name: "elements removed in FIFO order when cache overfills", + ops: func() []interface{} { + ops := make([]interface{}, 40) + for i := 0; i < limit; i++ { + ops[2*i] = put{ + i: i, + exists: false, + } + ops[2*i+1] = get{ + i: i, + ok: true, + } + } + for i := 0; i < limit; i++ { + ops[2*(i+limit)] = put{ + i: (2 * (i + limit)), + exists: false, + } + ops[2*(i+limit)+1] = get{ + ok: false, + } + } + return ops + }(), }, } @@ -77,23 +180,28 @@ func TestFIFOCache(t *testing.T) { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - cache, err := NewFIFO[int, int](tt.limit) - require.Equal(tt.expected, err) - if err != nil { - return - } - - for i := 0; i <= tt.maxVal; i++ { - exists := cache.Put(i, i) - require.False(exists) - v, ok := cache.Get(i) - require.True(ok) - require.Equal(i, v) - } + cache, err := NewFIFO[int, int](limit) + require.NoError(err) - if tt.check != nil { - tt.check(t, cache) + for _, op := range tt.ops { + if put, ok := op.(put); ok { + exists := cache.Put(put.i, put.i) + require.Equal(put.exists, exists) + } else if get, ok := op.(get); ok { + val, ok := cache.Get(get.i) + require.Equal(get.ok, ok) + require.Equal(get.i, val) + } else { + require.Fail("op can only be a put or a get") + } } }) } } + +func TestEmptyCacheFails(t *testing.T) { + require := require.New(t) + _, err := NewFIFO[int, int](0) + expectedErr := errors.New("maxSize must be greater than 0") + require.Equal(expectedErr, err) +} From 80b0b5802324f77a4a9c6cbe5f45f8778445d16f Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 12:44:39 +0200 Subject: [PATCH 05/10] simpler tests by having a limit of 2 --- cache/fifo_test.go | 211 +++++++++++++++++++-------------------------- 1 file changed, 91 insertions(+), 120 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index 904016b62c..f845e5314b 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -11,8 +11,6 @@ import ( ) func TestFIFOCacheInsertion(t *testing.T) { - limit := 10 - type put struct { i int exists bool @@ -28,151 +26,122 @@ func TestFIFOCacheInsertion(t *testing.T) { ops []interface{} }{ { - name: "inserting 10 elements works", - ops: func() []interface{} { - ops := make([]interface{}, 20) - for i := 0; i < limit; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - return ops - }(), + name: "inserting up to limit works", + ops: []interface{}{ + put{ + i: 0, + exists: false, + }, + put{ + i: 1, + exists: false, + }, + }, }, { name: "inserting after limit cleans first", - ops: func() []interface{} { - ops := make([]interface{}, 23) - for i := 0; i < limit; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - ops[2*limit] = put{ - i: 10, + ops: []interface{}{ + put{ + i: 0, exists: false, - } - ops[2*limit+1] = get{ + }, + put{ + i: 1, + exists: false, + }, + put{ + i: 2, + exists: false, + }, + get{ i: 0, ok: false, - } - ops[2*limit+2] = get{ + }, + get{ i: 1, ok: true, - } - return ops - }(), + }, + }, }, { name: "no elements removed when cache is exactly full", - ops: func() []interface{} { - ops := make([]interface{}, 20) - for i := 0; i < 9; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - ops[2*9] = put{ - i: 9, + ops: []interface{}{ + put{ + i: 0, exists: false, - } - ops[2*9+1] = get{ - i: 9, + }, + put{ + i: 1, + exists: false, + }, + get{ + i: 0, ok: true, - } - return ops - }(), + }, + get{ + i: 1, + ok: true, + }, + }, }, { name: "no elements removed when the cache is less than full", - ops: func() []interface{} { - ops := make([]interface{}, 12) - for i := 0; i < 5; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - ops[2*5] = put{ - i: 5, + ops: []interface{}{ + put{ + i: 0, exists: false, - } - ops[2*5+1] = get{ + }, + get{ i: 0, ok: true, - } - return ops - }(), + }, + }, }, { name: "inserting existing value when full doesn't free value", - ops: func() []interface{} { - ops := make([]interface{}, 20) - for i := 0; i < limit; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - ops[2*9] = put{ + ops: []interface{}{ + put{ + i: 0, + exists: false, + }, + put{ i: 0, exists: true, - } - ops[2*9+1] = get{ + }, + get{ i: 0, ok: true, - } - return ops - }(), + }, + }, }, { name: "elements removed in FIFO order when cache overfills", - ops: func() []interface{} { - ops := make([]interface{}, 40) - for i := 0; i < limit; i++ { - ops[2*i] = put{ - i: i, - exists: false, - } - ops[2*i+1] = get{ - i: i, - ok: true, - } - } - for i := 0; i < limit; i++ { - ops[2*(i+limit)] = put{ - i: (2 * (i + limit)), - exists: false, - } - ops[2*(i+limit)+1] = get{ - ok: false, - } - } - return ops - }(), + ops: []interface{}{ + put{ + i: 0, + exists: false, + }, + put{ + i: 1, + exists: false, + }, + put{ + i: 2, + exists: false, + }, + get{ + i: 0, + ok: false, + }, + put{ + i: 3, + exists: false, + }, + get{ + i: 1, + ok: false, + }, + }, }, } @@ -180,7 +149,7 @@ func TestFIFOCacheInsertion(t *testing.T) { t.Run(tt.name, func(t *testing.T) { require := require.New(t) - cache, err := NewFIFO[int, int](limit) + cache, err := NewFIFO[int, int](2) require.NoError(err) for _, op := range tt.ops { @@ -190,7 +159,9 @@ func TestFIFOCacheInsertion(t *testing.T) { } else if get, ok := op.(get); ok { val, ok := cache.Get(get.i) require.Equal(get.ok, ok) - require.Equal(get.i, val) + if ok { + require.Equal(get.i, val) + } } else { require.Fail("op can only be a put or a get") } From bf7ee73708e3d93de34f746ad5be9ada4646b3bb Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 12:47:56 +0200 Subject: [PATCH 06/10] fix test cases --- cache/fifo_test.go | 60 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index f845e5314b..e638e4504b 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -12,12 +12,12 @@ import ( func TestFIFOCacheInsertion(t *testing.T) { type put struct { - i int + kv int exists bool } type get struct { - i int + k int ok bool } @@ -29,11 +29,11 @@ func TestFIFOCacheInsertion(t *testing.T) { name: "inserting up to limit works", ops: []interface{}{ put{ - i: 0, + kv: 0, exists: false, }, put{ - i: 1, + kv: 1, exists: false, }, }, @@ -42,44 +42,40 @@ func TestFIFOCacheInsertion(t *testing.T) { name: "inserting after limit cleans first", ops: []interface{}{ put{ - i: 0, + kv: 0, exists: false, }, put{ - i: 1, + kv: 1, exists: false, }, put{ - i: 2, + kv: 2, exists: false, }, get{ - i: 0, + k: 0, ok: false, }, - get{ - i: 1, - ok: true, - }, }, }, { name: "no elements removed when cache is exactly full", ops: []interface{}{ put{ - i: 0, + kv: 0, exists: false, }, put{ - i: 1, + kv: 1, exists: false, }, get{ - i: 0, + k: 0, ok: true, }, get{ - i: 1, + k: 1, ok: true, }, }, @@ -88,11 +84,11 @@ func TestFIFOCacheInsertion(t *testing.T) { name: "no elements removed when the cache is less than full", ops: []interface{}{ put{ - i: 0, + kv: 0, exists: false, }, get{ - i: 0, + k: 0, ok: true, }, }, @@ -101,15 +97,19 @@ func TestFIFOCacheInsertion(t *testing.T) { name: "inserting existing value when full doesn't free value", ops: []interface{}{ put{ - i: 0, + kv: 0, + exists: false, + }, + put{ + kv: 1, exists: false, }, put{ - i: 0, + kv: 0, exists: true, }, get{ - i: 0, + k: 0, ok: true, }, }, @@ -118,27 +118,27 @@ func TestFIFOCacheInsertion(t *testing.T) { name: "elements removed in FIFO order when cache overfills", ops: []interface{}{ put{ - i: 0, + kv: 0, exists: false, }, put{ - i: 1, + kv: 1, exists: false, }, put{ - i: 2, + kv: 2, exists: false, }, get{ - i: 0, + k: 0, ok: false, }, put{ - i: 3, + kv: 3, exists: false, }, get{ - i: 1, + k: 1, ok: false, }, }, @@ -154,13 +154,13 @@ func TestFIFOCacheInsertion(t *testing.T) { for _, op := range tt.ops { if put, ok := op.(put); ok { - exists := cache.Put(put.i, put.i) + exists := cache.Put(put.kv, put.kv) require.Equal(put.exists, exists) } else if get, ok := op.(get); ok { - val, ok := cache.Get(get.i) + val, ok := cache.Get(get.k) require.Equal(get.ok, ok) if ok { - require.Equal(get.i, val) + require.Equal(get.k, val) } } else { require.Fail("op can only be a put or a get") From b634a0d4d177dc6dc11846825a0fcf57719a9ee0 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 14:11:37 +0200 Subject: [PATCH 07/10] typos --- cache/fifo_test.go | 4 ++-- examples/morpheusvm/controller/controller.go | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index e638e4504b..55a072c73b 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -60,7 +60,7 @@ func TestFIFOCacheInsertion(t *testing.T) { }, }, { - name: "no elements removed when cache is exactly full", + name: "no element removed when cache is exactly full", ops: []interface{}{ put{ kv: 0, @@ -81,7 +81,7 @@ func TestFIFOCacheInsertion(t *testing.T) { }, }, { - name: "no elements removed when the cache is less than full", + name: "no element removed when the cache is less than full", ops: []interface{}{ put{ kv: 0, diff --git a/examples/morpheusvm/controller/controller.go b/examples/morpheusvm/controller/controller.go index a4ffac0e93..c88aa37ceb 100644 --- a/examples/morpheusvm/controller/controller.go +++ b/examples/morpheusvm/controller/controller.go @@ -26,7 +26,6 @@ import ( "github.com/ava-labs/hypersdk/vm" ametrics "github.com/ava-labs/avalanchego/api/metrics" - hrpc "github.com/ava-labs/hypersdk/rpc" hstorage "github.com/ava-labs/hypersdk/storage" ) From d4b2f1d4877950ba2ad0328bb5261df28018856b Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 14:12:23 +0200 Subject: [PATCH 08/10] backout uselesss change --- examples/morpheusvm/controller/controller.go | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/morpheusvm/controller/controller.go b/examples/morpheusvm/controller/controller.go index c88aa37ceb..a4ffac0e93 100644 --- a/examples/morpheusvm/controller/controller.go +++ b/examples/morpheusvm/controller/controller.go @@ -26,6 +26,7 @@ import ( "github.com/ava-labs/hypersdk/vm" ametrics "github.com/ava-labs/avalanchego/api/metrics" + hrpc "github.com/ava-labs/hypersdk/rpc" hstorage "github.com/ava-labs/hypersdk/storage" ) From faa881556ecf99b939f6e010d5a1399575563fd5 Mon Sep 17 00:00:00 2001 From: Francois Hardrouyere Date: Mon, 29 Jul 2024 14:34:02 +0200 Subject: [PATCH 09/10] add a "not an LRU" test case --- cache/fifo_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index 55a072c73b..f3b63618a8 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -143,6 +143,35 @@ func TestFIFOCacheInsertion(t *testing.T) { }, }, }, + { + name: "elements removed in FIFO order and not LRU", + ops: []interface{}{ + put{ + kv: 0, + exists: false, + }, + put{ + kv: 1, + exists: false, + }, + put{ + kv: 0, + exists: true, + }, + get{ + k: 0, + ok: true, + }, + put{ + kv: 2, + exists: false, + }, + get{ + k: 0, + ok: false, + }, + }, + }, } for _, tt := range tests { From 738e23a171889bb43f2b4de5875cd1ac43e150f4 Mon Sep 17 00:00:00 2001 From: Aaron Buchwald Date: Tue, 13 Aug 2024 19:30:17 -0400 Subject: [PATCH 10/10] use switch in fifo unit tests --- cache/fifo_test.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cache/fifo_test.go b/cache/fifo_test.go index f3b63618a8..27bf0ce5ee 100644 --- a/cache/fifo_test.go +++ b/cache/fifo_test.go @@ -181,17 +181,18 @@ func TestFIFOCacheInsertion(t *testing.T) { cache, err := NewFIFO[int, int](2) require.NoError(err) - for _, op := range tt.ops { - if put, ok := op.(put); ok { - exists := cache.Put(put.kv, put.kv) - require.Equal(put.exists, exists) - } else if get, ok := op.(get); ok { - val, ok := cache.Get(get.k) - require.Equal(get.ok, ok) + for _, opIntf := range tt.ops { + switch op := opIntf.(type) { + case put: + exists := cache.Put(op.kv, op.kv) + require.Equal(op.exists, exists) + case get: + val, ok := cache.Get(op.k) + require.Equal(op.ok, ok) if ok { - require.Equal(get.k, val) + require.Equal(op.k, val) } - } else { + default: require.Fail("op can only be a put or a get") } }