-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
57 lines (47 loc) · 1.39 KB
/
main_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
package main
import (
"testing"
)
func TestRingBuffer(t *testing.T) {
rb := &ringBuffer{}
// Test adding values to the ring buffer
rb.add(10)
rb.add(20)
rb.add(30)
// Test the average calculation
avg := rb.avg()
expectedAvg := float32(20.0) // (10 + 20 + 30) / 3 = 20
if avg != expectedAvg {
t.Errorf("Average mismatch. Got: %f, Expected: %f", avg, expectedAvg)
}
}
func TestRingBufferFull(t *testing.T) {
rb := &ringBuffer{}
for i := 1; i <= 128; i++ { // 128 is the buffer size
rb.add(uint32(i))
}
// Test the average calculation after exceeding the buffer size
avg := rb.avg()
// triange calc for 0..128 = 128 * (128 + 1) / 2 = 8256
// 8256 / 128 = 64.5
expectedAvg := float32(64.5)
if avg != expectedAvg {
t.Errorf("Average mismatch after exceeding buffer size. Got: %f, Expected: %f", avg, expectedAvg)
}
}
func TestRingBufferOverflow(t *testing.T) {
rb := &ringBuffer{}
for i := 0; i < 130; i++ { // 2 more than the buffer size
rb.add(uint32(i + 1))
}
// Test the average calculation after exceeding the buffer size
avg := rb.avg()
// triange calc for 0..128 = 128 * (128 + 1) / 2 = 8256
// replace first two values (1, 2) with (129, 130)
// 8256 - 1 - 2 + 129 + 130 = 8256 + 256 = 8512
// 8512 / 128 = 66.5
expectedAvg := float32(66.5)
if avg != expectedAvg {
t.Errorf("Average mismatch after exceeding buffer size. Got: %f, Expected: %f", avg, expectedAvg)
}
}