github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/cache/calc_utils_test.go (about) 1 // The package is migrated from beego, you can get from following link: 2 // import( 3 // 4 // "github.com/beego/beego/v2/client/cache" 5 // 6 // ) 7 // Copyright 2023. All Rights Reserved. 8 // 9 // Licensed under the Apache License, Version 2.0 (the "License"); 10 // you may not use this file except in compliance with the License. 11 // You may obtain a copy of the License at 12 // 13 // http://www.apache.org/licenses/LICENSE-2.0 14 // 15 // Unless required by applicable law or agreed to in writing, software 16 // distributed under the License is distributed on an "AS IS" BASIS, 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 // See the License for the specific language governing permissions and 19 // limitations under the License. 20 package cache 21 22 import ( 23 "math" 24 "strconv" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestIncr(t *testing.T) { 31 // int 32 var originVal interface{} = int(1) 33 var updateVal interface{} = int(2) 34 val, err := incr(originVal) 35 assert.Nil(t, err) 36 assert.Equal(t, val, updateVal) 37 38 _, err = incr(int(1<<(strconv.IntSize-1) - 1)) 39 assert.Equal(t, ErrIncrementOverflow, err) 40 41 // int32 42 originVal = int32(1) 43 updateVal = int32(2) 44 val, err = incr(originVal) 45 assert.Nil(t, err) 46 assert.Equal(t, val, updateVal) 47 48 _, err = incr(int32(math.MaxInt32)) 49 assert.Equal(t, ErrIncrementOverflow, err) 50 51 // int64 52 originVal = int64(1) 53 updateVal = int64(2) 54 val, err = incr(originVal) 55 assert.Nil(t, err) 56 assert.Equal(t, val, updateVal) 57 58 _, err = incr(int64(math.MaxInt64)) 59 assert.Equal(t, ErrIncrementOverflow, err) 60 61 // uint 62 originVal = uint(1) 63 updateVal = uint(2) 64 val, err = incr(originVal) 65 66 assert.Nil(t, err) 67 assert.Equal(t, val, updateVal) 68 69 _, err = incr(uint(1<<(strconv.IntSize) - 1)) 70 assert.Equal(t, ErrIncrementOverflow, err) 71 72 // uint32 73 originVal = uint32(1) 74 updateVal = uint32(2) 75 val, err = incr(originVal) 76 assert.Nil(t, err) 77 assert.Equal(t, val, updateVal) 78 79 _, err = incr(uint32(math.MaxUint32)) 80 assert.Equal(t, ErrIncrementOverflow, err) 81 82 // uint64 83 originVal = uint64(1) 84 updateVal = uint64(2) 85 val, err = incr(originVal) 86 assert.Nil(t, err) 87 assert.Equal(t, val, updateVal) 88 89 _, err = incr(uint64(math.MaxUint64)) 90 assert.Equal(t, ErrIncrementOverflow, err) 91 // other type 92 _, err = incr("string") 93 assert.Equal(t, ErrNotIntegerType, err) 94 } 95 96 func TestDecr(t *testing.T) { 97 // int 98 var originVal interface{} = int(2) 99 var updateVal interface{} = int(1) 100 val, err := decr(originVal) 101 assert.Nil(t, err) 102 assert.Equal(t, val, updateVal) 103 104 _, err = decr(int(-1 << (strconv.IntSize - 1))) 105 assert.Equal(t, ErrDecrementOverflow, err) 106 // int32 107 originVal = int32(2) 108 updateVal = int32(1) 109 val, err = decr(originVal) 110 assert.Nil(t, err) 111 assert.Equal(t, val, updateVal) 112 113 _, err = decr(int32(math.MinInt32)) 114 assert.Equal(t, ErrDecrementOverflow, err) 115 116 // int64 117 originVal = int64(2) 118 updateVal = int64(1) 119 val, err = decr(originVal) 120 assert.Nil(t, err) 121 assert.Equal(t, val, updateVal) 122 123 _, err = decr(int64(math.MinInt64)) 124 assert.Equal(t, ErrDecrementOverflow, err) 125 126 // uint 127 originVal = uint(2) 128 updateVal = uint(1) 129 val, err = decr(originVal) 130 assert.Nil(t, err) 131 assert.Equal(t, val, updateVal) 132 133 _, err = decr(uint(0)) 134 assert.Equal(t, ErrDecrementOverflow, err) 135 136 // uint32 137 originVal = uint32(2) 138 updateVal = uint32(1) 139 val, err = decr(originVal) 140 assert.Nil(t, err) 141 assert.Equal(t, val, updateVal) 142 143 _, err = decr(uint32(0)) 144 assert.Equal(t, ErrDecrementOverflow, err) 145 146 // uint64 147 originVal = uint64(2) 148 updateVal = uint64(1) 149 val, err = decr(originVal) 150 assert.Nil(t, err) 151 assert.Equal(t, val, updateVal) 152 153 _, err = decr(uint64(0)) 154 assert.Equal(t, ErrDecrementOverflow, err) 155 156 // other type 157 _, err = decr("string") 158 assert.Equal(t, ErrNotIntegerType, err) 159 }