forked from tipabu/erasurecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_test.go
More file actions
282 lines (242 loc) · 6.97 KB
/
buffer_test.go
File metadata and controls
282 lines (242 loc) · 6.97 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package erasurecode
import (
"bytes"
"io"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
type bufferTest struct {
name string
size, k, m, blockSize int
}
var bufferTests = []bufferTest{
{
name: "200B",
size: 200,
k: 2,
m: 1,
blockSize: 32,
},
{
name: "262145B",
size: 262145,
k: 2,
m: 1,
blockSize: 32768,
},
{
name: "1MB",
size: 1000000,
k: 5,
m: 1,
blockSize: 32768,
},
}
func TestBuffer(t *testing.T) {
for _, test := range bufferTests {
t.Run(test.name, func(t *testing.T) {
size := test.size
k := test.k
blockSize := test.blockSize
b := NewBufferMatrix(blockSize, size, k)
data := make([]byte, size)
for i := 0; i < size; i++ {
data[i] = byte(i)
}
n, err := io.Copy(b, bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, int64(size), n)
newData := b.RealData()
assert.Equal(t, len(data), len(newData))
assert.Equal(t, data, newData)
b2 := NewBufferMatrix(blockSize, size, k)
n, err = b2.ReadFrom(bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, int64(size), n)
newData = b2.RealData()
assert.Equal(t, len(data), len(newData))
assert.Equal(t, data, newData)
})
}
}
func TestEncodeBufferMatrix(t *testing.T) {
for _, test := range bufferTests {
t.Run(test.name, func(t *testing.T) {
size := test.size
k := test.k
m := test.m
blockSize := test.blockSize
b := NewBufferMatrix(blockSize, size, k)
data := bytes.Repeat([]byte("A"), size)
n, err := io.Copy(b, bytes.NewReader(data))
assert.Equal(t, size, int(n))
b.Finish()
assert.NoError(t, err)
defer runtime.KeepAlive(b)
backend, _ := InitBackend(Params{Name: "isa_l_rs_vand", K: k, M: m, W: 8, HD: 5})
defer func() {
_ = backend.Close()
}()
encoded, err := backend.EncodeMatrixWithBufferMatrix(b, blockSize)
assert.NoError(t, err)
defer encoded.Free()
frags := encoded.Data
for i := 0; i < k+m; i++ {
assert.Equal(t, b.FragLen(), len(frags[i]))
}
decoded, err := backend.DecodeMatrix(frags, blockSize)
assert.NoError(t, err)
if err != nil {
t.FailNow()
}
defer decoded.Free()
assert.Equal(t, len(data), len(decoded.Data))
assert.Equal(t, data, decoded.Data[:len(data)])
for i := 0; i < len(data); i++ {
assert.Equal(t, data[i], decoded.Data[i])
}
})
}
}
func TestBufferNew(t *testing.T) {
for _, test := range bufferTests {
t.Run(test.name, func(t *testing.T) {
size := test.size
k := test.k
blockSize := test.blockSize
b := NewBufferMatrix(blockSize, size, k)
b.UseNewFormat()
data := make([]byte, size)
for i := range size {
data[i] = byte(i)
}
n, err := io.Copy(b, bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, int64(size), n)
newData := b.RealData()
assert.Equal(t, len(data), len(newData))
assert.Equal(t, data, newData)
b2 := NewBufferMatrix(blockSize, size, k)
b2.UseNewFormat()
n, err = b2.ReadFrom(bytes.NewReader(data))
assert.NoError(t, err)
assert.Equal(t, int64(size), n)
newData = b2.RealData()
assert.Equal(t, len(data), len(newData))
assert.Equal(t, data, newData)
})
}
}
func TestGetRealBlock(t *testing.T) {
b := NewBufferMatrix(32, 32*5, 2)
assert.Equal(t, 0, b.getRealBlock(0))
assert.Equal(t, 3, b.getRealBlock(1))
assert.Equal(t, 1, b.getRealBlock(2))
assert.Equal(t, 4, b.getRealBlock(3))
assert.Equal(t, 2, b.getRealBlock(4))
assert.Equal(t, 5, b.getRealBlock(5))
}
func TestGetOffset(t *testing.T) {
b := NewBufferMatrix(32, 32*5, 2)
b.curBlock = 0
off, size := b.getOffset()
assert.Equal(t, b.hdrSize, off)
assert.Equal(t, 32, size)
// 2nd block is located after the first part.
// 1st part is 3 blocks long included the header (3*(hdrSize+32))
// and the block is itself starts at offset hdrsize and is 32 bytes long
b.curBlock = 1
off, size = b.getOffset()
assert.Equal(t, 3*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 32, size)
}
// TestGetOffsetNew tests the new offset calculation
// for the case where the block size is not a multiple of the buffer size.
// It ensures that the offsets are calculated correctly
// for the first and last blocks in the buffer.
func TestGetOffsetNew(t *testing.T) {
b := NewBufferMatrix(32, 32*5, 2)
b.UseNewFormat()
b.curBlock = 0
b.leftInBlock = -1
off, size := b.getOffset()
assert.Equal(t, b.hdrSize, off)
assert.Equal(t, 32, size)
// 2nd block is located after the first part.
// 1st part is 3 blocks long included the header (3*(hdrSize+32))
// and the block is itself starts at offset hdrsize and is 32 bytes long
b.curBlock = 1
b.leftInBlock = -1
off, size = b.getOffset()
assert.Equal(t, 3*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 32, size)
// 5th block is located at the end of the first part
b.curBlock = 4
b.leftInBlock = -1
off, size = b.getOffset()
assert.Equal(t, 2*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 16, size)
// 6th block is located at the end of the second part
b.curBlock = 5
b.leftInBlock = -1
off, size = b.getOffset()
assert.Equal(t, 5*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 16, size)
}
// TestGetOffsetNew200 tests the new offset calculation for the case
// of size=200 and block size=32.
func TestGetOffsetNew200(t *testing.T) {
b := NewBufferMatrix(32, 200, 2)
b.UseNewFormat()
b.curBlock = 0
b.leftInBlock = -1
off, size := b.getOffsetNew()
assert.Equal(t, b.hdrSize, off)
assert.Equal(t, 32, size)
// 2nd block is located after the first part.
// 1st part is 3 blocks long included the header (3*(hdrSize+32))
// and the block is itself starts at offset hdrsize and is 32 bytes long
b.curBlock = 1
b.leftInBlock = -1
off, size = b.getOffsetNew()
assert.Equal(t, 4*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 32, size)
b.curBlock = 4
b.leftInBlock = -1
off, size = b.getOffsetNew()
assert.Equal(t, 2*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 32, size)
b.curBlock = 6
b.leftInBlock = -1
off, size = b.getOffsetNew()
assert.Equal(t, 3*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 4, size)
// 6th block is located at the end of the second part
b.curBlock = 7
b.leftInBlock = -1
off, size = b.getOffsetNew()
assert.Equal(t, 7*(b.hdrSize+32)+b.hdrSize, off)
assert.Equal(t, 4, size)
}
// TestIsBlockInLastSubGroup tests the IsBlockInLastSubGroup method
func TestIsBlockInLastSubGroup(t *testing.T) {
b := NewBufferMatrix(32, 32*5, 2)
nbSubGroups := b.SubGroups()
assert.Equal(t, 3, nbSubGroups)
assert.True(t, b.IsBlockInLastSubGroup(4))
assert.True(t, b.IsBlockInLastSubGroup(5))
b = NewBufferMatrix(32, 32*4+2, 4)
nbSubGroups = b.SubGroups()
assert.Equal(t, 2, nbSubGroups)
assert.False(t, b.IsBlockInLastSubGroup(3))
assert.True(t, b.IsBlockInLastSubGroup(4))
}
func TestComputeSizeOfLastSubGroup(t *testing.T) {
b := NewBufferMatrix(32, 200, 2)
b.UseNewFormat()
size := b.FragLenLastSubGroup()
assert.Equal(t, 4, size)
s := b.ComputeSizeOfLastSubGroup()
assert.Equal(t, 8, s)
}