-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathreedsolomon_test.cpp
More file actions
92 lines (77 loc) · 2.79 KB
/
reedsolomon_test.cpp
File metadata and controls
92 lines (77 loc) · 2.79 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
#include <cassert>
#include <vector>
#include <sys/time.h>
#include "reedsolomon.h"
#include "fec.h"
static std::vector<row_type> buildShardSet(int dataShards, int parityShards, const std::vector<std::vector<byte>> &payload) {
int total = dataShards + parityShards;
std::vector<row_type> shards(total);
for (int i = 0; i < dataShards; i++) {
shards[i] = std::make_shared<std::vector<byte>>(payload[i]);
}
for (int i = dataShards; i < total; i++) {
shards[i] = std::make_shared<std::vector<byte>>(payload[0].size(), 0);
}
return shards;
}
static void testConstructorMatchesFactory() {
const int dataShards = 4;
const int parityShards = 2;
const int shardLen = 6;
std::vector<std::vector<byte>> payload(dataShards, std::vector<byte>(shardLen));
for (int i = 0; i < dataShards; i++) {
for (int j = 0; j < shardLen; j++) {
payload[i][j] = byte((i + 1) * (j + 3));
}
}
auto shardsFromCtor = buildShardSet(dataShards, parityShards, payload);
auto shardsFromFactory = buildShardSet(dataShards, parityShards, payload);
ReedSolomon directCtor(dataShards, parityShards);
auto factoryCtor = ReedSolomon::New(dataShards, parityShards);
directCtor.Encode(shardsFromCtor);
factoryCtor.Encode(shardsFromFactory);
for (int i = dataShards; i < dataShards + parityShards; i++) {
assert(shardsFromCtor[i]->size() == shardsFromFactory[i]->size());
for (size_t j = 0; j < shardsFromCtor[i]->size(); j++) {
assert((*shardsFromCtor[i])[j] == (*shardsFromFactory[i])[j]);
}
}
}
static void testZeroLengthShardsEncodeReconstruct() {
const int dataShards = 3;
const int parityShards = 2;
const int total = dataShards + parityShards;
auto encoder = ReedSolomon::New(dataShards, parityShards);
std::vector<row_type> shards(total);
for (int i = 0; i < total; i++) {
shards[i] = std::make_shared<std::vector<byte>>();
}
encoder.Encode(shards);
for (int i = dataShards; i < total; i++) {
assert(shards[i]->empty());
}
shards[0] = nullptr;
encoder.Reconstruct(shards);
assert(shards[0] != nullptr);
assert(shards[0]->empty());
}
static void testFecAcceptsFirstPacket() {
const int dataShards = 3;
const int parityShards = 2;
FEC fec = FEC::New(32, dataShards, parityShards);
fecPacket pkt;
pkt.seqid = 0;
pkt.flag = typeData;
pkt.data = std::make_shared<std::vector<byte>>(4, byte(42));
struct timeval tv;
gettimeofday(&tv, NULL);
pkt.ts = uint32_t(tv.tv_sec * 1000 + tv.tv_usec / 1000);
auto recovered = fec.Input(pkt);
assert(recovered.empty());
}
int main() {
testConstructorMatchesFactory();
testZeroLengthShardsEncodeReconstruct();
testFecAcceptsFirstPacket();
return 0;
}