Skip to content

Commit 5c0985b

Browse files
committed
Fec utest: update Fnt and Nf4 tests
- Test Fnt sys & non-sys, vertical & horizontal - Update Nf4 test
1 parent 51947eb commit 5c0985b

File tree

1 file changed

+86
-56
lines changed

1 file changed

+86
-56
lines changed

test/fec_utest.cpp

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FecTestCommon : public ::testing::Test {
4040
public:
4141
const unsigned n_data = 3;
4242
const unsigned n_parities = 3;
43-
const size_t pkt_size = 16;
43+
const size_t pkt_size = 9;
4444

4545
void run_test_horizontal(
4646
fec::FecCode<T>& fec,
@@ -55,7 +55,7 @@ class FecTestCommon : public ::testing::Test {
5555

5656
vec::Vector<T> data_frags(gf, n_data);
5757
vec::Vector<T> copied_data_frags(gf, n_data);
58-
vec::Vector<T> encoded_frags(gf, fec.n);
58+
vec::Vector<T> encoded_frags(gf, fec.get_n_outputs());
5959
vec::Vector<T> received_frags(gf, n_data);
6060
vec::Vector<T> decoded_frags(gf, n_data);
6161
std::vector<int> ids;
@@ -65,10 +65,10 @@ class FecTestCommon : public ::testing::Test {
6565
ids.push_back(i);
6666
}
6767

68-
std::vector<quadiron::Properties> props(code_len);
68+
std::vector<quadiron::Properties> props(fec.get_n_outputs());
6969
for (int j = 0; j < 1000; j++) {
7070
if (props_flag) {
71-
for (int i = 0; i < code_len; i++) {
71+
for (int i = 0; i < fec.get_n_outputs(); i++) {
7272
props[i] = quadiron::Properties();
7373
}
7474
}
@@ -86,8 +86,25 @@ class FecTestCommon : public ::testing::Test {
8686
std::random_shuffle(ids.begin(), ids.end());
8787
for (unsigned i = 0; i < n_data; i++) {
8888
fragments_ids.set(i, ids.at(i));
89-
received_frags.set(i, encoded_frags.get(ids.at(i)));
9089
}
90+
if (fec.type == fec::FecType::SYSTEMATIC) {
91+
for (unsigned i = 0; i < n_data; i++) {
92+
if (fragments_ids.get(i) < n_data) {
93+
received_frags.set(
94+
i, data_frags.get(fragments_ids.get(i)));
95+
} else {
96+
received_frags.set(
97+
i,
98+
encoded_frags.get(fragments_ids.get(i) - n_data));
99+
}
100+
}
101+
} else {
102+
for (unsigned i = 0; i < n_data; i++) {
103+
received_frags.set(
104+
i, encoded_frags.get(fragments_ids.get(i)));
105+
}
106+
}
107+
91108
std::unique_ptr<fec::DecodeContext<T>> context =
92109
fec.init_context_dec(fragments_ids, props);
93110

@@ -100,86 +117,92 @@ class FecTestCommon : public ::testing::Test {
100117
void run_test_vertical(
101118
fec::FecCode<T>& fec,
102119
bool props_flag = false,
103-
bool is_nf4 = false,
104120
bool has_meta = false)
105121
{
106122
const int code_len = n_data + n_parities;
107123

108124
const quadiron::gf::Field<T>& gf = fec.get_gf();
109-
const quadiron::gf::NF4<T>& nf4 =
110-
static_cast<const quadiron::gf::NF4<T>&>(gf);
111125

112126
vec::Buffers<T> data_frags(n_data, pkt_size, has_meta);
113-
vec::Buffers<T> copied_data_frags(n_data, pkt_size, has_meta);
114-
vec::Buffers<T> encoded_frags(fec.n, pkt_size, has_meta);
127+
vec::Buffers<T> encoded_frags(fec.get_n_outputs(), pkt_size, has_meta);
115128
vec::Buffers<T> received_frags(n_data, pkt_size, has_meta);
116129
vec::Buffers<T> decoded_frags(n_data, pkt_size, has_meta);
117130
std::vector<int> ids;
118131
vec::Vector<T> fragments_ids(gf, n_data);
119132

133+
// It's necessary to set `data_frags` all zeros for `RsNf4` as
134+
// `data_frags` has not meta
135+
data_frags.zero_fill();
136+
120137
for (int i = 0; i < code_len; i++) {
121138
ids.push_back(i);
122139
}
123140

124-
std::vector<quadiron::Properties> props(code_len);
125-
for (int j = 0; j < 1000; j++) {
141+
std::vector<quadiron::Properties> props(fec.get_n_outputs());
142+
for (int j = 0; j < 1; j++) {
126143
if (props_flag) {
127-
for (int i = 0; i < code_len; i++) {
144+
for (int i = 0; i < fec.get_n_outputs(); i++) {
128145
props[i] = quadiron::Properties();
129146
}
130147
}
131-
132148
const std::vector<T*> mem = data_frags.get_mem();
133149
for (unsigned i = 0; i < n_data; i++) {
134-
for (size_t j = 0; j < pkt_size; ++j) {
135-
if (has_meta) {
136-
const T hi = is_nf4 ? nf4.unpacked_rand() : gf.rand();
137-
const T lo = is_nf4 ? nf4.unpacked_rand() : gf.rand();
138-
data_frags.set(i, j, hi, lo);
139-
} else {
140-
mem[i][j] = is_nf4 ? nf4.unpacked_rand() : gf.rand();
141-
}
150+
char* buf = reinterpret_cast<char*>(mem[i]);
151+
for (size_t j = 0; j < fec.buf_size; ++j) {
152+
buf[j] = static_cast<char>(gf.rand());
142153
}
143154
}
144155
if (has_meta) {
145156
data_frags.reset_meta();
146157
}
147158

148-
// FIXME: ngff4 will modify v after encode
149-
copied_data_frags.copy(data_frags);
150-
151159
fec.encode(encoded_frags, props, 0, data_frags);
152160

153161
std::random_shuffle(ids.begin(), ids.end());
154162
for (unsigned i = 0; i < n_data; i++) {
155163
fragments_ids.set(i, ids.at(i));
156-
received_frags.copy(encoded_frags, ids.at(i), i);
157164
}
165+
if (fec.type == fec::FecType::SYSTEMATIC) {
166+
for (unsigned i = 0; i < n_data; i++) {
167+
if (fragments_ids.get(i) < n_data) {
168+
received_frags.copy(
169+
data_frags, fragments_ids.get(i), i);
170+
} else {
171+
received_frags.copy(
172+
encoded_frags, fragments_ids.get(i) - n_data, i);
173+
}
174+
}
175+
} else {
176+
for (unsigned i = 0; i < n_data; i++) {
177+
received_frags.copy(encoded_frags, fragments_ids.get(i), i);
178+
}
179+
}
180+
158181
std::unique_ptr<fec::DecodeContext<T>> context =
159182
fec.init_context_dec(
160183
fragments_ids, props, pkt_size, &decoded_frags);
161184

162185
fec.decode(*context, decoded_frags, props, 0, received_frags);
163186

164-
ASSERT_EQ(copied_data_frags, decoded_frags);
187+
ASSERT_EQ(data_frags, decoded_frags);
165188
}
166189
}
167190

168191
void run_test(
169192
fec::FecCode<T>& fec,
170193
bool props_flag = false,
171194
bool is_nf4 = false,
172-
size_t pkt_size = 0,
195+
bool vertical_support = false,
173196
bool has_meta = false)
174197
{
175198
run_test_horizontal(fec, props_flag, is_nf4);
176-
if (pkt_size > 0) {
177-
run_test_vertical(fec, props_flag, is_nf4, has_meta);
199+
if (vertical_support) {
200+
run_test_vertical(fec, props_flag, has_meta);
178201
}
179202
}
180203
};
181204

182-
using AllTypes = ::testing::Types<uint32_t, uint64_t, __uint128_t>;
205+
using AllTypes = ::testing::Types<uint32_t, uint64_t>;
183206
TYPED_TEST_CASE(FecTestCommon, AllTypes);
184207

185208
TYPED_TEST(FecTestCommon, TestNf4) // NOLINT
@@ -188,9 +211,10 @@ TYPED_TEST(FecTestCommon, TestNf4) // NOLINT
188211

189212
for (int i = 1; i < iter_count; i++) {
190213
const unsigned word_size = 1 << i;
191-
fec::RsNf4<TypeParam> fec(word_size, this->n_data, this->n_parities);
214+
fec::RsNf4<TypeParam> fec(
215+
word_size, this->n_data, this->n_parities, this->pkt_size);
192216

193-
this->run_test(fec, true, true);
217+
this->run_test(fec, true, true, true);
194218
}
195219
}
196220

@@ -214,38 +238,44 @@ TYPED_TEST(FecTestCommon, TestGf2nFftAdd) // NOLINT
214238
}
215239

216240
template <typename T>
217-
class FecTestNo128 : public FecTestCommon<T> {
241+
class FecTestFnt : public FecTestCommon<T> {
218242
};
219243

220-
using No128 = ::testing::Types<uint32_t, uint64_t>;
221-
TYPED_TEST_CASE(FecTestNo128, No128);
244+
using FntType = ::testing::Types<uint16_t, uint32_t>;
245+
TYPED_TEST_CASE(FecTestFnt, FntType);
222246

223-
TYPED_TEST(FecTestNo128, TestFnt) // NOLINT
247+
TYPED_TEST(FecTestFnt, TestFnt) // NOLINT
224248
{
225-
for (unsigned word_size = 1; word_size <= 2; ++word_size) {
226-
fec::RsFnt<TypeParam> fec(
227-
fec::FecType::NON_SYSTEMATIC,
228-
word_size,
229-
this->n_data,
230-
this->n_parities,
231-
this->pkt_size);
232-
this->run_test(fec, true, false, this->pkt_size, true);
233-
}
249+
const unsigned word_size = sizeof(TypeParam) / 2;
250+
fec::RsFnt<TypeParam> fec(
251+
fec::FecType::NON_SYSTEMATIC,
252+
word_size,
253+
this->n_data,
254+
this->n_parities,
255+
this->pkt_size);
256+
257+
this->run_test(fec, true, false, true, true);
234258
}
235259

236-
TYPED_TEST(FecTestNo128, TestFntSys) // NOLINT
260+
TYPED_TEST(FecTestFnt, TestFntSys) // NOLINT
237261
{
238-
for (unsigned word_size = 1; word_size <= 2; ++word_size) {
239-
fec::RsFnt<TypeParam> fec(
240-
fec::FecType::SYSTEMATIC,
241-
word_size,
242-
this->n_data,
243-
this->n_parities,
244-
this->pkt_size);
245-
this->run_test(fec, true, false);
246-
}
262+
const unsigned word_size = sizeof(TypeParam) / 2;
263+
fec::RsFnt<TypeParam> fec(
264+
fec::FecType::SYSTEMATIC,
265+
word_size,
266+
this->n_data,
267+
this->n_parities,
268+
this->pkt_size);
269+
this->run_test(fec, true, false, true, true);
247270
}
248271

272+
template <typename T>
273+
class FecTestNo128 : public FecTestCommon<T> {
274+
};
275+
276+
using No128 = ::testing::Types<uint32_t, uint64_t>;
277+
TYPED_TEST_CASE(FecTestNo128, No128);
278+
249279
TYPED_TEST(FecTestNo128, TestGfpFft) // NOLINT
250280
{
251281
for (size_t word_size = 1; word_size <= 4 && word_size < sizeof(TypeParam);

0 commit comments

Comments
 (0)