github.com/blend/go-sdk@v1.20220411.3/mathutil/math_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package mathutil 9 10 import ( 11 "testing" 12 "time" 13 14 "github.com/blend/go-sdk/assert" 15 ) 16 17 func TestMin(t *testing.T) { 18 assert := assert.New(t) 19 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 20 minValue := Min(values) 21 assert.Equal(1.0, minValue) 22 } 23 24 func TestMinRev(t *testing.T) { 25 assert := assert.New(t) 26 values := []float64{10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0} 27 minValue := Min(values) 28 assert.Equal(1.0, minValue) 29 } 30 31 func TestMinEmpty(t *testing.T) { 32 assert := assert.New(t) 33 min := Min([]float64{}) 34 assert.Zero(min) 35 } 36 37 func TestMax(t *testing.T) { 38 assert := assert.New(t) 39 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 40 maxValue := Max(values) 41 assert.Equal(10.0, maxValue) 42 } 43 44 func TestMaxEmpty(t *testing.T) { 45 assert := assert.New(t) 46 max := Max([]float64{}) 47 assert.Zero(max) 48 } 49 50 func TestSum(t *testing.T) { 51 assert := assert.New(t) 52 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 53 sum := Sum(values) 54 assert.Equal(55.0, sum) 55 } 56 57 func TestSumInts(t *testing.T) { 58 assert := assert.New(t) 59 values := []int{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 60 sum := SumInts(values) 61 assert.Equal(55, sum) 62 } 63 64 func TestSumDurations(t *testing.T) { 65 assert := assert.New(t) 66 values := []time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second} 67 sum := SumDurations(values) 68 assert.Equal(6*time.Second, sum) 69 } 70 71 func TestSumEmpty(t *testing.T) { 72 assert := assert.New(t) 73 sum := Sum([]float64{}) 74 assert.Zero(sum) 75 } 76 77 func TestMean(t *testing.T) { 78 assert := assert.New(t) 79 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 80 mean := Mean(values) 81 assert.Equal(5.5, mean) 82 } 83 84 func TestMeanDurations(t *testing.T) { 85 assert := assert.New(t) 86 values := []time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second} 87 mean := MeanDurations(values) 88 assert.Equal(2*time.Second, mean) 89 } 90 91 func TestMeanEmpty(t *testing.T) { 92 assert := assert.New(t) 93 mean := Mean([]float64{}) 94 assert.Zero(mean) 95 } 96 97 func TestMedian(t *testing.T) { 98 assert := assert.New(t) 99 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 100 value := Median(values) 101 assert.Equal(5.5, value) 102 } 103 104 func TestMedianOdd(t *testing.T) { 105 assert := assert.New(t) 106 values := []float64{1.0, 2.0, 3.0} 107 value := Median(values) 108 assert.Equal(2.0, value) 109 } 110 111 func TestMedianEmpty(t *testing.T) { 112 assert := assert.New(t) 113 median := Median([]float64{}) 114 assert.Zero(median) 115 } 116 117 func TestModeSingleInput(t *testing.T) { 118 assert := assert.New(t) 119 values := []float64{1.0} 120 value := Mode(values) 121 assert.Equal([]float64{1.0}, value) 122 } 123 124 func TestModeAllDifferent(t *testing.T) { 125 assert := assert.New(t) 126 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0} 127 value := Mode(values) 128 assert.Equal([]float64{}, value) 129 } 130 131 func TestModeOddLength(t *testing.T) { 132 assert := assert.New(t) 133 values := []float64{1.0, 1.0, 3.0, 4.0, 5.0} 134 value := Mode(values) 135 assert.Equal([]float64{1.0}, value) 136 } 137 138 func TestModeEvenLength(t *testing.T) { 139 assert := assert.New(t) 140 values := []float64{1.0, 1.0, 1.0, 2.0, 3.0, 4.0} 141 value := Mode(values) 142 assert.Equal([]float64{1.0}, value) 143 } 144 145 func TestModeEvenLengthExtra(t *testing.T) { 146 assert := assert.New(t) 147 values := []float64{1.0, 1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 5.0, 5.0, 5.0} 148 value := Mode(values) 149 assert.Equal([]float64{5.0}, value) 150 } 151 152 func TestModeEmpty(t *testing.T) { 153 assert := assert.New(t) 154 empty := Mode([]float64{}) 155 assert.Empty(empty) 156 } 157 158 func TestVar(t *testing.T) { 159 assert := assert.New(t) 160 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 161 value := Var(values, 0) 162 assert.Equal(8.25, value) 163 } 164 165 func TestVarEmpty(t *testing.T) { 166 assert := assert.New(t) 167 zero := Var([]float64{}, 0) 168 assert.Zero(zero) 169 } 170 171 func TestVarP(t *testing.T) { 172 assert := assert.New(t) 173 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 174 value := VarP(values) 175 assert.Equal(8.25, value) 176 } 177 178 func TestVarPEmpty(t *testing.T) { 179 assert := assert.New(t) 180 zero := VarP([]float64{}) 181 assert.Zero(zero) 182 } 183 184 func TestVarS(t *testing.T) { 185 assert := assert.New(t) 186 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0} 187 value := VarS(values) 188 assert.Equal(7.5, value) 189 } 190 191 func TestVarSEmpty(t *testing.T) { 192 assert := assert.New(t) 193 zero := VarS([]float64{}) 194 assert.Zero(zero) 195 } 196 197 func TestStdDevP(t *testing.T) { 198 assert := assert.New(t) 199 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 200 value := StdDevP(values) 201 assert.InDelta(2.872, value, 0.001) 202 } 203 204 func TestStdDevPZero(t *testing.T) { 205 assert := assert.New(t) 206 values := []float64{1.0, 1.0, 1.0, 1.0, 1.0, 1.0} 207 value := StdDevP(values) 208 assert.Equal(0.0, value) 209 } 210 211 func TestStdDevPEmpty(t *testing.T) { 212 assert := assert.New(t) 213 zero := StdDevP([]float64{}) 214 assert.Zero(zero) 215 } 216 217 func TestStdDevS(t *testing.T) { 218 assert := assert.New(t) 219 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 220 value := StdDevS(values) 221 assert.InDelta(3.027, value, 0.001) 222 } 223 224 func TestStdDevSZero(t *testing.T) { 225 assert := assert.New(t) 226 values := []float64{1.0, 1.0, 1.0, 1.0, 1.0, 1.0} 227 value := StdDevS(values) 228 assert.Equal(0.0, value) 229 } 230 231 func TestStdDevSEmpty(t *testing.T) { 232 assert := assert.New(t) 233 zero := StdDevS([]float64{}) 234 assert.Zero(zero) 235 } 236 237 func TestRoundPlaces(t *testing.T) { 238 assert := assert.New(t) 239 value := RoundPlaces(0.55, 1) 240 assert.InDelta(0.6, value, 0.01) 241 } 242 243 func TestRoundDown(t *testing.T) { 244 assert := assert.New(t) 245 value := RoundPlaces(0.54, 1) 246 assert.InDelta(0.5, value, 0.01) 247 } 248 249 func TestRoundNegative(t *testing.T) { 250 assert := assert.New(t) 251 252 value := RoundPlaces(-0.55, 1) 253 assert.InDelta(-0.6, value, 0.01) 254 } 255 256 func TestPercentile(t *testing.T) { 257 assert := assert.New(t) 258 values := []float64{2.0, 10.0, 3.0, 5.0, 6.0, 8.0, 7.0, 9.0, 1.0, 4.0} 259 value := Percentile(values, 90.0) 260 assert.InDelta(9.5, value, 0.0001) 261 } 262 263 func TestPercentileSorted(t *testing.T) { 264 assert := assert.New(t) 265 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 266 value := PercentileSorted(values, 90.0) 267 assert.InDelta(9.5, value, 0.0001) 268 } 269 270 func TestPercentileNonInteger(t *testing.T) { 271 assert := assert.New(t) 272 values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 273 value := Percentile(values, 92.0) 274 assert.InDelta(9.0, value, 0.0001) 275 } 276 277 func TestPercentileEmpty(t *testing.T) { 278 assert := assert.New(t) 279 zero := Percentile([]float64{}, 80.0) 280 assert.Zero(zero) 281 } 282 283 func TestInEpsilon(t *testing.T) { 284 assert := assert.New(t) 285 286 assert.True(InEpsilon(0.0, 0)) 287 assert.False(InEpsilon(0.001, 0)) 288 }