-
Notifications
You must be signed in to change notification settings - Fork 1
/
circular_buffer_test.go
84 lines (72 loc) · 1.81 KB
/
circular_buffer_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
package gerty
import (
"testing"
"testing/quick"
)
var cases = []struct {
name string
arr []Result
capacity int
expectedLength int
}{
{"appending no elements should have length zero", []Result{}, 5, 0},
{"appending few elements should have pararmeters length", []Result{OK, NOK, OK, OK}, 5, 4},
{"appending more elements than capacity should have cap length", []Result{OK, OK, OK, NOK}, 2, 2},
}
func TestCircularBufferLength(t *testing.T) {
for _, c := range cases {
buffer := NewCircularBuffer(c.capacity)
for _, v := range c.arr {
buffer.Append(v)
}
retrieved := buffer.GetValues()
// test length.
if len(retrieved) != c.expectedLength {
t.Errorf(c.name)
}
// sanity check: test all returned elements are present.
for _, r := range retrieved {
if !contains(c.arr, r.Value) {
t.Errorf("element %d was retrieved but was not present in original array (%v)", r.Value, c.arr)
}
}
}
}
func containsAll(container, arr []Result) bool {
for i := range arr {
if !contains(container, arr[i]) {
return false
}
}
return true
}
func contains(arr []Result, i Result) bool {
for _, j := range arr {
if i == j {
return true
}
}
return false
}
func TestShouldAlwaysReturnLastAppendedValues(t *testing.T) {
f := func(gencap SmallInt, elementsToAdd []Result) bool {
capacity := gencap.value
start := 0
if len(elementsToAdd)-capacity > 0 {
start = len(elementsToAdd) - capacity
}
expected := elementsToAdd[start:]
buffer := NewCircularBuffer(capacity)
for _, el := range elementsToAdd {
buffer.Append(el)
}
retrieved := []Result{}
for _, v := range buffer.GetValues() {
retrieved = append(retrieved, v.Value)
}
return containsAll(retrieved, expected)
}
if err := quick.Check(f, nil); err != nil {
t.Errorf("error %v", err)
}
}