summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/test/src/test_bl.c
blob: d71e13e0582ce82bf2c52800a9da299d0ff3e835 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/test/src/test_bl.c
 * \brief   Test CE RX BL module.
 * \ingroup test
 */
#include "common/std.h"

#include "lib/test.h"
#include "lib/utils.h"
#include "ce/rx/rx.h"
#include "ce_rx_bl_fsm_defs.h"
#include "ce/rx/bitloading/inc/nsr.h"
#include "ce/rx/bitloading/inc/ber.h"
#include "ce/rx/bitloading/inc/poly.h"
#include "ce/rx/bitloading/inc/initial.h"
#include "ce/rx/bitloading/inc/bitloading.h"
#include "ce/rx/bitloading/bitloading.h"
#include "ce/rx/bitloading/inc/common.h"
#include "ce/rx/bitloading/nsr.h"
#include "ce/rx/ce_rx_param.h"
#include "ce/rx/tonemask.h"
#include "ce/rx/bitloading/pber.h"
#include "lib/rnd.h"

/* Test vectors. */
#include "vector.h"
#include "nsr_on_sound.h"
#include "nsr_on_sound_max.h"
#include "mean_on_sound_nsr.h"
#include "bl_initial_final.h"
#include "bl_initial_final_with_carriers_masked.h"
#include "polynomial_nsr.h"
#include "polynomial_raw.h"
#include "tonemap_initial_0db_nsr_margin_0_00_ber_margin.h"
#include "tonemap_initial_0db_nsr_margin_0_01_ber_margin.h"
#include "tonemap_initial_1db_nsr_margin_0_00_ber_margin.h"
#include "tonemap_initial_1db_nsr_margin_0_01_ber_margin.h"
#include "tonemap_initial_2db_nsr_margin_0_00_ber_margin.h"
#include "tonemap_initial_2db_nsr_margin_0_01_ber_margin.h"
#include "tonemap_initial_3db_nsr_margin_0_00_ber_margin.h"
#include "tonemap_initial_3db_nsr_margin_0_01_ber_margin.h"
#include "tonemap_initial_under_ber_0db_nsr_margin_0_00_ber_margin.h"
#include "ber_quantify.h"

#include <float.h>
#include <stdio.h>

/**
 * Number of tones that can differs between two test vectors.
 * This is used because some differences can appears between the two (for
 * example, my sorting differs from the one of MatLab).
 */
#define TONE_TOLERATED_ERROR (PHY_CARRIER_NB * 1 / 100)

/* Global CE/RX behavior stub. */
ce_rx_behavior_t ce_rx_behavior = CE_RX_BEHAVIOR_INTERVALS_DISABLE;

/**
 * Prepare structures for a bit loading after NSR on sound received and mean
 * has been computed.
 * \param  bl  bit loading context
 */
static void
test_bl_after_nsr_sound_init (ce_rx_bitloading_t *bl)
{
    dbg_assert (bl);
    ce_rx_bitloading_init (bl);
    const uint blk_size = BLK_SIZE / sizeof (u32);
    const uint chandata_count = PHY_CARRIER_NB / blk_size
        + (PHY_CARRIER_NB % blk_size ? 1 : 0);
    uint count;
    for (count = 0; count < nsr_on_sound_width; count++)
    {
        /* Allocate some channel data from the test vector. */
        phy_chandata_t *chan_data = ce_rx_bl_test_vector_to_chan_data
            (&nsr_on_sound[0][0], count, nsr_on_sound_width);
        /* Fake channel data received. */
        if (count == 0)
            ce_rx_bl_nsr_sum_init (bl, chan_data, chandata_count);
        else
            ce_rx_bl_nsr_sum_add (bl, chan_data, chandata_count);
        /* Clean channel data. */
        blk_release_desc_range_nb (&chan_data->blk, chandata_count);
    }
    /* Compute mean. */
    ce_rx_bl_nsr_compute_mean (bl);
}

static void
test_bl_after_nsr_sound_uninit (ce_rx_bitloading_t *bl)
{
    dbg_assert (bl);
    ce_rx_bitloading_uninit (bl);
}

/**
 * Test suite for the FSM of the CE RX bitloading.
 */
static void
test_suite_ce_rx_bl_fsm (test_t t)
{
    test_case_begin (t, "FSM");

    test_begin (t, "FSM initialization")
    {
        /* To prevent including the FSM (and therefore build it everywhere),
         * the initialization assumes that CE_RX_BL_FSM_STATE_IDLE is 0. */
        test_fail_if (CE_RX_BL_FSM_STATE_IDLE != 0);
    } test_end;
}

/**
 * Test suite for the mean on NSR.
 */
static void
test_suite_ce_rx_bl_mean_on_nsr (test_t t)
{
    test_case_begin (t, "Mean on NSR");

    const u8 max_nsr_count = 40 - 1;
    const u32 max_sum = (1 << 18) * 40 - 1;

    test_begin (t, "division by 1")
    {
        test_fail_if (ce_rx_bl_nsr_mean (0, 1) != 0);
        test_fail_if (ce_rx_bl_nsr_mean (1, 1) != 1);
        test_fail_if (ce_rx_bl_nsr_mean (12345, 1) != 12345);
        test_fail_if (ce_rx_bl_nsr_mean (max_sum, 1)
                      != max_sum);
    } test_end;

    test_begin (t, "division by maximum (39)")
    {
        test_fail_if (ce_rx_bl_nsr_mean (0, max_nsr_count)
                      != 0 / max_nsr_count);
        test_fail_if (ce_rx_bl_nsr_mean (1, max_nsr_count)
                      != 1 / max_nsr_count);
        test_fail_if (ce_rx_bl_nsr_mean (424242, max_nsr_count)
                      != 424242 / max_nsr_count);
        test_fail_if (ce_rx_bl_nsr_mean (max_nsr_count, max_nsr_count)
                      != max_nsr_count / max_nsr_count);
    } test_end;

    test_begin (t, "division with random numbers")
    {
        lib_rnd_t rnd_ctx;
        lib_rnd_init (&rnd_ctx, 4224);
        const uint iteration = 4242;
        uint count;
        for (count = 0; count < iteration; count++)
        {
            /* Generate two random numbers. */
            u32 sum = lib_rnd_uniform (&rnd_ctx, max_sum);
            u32 nsr_count = lib_rnd_uniform (&rnd_ctx, max_nsr_count) + 1;
            /* Test optimized version against normal one. */
            test_fail_if (ce_rx_bl_nsr_mean (sum, nsr_count)
                          != sum / nsr_count);
        }
    } test_end;

    test_begin (t, "mean computation on NSR")
    {
        ce_rx_bitloading_t bl;
        /* Go until computation of mean. */
        test_bl_after_nsr_sound_init (&bl);
        /* Check mean against test vector. */
        ce_rx_bl_test_vector_cmp_chan_data (t, &mean_on_sound_nsr[0][0],
                                            0, mean_on_sound_nsr_width,
                                            bl.noise_nrj,
                                            bl.noise_nrj_blk_count);
        /* Clean. */
        test_bl_after_nsr_sound_uninit (&bl);
    } test_end;
}

/**
 * Test suite of NSR sum.
 */
static void
test_suite_ce_rx_bl_nsr_sum (test_t t)
{
    test_case_begin (t, "sum computing");

    /* Allocate bitloading of a station. */
    ce_rx_bitloading_t bl;
    ce_rx_bitloading_init (&bl);

    const phy_fecrate_t fec_rate = PHY_FEC_RATE_16_21;

    const uint blk_size = BLK_SIZE / sizeof (u32);
    const uint chandata_count = PHY_CARRIER_NB / blk_size
        + (PHY_CARRIER_NB % blk_size ? 1 : 0);

    test_begin (t, "sum initialization")
    {
        /* Allocate some channel data from the test vector. */
        phy_chandata_t *chan_data = ce_rx_bl_test_vector_to_chan_data
            (&nsr_on_sound[0][0], 0, nsr_on_sound_width);
        /* Fake channel data received. */
        ce_rx_bl_nsr_sum_init (&bl, chan_data, chandata_count);
        /* Check initialization. */
        ce_rx_bl_test_vector_cmp_chan_data (t, &nsr_on_sound[0][0], 0,
                                            nsr_on_sound_width,
                                            bl.noise_nrj,
                                            bl.noise_nrj_blk_count);
        /* Clean channel data. */
        blk_release_desc_range_nb (&chan_data->blk, chandata_count);
    } test_end;

    test_begin (t, "sum continuation")
    {
        uint count;
        uint index;
        /* Create a new vector to check the computed sum. */
        u32 sum_vector[PHY_CARRIER_NB][1];
        for (index = 0; index < PHY_CARRIER_NB; index++)
        {
            /* Initialize to first NSR. */
            sum_vector[index][0] = nsr_on_sound[index][0];
        }
        for (count = 1; count < nsr_on_sound_width; count++)
        {
            /* Allocate some channel data from the test vector. */
            phy_chandata_t *chan_data = ce_rx_bl_test_vector_to_chan_data
                (&nsr_on_sound[0][0], count, nsr_on_sound_width);
            /* Fake channel data received. */
            ce_rx_bl_nsr_sum_add (&bl, chan_data, chandata_count);
            /* Add current column of the vector to the sum vector. */
            for (index = 0; index < PHY_CARRIER_NB; index++)
            {
                sum_vector[index][0] += nsr_on_sound[index][count];
            }
            /* Clean channel data. */
            blk_release_desc_range_nb (&chan_data->blk, chandata_count);
        }
        test_fail_if (bl.mean_count != nsr_on_sound_width);
        /* Check sum has been correctly computed. */
        ce_rx_bl_test_vector_cmp_chan_data (t, &sum_vector[0][0], 0, 1,
                                            bl.noise_nrj,
                                            bl.noise_nrj_blk_count);
    } test_end;

    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);
    tonemap_t *tm = tonemap_alloc ();
    ce_rx_bl_ber_impact_t opti[PHY_CARRIER_NB];
    /* Compute mean. */
    ce_rx_bl_nsr_compute_mean (&bl);
    uint tone_en;
    u64 ber_pt = ce_rx_bl_ber_pt_bpt (fec_rate, ti.carrier_nb,
                                      ce_rx_bl_initial_bpt[fec_rate]);

    test_begin (t, "initial tone map (non optimized)")
    {
        ce_rx_bl_update_tone_map_under_ber_consign (ber_pt, &ti, &bl, tm,
                                                    opti, &tone_en);
        uint tone, i = 0, j = 0;
        uint tone_diff = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
        TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
        {
            if (CE_BIT_PER_MOD[tone] !=
                tonemap_initial_under_ber_0db_nsr_margin_0_00_ber_margin[i][0])
                tone_diff++;
            test_fail_if (opti[j].carrier_index != i);
            j++;
            i++;
        }
        TONEMAP_READ_CLOSE
        else
        TONEMAP_READ_OPEN
        {
             i++;
        }
        TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
        test_fail_if (tone_diff != 0, "%d computed tone(s) differs from test "
                      "vector", tone_diff);
    } test_end;

    test_begin (t, "sort optimization table")
    {
        uint i;
        ce_rx_bl_sort_optimization (opti, ti.carrier_nb);
        for (i = 0; i < ti.carrier_nb - 1; i++)
        {
            test_fail_if (opti[i].ber_diff > opti[i + 1].ber_diff);
        }
    } test_end;

    tonemap_free (tm);

    tm = tonemap_alloc ();
    test_begin (t, "initial tone map (optimized)")
    {
        bool of;
        u64 ber_pt = ce_rx_bl_ber_pt_bpt (fec_rate, ti.carrier_nb,
                                          ce_rx_bl_initial_bpt[fec_rate]);
        u64 ber_weighted_sum =
            ce_rx_bl_update_tone_map_under_ber_consign (ber_pt, &ti, &bl, tm,
                                                        opti, &tone_en);
        ce_rx_bl_sort_optimization (opti, ti.carrier_nb);
        ce_rx_bl_update_tone_map_at_ber_consign (ber_pt, &ti, &bl, tm, opti,
                                                 &ber_weighted_sum, &tone_en,
                                                 &of);
        uint tone, i = 0;
        uint tone_diff = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
        TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
        {
            if (CE_BIT_PER_MOD[tone]
                != tonemap_initial_0db_nsr_margin_0_00_ber_margin[i][0])
                tone_diff++;
            i++;
        }
        TONEMAP_READ_CLOSE
        else
        TONEMAP_READ_OPEN
        {
             i++;
        }
        TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
        test_fail_if (tone_diff > TONE_TOLERATED_ERROR, "%d computed tone(s) differs from test "
                      "vector", tone_diff);
    } test_end;
    tonemap_free (tm);

    test_begin (t, "prevent overflow when computing BER weighted sum")
    {
        /* Create a fake optimization table. */
        uint i;

        tm = tonemap_alloc ();

        /* Fake optimization table, first entry is ok, second one will stop
         * optimization loop. */
        for (i = 0; i < 2; i++)
        {
            opti[i].carrier_index = opti[i].ber_lower
                = opti[i].ber_diff = 0x42 + i;
        }
        tonemap_write_tone (tm, opti[0].carrier_index, 0);
        /* Will make the loop stop. */
        tonemap_write_tone (tm, opti[1].carrier_index, 7);

        /* Set big value which will overflow and create a small number after
         * computation. */
        u64 ber_pt = 1ull << 51;
        tm->bits_per_symbol = 1 << 13;
        u64 ber_weighted_sum = CE_RX_BL_BER_DEFAULT_OVER - 1;

        bool assert = false;
        bool of;
        dbg_fatal_try_begin
        {
            ce_rx_bl_update_tone_map_at_ber_consign (ber_pt, &ti, &bl, tm,
                                                     opti, &ber_weighted_sum,
                                                     &tone_en, &of);
        }
        dbg_fatal_try_catch_void ()
        {
            assert = true;
        }
        dbg_fatal_try_end;
        test_fail_if (assert == true);

        tonemap_free (tm);
    } test_end;

    /* Clean bitloading. */
    ce_rx_bitloading_uninit (&bl);
}

/**
 * Test suite for the polynomial sub-module.
 */
static void
test_suite_ce_rx_bl_poly (test_t t)
{
    test_case_begin (t, "polynomial module");

    test_begin (t, "polynomial")
    {
        const uint raw_start = 0;
        const uint raw_step = 7;
        uint i, x;
        s64 coef[4] = { -15, 3, -2, 1 };
        /* Go through each entry of the vector. */
        for (i = 0, x = raw_start; i < polynomial_raw_height;
             x += raw_step, i++)
        {
            /* Compute poly. */
            s64 poly = ce_rx_bl_poly (3, coef, x);
            /* Test failed if difference over tolerated error. */
            test_fail_if (ABS (poly - polynomial_raw[i][0]) != 0);
        }
    } test_end;

    test_begin (t, "degree 0 is supported")
    {
        s64 coef[1] = { 42 };
        test_fail_if (ce_rx_bl_poly (0, coef, 4242) != coef[0]);
    } test_end;
}

/**
 * Test suite for the BER sub-module.
 */
static void
test_suite_ce_rx_bl_ber (test_t t)
{
    test_case_begin (t, "BER module");

    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);

    test_begin (t, "BER polynomial")
    {
        uint i,j;
        const uint nsr_step = 11;
        const u32 *nsr_polychange = &ce_rx_bl_ber_poly_coef[0].range[0];
        uint nsr;
        i = 1;
        j = 0;
        nsr = (*nsr_polychange++) + nsr_step;
        while ( j < ce_rx_bl_ber_poly_coef[0].poly_count )
        {
            /* Go through each entry of the vector. */
            for (;
                 i < polynomial_nsr_height && nsr <= (nsr_polychange[j] - nsr_step);
                 i++, nsr += nsr_step)
            {
                /* Compute BER. */
                s64 ber = ce_rx_bl_ber_for_mod_initial (nsr, 1);
                /* Test failed if difference over tolerated error. */
                test_fail_if (ABS (ber - polynomial_nsr[i][0]) != 0);
            }
            nsr = nsr_polychange[j++] + 1 ;
        }
    } test_end;

    test_begin (t, "modulation under BER consign (no iteration)")
    {
        uint i;
        const phy_fecrate_t fec_rate = PHY_FEC_RATE_16_21;
        u64 ber_pt = ce_rx_bl_ber_pt_bpt (fec_rate, ti.carrier_nb,
                                          ce_rx_bl_initial_bpt[fec_rate]);
        u8 mod_lower;
        u64 ber_lower, ber_upper;
        uint tone_diff = 0;
        /* Go through each entry of the vector. */
        for (i = 0; i <
             tonemap_initial_under_ber_0db_nsr_margin_0_00_ber_margin_height;
             i++)
        {
            /* When there is some NSR. */
            if (mean_on_sound_nsr[i][0])
            {
                /* Get lower modulation. */
                ce_rx_bl_ber_vs_nsr (ce_rx_bl_ber_poly_coef,
                                     mean_on_sound_nsr[i][0], ber_pt,
                                     &mod_lower, &ber_lower, &ber_upper);
                /* Check from vector. */
                if (CE_BIT_PER_MOD[mod_lower] !=
                    tonemap_initial_under_ber_0db_nsr_margin_0_00_ber_margin[i][0])
                    tone_diff++;
                /* Sanity check. */
                test_fail_if (ber_lower == (u64) -1 || ber_upper == (u64) -1);
            }
        }
        test_fail_if (tone_diff != 0, "%d computed tone(s) differs from test "
                      "vector", tone_diff);
    } test_end;

    test_begin (t, "BER pt from bits per tone")
    {
        phy_fecrate_t i;
        lib_rnd_t rnd_ctx;
        lib_rnd_init (&rnd_ctx, 4224);
        const uint precision = 10000;
        double j;
        for (i = PHY_FEC_RATE_1_2; i < PHY_FEC_RATE_NB; i++)
        {
            for (j = CE_BIT_PER_MOD[1] * CE_RX_BL_BPT_QUANT_FACTOR;
                 j < CE_BIT_PER_MOD[CE_MOD_COUNT - 1]
                 * CE_RX_BL_BPT_QUANT_FACTOR;
                 j += lib_rnd_uniform (&rnd_ctx, precision))
            {
                s64 ber_pt = ce_rx_bl_ber_pt_bpt (i, ti.carrier_nb, j);
                test_fail_if (ber_pt < 0
                              || ber_pt > CE_RX_BL_BER_DEFAULT_OVER);
            }
        }
    } test_end;

    test_begin (t, "BER target for ROBO tone map")
    {
        tonemask_info_t ti;
        ti.carrier_nb = tonemask_default (ti.tonemask);
        test_fail_if (ce_rx_bl_ber_pt_robo (ti.carrier_nb)
                      != ce_rx_bl_ber_pt_bpt
                      (PHY_FEC_RATE_1_2, ti.carrier_nb,
                       2 * CE_RX_BL_BPT_QUANT_FACTOR));
    } test_end;

    test_begin (t, "BER quantification")
    {
        ce_rx_measure_mbox_t measure;

        /* According to TNS, 16 bits of error is tolerated. */
        const s64 error_tolerated = 1 << 16;
        uint i, pb_size;
        u64 ber_reached, res;
        s64 diff;

        for (i = 0; i < ber_quantify_height; i++)
        {
            ber_reached = ber_quantify[i][0];
            pb_size = ber_quantify[i][1];
            measure.false_pb_count = ber_quantify[i][2];
            measure.total_pb_count = ber_quantify[i][3];
            measure.ber_sum = ber_quantify[i][4];

            /* Quantify. */
            res = ce_rx_bl_ber_quantify (&measure, ber_reached, pb_size);

            /* Compute difference between expected and computed ber. */
            diff = ber_quantify[i][5] - res;

            /* Check if over tolerated error. */
            test_fail_if (ABS (diff) > error_tolerated);
        }
    } test_end;
}

/**
 * Test suite for the BER sub-module.
 */
static void
test_suite_ce_rx_bl_initial (test_t t)
{
    test_case_begin (t, "initial");

    test_begin (t, "test initial tone map generation with special SNR")
    {
        ce_rx_bitloading_t bl;
        tonemask_info_t ti;
        ti.carrier_nb = tonemask_default (ti.tonemask);

        /* nsr sound init with max sound. */
        ce_rx_bitloading_init (&bl);
        const uint blk_size = BLK_SIZE / sizeof (u32);
        const uint chandata_count = PHY_CARRIER_NB / blk_size
            + (PHY_CARRIER_NB % blk_size ? 1 : 0);
        uint count;
        for (count = 0; count < nsr_on_sound_max_width; count++)
        {
            /* Allocate some channel data from the test vector. */
            phy_chandata_t *chan_data = ce_rx_bl_test_vector_to_chan_data
                (&nsr_on_sound_max[0][0], count, nsr_on_sound_max_width);
            /* Fake channel data received. */
            if (count == 0)
                ce_rx_bl_nsr_sum_init (&bl, chan_data, chandata_count);
            else
                ce_rx_bl_nsr_sum_add (&bl, chan_data, chandata_count);
            /* Clean channel data. */
            blk_release_desc_range_nb (&chan_data->blk, chandata_count);
        }
        /* Compute mean. */
        ce_rx_bl_nsr_compute_mean (&bl);

        /* Test tone map iterative with this special NSR. */
        tonemap_t *tm = ce_rx_bl_initial (&ti, &ti, &bl);
        test_fail_if (tm != NULL);

        /* Clean. */
        ce_rx_bitloading_uninit (&bl);
        test_bl_after_nsr_sound_uninit (&bl);
    } test_end;

    test_begin (t, "generation initial tone map optimized & iterative")
    {
        ce_rx_bitloading_t bl;
        /* Go until computation of mean. */
        test_bl_after_nsr_sound_init (&bl);
        tonemask_info_t ti;
        ti.carrier_nb = tonemask_default (ti.tonemask);

        uint tone_diff = 0;
        s64 nsr_prev = -1;
        uint nsr_diff = 0;
        uint bit_per_tone = 0, bit_per_tone_vect = 0;
        tonemap_t *tm = ce_rx_bl_initial (&ti, &ti, &bl);
        uint tone, i = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
        TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
        {
            if (CE_BIT_PER_MOD[tone] != bl_initial_final[i][0])
            {
                /* Modulation differs. */
                tone_diff++;
                /* Is it a sort problem of same NSR? */
                /* Initialize NSR prev the first time. */
                if (nsr_prev == -1)
                    nsr_prev = mean_on_sound_nsr[i][0];
                /* Check if not first time. */
                else if (nsr_prev != mean_on_sound_nsr[i][0])
                    nsr_diff++;
            }
            /* Compute bit of this tone map. */
            bit_per_tone_vect += bl_initial_final[i][0];
            bit_per_tone += CE_BIT_PER_MOD[tone];
            i++;
        }
        TONEMAP_READ_CLOSE
        else
        TONEMAP_READ_OPEN
        {
             i++;
        }
        TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
        tonemap_free (tm);
        test_fail_if (tm->fecrate != PHY_FEC_RATE_16_21);
        test_fail_if (bit_per_tone != bit_per_tone_vect);
        test_fail_if (tone_diff > TONE_TOLERATED_ERROR, "%d computed "
                      "tone(s) differs from test vector", tone_diff);
        if (tone_diff)
            test_fail_if (nsr_diff != 0);

        /* Clean. */
        test_bl_after_nsr_sound_uninit (&bl);
    } test_end;

    test_begin (t, "generation initial tone map with first two carriers "
                "after PHY_CARRIER_OFFSET masked")
    {
        ce_rx_bitloading_t bl;
        /* Go until computation of mean. */
        test_bl_after_nsr_sound_init (&bl);
        tonemask_info_t ti, modified_ti;
        ti.carrier_nb = tonemask_default (ti.tonemask);
        ce_rx_tonemask_init (&modified_ti, &ti);

        uint tone_diff = 0;
        s64 nsr_prev = -1;
        uint nsr_diff = 0;
        uint bit_per_tone = 0, bit_per_tone_vect = 0;
        tonemap_t *tm = ce_rx_bl_initial (&ti, &modified_ti, &bl);
        uint tone, i = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
        TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
        {
            if (CE_BIT_PER_MOD[tone] != bl_initial_final_with_carriers_masked[i][0])
            {
                /* Modulation differs. */
                tone_diff++;
                /* Is it a sort problem of same NSR? */
                /* Initialize NSR prev the first time. */
                if (nsr_prev == -1)
                    nsr_prev = mean_on_sound_nsr[i][0];
                /* Check if not first time. */
                else if (nsr_prev != mean_on_sound_nsr[i][0])
                    nsr_diff++;
            }
            /* Compute bit of this tone map. */
            bit_per_tone_vect += bl_initial_final_with_carriers_masked[i][0];
            bit_per_tone += CE_BIT_PER_MOD[tone];
            i++;
        }
        TONEMAP_READ_CLOSE
        else
        TONEMAP_READ_OPEN
        {
             i++;
        }
        TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
        tonemap_free (tm);
        test_fail_if (tm->fecrate != PHY_FEC_RATE_16_21);
        test_fail_if (bit_per_tone != bit_per_tone_vect);
        test_fail_if (tone_diff > TONE_TOLERATED_ERROR, "%d computed "
                      "tone(s) differs from test vector", tone_diff);
        if (tone_diff)
            test_fail_if (nsr_diff != 0);

        /* Clean. */
        test_bl_after_nsr_sound_uninit (&bl);
    } test_end;

    test_begin (t, "generation initial tone map with modified tonemask")
    {
        ce_rx_bitloading_t bl;
        /* Go until computation of mean. */
        test_bl_after_nsr_sound_init (&bl);
        tonemask_info_t ti, modified_ti;
        uint i;
        ti.carrier_nb = tonemask_default (ti.tonemask);
        ce_rx_tonemask_init (&modified_ti, &ti);

        for (i = 0; i < PHY_TONEMASK_WORDS - 1; i++)
        {
            modified_ti.tonemask[i] = i % 2 ? BITS_ONES (32) : 0;
        }
        modified_ti.tonemask[i] = BITS_ONES (32);
        modified_ti.carrier_nb = tonemask_carrier_nb (modified_ti.tonemask);

        tonemap_t *tm = ce_rx_bl_initial (&ti, &modified_ti, &bl);
        uint tone;
        bool masked = false;
        i = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
        TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
        {
            if (masked)
            {
                test_fail_if (tone != 0, "%d", i);
            }
            i++;
            if (!(i % 32))
                masked = !masked;
            if (i == PHY_TONEMASK_WORDS * 32)
                masked = true;
        }
        TONEMAP_READ_CLOSE
        else
        TONEMAP_READ_OPEN
        {
             i++;
             if (!(i % 32))
                masked = !masked;
             if (i == PHY_TONEMASK_WORDS * 32)
                masked = true;
        }
        TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
        tonemap_free (tm);

        /* Clean. */
        test_bl_after_nsr_sound_uninit (&bl);
    } test_end;
}

/**
 * Test suite to check get SNR functionality.
 */
static void
test_suite_ce_rx_bl_get_snr (test_t t)
{
    test_case_begin (t, "get SNR");

    ce_rx_bitloading_t bl;
    ce_rx_bitloading_init (&bl);

    blk_t * const fake_nsr = INVALID_PTR;
    bl.noise_nrj = fake_nsr;
    bl.noise_nrj_blk_count = 1;

    test_begin (t, "check SNR available only in certain states")
    {
        for (bl.fsm = 0; bl.fsm < CE_RX_BL_FSM_STATE_NB; bl.fsm++)
        {
            blk_t *nsr = ce_rx_bl_get_nsr (&bl);
            switch (bl.fsm)
            {
            case CE_RX_BL_FSM_STATE_TRACKING:
                test_fail_if (nsr != fake_nsr);
                break;
            default:
                test_fail_if (nsr != NULL);
                break;
            }
        }
    } test_end;

    /* Clean. */
    bl.noise_nrj = NULL;
    bl.noise_nrj_blk_count = 0;
    ce_rx_bitloading_uninit (&bl);
}

/**
 * Test suite to check NSR margin.
 */
static void
test_suite_ce_rx_bl_nsr_margin (test_t t)
{
    test_case_begin (t, "NSR margin");

    ce_rx_bitloading_t bl;
    ce_rx_bitloading_init (&bl);

    test_begin (t, "NSR margin initialisation")
    {
        /* Should be disable by default. */
        test_fail_if (ce_rx_bl_nsr_margin_ !=
                      CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);
    } test_end;

    test_begin (t, "NSR margin configuration")
    {
        ce_rx_bl_nsr_margin_set (CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);
        test_fail_if (ce_rx_bl_nsr_margin_ !=
                      CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);
        ce_rx_bl_nsr_margin_set (2 * CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);
        test_fail_if (ce_rx_bl_nsr_margin_ != 2
                      * CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);
    } test_end;

    /* Clean. */
    ce_rx_bitloading_uninit (&bl);

    /* Ease utilisation test vectors. */
#define NSR_MARGIN_VECTOR_COUNT 4
#define BER_MARGIN_VECTOR_COUNT 2
    u32 *tonemap_initial_nsr_margin[NSR_MARGIN_VECTOR_COUNT
        * BER_MARGIN_VECTOR_COUNT] =
    {
        &tonemap_initial_0db_nsr_margin_0_00_ber_margin[0][0],
        &tonemap_initial_1db_nsr_margin_0_00_ber_margin[0][0],
        &tonemap_initial_2db_nsr_margin_0_00_ber_margin[0][0],
        &tonemap_initial_3db_nsr_margin_0_00_ber_margin[0][0],
        &tonemap_initial_0db_nsr_margin_0_01_ber_margin[0][0],
        &tonemap_initial_1db_nsr_margin_0_01_ber_margin[0][0],
        &tonemap_initial_2db_nsr_margin_0_01_ber_margin[0][0],
        &tonemap_initial_3db_nsr_margin_0_01_ber_margin[0][0],
    };
    u8 tonemap_initial_nsr_margin_quant[NSR_MARGIN_VECTOR_COUNT] =
    {
        64,
        81,
        101,
        128,
    };
    s64 tonemap_initial_ber_margin_quant[BER_MARGIN_VECTOR_COUNT] =
    {
        0ll,
        90071992547410ll,
    };

    /* A few preparations for the test. */
    tonemap_t *tm = tonemap_alloc ();
    ce_rx_bl_ber_impact_t opti[PHY_CARRIER_NB];
    const phy_fecrate_t fec_rate = PHY_FEC_RATE_16_21;
    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);

    char test_name[100], ber_margin_name[100];
    uint i, j;
    /* For each test vectors. */
    for (i = 0; i < BER_MARGIN_VECTOR_COUNT; i++)
    {
        snprintf (ber_margin_name, COUNT (ber_margin_name),
                  "BER target margin = %d%%", i);
        /* Set BER margin. */
        s64 ber_margin[PHY_FEC_RATE_NB] =
        {
            tonemap_initial_ber_margin_quant[i],
            tonemap_initial_ber_margin_quant[i],
        };
        ce_rx_bl_ber_margin_set (ber_margin);
        for (j = 0; j < NSR_MARGIN_VECTOR_COUNT; j++)
        {
            snprintf (test_name, COUNT (test_name), "%s, NSR margin = %ddB",
                      ber_margin_name, j);
            test_begin (t, test_name)
            {
                /* Set NSR margin. */
                ce_rx_bl_nsr_margin_set (tonemap_initial_nsr_margin_quant[j]);
                /* Go until computation of mean. */
                test_bl_after_nsr_sound_init (&bl);
                /* Do the bit loading. */
                s64 ber_pt = ce_rx_bl_ber_pt_bpt
                    (fec_rate, ti.carrier_nb, ce_rx_bl_initial_bpt[fec_rate]);
                dbg_assert (ce_rx_bl_ber_margin_[fec_rate] < ber_pt);
                ber_pt -= ce_rx_bl_ber_margin_[fec_rate];
                uint tone_en;
                u64 ber_weighted_sum =
                    ce_rx_bl_update_tone_map_under_ber_consign (ber_pt, &ti,
                                                                &bl, tm, opti,
                                                                &tone_en);
                ce_rx_bl_sort_optimization (opti, ti.carrier_nb);
                bool of;
                ce_rx_bl_update_tone_map_at_ber_consign (ber_pt, &ti, &bl, tm,
                                                         opti,
                                                         &ber_weighted_sum,
                                                         &tone_en, &of);
                /* Check vectors. */
                uint tone, c = 0;
                uint tone_diff = 0;
#define TONEMAP_READ_OPEN {
#define TONEMAP_READ_CLOSE }
                TONEMAP_READ_BEGIN (tm, ti.tonemask, tone)
                {
                    if (CE_BIT_PER_MOD[tone] !=
                        (tonemap_initial_nsr_margin[j
                         + i * NSR_MARGIN_VECTOR_COUNT])[c])
                        tone_diff++;
                    c++;
                }
                TONEMAP_READ_CLOSE
                else
                TONEMAP_READ_OPEN
                {
                    c++;
                }
                TONEMAP_READ_END;
#undef TONEMAP_READ_OPEN
#undef TONEMAP_READ_CLOSE
                test_fail_if (tone_diff > TONE_TOLERATED_ERROR,
                              "%d computed tone(s) differs from test vector",
                              tone_diff);
                test_bl_after_nsr_sound_uninit (&bl);
            } test_end;
        }
    }
    tonemap_free (tm);

#undef NSR_MARGIN_VECTOR_COUNT
}

/**
 * Test suite to check BER target margin.
 */
static void
test_suite_ce_rx_bl_ber_target_margin (test_t t)
{
    test_case_begin (t, "BER target margin");

    ce_rx_bitloading_t bl;
    ce_rx_bitloading_init (&bl);

    /* Default BER target margin should be set to 0 (no margin). */
    const s64 ber_margin_default_value = 0ll;

    test_begin (t, "BER target margin initialisation")
    {
        /* The BER target margin should be set to the same value for both FEC
         * rate (in fact, it should not depend, but I guess it will in the
         * future...). */
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21]);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != ber_margin_default_value);
    } test_end;

    test_begin (t, "BER target margin configuration")
    {
        s64 ber_margin[PHY_FEC_RATE_NB];
        const s64 ber_margin_val = 123456789ll;
        /* Set both values at the same time. */
        ber_margin[PHY_FEC_RATE_1_2] = ber_margin[PHY_FEC_RATE_16_21]
            = ber_margin_val;
        ce_rx_bl_ber_margin_set (ber_margin);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != ber_margin_val);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21]
                      != ber_margin_val);
        /* Reset to default value only one. */
        ber_margin[PHY_FEC_RATE_1_2] = ber_margin_default_value;
        ber_margin[PHY_FEC_RATE_16_21] = 0ll;
        ce_rx_bl_ber_margin_set (ber_margin);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != ber_margin_default_value);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21]
                      != 0ll);
        /* Reset to default value the other one. */
        ber_margin[PHY_FEC_RATE_1_2] = ber_margin_default_value;
        ber_margin[PHY_FEC_RATE_16_21] = ber_margin_default_value;
        ce_rx_bl_ber_margin_set (ber_margin);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != ber_margin_default_value);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21]
                      != ber_margin_default_value);
    } test_end;

    test_begin (t, "BER target margin in two's complement")
    {
        s64 ber_margin[PHY_FEC_RATE_NB] = { 0ll, 0ll };
        /* Set to 0. */
        ce_rx_bl_ber_margin_set (ber_margin);
        /* Set to -1. */
        u64 tmp = ~(0ull);
        ber_margin[PHY_FEC_RATE_1_2] =  ber_margin[PHY_FEC_RATE_16_21]
            = tmp;
        ce_rx_bl_ber_margin_set (ber_margin);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2]
                      != -1ll);
        test_fail_if (ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21]
                      != -1ll);
    } test_end;

    /* Clean. */
    ce_rx_bitloading_uninit (&bl);
}

static void
test_suite_ce_rx_bl_tone_map_selection (test_t t)
{
    test_case_begin (t, "Tone map selection");

    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);
    tonemask_update (&ti);
    tonemap_t tm;

    test_begin (t, "more bits per symbols")
    {
        tm.bits_per_symbol = 3 * ti.carrier_nb;
        test_fail_if (ce_rx_bl_tonemap_better_than_robo (&tm, &ti) != true);
    } test_end;
    test_begin (t, "equal bits symbols")
    {
        tm.bits_per_symbol = ti.tonemap_robo[PHY_MOD_ROBO].bits_per_symbol;
        /* ROBO must be used, it is more robust. */
        test_fail_if (ce_rx_bl_tonemap_better_than_robo (&tm, &ti) != false);
    } test_end;
    test_begin (t, "more bits per symbols")
    {
        tm.bits_per_symbol = 42;
        test_fail_if (ce_rx_bl_tonemap_better_than_robo (&tm, &ti) != false);
    } test_end;
}

/**
 * Test suite to prevent access outside of optimization table.
 */
static void
test_suite_ce_rx_bl_optimization_table (test_t t)
{
    test_case_begin (t, "optimization table");

    ce_rx_bitloading_t bl;
    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);
    ce_rx_bl_ber_impact_t opti[PHY_CARRIER_NB];
    uint tone_en;
    u64 berws;

    test_begin (t, "set table with impossible values for unused parts")
    {
        tonemap_t *tm = tonemap_alloc ();
        TONEMAP_WRITE_BEGIN (tm, ti.tonemask)
        {
            TONEMAP_WRITE_MOD (1);
        }
        TONEMAP_WRITE_END;
        tm->bits_per_symbol = ti.carrier_nb;
        /* Fill optimization table for parts which should not be used with
         * some values which will trigger a bug. */
        uint i;
        dbg_assert (((u16) -1) > PHY_CARRIER_NB);
        for (i = 0; i < ti.carrier_nb; i++)
        {
            opti[i].carrier_index = i;
            opti[i].ber_lower = opti[i].ber_diff = 1;
        }
        for (i = ti.carrier_nb; i < PHY_CARRIER_NB - 1; i++)
        {
            opti[i].carrier_index = (u16) -1;
            opti[i].ber_lower = opti[i].ber_diff = 0;
        }
        bool assert = false;

        tone_en = berws = 0;

        dbg_fatal_try_begin
        {
            bool of;
            ce_rx_bl_update_tone_map_at_ber_consign
                (CE_RX_BL_BER_DEFAULT_OVER - 1, &ti, &bl, tm, opti,
                 &berws, &tone_en, &of);
        }
        dbg_fatal_try_catch_void ()
        {
            assert = true;
        }
        dbg_fatal_try_end;

        /* Clean. */
        tonemap_free (tm);
        test_fail_if (assert == true, "access outside of optimization table");
    } test_end;

    test_begin (t, "last tone removed after optimization when BER over "
                "target")
    {
        u64 target = ti.carrier_nb / 2;
        tonemap_t *tm = tonemap_alloc ();
        TONEMAP_WRITE_BEGIN (tm, ti.tonemask)
        {
            TONEMAP_WRITE_MOD (0);
        }
        TONEMAP_WRITE_END;
        tm->bits_per_symbol = 0;
        uint i;
        for (i = 0; i < target; i++)
        {
            opti[i].carrier_index = i;
            opti[i].ber_lower = 0;
            opti[i].ber_diff = 0;
        }
        for (i = target; i < ti.carrier_nb; i++)
        {
            opti[i].carrier_index = i;
            opti[i].ber_lower = 0;
            opti[i].ber_diff = 1;
        }
        tone_en = berws = 0;
        bool of;
        ce_rx_bl_update_tone_map_at_ber_consign
            (1, &ti, &bl, tm, opti, &berws, &tone_en, &of);
        test_fail_if (tone_en != target);
        /* Clean. */
        tonemap_free (tm);
    } test_end;

    test_begin (t, "do not remove last tone when over optimization table")
    {
        tonemap_t *tm = tonemap_alloc ();
        TONEMAP_WRITE_BEGIN (tm, ti.tonemask)
        {
            TONEMAP_WRITE_MOD (0);
        }
        TONEMAP_WRITE_END;
        tm->bits_per_symbol = 0;
        uint i;
        for (i = 0; i < ti.carrier_nb; i++)
        {
            opti[i].carrier_index = i;
            opti[i].ber_lower = 0;
            opti[i].ber_diff = 1;
        }
        tone_en = berws = 0;
        bool of;
        ce_rx_bl_update_tone_map_at_ber_consign
            (CE_RX_BL_BER_DEFAULT_OVER - 1, &ti, &bl, tm, opti, &berws,
             &tone_en, &of);
        test_fail_if (tone_en != ti.carrier_nb);
        /* Clean. */
        tonemap_free (tm);
    } test_end;
}

/**
 * Test suite to check NSR cleaning.
 */
static void
test_suite_ce_rx_bl_nsr_clean (test_t t)
{
    test_case_begin (t, "NSR cleanup");

    ce_rx_bitloading_t bl;

    test_begin (t, "clean some NSR")
    {
        blk_t *last = NULL;
        const uint nsr_count = 3;
        bl.noise_nrj = blk_alloc_desc_range (nsr_count, &last);
        bl.noise_nrj_blk_count = nsr_count;
        ce_rx_bl_nsr_clean (&bl);
        test_fail_if (bl.noise_nrj_blk_count != 0);
    } test_end;

    test_begin (t, "clean with no NSR")
    {
        /* We want this behavior, this will maybe trigger some bugs. */
        bool assert = false;
        dbg_fatal_try_begin
        {
            ce_rx_bl_nsr_clean (&bl);
        }
        dbg_fatal_try_catch_void ()
        {
            assert = true;
        }
        dbg_fatal_try_end;
        test_fail_if (assert != true);
    } test_end;
}

/**
 * Test suite to check BER sliding means.
 */
static void
test_suite_ce_rx_bl_ber_sliding_mean (test_t t)
{
    test_case_begin (t, "BER sliding means");

    ce_rx_bitloading_t bl;
    uint i;

    test_begin (t, "BER sliding means reset")
    {
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_FAST]
            = bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 0x4242;
        ce_rx_bl_ber_sliding_mean_reset (&bl);
        for (i = 0; i < CE_RX_BL_BER_SLIDING_MEAN_NB; i++)
            test_fail_if (bl.ber_sliding_mean[i] != -1);

    } test_end;

    test_begin (t, "BER sliding means init")
    {
        ce_rx_bl_ber_sliding_mean_reset (&bl);
        ce_rx_bl_ber_sliding_mean_update (&bl, 0x42);
        for (i = 0; i < CE_RX_BL_BER_SLIDING_MEAN_NB; i++)
            test_fail_if (bl.ber_sliding_mean[i] != 0x42);
    } test_end;

    test_begin (t, "BER sliding means update: same value")
    {
        ce_rx_bl_ber_sliding_mean_reset (&bl);
        for (i = 0; i < 0x24; i++)
            ce_rx_bl_ber_sliding_mean_update (&bl, 0x42);
        for (i = 0; i < CE_RX_BL_BER_SLIDING_MEAN_NB; i++)
            test_fail_if (bl.ber_sliding_mean[i] != 0x42);
    } test_end;

    test_begin (t, "BER sliding means update: slow means is slower than fast "
                "one")
    {
        ce_rx_bl_ber_sliding_mean_reset (&bl);
        for (i = 0; i < 0x42; i++)
        {
            ce_rx_bl_ber_sliding_mean_update (&bl, 0x42 + i * 0x42);
            if (i)
                test_fail_if
                    (bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_SLOW]
                     >= bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_FAST]);
        }
    } test_end;
}

/**
 * Test suite to check tone map is correctly configured by bit loading.
 */
static void
test_suite_ce_rx_bl_tm_configuration (test_t t)
{
    ce_rx_bitloading_t bl;
    test_bl_after_nsr_sound_init (&bl);
    tonemask_info_t ti;
    ti.carrier_nb = tonemask_default (ti.tonemask);

    test_begin (t, "tone map contention period usage flag enabled")
    {
        tonemap_t *tm = ce_rx_bl_initial (&ti, &ti, &bl);
        dbg_assert (tm);
        test_fail_if (tm->cpf != true);
        tonemap_free (tm);
    } test_end;
    /* Clean. */
    test_bl_after_nsr_sound_uninit (&bl);
}

int
main (int argc, char **argv)
{
    test_t t;
    test_init (t, argc, argv);

    s64 ber_margin_disable[PHY_FEC_RATE_NB] = { 0ll, 0ll };

    test_suite_begin (t, "CE:RX:BL");

    ce_rx_bl_ber_conf_init ();

    /* BER target margin. */
    test_suite_ce_rx_bl_ber_target_margin (t);

    /* NSR margin. */
    test_suite_ce_rx_bl_nsr_margin (t);

    /* Disable NSR margin (by setting it to 1 *
     * CE_RX_BL_NSR_MARGIN_QUANT_FACTOR). */
    ce_rx_bl_nsr_margin_set (CE_RX_BL_NSR_MARGIN_QUANT_FACTOR);

    /* Disable BER target margin. */
    ce_rx_bl_ber_margin_set (ber_margin_disable);

    /* Start test suite for the CE RX bitloading FSM. */
    test_suite_ce_rx_bl_fsm (t);

    /* Start test suite for the mean on NSR. */
    test_suite_ce_rx_bl_mean_on_nsr (t);

    /* Start test on NSR sum. */
    test_suite_ce_rx_bl_nsr_sum (t);

    /* Start test suite on BER. */
    test_suite_ce_rx_bl_ber (t);

    /* Start test suite on polynomial. */
    test_suite_ce_rx_bl_poly (t);

    /* Start test suite on initial tone map generation. */
    test_suite_ce_rx_bl_initial (t);

    /* Get NSR. */
    test_suite_ce_rx_bl_get_snr (t);

    /* Test tone map selection. */
    test_suite_ce_rx_bl_tone_map_selection (t);

    /* Optimization table access. */
    test_suite_ce_rx_bl_optimization_table (t);

    /* Test NSR cleaning. */
    test_suite_ce_rx_bl_nsr_clean (t);

    /* Test BER sliding means reseting and computing. */
    test_suite_ce_rx_bl_ber_sliding_mean (t);

    /* Test tone map is correctly configure for usage. */
    test_suite_ce_rx_bl_tm_configuration (t);

    /* Memory check. */
    test_case_begin (t, "General");
    test_begin (t, "Memory")
    {
        test_fail_unless (blk_check_memory ());
    } test_end;

    /* Result. */
    test_result (t);
    return (test_nb_failed (t) == 0 ? 0 : 1);
}