-
Notifications
You must be signed in to change notification settings - Fork 5
/
buffer_test.go
100 lines (79 loc) · 1.91 KB
/
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package origins
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuffer(t *testing.T) {
assert := assert.New(t)
e := &Ident{"", "bill"}
a := &Ident{"", "likes"}
v1 := &Ident{"", "kale"}
v2 := &Ident{"", "pizza"}
v3 := &Ident{"", "coffee"}
f0 := &Fact{
Entity: e,
Attribute: a,
Value: v1,
}
f1 := &Fact{
Entity: e,
Attribute: a,
Value: v2,
}
f2 := &Fact{
Entity: e,
Attribute: a,
Value: v3,
}
facts := Facts{f0, f1, f2}
buf := NewBuffer(facts)
// Internal size defaults to the len of the passed buffer.
// Write position is set to the len of the buffer.
assert.Equal(3, len(buf.buf))
assert.Equal(0, buf.off)
assert.Equal(3, buf.Len())
// Read the next fact.
f := buf.Next()
assert.Equal(f0, f)
assert.Equal(1, buf.off)
// One fact read, 2 remain
assert.Equal(2, buf.Len())
// Append another fact. The contents is shifted so the
// internal size stays the same.
buf.Append(f0)
assert.Equal(3, buf.Len())
assert.Equal(3, len(buf.buf))
// Truncate the internal buffer to only include unread facts.
buf.Truncate(1)
assert.Equal(1, buf.Len())
assert.Equal(1, len(buf.buf))
assert.Equal(0, buf.off)
// Confirm.
f = buf.Next()
assert.Equal(1, buf.off)
assert.Equal(f1, f)
// Buffer is consumed, buffer is truncated.
f = buf.Next()
assert.Equal(0, buf.off)
if f != nil {
t.Error("expected nil on next")
}
// Append beyond internal size, it should at least hold the
// number of passed values.
buf.Append(f0, f1, f2, f0, f1)
assert.Equal(5, buf.Len())
assert.Equal(5, len(buf.buf))
assert.Equal(0, buf.off)
// Return a copy of the unread facts.
facts = buf.Facts()
assert.Equal(Facts{f0, f1, f2, f0, f1}, facts)
assert.Equal(0, buf.Len())
assert.Equal(0, buf.off)
// No facts returned.
facts = buf.Facts()
assert.Equal(Facts{}, facts)
// Append and copy.
buf.Append(f0)
facts = buf.Facts()
assert.Equal(Facts{f0}, facts)
}