-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.cpp
More file actions
1119 lines (948 loc) · 33 KB
/
test.cpp
File metadata and controls
1119 lines (948 loc) · 33 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <sys/time.h>
#include "Helper.h"
#include "Symbol.h"
#include "Generators.h"
using namespace std;
/* ── Minimal test framework ──────────────────────────────────── */
static int tests_run = 0;
static int tests_passed = 0;
static int tests_failed = 0;
static const char *current_test = "";
#define TEST(name) \
do { \
current_test = #name; \
tests_run++; \
cout << " " << #name << " ... "; \
name(); \
tests_passed++; \
cout << "OK" << endl; \
} while(0)
/* ASSERT prints file:line, records failure, and returns from the
calling test function so later asserts don't crash on bad state. */
#define ASSERT(cond) \
do { \
if (!(cond)) { \
cout << "FAIL" << endl; \
cerr << " " << __FILE__ << ":" << __LINE__ \
<< ": ASSERT(" << #cond << ") failed" << endl; \
tests_failed++; \
tests_passed--; \
return; \
} \
} while(0)
#define ASSERT_EQ(a, b) \
do { \
if ((a) != (b)) { \
cout << "FAIL" << endl; \
cerr << " " << __FILE__ << ":" << __LINE__ \
<< ": ASSERT_EQ(" << #a << ", " << #b << ") failed: " \
<< (a) << " != " << (b) << endl; \
tests_failed++; \
tests_passed--; \
return; \
} \
} while(0)
/* ── GF(256) arithmetic tests ──────────────────────────────────── */
void test_octmul_identity() {
for (int a = 0; a < 256; a++) {
ASSERT_EQ(octmul(a, 1), a);
ASSERT_EQ(octmul(1, a), a);
}
}
void test_octmul_zero() {
for (int a = 0; a < 256; a++) {
ASSERT_EQ(octmul(a, 0), 0);
ASSERT_EQ(octmul(0, a), 0);
}
}
void test_octmul_commutative() {
for (int a = 1; a < 256; a += 17)
for (int b = 1; b < 256; b += 13)
ASSERT_EQ(octmul(a, b), octmul(b, a));
}
void test_octdiv_identity() {
for (int a = 0; a < 256; a++)
ASSERT_EQ(octdiv(a, 1), a);
}
void test_octdiv_zero_numerator() {
for (int b = 1; b < 256; b++)
ASSERT_EQ(octdiv(0, b), 0);
}
void test_octmul_octdiv_inverse() {
/* (a * b) / b == a for all non-zero a,b */
for (int a = 1; a < 256; a += 7)
for (int b = 1; b < 256; b += 11) {
unsigned char prod = octmul(a, b);
ASSERT_EQ(octdiv(prod, b), a);
}
}
void test_octdiv_self() {
/* a / a == 1 for all non-zero a */
for (int a = 1; a < 256; a++)
ASSERT_EQ(octdiv(a, a), 1);
}
/* ── Deg() tests ───────────────────────────────────────────── */
/*
* RFC 6330 Section 5.3.5.2: d = min(j, W-2) where j is the minimum
* value such that v < f[j]. f[31] = {0, 5243, 529531, ..., 1048576}.
* Deg() needs a Generators object with W initialized.
*/
void test_deg_at_boundary() {
/* Use K=8 which gives W=17, so min(j, W-2) = min(j, 15) */
Generators g;
g.gen(8, 8, 4);
/* f[0]=0: v=0 must give j=1 (not 0), since we need v < f[j] */
ASSERT_EQ((int)g.Deg(0), 1);
/* f[1]=5243: v=5242 gives j=1, v=5243 must give j=2 (not 1) */
ASSERT_EQ((int)g.Deg(5242), 1);
ASSERT_EQ((int)g.Deg(5243), 2);
/* f[2]=529531: v=529530 gives j=2, v=529531 must give j=3 */
ASSERT_EQ((int)g.Deg(529530), 2);
ASSERT_EQ((int)g.Deg(529531), 3);
}
void test_deg_interior_values() {
Generators g;
g.gen(8, 8, 4); /* W=17, so cap is min(j, 15) */
/* v=1 is in [f[0]=0, f[1]=5243), so j=1 */
ASSERT_EQ((int)g.Deg(1), 1);
/* v=100000 is in [f[1]=5243, f[2]=529531), so j=2 */
ASSERT_EQ((int)g.Deg(100000), 2);
/* v=1048575 is in [f[29]=1017662, f[30]=1048576), so j=30 -> min(30,15)=15 */
ASSERT_EQ((int)g.Deg(1048575), 15);
}
/* ── Symbol tests ──────────────────────────────────────────── */
void test_symbol_default_constructor() {
Symbol s;
ASSERT_EQ(s.nbytes, 0);
ASSERT(s.data == NULL);
ASSERT_EQ(s.sbn, -1);
ASSERT_EQ(s.esi, -1);
}
void test_symbol_sized_constructor() {
Symbol s(8);
ASSERT_EQ(s.nbytes, 8);
ASSERT(s.data != NULL);
/* should be zeroed */
for (int i = 0; i < 8; i++)
ASSERT_EQ(((unsigned char*)s.data)[i], 0);
}
void test_symbol_init_and_fill() {
Symbol s;
s.init(4);
ASSERT_EQ(s.nbytes, 4);
char buf[4] = {0x11, 0x22, 0x33, 0x44};
s.fillData(buf, 4);
ASSERT(memcmp(s.data, buf, 4) == 0);
}
void test_symbol_assign() {
Symbol a(4), b(4);
char buf[4] = {(char)0xAA, (char)0xBB, (char)0xCC, (char)0xDD};
a.fillData(buf, 4);
b = a;
ASSERT_EQ(b.nbytes, 4);
ASSERT(memcmp(b.data, buf, 4) == 0);
/* verify it's a copy, not shared pointer */
ASSERT(a.data != b.data);
}
void test_symbol_xor() {
Symbol a(4), b(4);
char abuf[4] = {(char)0xFF, 0x00, (char)0xAA, 0x55};
char bbuf[4] = {0x0F, (char)0xF0, 0x55, (char)0xAA};
a.fillData(abuf, 4);
b.fillData(bbuf, 4);
a.xxor(&b);
unsigned char *p = (unsigned char *)a.data;
ASSERT_EQ(p[0], 0xF0);
ASSERT_EQ(p[1], 0xF0);
ASSERT_EQ(p[2], 0xFF);
ASSERT_EQ(p[3], 0xFF);
}
void test_symbol_mul_div() {
Symbol s(4);
char buf[4] = {0x00, 0x01, 0x53, (char)0xFF};
s.fillData(buf, 4);
/* save original */
char orig[4];
memcpy(orig, s.data, 4);
unsigned char factor = 0x37;
s.mul(factor);
s.div(factor);
/* mul then div by same value should round-trip */
ASSERT(memcmp(s.data, orig, 4) == 0);
}
void test_symbol_muladd() {
/* muladd(s, u) computes: this ^= s * u (in GF(256), byte-wise) */
Symbol a(4), b(4);
char abuf[4] = {0, 0, 0, 0};
char bbuf[4] = {0x01, 0x02, 0x03, 0x04};
a.fillData(abuf, 4);
b.fillData(bbuf, 4);
/* a ^= b * 1 should give a == b */
a.muladd(&b, 1);
ASSERT(memcmp(a.data, b.data, 4) == 0);
/* a ^= b * 1 again should give zero (a ^ b ^ b == 0) */
a.muladd(&b, 1);
for (int i = 0; i < 4; i++)
ASSERT_EQ(((unsigned char*)a.data)[i], 0);
}
/* ── Helper / Combin tests ─────────────────────────────────── */
void test_combin_base_cases() {
ASSERT_EQ(Combin(5, 0), 1);
ASSERT_EQ(Combin(5, 1), 5);
ASSERT_EQ(Combin(5, 5), 1); /* C(n,n)=1 via Combin(5,0) */
}
void test_combin_known_values() {
ASSERT_EQ(Combin(10, 2), 45);
ASSERT_EQ(Combin(10, 3), 120);
ASSERT_EQ(Combin(6, 3), 20);
ASSERT_EQ(Combin(7, 4), 35);
}
void test_combin_symmetry() {
/* C(n,k) == C(n, n-k) */
ASSERT_EQ(Combin(8, 3), Combin(8, 5));
ASSERT_EQ(Combin(10, 4), Combin(10, 6));
}
void test_helper_partition() {
Helper h;
PartitionS p = h.partition(13, 4);
/* 13 / 4: ceil=4, floor=3, JL = 13 - 3*4 = 1, JS = 4 - 1 = 3 */
ASSERT_EQ(p.IL, 4);
ASSERT_EQ(p.IS, 3);
ASSERT_EQ(p.JL, 1);
ASSERT_EQ(p.JS, 3);
}
void test_helper_partition_even() {
Helper h;
PartitionS p = h.partition(12, 4);
ASSERT_EQ(p.IL, 3);
ASSERT_EQ(p.IS, 3);
ASSERT_EQ(p.JL, 0);
ASSERT_EQ(p.JS, 4);
}
/* ── Encoder-only tests ────────────────────────────────────── */
void test_encoder_init_valid() {
Encoder *enc = new Encoder();
ASSERT(enc->init(8, 4));
delete enc;
}
void test_encoder_init_invalid_K() {
Encoder *enc = new Encoder();
ASSERT(!enc->init(0, 4));
delete enc;
}
void test_encoder_produces_repairs() {
int K = 8, T = 4, overhead = 4;
char **source = new char*[K];
for (int i = 0; i < K; i++) {
source[i] = new char[T];
for (int j = 0; j < T; j++)
source[i][j] = (char)(i * 10 + j);
}
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
ASSERT(repairs != NULL);
/* each repair symbol should have the right size */
for (int i = 0; i < overhead; i++) {
ASSERT(repairs[i] != NULL);
ASSERT_EQ(repairs[i]->nbytes, T);
delete repairs[i];
}
delete[] repairs;
delete enc;
for (int i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
/* ── Full encode→decode round-trip helpers ─────────────────── */
/*
* Encode K symbols of size T, drop specific source symbols listed in
* lost_indices[], supply 'overhead' extra repair symbols, decode, and
* verify every lost symbol is recovered exactly.
* Returns true on success.
*/
static bool roundtrip(int K, int T, int overhead,
int *lost_indices, int nlost)
{
int i, j;
/* build deterministic source data */
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)((i * 7 + j * 3) & 0xFF);
}
/* encode */
Encoder *enc = new Encoder();
if (!enc->init(K, T)) { delete enc; return false; }
Symbol **repairs = enc->encode(source, overhead);
if (!repairs) { delete enc; return false; }
/* build received set: source symbols minus lost ones, then all repairs */
int max_received = K + overhead;
char **received = new char*[max_received];
int *esi = new int[max_received];
int n = 0;
for (i = 0; i < K; i++) {
bool is_lost = false;
for (j = 0; j < nlost; j++)
if (lost_indices[j] == i) { is_lost = true; break; }
if (!is_lost) {
received[n] = new char[T];
memcpy(received[n], source[i], T);
esi[n] = i;
n++;
}
}
for (i = 0; i < overhead; i++) {
received[n] = new char[T];
memcpy(received[n], repairs[i]->data, T);
esi[n] = K + i;
n++;
delete repairs[i];
}
delete[] repairs;
delete enc;
/* decode */
bool ok = true;
if (n < K) {
ok = false;
} else {
Decoder *dec = new Decoder();
if (!dec->init(K, T)) { ok = false; }
else if (!dec->decode(received, n, esi)) { ok = false; }
else {
for (i = 0; i < nlost; i++) {
Symbol *s = dec->recover(lost_indices[i]);
if (!s || memcmp(s->data, source[lost_indices[i]], T) != 0)
ok = false;
delete s;
if (!ok) break;
}
}
delete dec;
}
/* cleanup */
for (i = 0; i < n; i++)
delete[] received[i];
delete[] received;
delete[] esi;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
return ok;
}
/* ── Round-trip test cases ─────────────────────────────────── */
void test_roundtrip_no_loss() {
/* K=8, T=4, 0 lost, 4 overhead — trivial case */
ASSERT(roundtrip(8, 4, 4, NULL, 0));
}
void test_roundtrip_one_lost() {
int lost[] = {3};
ASSERT(roundtrip(8, 4, 4, lost, 1));
}
void test_roundtrip_two_lost() {
int lost[] = {0, 7}; /* first and last */
ASSERT(roundtrip(8, 4, 6, lost, 2));
}
void test_roundtrip_three_lost() {
int lost[] = {1, 4, 6};
ASSERT(roundtrip(8, 4, 8, lost, 3));
}
void test_roundtrip_larger_symbol() {
/* T=16 bytes (4 ints) */
int lost[] = {2, 5};
ASSERT(roundtrip(8, 16, 6, lost, 2));
}
void test_roundtrip_K4() {
int lost[] = {1};
ASSERT(roundtrip(4, 4, 4, lost, 1));
}
void test_roundtrip_K16() {
int lost[] = {0, 5, 10};
ASSERT(roundtrip(16, 4, 8, lost, 3));
}
void test_roundtrip_K20() {
int lost[] = {3, 7, 12, 18};
ASSERT(roundtrip(20, 8, 10, lost, 4));
}
void test_roundtrip_K50() {
int lost[] = {0, 12, 25, 37, 49};
ASSERT(roundtrip(50, 4, 12, lost, 5));
}
void test_roundtrip_K100() {
int lost[] = {0, 33, 66, 99};
ASSERT(roundtrip(100, 4, 10, lost, 4));
}
/* Regression: K > 466 caused OCT_EXP out-of-bounds in _3_Matrix_GHDPC.
OCT_EXP[510] was indexed by (k-j) which can reach K1+S-1 > 509. */
void test_roundtrip_K512() {
int lost[] = {0, 100, 255, 400, 511};
ASSERT(roundtrip(512, 4, 12, lost, 5));
}
/* ── Error handling tests ──────────────────────────────────── */
void test_decode_insufficient_symbols() {
/* overhead < nlost means not enough symbols to decode */
int K = 8, T = 4, overhead = 2, nlost = 4;
int i, j;
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)((i * 7 + j * 3) & 0xFF);
}
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
ASSERT(repairs != NULL);
/* build received: drop nlost source symbols, add only overhead repairs */
int n = 0;
int max_recv = K + overhead;
char **received = new char*[max_recv];
int *esi = new int[max_recv];
for (i = nlost; i < K; i++) {
received[n] = new char[T];
memcpy(received[n], source[i], T);
esi[n] = i;
n++;
}
for (i = 0; i < overhead; i++) {
received[n] = new char[T];
memcpy(received[n], repairs[i]->data, T);
esi[n] = K + i;
n++;
delete repairs[i];
}
delete[] repairs;
delete enc;
/* n = K - nlost + overhead = 8 - 4 + 2 = 6 < K, decode should fail */
ASSERT(n < K);
Decoder *dec = new Decoder();
dec->init(K, T);
Symbol **result = dec->decode(received, n, esi);
ASSERT(result == NULL);
delete dec;
for (i = 0; i < n; i++)
delete[] received[i];
delete[] received;
delete[] esi;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
void test_roundtrip_fails_insufficient_overhead() {
/* roundtrip helper should return false when overhead < nlost */
int lost[] = {0, 1, 2, 3, 4};
ASSERT(!roundtrip(8, 4, 2, lost, 5));
}
void test_encoder_init_K_too_large() {
Encoder *enc = new Encoder();
ASSERT(!enc->init(56404, 4));
delete enc;
}
void test_encoder_init_K_negative() {
Encoder *enc = new Encoder();
ASSERT(!enc->init(-1, 4));
delete enc;
}
void test_decoder_init_invalid_K() {
Decoder *dec = new Decoder();
ASSERT(!dec->init(0, 4));
delete dec;
}
void test_decoder_init_K_too_large() {
Decoder *dec = new Decoder();
ASSERT(!dec->init(56404, 4));
delete dec;
}
void test_recover_invalid_esi() {
/* recover(x) with x >= K should return NULL */
int K = 8, T = 4;
/* set up a valid encode-decode first */
int lost[] = {0};
int i, j;
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)(i + j);
}
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, 4);
ASSERT(repairs != NULL);
int n = 0;
int max_recv = K + 4;
char **received = new char*[max_recv];
int *esi = new int[max_recv];
for (i = 1; i < K; i++) {
received[n] = new char[T];
memcpy(received[n], source[i], T);
esi[n] = i;
n++;
}
for (i = 0; i < 4; i++) {
received[n] = new char[T];
memcpy(received[n], repairs[i]->data, T);
esi[n] = K + i;
n++;
delete repairs[i];
}
delete[] repairs;
delete enc;
Decoder *dec = new Decoder();
dec->init(K, T);
dec->decode(received, n, esi);
/* recover with x == K (out of range) should return NULL */
Symbol *s = dec->recover(K);
ASSERT(s == NULL);
/* recover with x == K+10 should also return NULL */
s = dec->recover(K + 10);
ASSERT(s == NULL);
/* valid recover should succeed */
s = dec->recover(0);
ASSERT(s != NULL);
delete s;
delete dec;
for (i = 0; i < n; i++)
delete[] received[i];
delete[] received;
delete[] esi;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
void test_encoder_init_boundary_K() {
/* K=1 (minimum valid) should succeed */
Encoder *enc1 = new Encoder();
ASSERT(enc1->init(1, 4));
delete enc1;
/* K=56403 (maximum valid) should succeed */
Encoder *enc2 = new Encoder();
ASSERT(enc2->init(56403, 4));
delete enc2;
}
void test_decode_all_lost() {
/* all source symbols lost, only overhead repair symbols available
overhead == K should still work (enough symbols) */
int K = 4, T = 4, overhead = K;
int i, j;
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)((i * 5 + j) & 0xFF);
}
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
ASSERT(repairs != NULL);
/* received: only repair symbols, no source symbols */
char **received = new char*[overhead];
int *esi = new int[overhead];
for (i = 0; i < overhead; i++) {
received[i] = new char[T];
memcpy(received[i], repairs[i]->data, T);
esi[i] = K + i;
delete repairs[i];
}
delete[] repairs;
delete enc;
Decoder *dec = new Decoder();
dec->init(K, T);
Symbol **result = dec->decode(received, overhead, esi);
ASSERT(result != NULL);
/* verify all recovered symbols */
for (i = 0; i < K; i++) {
Symbol *s = dec->recover(i);
ASSERT(s != NULL);
ASSERT(memcmp(s->data, source[i], T) == 0);
delete s;
}
delete dec;
for (i = 0; i < overhead; i++)
delete[] received[i];
delete[] received;
delete[] esi;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
void test_decode_all_lost_insufficient() {
/* all K source symbols lost, but fewer than K repair symbols */
int K = 8, T = 4, overhead = 4;
int i, j;
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)(i + j);
}
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
ASSERT(repairs != NULL);
/* received: only 4 repair symbols, need 8 */
char **received = new char*[overhead];
int *esi = new int[overhead];
for (i = 0; i < overhead; i++) {
received[i] = new char[T];
memcpy(received[i], repairs[i]->data, T);
esi[i] = K + i;
delete repairs[i];
}
delete[] repairs;
delete enc;
Decoder *dec = new Decoder();
dec->init(K, T);
Symbol **result = dec->decode(received, overhead, esi);
ASSERT(result == NULL);
delete dec;
for (i = 0; i < overhead; i++)
delete[] received[i];
delete[] received;
delete[] esi;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
/* ── Bandwidth / throughput tests ──────────────────────────── */
static double now_sec() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
/*
* Measure encoding bandwidth: create K source symbols of size T,
* encode with overhead repair symbols, repeat for 'iterations' rounds.
* Reports MB/s of source data encoded.
*/
static void bench_encode(int K, int T, int overhead, int iterations) {
int i, j;
/* build source data */
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)((i * 7 + j * 3) & 0xFF);
}
double t0 = now_sec();
for (int iter = 0; iter < iterations; iter++) {
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
if (repairs) {
for (i = 0; i < overhead; i++)
delete repairs[i];
delete[] repairs;
}
delete enc;
}
double elapsed = now_sec() - t0;
double total_bytes = (double)K * T * iterations;
double mbps = total_bytes / elapsed / (1024.0 * 1024.0);
cout << " K=" << K << " T=" << T << " overhead=" << overhead
<< " iters=" << iterations
<< " encode: " << mbps << " MB/s"
<< " (" << elapsed << "s)" << endl;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
/*
* Measure decoding bandwidth: encode, drop 'nlost' source symbols,
* decode and recover, repeat for 'iterations' rounds.
* Reports MB/s of source data decoded.
*/
static void bench_decode(int K, int T, int overhead, int nlost, int iterations) {
int i, j;
/* build source data */
char **source = new char*[K];
for (i = 0; i < K; i++) {
source[i] = new char[T];
for (j = 0; j < T; j++)
source[i][j] = (char)((i * 7 + j * 3) & 0xFF);
}
/* deterministic lost indices: evenly spaced */
int *lost_indices = new int[nlost];
for (i = 0; i < nlost; i++)
lost_indices[i] = (i * K) / nlost;
/* pre-encode to get repair symbols for each iteration */
double t0 = now_sec();
for (int iter = 0; iter < iterations; iter++) {
/* encode */
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
/* build received set */
int max_recv = K + overhead;
char **received = new char*[max_recv];
int *esi = new int[max_recv];
int n = 0;
for (i = 0; i < K; i++) {
bool is_lost = false;
for (j = 0; j < nlost; j++)
if (lost_indices[j] == i) { is_lost = true; break; }
if (!is_lost) {
received[n] = new char[T];
memcpy(received[n], source[i], T);
esi[n] = i;
n++;
}
}
for (i = 0; i < overhead; i++) {
received[n] = new char[T];
memcpy(received[n], repairs[i]->data, T);
esi[n] = K + i;
n++;
delete repairs[i];
}
delete[] repairs;
delete enc;
/* decode */
Decoder *dec = new Decoder();
dec->init(K, T);
dec->decode(received, n, esi);
for (i = 0; i < nlost; i++) {
Symbol *s = dec->recover(lost_indices[i]);
delete s;
}
delete dec;
for (i = 0; i < n; i++)
delete[] received[i];
delete[] received;
delete[] esi;
}
double elapsed = now_sec() - t0;
double total_bytes = (double)K * T * iterations;
double mbps = total_bytes / elapsed / (1024.0 * 1024.0);
cout << " K=" << K << " T=" << T << " lost=" << nlost
<< " overhead=" << overhead
<< " iters=" << iterations
<< " encode+decode: " << mbps << " MB/s"
<< " (" << elapsed << "s)" << endl;
delete[] lost_indices;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
/*
* Stream bandwidth test: simulate processing a large data stream by
* splitting it into blocks of K symbols each. Measures total throughput
* for both encoding and decoding pipelines.
*/
static void bench_stream(int K, int T, int overhead, int nlost,
int total_data_kb) {
int i, j;
int block_bytes = K * T;
int nblocks = (total_data_kb * 1024 + block_bytes - 1) / block_bytes;
if (nblocks < 1) nblocks = 1;
char **source = new char*[K];
for (i = 0; i < K; i++)
source[i] = new char[T];
int *lost_indices = new int[nlost];
for (i = 0; i < nlost; i++)
lost_indices[i] = (i * K) / nlost;
/* --- Encoding pass --- */
double enc_t0 = now_sec();
for (int blk = 0; blk < nblocks; blk++) {
/* fill source with block-specific data */
for (i = 0; i < K; i++)
for (j = 0; j < T; j++)
source[i][j] = (char)((blk + i * 7 + j * 3) & 0xFF);
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
if (repairs) {
for (i = 0; i < overhead; i++)
delete repairs[i];
delete[] repairs;
}
delete enc;
}
double enc_elapsed = now_sec() - enc_t0;
/* --- Decoding pass --- */
double dec_t0 = now_sec();
for (int blk = 0; blk < nblocks; blk++) {
for (i = 0; i < K; i++)
for (j = 0; j < T; j++)
source[i][j] = (char)((blk + i * 7 + j * 3) & 0xFF);
Encoder *enc = new Encoder();
enc->init(K, T);
Symbol **repairs = enc->encode(source, overhead);
int max_recv = K + overhead;
char **received = new char*[max_recv];
int *esi = new int[max_recv];
int n = 0;
for (i = 0; i < K; i++) {
bool is_lost = false;
for (j = 0; j < nlost; j++)
if (lost_indices[j] == i) { is_lost = true; break; }
if (!is_lost) {
received[n] = new char[T];
memcpy(received[n], source[i], T);
esi[n] = i;
n++;
}
}
for (i = 0; i < overhead; i++) {
received[n] = new char[T];
memcpy(received[n], repairs[i]->data, T);
esi[n] = K + i;
n++;
delete repairs[i];
}
delete[] repairs;
delete enc;
Decoder *dec = new Decoder();
dec->init(K, T);
dec->decode(received, n, esi);
for (i = 0; i < nlost; i++) {
Symbol *s = dec->recover(lost_indices[i]);
delete s;
}
delete dec;
for (i = 0; i < n; i++)
delete[] received[i];
delete[] received;
delete[] esi;
}
double dec_elapsed = now_sec() - dec_t0;
double total_bytes = (double)nblocks * block_bytes;
double enc_mbps = total_bytes / enc_elapsed / (1024.0 * 1024.0);
double dec_mbps = total_bytes / dec_elapsed / (1024.0 * 1024.0);
cout << " stream " << total_data_kb << "KB"
<< " (K=" << K << " T=" << T << " blocks=" << nblocks
<< " loss=" << nlost << "/" << K << ")"
<< " encode: " << enc_mbps << " MB/s"
<< " decode: " << dec_mbps << " MB/s" << endl;
delete[] lost_indices;
for (i = 0; i < K; i++)
delete[] source[i];
delete[] source;
}
void test_bandwidth_encode_small() {
cout << endl << " Encoding throughput:" << endl;
bench_encode(8, 4, 4, 500);
bench_encode(8, 64, 4, 500);
bench_encode(8, 256, 4, 500);
bench_encode(8, 1024, 4, 200);
}
void test_bandwidth_encode_medium() {
bench_encode(50, 64, 10, 100);
bench_encode(50, 256, 10, 100);
bench_encode(50, 1024, 10, 50);
}
void test_bandwidth_encode_large() {
bench_encode(100, 256, 15, 50);
bench_encode(100, 1024, 15, 20);
bench_encode(256, 1024, 20, 10);
}
void test_bandwidth_decode_small() {