forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialization_frozen_test.go
127 lines (114 loc) · 3.61 KB
/
serialization_frozen_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// +build 386,!appengine amd64,!appengine arm,!appengine arm64,!appengine ppc64le,!appengine mipsle,!appengine mips64le,!appengine mips64p32le,!appengine wasm,!appengine
package roaring
import (
"bytes"
"io/ioutil"
"reflect"
"testing"
)
func TestFrozenFormat(t *testing.T) {
tests := [...]struct{
name, frozenPath, portablePath string
}{
{
name: "bitmaps only",
frozenPath: "testfrozendata/bitmaps_only.frozen",
portablePath: "testfrozendata/bitmaps_only.portable",
},
{
name: "arrays only",
frozenPath: "testfrozendata/arrays_only.frozen",
portablePath: "testfrozendata/arrays_only.portable",
},
{
name: "runs only",
frozenPath: "testfrozendata/runs_only.frozen",
portablePath: "testfrozendata/runs_only.portable",
},
{
name: "mixed",
frozenPath: "testfrozendata/mixed.frozen",
portablePath: "testfrozendata/mixed.portable",
},
}
for _, test := range tests {
// NOTE: opted for loading files twice rather than optimizing it because:
// 1. It's still cheap enough, it's small files; and
// 2. In a buggy scenario one of the tests may write into the buffer and cause
// a race condition, making it harder to figure out why the tests fail.
name, fpath, ppath := test.name, test.frozenPath, test.portablePath
t.Run("view " + name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := ioutil.ReadFile(fpath)
if err != nil {
t.Fatalf("failed to open %s: %s", fpath, err)
}
portableBuf, err := ioutil.ReadFile(ppath)
if err != nil {
t.Fatalf("failed to open %s: %s", ppath, err)
}
frozen, portable := New(), New()
if err := frozen.FrozenView(frozenBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", fpath, err)
}
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
if !frozen.Equals(portable) {
t.Fatalf("bitmaps for %s and %s differ", fpath, ppath)
}
})
t.Run("freeze " + name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := ioutil.ReadFile(fpath)
if err != nil {
t.Fatalf("failed to open %s: %s", fpath, err)
}
portableBuf, err := ioutil.ReadFile(ppath)
if err != nil {
t.Fatalf("failed to open %s: %s", ppath, err)
}
portable := New()
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
frozenSize := portable.GetFrozenSizeInBytes()
if int(frozenSize) != len(frozenBuf) {
t.Errorf("size for serializing %s differs from %s's size", ppath, fpath)
}
frozen, err := portable.Freeze()
if err != nil {
t.Fatalf("can't freeze %s: %s", ppath, err)
}
if !reflect.DeepEqual(frozen, frozenBuf) {
t.Fatalf("frozen file for %s and %s differ", fpath, ppath)
}
})
t.Run("freeze with writer" + name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := ioutil.ReadFile(fpath)
if err != nil {
t.Fatalf("failed to open %s: %s", fpath, err)
}
portableBuf, err := ioutil.ReadFile(ppath)
if err != nil {
t.Fatalf("failed to open %s: %s", ppath, err)
}
portable := New()
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
wr := &bytes.Buffer{}
frozenSize, err := portable.WriteFrozenTo(wr)
if err != nil {
t.Fatalf("can't freeze %s: %s", ppath, err)
}
if int(frozenSize) != len(frozenBuf) {
t.Errorf("size for serializing %s differs from %s's size", ppath, fpath)
}
if !reflect.DeepEqual(wr.Bytes(), frozenBuf) {
t.Fatalf("frozen file for %s and %s differ", fpath, ppath)
}
})
}
}