Skip to content

Commit 3e5f5b2

Browse files
caching: Minor refactoring of tests (#183)
Minor refactoring of tests Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
1 parent 075b3fc commit 3e5f5b2

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

caching/caching_test.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import (
66
"testing"
77
)
88

9+
type testCase[T any] struct {
10+
name string
11+
input int
12+
want T
13+
}
14+
915
// TestCacheWrapper tests the non-thread-safe caching wrapper.
1016
func TestCacheWrapper(t *testing.T) {
1117
// Example function: Calculate factorial of a number.
@@ -20,30 +26,26 @@ func TestCacheWrapper(t *testing.T) {
2026

2127
cachedFactorial := CacheWrapper(factorial)
2228

23-
tests := []struct {
24-
name string
25-
arg int
26-
want *big.Int
27-
}{
29+
tests := []testCase[*big.Int]{
2830
{
29-
name: "success - calculate factorial of 5",
30-
arg: 5,
31-
want: big.NewInt(120),
31+
name: "success - calculate factorial of 5",
32+
input: 5,
33+
want: big.NewInt(120),
3234
},
3335
{
34-
name: "success - calculate factorial of 0",
35-
arg: 0,
36-
want: big.NewInt(1),
36+
name: "success - calculate factorial of 0",
37+
input: 0,
38+
want: big.NewInt(1),
3739
},
3840
{
39-
name: "success - repeated call with factorial of 5",
40-
arg: 5,
41-
want: big.NewInt(120),
41+
name: "success - repeated call with factorial of 5",
42+
input: 5,
43+
want: big.NewInt(120),
4244
},
4345
}
4446
for _, tt := range tests {
4547
t.Run(tt.name, func(t *testing.T) {
46-
if got := cachedFactorial(tt.arg); got.Cmp(tt.want) != 0 {
48+
if got := cachedFactorial(tt.input); got.Cmp(tt.want) != 0 {
4749
t.Errorf("CacheWrapper() = %v, want %v", got, tt.want)
4850
}
4951
})
@@ -59,30 +61,28 @@ func TestSafeCacheWrapper(t *testing.T) {
5961

6062
cachedDouble := SafeCacheWrapper(double)
6163

62-
tests := []struct {
63-
name string
64-
arg int
65-
want int
66-
}{
64+
tests := []testCase[int]{
6765
{
68-
name: "success - double 4",
69-
arg: 4,
70-
want: 8,
66+
name: "success - double 4",
67+
input: 4,
68+
want: 8,
7169
},
7270
{
73-
name: "success - double 0",
74-
arg: 0,
75-
want: 0,
71+
name: "success - double 0",
72+
input: 0,
73+
want: 0,
7674
},
7775
{
78-
name: "success - repeated call with double 4",
79-
arg: 4,
80-
want: 8,
76+
name: "success - repeated call with double 4",
77+
input: 4,
78+
want: 8,
8179
},
8280
}
8381
for _, tt := range tests {
8482
t.Run(tt.name, func(t *testing.T) {
85-
if got := cachedDouble(tt.arg); got != tt.want {
83+
t.Parallel()
84+
85+
if got := cachedDouble(tt.input); got != tt.want {
8686
t.Errorf("SafeCacheWrapper() = %v, want %v", got, tt.want)
8787
}
8888
})
@@ -99,12 +99,12 @@ func TestSafeCacheWrapperConcurrency(t *testing.T) {
9999
cachedSquare := SafeCacheWrapper(square)
100100
var wg sync.WaitGroup
101101

102-
const routines = 10
103-
104102
// Test concurrency with multiple goroutines.
105-
results := make([]int, routines)
106-
wg.Add(routines)
107-
for i := range routines {
103+
const numRoutines = 10
104+
105+
results := make([]int, numRoutines)
106+
wg.Add(numRoutines)
107+
for i := range numRoutines {
108108
go func(idx int) {
109109
defer wg.Done()
110110
results[idx] = cachedSquare(4) // All goroutines calculate square of 4.

0 commit comments

Comments
 (0)