-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate_test.go
180 lines (167 loc) · 3.9 KB
/
estimate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2024 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backup
import (
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCalculateStats(t *testing.T) {
tests := []struct {
name string
data []float64
expected estimateStats
}{
{
name: "Normal dataset",
data: []float64{10, 20, 30, 40, 50},
expected: estimateStats{
Mean: 30.0,
Variance: 250.0,
},
},
{
name: "Single value dataset",
data: []float64{100},
expected: estimateStats{
Mean: 100.0,
Variance: 0.0,
},
},
{
name: "Empty dataset",
data: []float64{},
expected: estimateStats{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stats := calculateStats(tt.data)
assert.Equal(t, tt.expected.Mean, stats.Mean, "Mean should be calculated correctly")
assert.Equal(t, tt.expected.Variance, stats.Variance, "Variance should be calculated correctly")
})
}
}
func TestConfidenceInterval(t *testing.T) {
tests := []struct {
name string
stats estimateStats
sampleSize int
expectedLo float64
expectedHi float64
}{
{
name: "Normal dataset",
stats: estimateStats{
Mean: 42.0,
Variance: 25.0,
},
sampleSize: 100,
expectedLo: 40.712,
expectedHi: 43.288,
},
{
name: "Small sample size",
stats: estimateStats{
Mean: 50.0,
Variance: 0.0,
},
sampleSize: 1,
expectedLo: 50.0,
expectedHi: 50.0,
},
{
name: "Empty dataset",
stats: estimateStats{
Mean: 0.0,
Variance: 0.0,
},
sampleSize: 0,
expectedLo: 0.0,
expectedHi: 0.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
low, high := confidenceInterval(tt.stats, tt.sampleSize)
assert.Equal(t, tt.expectedLo, low, "Low should be calculated correctly")
assert.Equal(t, tt.expectedHi, high, "High should be calculated correctly")
})
}
}
func TestGetCompressRatio(t *testing.T) {
policyCompressed := NewCompressionPolicy(CompressZSTD, 20)
policyUnCompressed := NewCompressionPolicy(CompressNone, 0)
sampleData := []byte("sample data for compression")
tests := []struct {
name string
policy *CompressionPolicy
data []byte
result float64
}{
{
name: "Valid compression",
policy: policyCompressed,
data: sampleData,
result: 0.675,
},
{
name: "Uncompressed",
policy: policyUnCompressed,
data: sampleData,
result: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ratio, err := getCompressRatio(tt.policy, tt.data)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, tt.result, ratio, "Ratio should be correct")
})
}
}
func TestGetEstimate(t *testing.T) {
logger := slog.New(slog.NewTextHandler(nil, nil))
tests := []struct {
name string
data []float64
total float64
expected float64
}{
{
name: "Normal dataset",
data: []float64{10, 20, 30, 40, 50},
total: 100,
expected: 3000.0,
},
{
name: "Single value dataset",
data: []float64{100},
total: 10,
expected: 1000.0,
},
{
name: "Empty dataset",
data: []float64{},
total: 100,
expected: 0.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getEstimate(tt.data, tt.total, logger)
assert.Equal(t, tt.expected, result, "Estimate should be calculated correctly")
})
}
}