-
Notifications
You must be signed in to change notification settings - Fork 6
/
calc_utils_test.go
141 lines (135 loc) · 2.78 KB
/
calc_utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cache
import (
"math"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIncr(t *testing.T) {
testCases := []struct {
name string
originVal any
updateVal any
afterIncr any
wantErr error
}{
{
name: "int",
originVal: 1,
updateVal: 2,
afterIncr: 1<<(strconv.IntSize-1) - 1,
wantErr: ErrIncrementOverflow,
},
{
name: "int32",
originVal: int32(1),
updateVal: int32(2),
afterIncr: int32(math.MaxInt32),
wantErr: ErrIncrementOverflow,
},
{
name: "int64",
originVal: int64(1),
updateVal: int64(2),
afterIncr: int64(math.MaxInt64),
wantErr: ErrIncrementOverflow,
},
{
name: "uint",
originVal: uint(1),
updateVal: uint(2),
afterIncr: uint(1<<(strconv.IntSize) - 1),
wantErr: ErrIncrementOverflow,
},
{
name: "uint32",
originVal: uint32(1),
updateVal: uint32(2),
afterIncr: uint32(math.MaxUint32),
wantErr: ErrIncrementOverflow,
},
{
name: "uint64",
originVal: uint64(1),
updateVal: uint64(2),
afterIncr: uint64(math.MaxUint64),
wantErr: ErrIncrementOverflow,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
val, err := incr(tc.originVal)
assert.Nil(t, err)
assert.Equal(t, val, tc.updateVal)
_, err = incr(tc.afterIncr)
assert.Equal(t, ErrIncrementOverflow, err)
})
}
// other type
_, err := incr("string")
assert.Equal(t, ErrNotIntegerType, err)
}
func TestDecr(t *testing.T) {
testCases := []struct {
name string
originVal any
updateVal any
afterDecr any
wantErr error
}{
{
name: "int",
originVal: 2,
updateVal: 1,
afterDecr: -1 << (strconv.IntSize - 1),
wantErr: ErrDecrementOverflow,
},
{
name: "int32",
originVal: int32(2),
updateVal: int32(1),
afterDecr: int32(math.MinInt32),
wantErr: ErrDecrementOverflow,
},
{
name: "int64",
originVal: int64(2),
updateVal: int64(1),
afterDecr: int64(math.MinInt64),
wantErr: ErrDecrementOverflow,
},
{
name: "uint",
originVal: uint(2),
updateVal: uint(1),
afterDecr: uint(0),
wantErr: ErrDecrementOverflow,
},
{
name: "uint32",
originVal: uint32(2),
updateVal: uint32(1),
afterDecr: uint32(0),
wantErr: ErrDecrementOverflow,
},
{
name: "uint64",
originVal: uint64(2),
updateVal: uint64(1),
afterDecr: uint64(0),
wantErr: ErrDecrementOverflow,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
val, err := decr(tc.originVal)
assert.Nil(t, err)
assert.Equal(t, val, tc.updateVal)
_, err = decr(tc.afterDecr)
assert.Equal(t, ErrDecrementOverflow, err)
})
}
// other type
_, err := decr("string")
assert.Equal(t, ErrNotIntegerType, err)
}