-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory_ring_test.go
52 lines (45 loc) · 1.09 KB
/
memory_ring_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
package mimic
import (
"bytes"
"testing"
)
func TestRingMemory(t *testing.T) {
ring := RingMemory{
Backing: make([]byte, 16),
}
// A byte slice to compair against
cmp := make([]byte, 16)
err := ring.Write(0, []byte{1, 2, 3, 4, 5, 6, 7, 8})
if err != nil {
t.Fatal(err)
}
copy(cmp[0:8], []byte{1, 2, 3, 4, 5, 6, 7, 8})
if !bytes.Equal(cmp, ring.Backing) {
t.Fatalf("expected: %v, got: %v", cmp, ring.Backing)
}
err = ring.Write(8, []byte{1, 2, 3, 4, 5, 6, 7, 8})
if err != nil {
t.Fatal(err)
}
copy(cmp[8:], []byte{1, 2, 3, 4, 5, 6, 7, 8})
if !bytes.Equal(cmp, ring.Backing) {
t.Fatalf("expected: %v, got: %v", cmp, ring.Backing)
}
err = ring.Write(12, []byte{1, 2, 3, 4, 5, 6, 7, 8})
if err != nil {
t.Fatal(err)
}
copy(cmp[12:], []byte{1, 2, 3, 4})
copy(cmp, []byte{5, 6, 7, 8})
if !bytes.Equal(cmp, ring.Backing) {
t.Fatalf("expected: %v, got: %v", cmp, ring.Backing)
}
b := make([]byte, 8)
err = ring.Read(12, b)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, []byte{1, 2, 3, 4, 5, 6, 7, 8}) {
t.Fatalf("expected: %v, got: %v", cmp, ring.Backing)
}
}