github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ts/testmodel/functions_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package testmodel 12 13 import "testing" 14 15 func TestAggregates(t *testing.T) { 16 tests := []struct { 17 data DataSeries 18 // Expected values 19 sum float64 20 min float64 21 max float64 22 first float64 23 last float64 24 avg float64 25 variance float64 26 }{ 27 // Single value. 28 { 29 data: DataSeries{ 30 dp(1, 23432), 31 }, 32 sum: 23432, 33 min: 23432, 34 max: 23432, 35 first: 23432, 36 last: 23432, 37 avg: 23432, 38 variance: 0, 39 }, 40 // Value is constant. 41 { 42 data: DataSeries{ 43 dp(1, 2), 44 dp(2, 2), 45 dp(3, 2), 46 dp(4, 2), 47 dp(5, 2), 48 }, 49 sum: 10, 50 min: 2, 51 max: 2, 52 first: 2, 53 last: 2, 54 avg: 2, 55 variance: 0, 56 }, 57 // Value is constant zero. 58 { 59 data: DataSeries{ 60 dp(1, 0), 61 dp(2, 0), 62 dp(3, 0), 63 dp(4, 0), 64 dp(5, 0), 65 }, 66 sum: 0, 67 min: 0, 68 max: 0, 69 first: 0, 70 last: 0, 71 avg: 0, 72 variance: 0, 73 }, 74 // Value not constant, compute variance. 75 { 76 data: DataSeries{ 77 dp(1, 2), 78 dp(2, 4), 79 dp(3, 6), 80 }, 81 sum: 12, 82 min: 2, 83 max: 6, 84 first: 2, 85 last: 6, 86 avg: 4, 87 variance: 2.6666666666666665, 88 }, 89 // Negative values, variance still positive. 90 { 91 data: DataSeries{ 92 dp(1, -3), 93 dp(2, -9), 94 dp(3, -6), 95 }, 96 sum: -18, 97 min: -9, 98 max: -3, 99 first: -3, 100 last: -6, 101 avg: -6, 102 variance: 6, 103 }, 104 // Some fairly random numbers, variance and average computed from an online 105 // calculator. Gives good confidence that the functions are actually doing 106 // the correct thing. 107 { 108 data: DataSeries{ 109 dp(1, 60), 110 dp(2, 3), 111 dp(3, 352), 112 dp(4, 7), 113 dp(5, 143), 114 }, 115 sum: 565, 116 min: 3, 117 max: 352, 118 first: 60, 119 last: 143, 120 avg: 113, 121 variance: 16833.2, 122 }, 123 } 124 for _, tt := range tests { 125 t.Run("", func(t *testing.T) { 126 if actual := AggregateSum(tt.data); actual != tt.sum { 127 t.Errorf("AggregateSum() = %v, expected %v", actual, tt.sum) 128 } 129 if actual := AggregateMin(tt.data); actual != tt.min { 130 t.Errorf("AggregateMin() = %v, expected %v", actual, tt.min) 131 } 132 if actual := AggregateMax(tt.data); actual != tt.max { 133 t.Errorf("AggregateMax() = %v, expected %v", actual, tt.max) 134 } 135 if actual := AggregateFirst(tt.data); actual != tt.first { 136 t.Errorf("AggregateFirst() = %v, expected %v", actual, tt.first) 137 } 138 if actual := AggregateLast(tt.data); actual != tt.last { 139 t.Errorf("AggregateLast() = %v, expected %v", actual, tt.last) 140 } 141 if actual := AggregateAverage(tt.data); actual != tt.avg { 142 t.Errorf("AggregateAverage() = %v, expected %v", actual, tt.avg) 143 } 144 if actual := AggregateVariance(tt.data); actual != tt.variance { 145 t.Errorf("AggregateVariance() = %v, expected %v", actual, tt.variance) 146 } 147 }) 148 } 149 }