github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/resource/math_test.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resource 18 19 import ( 20 "testing" 21 ) 22 23 func TestDetectOverflowAdd(t *testing.T) { 24 for _, test := range []struct { 25 a, b int64 26 c int64 27 ok bool 28 }{ 29 {0, 0, 0, true}, 30 {-1, 1, 0, true}, 31 {0, 1, 1, true}, 32 {2, 2, 4, true}, 33 {2, -2, 0, true}, 34 {-2, -2, -4, true}, 35 36 {mostNegative, -1, 0, false}, 37 {mostNegative, 1, mostNegative + 1, true}, 38 {mostPositive, -1, mostPositive - 1, true}, 39 {mostPositive, 1, 0, false}, 40 41 {mostNegative, mostPositive, -1, true}, 42 {mostPositive, mostNegative, -1, true}, 43 {mostPositive, mostPositive, 0, false}, 44 {mostNegative, mostNegative, 0, false}, 45 46 {-mostPositive, mostNegative, 0, false}, 47 {mostNegative, -mostPositive, 0, false}, 48 {-mostPositive, -mostPositive, 0, false}, 49 } { 50 c, ok := int64Add(test.a, test.b) 51 if c != test.c { 52 t.Errorf("%v: unexpected result: %d", test, c) 53 } 54 if ok != test.ok { 55 t.Errorf("%v: unexpected overflow: %t", test, ok) 56 } 57 // addition is commutative 58 d, ok2 := int64Add(test.b, test.a) 59 if c != d || ok != ok2 { 60 t.Errorf("%v: not commutative: %d %t", test, d, ok2) 61 } 62 } 63 } 64 65 func TestDetectOverflowMultiply(t *testing.T) { 66 for _, test := range []struct { 67 a, b int64 68 c int64 69 ok bool 70 }{ 71 {0, 0, 0, true}, 72 {-1, 1, -1, true}, 73 {-1, -1, 1, true}, 74 {1, 1, 1, true}, 75 {0, 1, 0, true}, 76 {1, 0, 0, true}, 77 {2, 2, 4, true}, 78 {2, -2, -4, true}, 79 {-2, -2, 4, true}, 80 81 {mostNegative, -1, 0, false}, 82 {mostNegative, 1, mostNegative, true}, 83 {mostPositive, -1, -mostPositive, true}, 84 {mostPositive, 1, mostPositive, true}, 85 86 {mostNegative, mostPositive, 0, false}, 87 {mostPositive, mostNegative, 0, false}, 88 {mostPositive, mostPositive, 1, false}, 89 {mostNegative, mostNegative, 0, false}, 90 91 {-mostPositive, mostNegative, 0, false}, 92 {mostNegative, -mostPositive, 0, false}, 93 {-mostPositive, -mostPositive, 1, false}, 94 } { 95 c, ok := int64Multiply(test.a, test.b) 96 if c != test.c { 97 t.Errorf("%v: unexpected result: %d", test, c) 98 } 99 if ok != test.ok { 100 t.Errorf("%v: unexpected overflow: %t", test, ok) 101 } 102 // multiplication is commutative 103 d, ok2 := int64Multiply(test.b, test.a) 104 if c != d || ok != ok2 { 105 t.Errorf("%v: not commutative: %d %t", test, d, ok2) 106 } 107 } 108 } 109 110 func TestDetectOverflowScale(t *testing.T) { 111 for _, a := range []int64{0, -1, 1, 10, -10, mostPositive, mostNegative, -mostPositive} { 112 for _, b := range []int64{1, 2, 10, 100, 1000, mostPositive} { 113 expect, expectOk := int64Multiply(a, b) 114 115 c, ok := int64MultiplyScale(a, b) 116 if c != expect { 117 t.Errorf("%d*%d: unexpected result: %d", a, b, c) 118 } 119 if ok != expectOk { 120 t.Errorf("%d*%d: unexpected overflow: %t", a, b, ok) 121 } 122 } 123 for _, test := range []struct { 124 base int64 125 fn func(a int64) (int64, bool) 126 }{ 127 {10, int64MultiplyScale10}, 128 {100, int64MultiplyScale100}, 129 {1000, int64MultiplyScale1000}, 130 } { 131 expect, expectOk := int64Multiply(a, test.base) 132 c, ok := test.fn(a) 133 if c != expect { 134 t.Errorf("%d*%d: unexpected result: %d", a, test.base, c) 135 } 136 if ok != expectOk { 137 t.Errorf("%d*%d: unexpected overflow: %t", a, test.base, ok) 138 } 139 } 140 } 141 } 142 143 func TestRemoveInt64Factors(t *testing.T) { 144 for _, test := range []struct { 145 value int64 146 max int64 147 result int64 148 scale int32 149 }{ 150 {100, 10, 1, 2}, 151 {100, 10, 1, 2}, 152 {100, 100, 1, 1}, 153 {1, 10, 1, 0}, 154 } { 155 r, s := removeInt64Factors(test.value, test.max) 156 if r != test.result { 157 t.Errorf("%v: unexpected result: %d", test, r) 158 } 159 if s != test.scale { 160 t.Errorf("%v: unexpected scale: %d", test, s) 161 } 162 } 163 } 164 165 func TestNegativeScaleInt64(t *testing.T) { 166 for _, test := range []struct { 167 base int64 168 scale Scale 169 result int64 170 exact bool 171 }{ 172 {1234567, 0, 1234567, true}, 173 {1234567, 1, 123457, false}, 174 {1234567, 2, 12346, false}, 175 {1234567, 3, 1235, false}, 176 {1234567, 4, 124, false}, 177 178 {-1234567, 0, -1234567, true}, 179 {-1234567, 1, -123457, false}, 180 {-1234567, 2, -12346, false}, 181 {-1234567, 3, -1235, false}, 182 {-1234567, 4, -124, false}, 183 184 {1000, 0, 1000, true}, 185 {1000, 1, 100, true}, 186 {1000, 2, 10, true}, 187 {1000, 3, 1, true}, 188 {1000, 4, 1, false}, 189 190 {-1000, 0, -1000, true}, 191 {-1000, 1, -100, true}, 192 {-1000, 2, -10, true}, 193 {-1000, 3, -1, true}, 194 {-1000, 4, -1, false}, 195 196 {0, 0, 0, true}, 197 {0, 1, 0, true}, 198 {0, 2, 0, true}, 199 200 // negative scale is undefined behavior 201 {1000, -1, 1000, true}, 202 } { 203 result, exact := negativeScaleInt64(test.base, test.scale) 204 if result != test.result { 205 t.Errorf("%v: unexpected result: %d", test, result) 206 } 207 if exact != test.exact { 208 t.Errorf("%v: unexpected exact: %t", test, exact) 209 } 210 } 211 }