-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
80 lines (62 loc) · 1.7 KB
/
writer_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
//
// Copyright (C) 2024 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/kshard/wreck
//
package wreck_test
import (
"bytes"
"io"
"testing"
"github.com/kshard/wreck"
)
func TestWriter(t *testing.T) {
sizes := []int{0, 1, 3, 16, 256, 1024}
for _, szData := range sizes {
for _, szUniqueKey := range sizes {
for _, szSortKey := range sizes {
input := data(szUniqueKey, szSortKey, szData)
b := &bytes.Buffer{}
w := wreck.NewWriter[uint8](b)
if err := w.Write(input.UniqueKey, input.SortKey, input.Vector); err != nil {
t.Errorf("unable to encode chunk: %v", err)
}
assertEncodedChunk(t, b, input)
}
}
}
}
func TestEncoder(t *testing.T) {
sizes := []int{0, 1, 3, 16, 256, 1024}
w := wreck.NewEncoder[uint8]()
for _, szData := range sizes {
for _, szUniqueKey := range sizes {
for _, szSortKey := range sizes {
input := data(szUniqueKey, szSortKey, szData)
pckt, err := w.Encode(input.UniqueKey, input.SortKey, input.Vector)
if err != nil {
t.Errorf("unable to encode chunk: %v", err)
}
assertEncodedChunk(t, bytes.NewBuffer(pckt), input)
}
}
}
}
func assertEncodedChunk(t *testing.T, r io.Reader, expected pack) {
t.Helper()
var output wreck.Chunk
if err := wreck.Decode(r, &output); err != nil {
t.Errorf("unable to decode chunk: %v", err)
}
if !bytes.Equal(expected.UniqueKey, output.UniqueKey) {
t.Errorf("unique key is corrupted")
}
if !bytes.Equal(expected.SortKey, output.SortKey) {
t.Errorf("sort key is corrupted")
}
if !bytes.Equal(expected.Vector, output.Vector) {
t.Errorf("data key is corrupted")
}
}