-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_rs.c
816 lines (667 loc) · 21.7 KB
/
test_rs.c
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
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#include <time.h>
#define PROFILE
#include "rs.h"
#include "rs.c"
void print_matrix1(gf* matrix, int nrows, int ncols);
void print_matrix2(gf** matrix, int nrows, int ncols);
void print_buf(gf* buf, char *fmt, size_t len) {
size_t i = 0;
while(i < len) {
printf(fmt, buf[i]);
i++;
if((i % 16) == 0) {
printf("\n");
}
}
printf("\n");
}
void test_galois(void) {
printf("%s:\n", __FUNCTION__);
//copy from golang rs version
assert(galMultiply(3, 4) == 12);
assert(galMultiply(7, 7) == 21);
assert(galMultiply(23, 45) == 41);
{
gf in[] = {0, 1, 2, 3, 4, 5, 6, 10, 50, 100, 150, 174, 201, 255, 99, 32, 67, 85};
gf out[sizeof(in)/sizeof(gf)] = {0};
gf expect[] = {0x0, 0x19, 0x32, 0x2b, 0x64, 0x7d, 0x56, 0xfa, 0xb8, 0x6d, 0xc7, 0x85, 0xc3, 0x1f, 0x22, 0x7, 0x25, 0xfe};
gf expect2[] = {0x0, 0xb1, 0x7f, 0xce, 0xfe, 0x4f, 0x81, 0x9e, 0x3, 0x6, 0xe8, 0x75, 0xbd, 0x40, 0x36, 0xa3, 0x95, 0xcb};
int rlt;
addmul(out, in, 25, sizeof(int)/sizeof(gf));
rlt = memcmp(out, expect, sizeof(int)/sizeof(gf));
assert(0 == rlt);
memset(out, 0, sizeof(in)/sizeof(gf));
addmul(out, in, 177, sizeof(in)/sizeof(gf));
rlt = memcmp(out, expect2, sizeof(int)/sizeof(gf));
assert(0 == rlt);
}
assert(galExp(2,2) == 4);
assert(galExp(5,20) == 235);
assert(galExp(13,7) == 43);
}
void test_sub_matrix(void) {
int r, c, ptr, nrows = 10, ncols = 20;
gf* m1 = (gf*)RS_MALLOC(nrows * ncols);
gf *test1;
printf("%s:\n", __FUNCTION__);
ptr = 0;
for(r = 0; r < nrows; r++) {
for(c = 0; c < ncols; c++) {
m1[ptr] = ptr;
ptr++;
}
}
test1 = sub_matrix(m1, 0, 0, 3, 4, nrows, ncols);
for(r = 0; r < 3; r++) {
for(c = 0; c < 4; c++) {
assert(test1[r*4 + c] == (r*ncols + c));
}
}
free(test1);
test1 = sub_matrix(m1, 3, 2, 7, 9, nrows, ncols);
for(r = 0; r < (7-3); r++) {
for(c = 0; c < (9-2); c++) {
assert(test1[r*(9-2) + c] == ((r+3)*ncols + (c+2)));
}
}
free(m1);
}
void test_multiply(void) {
gf a[] = {1,2,3,4};
gf b[] = {5,6,7,8};
gf exp[] = {11,22,19,42};
gf *out;
int rlt;
printf("%s:\n", __FUNCTION__);
out = multiply1(a, 2, 2, b, 2, 2);
rlt = memcmp(out, exp, 4);
assert(0 == rlt);
}
void test_inverse(void) {
printf("%s:\n", __FUNCTION__);
{
gf a[] = {56, 23, 98, 3, 100, 200, 45, 201, 123};
gf ae[] = {175, 133, 33, 130, 13, 245, 112, 35, 126};
int rlt = invert_mat(a, 3);
assert(0 == rlt);
rlt = memcmp(a, ae, 3*3);
assert(0 == rlt);
}
{
gf a[] = { 1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1,
7, 7, 6, 6, 1};
gf ae[] = {1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
123, 123, 1, 122, 122,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0};
int rlt = invert_mat(a, 5);
assert(0 == rlt);
rlt = memcmp(a, ae, 5*5);
assert(0 == rlt);
}
{
/* error matrix */
gf a[] = {4,2,12,6};
int rlt = invert_mat(a, 2);
assert(0 != rlt);
}
}
unsigned char* test_create_random(reed_solomon *rs, int data_size, int block_size) {
struct timeval tv;
unsigned char* data;
int i, n, seed, nr_blocks;
gettimeofday(&tv, 0);
seed = tv.tv_sec ^ tv.tv_usec;
srandom(seed);
nr_blocks = (data_size+block_size-1)/block_size;
nr_blocks = ((nr_blocks + rs->data_shards - 1)/ rs->data_shards) * rs->data_shards;
n = nr_blocks / rs->data_shards;
nr_blocks += n * rs->parity_shards;
data = (unsigned char *) malloc(nr_blocks * block_size);
for(i = 0; i < data_size; i++) {
data[i] = (unsigned char)(random() % 255);
}
memset(data + data_size, 0, nr_blocks*block_size - data_size);
return data;
}
int test_create_encoding(
reed_solomon *rs,
unsigned char *data,
int data_size,
int block_size
) {
unsigned char **data_blocks;
int data_shards, parity_shards;
int i, n, nr_shards, nr_blocks, nr_fec_blocks;
data_shards = rs->data_shards;
parity_shards = rs->parity_shards;
nr_blocks = (data_size+block_size-1)/block_size;
nr_blocks = ((nr_blocks+data_shards-1)/data_shards) * data_shards;
n = nr_blocks / data_shards;
nr_fec_blocks = n * parity_shards;
nr_shards = nr_blocks + nr_fec_blocks;
data_blocks = (unsigned char**)malloc(nr_shards * sizeof(unsigned char*));
for(i = 0; i < nr_shards; i++) {
data_blocks[i] = data + i*block_size;
}
n = reed_solomon_encode2(rs, data_blocks, nr_shards, block_size);
free(data_blocks);
return n;
}
int test_data_decode(
reed_solomon *rs,
unsigned char *data,
int data_size,
int block_size,
int *erases,
int erase_count) {
unsigned char **data_blocks;
unsigned char *zilch;
int data_shards, parity_shards;
int i, j, n, nr_shards, nr_blocks, nr_fec_blocks;
data_shards = rs->data_shards;
parity_shards = rs->parity_shards;
nr_blocks = (data_size+block_size-1)/block_size;
nr_blocks = ((nr_blocks+data_shards-1)/data_shards) * data_shards;
n = nr_blocks / data_shards;
nr_fec_blocks = n * parity_shards;
nr_shards = nr_blocks + nr_fec_blocks;
data_blocks = (unsigned char**)malloc(nr_shards * sizeof(unsigned char*));
for(i = 0; i < nr_shards; i++) {
data_blocks[i] = data + i*block_size;
}
zilch = (unsigned char*)calloc(1, nr_shards);
for(i = 0; i < erase_count; i++) {
j = erases[i];
memset(data + j*block_size, 137, block_size);
zilch[j] = 1; //mark as erased
}
n = reed_solomon_reconstruct(rs, data_blocks, zilch, nr_shards, block_size);
free(data_blocks);
free(zilch);
return n;
}
void test_one_encoding(void) {
reed_solomon *rs;
unsigned char* data;
int block_size = 50000;
int data_size = 10*block_size;
int err;
printf("%s:\n", __FUNCTION__);
rs = reed_solomon_new(10, 3);
data = test_create_random(rs, data_size, block_size);
err = test_create_encoding(rs, data, data_size, block_size);
free(data);
reed_solomon_release(rs);
assert(0 == err);
}
int test_one_decoding_13(int *erases, int erase_count) {
reed_solomon *rs;
unsigned char *data, *origin;
int block_size = 50000;
int data_size = 10*block_size;
int err, err2;
rs = reed_solomon_new(10, 3);
data = test_create_random(rs, data_size, block_size);
err = test_create_encoding(rs, data, data_size, block_size);
assert(0 == err);
origin = (unsigned char*)malloc(data_size);
memcpy(origin, data, data_size);
err = test_data_decode(rs, data, data_size, block_size, erases, erase_count);
if(0 == err) {
err2 = memcmp(origin, data, data_size);
assert(0 == err2);
} else {
//failed here
err2 = memcmp(origin, data, data_size);
assert(0 != err2);
}
free(data);
free(origin);
reed_solomon_release(rs);
return err;
}
void test_one_decoding(void) {
printf("%s:\n", __FUNCTION__);
{
int erases[] = {0};
int err;
// lost nothing
err = test_one_decoding_13(erases, 0);
assert(0 == err);
}
{
int erases[] = {0};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// lost only one
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 5;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 9;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {0, 1};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// lost two
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 3;
erases[1] = 7;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 9;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 12;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {0, 1, 4};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// lost three
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 3;
erases[1] = 8;
erases[2] = 7;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 9;
erases[2] = 1;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 12;
erases[2] = 9;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 12;
erases[2] = 9;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
erases[0] = 11;
erases[1] = 12;
erases[2] = 10;
err = test_one_decoding_13(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {0, 1, 4, 8};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// lost 4, failed!
err = test_one_decoding_13(erases, erases_count);
assert(0 != err);
erases[0] = 11;
erases[1] = 12;
erases[2] = 10;
erases[3] = 9;
err = test_one_decoding_13(erases, erases_count);
assert(0 != err);
}
}
int test_one_decoding_13_6(int *erases, int erase_count) {
reed_solomon *rs;
unsigned char *data, *origin;
int block_size = 50000;
int data_size = 10*block_size*6;
int err, err2;
rs = reed_solomon_new(10, 3);
data = test_create_random(rs, data_size, block_size);
err = test_create_encoding(rs, data, data_size, block_size);
assert(0 == err);
origin = (unsigned char*)malloc(data_size);
memcpy(origin, data, data_size);
err = test_data_decode(rs, data, data_size, block_size, erases, erase_count);
if(0 == err) {
err2 = memcmp(origin, data, data_size);
assert(0 == err2);
} else {
//failed here
err2 = memcmp(origin, data, data_size);
assert(0 != err2);
}
free(data);
free(origin);
reed_solomon_release(rs);
return err;
}
void test_encoding(void) {
reed_solomon *rs;
unsigned char *data;
int block_size = 50000;
//multi shards encoding
int data_size = 13*block_size*6;
int err;
printf("%s:\n", __FUNCTION__);
rs = reed_solomon_new(10, 3);
data = test_create_random(rs, data_size, block_size);
err = test_create_encoding(rs, data, data_size, block_size);
free(data);
reed_solomon_release(rs);
assert(0 == err);
}
void test_reconstruct(void) {
#define FEC_START (10*6)
printf("%s:\n", __FUNCTION__);
{
int erases[] = {0};
int err;
// lost nothing
err = test_one_decoding_13_6(erases, 0);
assert(0 == err);
}
{
int erases[] = {0, 1, 9, 10+2, 10+4, 10+9};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// shard1 shard2 both lost three
err = test_one_decoding_13_6(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {0, 9, FEC_START + 1, 10+2, 10+9, FEC_START + 3 + 2};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// shard1 shard2 both lost three, and both lost one in fec
err = test_one_decoding_13_6(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {11, 12, 10, 9};
int erases_count = sizeof(erases)/sizeof(int);
int err;
/* this is ok. not lost 4 but shard1 lost 1, shard2 lost 3, we can reconstruct it */
err = test_one_decoding_13_6(erases, erases_count);
assert(0 == err);
}
{
int erases[] = {0, 1, 4, 8};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// shard1 lost 4, failed!
err = test_one_decoding_13_6(erases, erases_count);
assert(0 != err);
}
{
int erases[] = {10, 11, 14, 18};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// shard2 lost 4, failed!
err = test_one_decoding_13_6(erases, erases_count);
assert(0 != err);
}
{
int erases[] = {0, 1, 4, 8, 10, 11, 14, 18};
int erases_count = sizeof(erases)/sizeof(int);
int err;
// shard1 and shard2 both lost 4, failed!
err = test_one_decoding_13_6(erases, erases_count);
assert(0 != err);
}
{
int erases[] = {11, 12, 10, 9, FEC_START+3+0, FEC_START+3+1, FEC_START+3+2};
int erases_count = sizeof(erases)/sizeof(int);
int err;
err = test_one_decoding_13_6(erases, erases_count);
assert(0 != err);
}
}
double benchmarkEncodeTest(int n, int dataShards, int parityShards, int shardSize) {
clock_t start, end;
double millis;
unsigned char* data;
int i;
int dataSize = shardSize*dataShards;
reed_solomon* rs = reed_solomon_new(dataShards, parityShards);
data = test_create_random(rs, dataSize, shardSize);
start = clock();
for(i = 0; i < n; i++) {
test_create_encoding(rs, data, dataSize, shardSize);
}
end = clock();
millis = (double)(end - start) * 1000.0 / CLOCKS_PER_SEC;
return (millis);
}
/* TODO please check, is this benchmark ok? */
void benchmarkEncode(void) {
double millis;
double per_sec_in_bytes;
double MB = 1024.0 * 1024.0;
double millis_to_sec = 1000*1000;
int n;
int size;
printf("%s:\n", __FUNCTION__);
n = 20000;
size = 10000;
millis = benchmarkEncodeTest(n, 10, 2, size);
per_sec_in_bytes = (10*2*size/MB) * millis_to_sec * n / millis;
printf("10x2x10000, test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
n = 200;
millis = benchmarkEncodeTest(n, 100, 20, size);
per_sec_in_bytes = (100*20*size/MB) * millis_to_sec * n / millis;
printf("100x20x10000, test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
n = 200;
size = 1024*1024;
millis = benchmarkEncodeTest(n, 17, 3, size);
per_sec_in_bytes = (17*3*size/MB) * millis_to_sec * n / millis;
printf("17x3x(1024*1024), test_count=%d millis=%lf per_sec_in_bytes=%lfMB/s\n", n, millis, per_sec_in_bytes);
}
void test_001(void) {
reed_solomon* rs = reed_solomon_new(11, 6);
print_matrix1(rs->m, rs->data_shards, rs->data_shards);
print_matrix1(rs->parity, rs->parity_shards, rs->data_shards);
reed_solomon_release(rs);
}
void test_002(void) {
char text[] = "hello world", output[256];
int block_size = 1;
int nrDataBlocks = sizeof(text)/sizeof(char) - 1;
unsigned char* data_blocks[128];
unsigned char* fec_blocks[128];
int nrFecBlocks = 6;
//decode
unsigned int fec_block_nos[128], erased_blocks[128];
unsigned char* dec_fec_blocks[128];
int nr_fec_blocks;
int i;
reed_solomon* rs = reed_solomon_new(nrDataBlocks, nrFecBlocks);
printf("%s:\n", __FUNCTION__);
for(i = 0; i < nrDataBlocks; i++) {
data_blocks[i] = (unsigned char*)&text[i];
}
memset(output, 0, sizeof(output));
memcpy(output, text, nrDataBlocks);
for(i = 0; i < nrFecBlocks; i++) {
fec_blocks[i] = (unsigned char*)&output[i + nrDataBlocks];
}
reed_solomon_encode(rs, data_blocks, fec_blocks, block_size);
print_buf((gf*)output, "%d ", nrFecBlocks+nrDataBlocks);
text[1] = 'x';
text[3] = 'y';
text[4] = 'z';
erased_blocks[0] = 4;
erased_blocks[1] = 1;
erased_blocks[2] = 3;
fec_block_nos[0] = 1;
fec_block_nos[1] = 3;
fec_block_nos[2] = 5;
dec_fec_blocks[0] = fec_blocks[1];
dec_fec_blocks[1] = fec_blocks[3];
dec_fec_blocks[2] = fec_blocks[5];
nr_fec_blocks = 3;
printf("erased:%s\n", text);
reed_solomon_decode(rs, data_blocks, block_size, dec_fec_blocks,
fec_block_nos, erased_blocks, nr_fec_blocks);
printf("fixed:%s\n", text);
reed_solomon_release(rs);
}
void test_003(void) {
char text[] = "hello world hello world ", output[256];
int block_size = 2;
int nrDataBlocks = (sizeof(text)/sizeof(char) - 1) / block_size;
unsigned char* data_blocks[128];
unsigned char* fec_blocks[128];
int nrFecBlocks = 6;
//decode
unsigned int fec_block_nos[128], erased_blocks[128];
unsigned char* dec_fec_blocks[128];
int nr_fec_blocks;
int i;
reed_solomon* rs = reed_solomon_new(nrDataBlocks, nrFecBlocks);
printf("%s:\n", __FUNCTION__);
//printf("text size=%d\n", (int)(sizeof(text)/sizeof(char) - 1) );
for(i = 0; i < nrDataBlocks; i++) {
data_blocks[i] = (unsigned char*)&text[i*block_size];
}
memset(output, 0, sizeof(output));
memcpy(output, text, nrDataBlocks*block_size);
//print_matrix1((gf*)output, nrDataBlocks + nrFecBlocks, block_size);
for(i = 0; i < nrFecBlocks; i++) {
fec_blocks[i] = (unsigned char*)&output[i*block_size + nrDataBlocks*block_size];
}
reed_solomon_encode(rs, data_blocks, fec_blocks, block_size);
printf("golang output(example/test_rs.go):\n [[104 101] [108 108] [111 32] [119 111] [114 108] [100 32] [104 101] [108 108] [111 32] [119 111] [114 108] [100 32] \n[157 178] [83 31] [48 240] [254 93] [31 89] [151 184]]\n");
printf("c verion output:\n");
print_buf((gf*)output, "%d ", nrFecBlocks*block_size + nrDataBlocks*block_size);
//print_matrix1((gf*)output, nrDataBlocks + nrFecBlocks, block_size);
//decode
text[1*block_size] = 'x';
text[10*block_size+1] = 'y';
text[4*block_size] = 'z';
erased_blocks[0] = 4;
erased_blocks[1] = 1;
erased_blocks[2] = 10;
fec_block_nos[0] = 1;
fec_block_nos[1] = 3;
fec_block_nos[2] = 5;
dec_fec_blocks[0] = fec_blocks[1];
dec_fec_blocks[1] = fec_blocks[3];
dec_fec_blocks[2] = fec_blocks[5];
nr_fec_blocks = 3;
printf("erased:%s\n", text);
reed_solomon_decode(rs, data_blocks, block_size, dec_fec_blocks,
fec_block_nos, erased_blocks, nr_fec_blocks);
printf("fixed:%s\n", text);
reed_solomon_release(rs);
}
void test_004(void) {
//char text[] = "hello world hello world ";
int dataShards = 30;
int parityShards = 21;
int blockSize = 280;
struct timeval tv;
int i, j, n, seed, size, nrShards, nrBlocks, nrFecBlocks;
unsigned char *origin, *data;
unsigned char **data_blocks;
unsigned char *zilch;
reed_solomon *rs;
gettimeofday(&tv, 0);
seed = tv.tv_sec ^ tv.tv_usec;
srandom(seed);
fec_init();
//size = sizeof(text)/sizeof(char)-1;
size = 1024*1024;
origin = (unsigned char *) malloc(size);
//memcpy(origin, text, size);
for(i = 0; i < size; i++) {
origin[i] = (unsigned char)(random() % 255);
}
nrBlocks = (size+blockSize-1) / blockSize;
nrBlocks = ((nrBlocks+dataShards-1)/dataShards) * dataShards;
n = nrBlocks / dataShards;
nrFecBlocks = n*parityShards;
nrShards = nrBlocks + nrFecBlocks;
data = (unsigned char *) malloc(nrShards * blockSize);
memcpy(data, origin, size);
memset(data + size, 0, nrShards*blockSize - size);
printf("nrBlocks=%d nrFecBlocks=%d nrShards=%d n=%d left=%d\n", nrBlocks, nrFecBlocks, nrShards, n, nrShards*blockSize - size);
//print_buf(origin, "%d ", size);
//print_buf(data, "%d ", nrShards*blockSize);
data_blocks = (unsigned char**)malloc( nrShards * sizeof(unsigned char**) );
for(i = 0; i < nrShards; i++) {
data_blocks[i] = data + i*blockSize;
}
rs = reed_solomon_new(dataShards, parityShards);
reed_solomon_encode2(rs, data_blocks, nrShards, blockSize);
i = memcmp(origin, data, size);
assert(0 == i);
//print_matrix2(data_blocks, nrShards, blockSize);
zilch = (unsigned char*)calloc(1, nrShards);
n = parityShards;
/* int es[100];
es[0] = 3;
es[1] = 3;
es[2] = 2;
es[3] = 8; */
for(i = 0; i < n-2; i++) {
j = random() % (nrBlocks-1);
//j = es[i];
memset(data + j*blockSize, 137, blockSize);
zilch[j] = 1; //erased!
printf("erased %d\n", j);
}
if(nrFecBlocks > 2) {
for(i = 0; i < 2; i++) {
j = nrBlocks + (random() % nrFecBlocks);
memset(data + j*blockSize, 139, blockSize);
zilch[j] = 1;
printf("erased %d\n", j);
}
}
reed_solomon_reconstruct(rs, data_blocks, zilch, nrShards, blockSize);
i = memcmp(origin, data, size);
//print_buf(origin, "%d ", nrBlocks);
//print_buf(data, "%d ", nrBlocks);
printf("rlt=%d\n", i);
assert(0 == i);
free(origin);
free(data);
free(data_blocks);
free(zilch);
reed_solomon_release(rs);
}
int main(void) {
fec_init();
test_galois();
test_sub_matrix();
test_multiply();
test_inverse();
test_one_encoding();
test_one_decoding();
test_encoding();
test_reconstruct();
printf("reach here means test all ok\n");
benchmarkEncode();
//test_001();
//test_002();
test_003();
//test_004();
return 0;
}