-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlf.cc
executable file
·134 lines (111 loc) · 4.29 KB
/
rlf.cc
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
#include "rlf.h"
#include <gmock/gmock-matchers.h>
#include <gmock/gmock-more-matchers.h>
#include <gtest/gtest.h>
#include <spdlog/spdlog.h>
using namespace testing;
TEST(Well512, BitDistribution)
{
spdlog::set_level(spdlog::level::debug);
well_512 generator;
generator.set_seed(13u);
auto total_samples = 10'000'000u;
auto sum_values = 0u;
for (auto idx = 0u; idx < total_samples; ++idx)
{
auto val = generator.rand_bit();
sum_values += val;
}
auto avg = double(sum_values) / double(total_samples);
EXPECT_THAT(avg, DoubleNear(0.5, 0.0001));
}
TEST(RLF, EncodeSimple)
{
spdlog::set_level(spdlog::level::debug);
std::vector<char*> encoded_symbols;
auto single_data_size = 4u;
auto multiple_data = 500u;
auto total_data_size = single_data_size * multiple_data;
auto symbol_length = 2u;
auto input_symbol_num = total_data_size / symbol_length;
auto seed = 13u;
auto encode_number = input_symbol_num + 10;
std::vector<char> data{};
{
unsigned char raw_data[] = {0xDE, 0xAD, 0xBE, 0xEF};
data.resize(total_data_size);
for (auto copy_num = 0u; copy_num < multiple_data; ++copy_num)
memcpy(data.data() + single_data_size * copy_num, raw_data, single_data_size);
Codes::Fountain::RLF encoder;
encoder.set_seed(seed);
encoder.set_input_data(data.data(), data.size(), true);
encoder.set_symbol_length(symbol_length);
for (auto enc_num = 0u; enc_num < encode_number; ++enc_num)
encoded_symbols.push_back(encoder.generate_symbol());
}
{
Codes::Fountain::RLF decoder;
decoder.set_seed(seed);
decoder.set_input_data_size(total_data_size);
decoder.set_symbol_length(symbol_length);
for (auto enc_num = 0u; enc_num < encoded_symbols.size(); ++enc_num)
decoder.feed_symbol(encoded_symbols[enc_num], enc_num, true);
ASSERT_TRUE(decoder.decode());
auto* payload = decoder.decoded_buffer();
std::vector<char> decoded;
decoded.resize(total_data_size);
memcpy(decoded.data(), payload, total_data_size);
delete[] payload;
ASSERT_THAT(data, Eq(decoded));
}
for (const auto* encoded_symbol : encoded_symbols)
delete[] encoded_symbol;
}
TEST(RLF, EncodeOnTheFly)
{
spdlog::set_level(spdlog::level::debug);
std::vector<char*> encoded_symbols;
auto single_data_size = 4u;
auto multiple_data = 500u;
auto total_data_size = single_data_size * multiple_data;
auto symbol_length = 2u;
auto input_symbol_num = total_data_size / symbol_length;
auto seed = 13u;
auto encode_number = input_symbol_num + 10;
std::vector<char> data{};
{
unsigned char raw_data[] = {0xDE, 0xAD, 0xBE, 0xEF};
data.resize(total_data_size);
for (auto copy_num = 0u; copy_num < multiple_data; ++copy_num)
memcpy(data.data() + single_data_size * copy_num, raw_data, single_data_size);
Codes::Fountain::RLF encoder;
encoder.set_seed(seed);
encoder.set_input_data(data.data(), data.size(), true);
encoder.set_symbol_length(symbol_length);
for (auto enc_num = 0u; enc_num < encode_number; ++enc_num)
encoded_symbols.push_back(encoder.generate_symbol());
}
{
Codes::Fountain::RLF decoder;
decoder.set_seed(seed);
decoder.set_input_data_size(total_data_size);
decoder.set_symbol_length(symbol_length);
auto already_decoded = false;
for (auto enc_num = 0u; enc_num < encoded_symbols.size(); ++enc_num)
{
decoder.feed_symbol(encoded_symbols[enc_num], enc_num, true);
already_decoded = decoder.decode(true);
if (already_decoded)
break;
}
ASSERT_TRUE(already_decoded);
auto* payload = decoder.decoded_buffer();
std::vector<char> decoded;
decoded.resize(total_data_size);
memcpy(decoded.data(), payload, total_data_size);
delete[] payload;
ASSERT_THAT(data, Eq(decoded));
for (const auto* encoded_symbol : encoded_symbols)
delete[] encoded_symbol;
}
}