| 1 |
/* |
| 2 |
Copyright 2016 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 integer |
| 18 |
|
| 19 |
import "testing" |
| 20 |
|
| 21 |
func TestIntMax(t *testing.T) { |
| 22 |
tests := []struct { |
| 23 |
nums []int |
| 24 |
expectedMax int |
| 25 |
}{ |
| 26 |
{ |
| 27 |
nums: []int{-1, 0}, |
| 28 |
expectedMax: 0, |
| 29 |
}, |
| 30 |
{ |
| 31 |
nums: []int{-1, -2}, |
| 32 |
expectedMax: -1, |
| 33 |
}, |
| 34 |
{ |
| 35 |
nums: []int{0, 1}, |
| 36 |
expectedMax: 1, |
| 37 |
}, |
| 38 |
{ |
| 39 |
nums: []int{1, 2}, |
| 40 |
expectedMax: 2, |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
for i, test := range tests { |
| 45 |
t.Logf("executing scenario %d", i) |
| 46 |
if max := IntMax(test.nums[0], test.nums[1]); max != test.expectedMax { |
| 47 |
t.Errorf("expected %v, got %v", test.expectedMax, max) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestIntMin(t *testing.T) { |
| 53 |
tests := []struct { |
| 54 |
nums []int |
| 55 |
expectedMin int |
| 56 |
}{ |
| 57 |
{ |
| 58 |
nums: []int{-1, 0}, |
| 59 |
expectedMin: -1, |
| 60 |
}, |
| 61 |
{ |
| 62 |
nums: []int{-1, -2}, |
| 63 |
expectedMin: -2, |
| 64 |
}, |
| 65 |
{ |
| 66 |
nums: []int{0, 1}, |
| 67 |
expectedMin: 0, |
| 68 |
}, |
| 69 |
{ |
| 70 |
nums: []int{1, 2}, |
| 71 |
expectedMin: 1, |
| 72 |
}, |
| 73 |
} |
| 74 |
|
| 75 |
for i, test := range tests { |
| 76 |
t.Logf("executing scenario %d", i) |
| 77 |
if min := IntMin(test.nums[0], test.nums[1]); min != test.expectedMin { |
| 78 |
t.Errorf("expected %v, got %v", test.expectedMin, min) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestInt32Max(t *testing.T) { |
| 84 |
tests := []struct { |
| 85 |
nums []int32 |
| 86 |
expectedMax int32 |
| 87 |
}{ |
| 88 |
{ |
| 89 |
nums: []int32{-1, 0}, |
| 90 |
expectedMax: 0, |
| 91 |
}, |
| 92 |
{ |
| 93 |
nums: []int32{-1, -2}, |
| 94 |
expectedMax: -1, |
| 95 |
}, |
| 96 |
{ |
| 97 |
nums: []int32{0, 1}, |
| 98 |
expectedMax: 1, |
| 99 |
}, |
| 100 |
{ |
| 101 |
nums: []int32{1, 2}, |
| 102 |
expectedMax: 2, |
| 103 |
}, |
| 104 |
} |
| 105 |
|
| 106 |
for i, test := range tests { |
| 107 |
t.Logf("executing scenario %d", i) |
| 108 |
if max := Int32Max(test.nums[0], test.nums[1]); max != test.expectedMax { |
| 109 |
t.Errorf("expected %v, got %v", test.expectedMax, max) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestInt32Min(t *testing.T) { |
| 115 |
tests := []struct { |
| 116 |
nums []int32 |
| 117 |
expectedMin int32 |
| 118 |
}{ |
| 119 |
{ |
| 120 |
nums: []int32{-1, 0}, |
| 121 |
expectedMin: -1, |
| 122 |
}, |
| 123 |
{ |
| 124 |
nums: []int32{-1, -2}, |
| 125 |
expectedMin: -2, |
| 126 |
}, |
| 127 |
{ |
| 128 |
nums: []int32{0, 1}, |
| 129 |
expectedMin: 0, |
| 130 |
}, |
| 131 |
{ |
| 132 |
nums: []int32{1, 2}, |
| 133 |
expectedMin: 1, |
| 134 |
}, |
| 135 |
} |
| 136 |
|
| 137 |
for i, test := range tests { |
| 138 |
t.Logf("executing scenario %d", i) |
| 139 |
if min := Int32Min(test.nums[0], test.nums[1]); min != test.expectedMin { |
| 140 |
t.Errorf("expected %v, got %v", test.expectedMin, min) |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestInt64Max(t *testing.T) { |
| 146 |
tests := []struct { |
| 147 |
nums []int64 |
| 148 |
expectedMax int64 |
| 149 |
}{ |
| 150 |
{ |
| 151 |
nums: []int64{-1, 0}, |
| 152 |
expectedMax: 0, |
| 153 |
}, |
| 154 |
{ |
| 155 |
nums: []int64{-1, -2}, |
| 156 |
expectedMax: -1, |
| 157 |
}, |
| 158 |
{ |
| 159 |
nums: []int64{0, 1}, |
| 160 |
expectedMax: 1, |
| 161 |
}, |
| 162 |
{ |
| 163 |
nums: []int64{1, 2}, |
| 164 |
expectedMax: 2, |
| 165 |
}, |
| 166 |
} |
| 167 |
|
| 168 |
for i, test := range tests { |
| 169 |
t.Logf("executing scenario %d", i) |
| 170 |
if max := Int64Max(test.nums[0], test.nums[1]); max != test.expectedMax { |
| 171 |
t.Errorf("expected %v, got %v", test.expectedMax, max) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestInt64Min(t *testing.T) { |
| 177 |
tests := []struct { |
| 178 |
nums []int64 |
| 179 |
expectedMin int64 |
| 180 |
}{ |
| 181 |
{ |
| 182 |
nums: []int64{-1, 0}, |
| 183 |
expectedMin: -1, |
| 184 |
}, |
| 185 |
{ |
| 186 |
nums: []int64{-1, -2}, |
| 187 |
expectedMin: -2, |
| 188 |
}, |
| 189 |
{ |
| 190 |
nums: []int64{0, 1}, |
| 191 |
expectedMin: 0, |
| 192 |
}, |
| 193 |
{ |
| 194 |
nums: []int64{1, 2}, |
| 195 |
expectedMin: 1, |
| 196 |
}, |
| 197 |
} |
| 198 |
|
| 199 |
for i, test := range tests { |
| 200 |
t.Logf("executing scenario %d", i) |
| 201 |
if min := Int64Min(test.nums[0], test.nums[1]); min != test.expectedMin { |
| 202 |
t.Errorf("expected %v, got %v", test.expectedMin, min) |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func TestRoundToInt32(t *testing.T) { |
| 208 |
tests := []struct { |
| 209 |
num float64 |
| 210 |
exp int32 |
| 211 |
}{ |
| 212 |
{ |
| 213 |
num: 5.5, |
| 214 |
exp: 6, |
| 215 |
}, |
| 216 |
{ |
| 217 |
num: -3.7, |
| 218 |
exp: -4, |
| 219 |
}, |
| 220 |
{ |
| 221 |
num: 3.49, |
| 222 |
exp: 3, |
| 223 |
}, |
| 224 |
{ |
| 225 |
num: -7.9, |
| 226 |
exp: -8, |
| 227 |
}, |
| 228 |
{ |
| 229 |
num: -4.499999, |
| 230 |
exp: -4, |
| 231 |
}, |
| 232 |
{ |
| 233 |
num: 0, |
| 234 |
exp: 0, |
| 235 |
}, |
| 236 |
{ |
| 237 |
num: 0.49999999999999994, |
| 238 |
exp: 0, |
| 239 |
}, |
| 240 |
} |
| 241 |
|
| 242 |
for i, test := range tests { |
| 243 |
t.Logf("executing scenario %d", i) |
| 244 |
if got := RoundToInt32(test.num); got != test.exp { |
| 245 |
t.Errorf("expected %d, got %d", test.exp, got) |
| 246 |
} |
| 247 |
} |
| 248 |
} |