summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/test/src/test_bl.c
blob: 2c28b6ec2f0561e1579581005523bcc1189abe39 (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
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
/* 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_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/tonemask.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 "bl_iteratif_opti_table_shift.h"
#include "bl_iteratif_tm_update_new_tm_IN_1_tone_map_input.h"
#include "bl_iteratif_tm_update_new_tm_IN_2_opti_table.h"
#include "bl_iteratif_tm_update_new_tm_IN_3_ber_input.h"
#include "bl_iteratif_tm_update_new_tm_IN_4_cursor_input.h"
#include "bl_iteratif_tm_update_new_tm_OUT_1_tone_map_output.h"
#include "bl_iteratif_tm_update_new_tm_OUT_2_cursor_output.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)

/**
 * 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, fec_rate,
                                                    &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)")
    {
        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,
                                                        fec_rate, &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);
        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 != 0, "%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;
        dbg_fatal_try_begin
        {
            ce_rx_bl_update_tone_map_at_ber_consign (ber_pt, &ti, &bl, tm,
                                                     opti, &ber_weighted_sum,
                                                     &tone_en);
        }
        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;
        const uint nsr_step = 11;
        const uint nsr_start = 5461;
        uint nsr;
        /* Go through each entry of the vector. */
        for (i = 1, nsr = nsr_start + nsr_step;
             i < polynomial_nsr_height; i++, nsr += nsr_step)
        {
            /* Compute BER. */
            s64 ber = ce_rx_bl_ber_for_mod_initial (nsr, 1, PHY_FEC_RATE_1_2);
            /* Test failed if difference over tolerated error. */
            test_fail_if (ABS (ber - polynomial_nsr[i][0]) != 0);
        }
    } test_end;

    test_begin (t, "theoretical BER respect range")
    {
        uint i, j;
        s64 ber, ber_prev;
        phy_fecrate_t rate;

        /* For each FEC rate. */
        for (rate = PHY_FEC_RATE_1_2; rate < PHY_FEC_RATE_NB; rate++)
        {
            /* Go through each entry of the vector. */
            for (i = 0; i < mean_on_sound_nsr_height;
                 i++)
            {
                /* When there is some NSR. */
                if (mean_on_sound_nsr[i][0])
                {
                    for (j = 0; j < CE_MOD_COUNT; j++)
                    {
                        /* Get theoretical BER. */
                        ber = ce_rx_bl_ber_for_mod
                            (ce_rx_bl_ber_poly_coef[rate],
                             mean_on_sound_nsr[i][0], j);
                        /* Check its respect range. */
                        test_fail_if (ber < CE_RX_BL_BER_DEFAULT_UNDER
                                      || ber > CE_RX_BL_BER_DEFAULT_OVER);
                        /* Check that previous BER is always lower than new
                         * BER. */
                        if (j)
                            test_fail_if (ber_prev > ber);
                        ber_prev = ber;
                    }
                }
            }
        }
    } 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[fec_rate],
                                     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")
    {
        const uint pb[2] = { 1, 255 };
        const s64 ber[][2] =
        {
            /* From hardware, result. */
            { 0, 0 }, /* Minimum. */
            /* 64: 12.3% of bits corrected. */
            { 8 * 64, 1108578369814276LL }, /* Max. */
            /* A random value. */
            { 42, 90938069398827LL } ,
        };
        /* According to TNS, 16 bits of error is tolerated. */
        const s64 error_tolerated = 1 << 16;
        uint i, j;
        for (i = 0; i < COUNT (ber); i++)
        {
            for (j = 0; j < 2; j++)
            {
                /* Quantify. */
                u64 res = ce_rx_bl_ber_quantify
                    (ber[i][0] * pb[j], pb[j], 520 * 8);
                /* Compute diff. */
                s64 diff = ber[i][1] - 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);

        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);

        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);

        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;

    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;
    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,
                                                                fec_rate, &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);
                /* 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
        {
            ce_rx_bl_update_tone_map_at_ber_consign
                (CE_RX_BL_BER_DEFAULT_OVER - 1, &ti, &bl, tm, opti,
                 &berws, &tone_en);
        }
        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;
        ce_rx_bl_update_tone_map_at_ber_consign
            (1, &ti, &bl, tm, opti, &berws, &tone_en);
        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;
        ce_rx_bl_update_tone_map_at_ber_consign
            (CE_RX_BL_BER_DEFAULT_OVER - 1, &ti, &bl, tm, opti, &berws,
             &tone_en);
        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);
}

static void
test_suite_ce_rx_bl_tm_update (test_t t)
{
    /**
     * Test for the number of tones to shift in the optimization table
     * depending of a BER target.
     */
    test_begin (t, "test the optimization table shift counter")
    {
        ce_rx_bl_tone_map_update_actions_t out_action;
        u16 out;
        u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB];
        uint test_cpt = 0;
        u64 ber_target = 1000;

        for (test_cpt = 0; test_cpt < 9; test_cpt++)
        {
            /*
             * Each ber means can be in 3 zones:
             * (1) Above the ber target.
             * (2) Between ber target and 10% of ber target.
             * (3) Under 10% of ber target.
             * This suppose 3^2=9 different positions.
             */
            switch (test_cpt)
            {
                case 0:
                    /* Slow (1) and Fast (1). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 1200;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 1200;
                break;
                case 1:
                    /* Slow (1) and Fast (2). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 1200;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 999;
                break;
                case 2:
                    /* Slow (1) and Fast (3). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 1200;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 42;
                break;
                case 3:
                    /* Slow (2) and Fast (1). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 999;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 1200;
                break;
                case 4:
                    /* Slow (2) and Fast (2). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 999;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 999;
                break;
                case 5:
                    /* Slow (2) and Fast (3). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 999;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 42;
                break;
                case 6:
                    /* Slow (3) and Fast (1). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 42;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 1200;
                break;
                case 7:
                    /* Slow (3) and Fast (2). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 42;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 999;
                break;
                case 8:
                    /* Slow (3) and Fast (3). */
                    means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 42;
                    means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 42;
                break;
            }

            /* Compute action to perform. */
            out_action = ce_rx_bl_tone_map_update_action (ber_target, means);

            /* Output of the tested function. */
            out = ce_rx_bl_tone_map_update_count (out_action,
                                                  ber_target,
                                                  means);

            /* Tests. */
            test_fail_if (out_action == CE_RX_BL_TONE_MAP_UPDATE_NONE
                          && out != 0);
            test_fail_if (out_action == CE_RX_BL_TONE_MAP_UPDATE_MINUS
                          && out == 0);
            test_fail_if (out_action == CE_RX_BL_TONE_MAP_UPDATE_PLUS
                          && out == 0);
            test_fail_if (out_action != CE_RX_BL_TONE_MAP_UPDATE_NONE
                          && out_action != CE_RX_BL_TONE_MAP_UPDATE_MINUS
                          && out_action != CE_RX_BL_TONE_MAP_UPDATE_PLUS);
        }
    } test_end;

    /**
     * Test for the number of tones to shift in the optimization table
     * depending of a BER target. Data used here come from vector test.
     */
    test_begin (t, "test the optimization table shift counter (test vector)")
    {
        /* Input data from test vector. */
        u64 ber_target;
        u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB];
        /* Output data from test vector. */
        u16 shift_count;
        ce_rx_bl_tone_map_update_actions_t action;

        u16 t;

        for (t = 0; t < bl_iteratif_opti_table_shift_height; t++)
        {
            /* Fill with data from test vector. */
            ber_target = bl_iteratif_opti_table_shift[t][0];
            means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = bl_iteratif_opti_table_shift[t][1];
            means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = bl_iteratif_opti_table_shift[t][2];
            shift_count = bl_iteratif_opti_table_shift[t][3];
            action = bl_iteratif_opti_table_shift[t][4];

            /* Check the performed action is correct. */
            test_fail_if (action !=
                          ce_rx_bl_tone_map_update_action (ber_target,
                                                           means));
            /* Check the shift count is correct. */
            test_fail_if (shift_count !=
                          ce_rx_bl_tone_map_update_count (action,
                                                          ber_target,
                                                          means));
        }
    } test_end;

    /**
     * Test to check the tone map update action to perform
     * depending of sliding means and ber target.
     * Each ber means can be in 3 zones:
     * (1) Above the ber target.
     * (2) Between ber target and 10% of ber target.
     * (3) Under 10% of ber target.
     * This suppose 3^2=9 different positions.
     */
    test_begin (t, "test update action to perform considering sliding means")
    {
        ce_rx_bl_tone_map_update_actions_t out;
        u64 ber_target = 0;
        u64 ber_target_limit = 0;
        u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB];

        lib_rnd_t rand;
        lib_rnd_init (&rand, 4251);
        uint test_limit = 300;
        uint all[9] = {0,0,0,0,0,0,0,0,0};
        u8 test_finished = 0;
        u8 cpt, cpt2;

        while (!test_finished)
        {
            /* Generate random ber means and target. */
            means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = lib_rnd32 (&rand)
                + ((u64) lib_rnd32 (&rand) << 32);
            means[CE_RX_BL_BER_SLIDING_MEAN_FAST] = lib_rnd32 (&rand)
                + ((u64) lib_rnd32 (&rand) << 32);
            ber_target = ((u64) lib_rnd32 (&rand) << 32) + lib_rnd32 (&rand);
            ber_target_limit = ber_target - (u64) (ber_target
                / CE_RX_BL_TONE_MAP_UPDATE_MIDDLE_PERCENTAGE);

            /* Get output of the function under test. */
            out = ce_rx_bl_tone_map_update_action (ber_target,
                                                   means);

            /* Slow (1) and Fast (1). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] > ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] > ber_target)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_MINUS);
                if (all[0] < test_limit)
                    all[0]++;
            }

            /* Slow (1) and Fast (2). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] > ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] >= ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_NONE);
                if (all[1] < test_limit)
                    all[1]++;
            }

            /* Slow (1) and Fast (3). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] > ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] < ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_NONE);
                if (all[2] < test_limit)
                    all[2]++;
            }

            /* Slow (2) and Fast (1). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] >= ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] > ber_target)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_MINUS);
                if (all[3] < test_limit)
                    all[3]++;
            }

            /* Slow (2) and Fast (2). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] >= ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] >= ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_NONE);
                if (all[4] < test_limit)
                    all[4]++;
            }

            /* Slow (2) and Fast (3). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] > ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] < ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_NONE);
                if (all[5] < test_limit)
                    all[5]++;
            }

            /* Slow (3) and Fast (1). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] < ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] > ber_target)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_MINUS);
                if (all[6] < test_limit)
                    all[6]++;
            }

            /* Slow (3) and Fast (2). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] < ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] <= ber_target
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] >= ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_NONE);
                if (all[7] < test_limit)
                    all[7]++;
            }

            /* Slow (3) and Fast (3). */
            if (means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] < ber_target_limit
                && means[CE_RX_BL_BER_SLIDING_MEAN_FAST] < ber_target_limit)
            {
                test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_PLUS);
                if (all[8] < test_limit)
                    all[8]++;
            }

            /* Is this the end ? */
            cpt2 = 0;
            for (cpt = 0; cpt < 9; cpt++)
                if (all[cpt] >= test_limit)
                    cpt2++;
                else
                    break;
            if (cpt2 == 9)
                test_finished = 1;
            else
                cpt2 = 0;
        }
    } test_end;

    /**
    * Test for creating a new tone map based on old one with some
    * adjustments done using the optimization table.
    * This test verify that we do not compute a new tone map when we
    * we don't need to.
    */
    test_begin (t, "Verify that we don't do any thing when we don't need to")
    {
        ce_rx_bl_tone_map_update_status_t out = 0;
        u64 ber_target = 0;
        ce_rx_bitloading_t bl;
        uint tone_en = 0;
        tonemap_t *new_tonemap = NULL;
        tonemap_t *tm = NULL;

        tm = tonemap_alloc ();
        ber_target = 100;
        ce_rx_bitloading_init (&bl);
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 99;
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 98;
        bl.opti_table_cursor = 100;
        bl.opti_table = blk_table_init (sizeof (u16), PHY_CARRIER_NB);

        out = ce_rx_bl_tone_map_update_compute_new_tonemap (&bl,
                                                            ber_target,
                                                            tm,
                                                            tone_en,
                                                            &new_tonemap);
        test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_STATUS_NOTHING);
        test_fail_if (new_tonemap != NULL);
        test_fail_if (bl.opti_table_cursor != 100);
        tonemap_free (tm);
        ce_rx_bitloading_uninit (&bl);
    } test_end;

    /**
    * Test for creating a new tone map based on old one with some
    * adjustments done using the optimization table.
    * This test verify that we correctly compute a new tone when we have
    * to do a Tone Map +.
    */
    test_begin (t, "Verify TM+ in a classic case")
    {
        ce_rx_bl_tone_map_update_status_t out = 0;
        u64 ber_target = 0;
        ce_rx_bitloading_t bl;
        uint tone_en = 0;
        tonemap_t *new_tonemap = NULL;
        tonemap_t *tm = NULL;
        tonemask_info_t ti;

        uint i = 0;
        uint old_opti_cursor = 0;
        uint bits_per_symbol = 0;
        u16 *tmp;

        /* Preparing input data and supposed output. */
        ti.carrier_nb = tonemask_default (ti.tonemask);
        tm = tonemap_alloc ();
        tone_en = ti.carrier_nb;
        ce_rx_bitloading_init (&bl);
        bl.opti_table = blk_table_init (sizeof (u16), tone_en);
        uint pos_opti = 0;
        uint tone_index_real = 0;
#define TONEMAP_WRITE_OPEN {
#define TONEMAP_WRITE_CLOSE }
        TONEMAP_WRITE_BEGIN (tm, ti.tonemask)
        {
            tmp = (u16 *) blk_table_get (bl.opti_table, pos_opti++);
            *tmp = tone_index_real++;
            TONEMAP_WRITE_MOD (1);
            tm->bits_per_symbol += CE_BIT_PER_MOD[1];
        }
        TONEMAP_WRITE_CLOSE
        else
        TONEMAP_WRITE_OPEN
        {
            tone_index_real++;
        }
        TONEMAP_WRITE_END;
#undef TONEMAP_WRITE_OPEN
#undef TONEMAP_WRITE_CLOSE
        bl.opti_table_cursor = tone_en / 2;
        old_opti_cursor = bl.opti_table_cursor;
        /* Produce a TM+ operation. */
        ber_target = 100;
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 40;
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 30;

        /* Test. */
        out = ce_rx_bl_tone_map_update_compute_new_tonemap (&bl,
                                                            ber_target,
                                                            tm,
                                                            tone_en,
                                                            &new_tonemap);

        /* Check basic errors. */
        if (ce_rx_bl_tone_map_update_count (CE_RX_BL_TONE_MAP_UPDATE_PLUS,
                ber_target, (u64 *) bl.ber_sliding_mean)
            + bl.opti_table_cursor <= tone_en)
        {
            test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_STATUS_OK);
            test_fail_if (bl.opti_table_cursor != old_opti_cursor
                + ce_rx_bl_tone_map_update_count (
                CE_RX_BL_TONE_MAP_UPDATE_PLUS, ber_target, (u64 *) bl.ber_sliding_mean));
        }
        else
        {
            test_fail_if (out !=
                CE_RX_BL_TONE_MAP_UPDATE_STATUS_OUT_OF_RANGE_MAX);
            test_fail_if (bl.opti_table_cursor != tone_en - 1);
        }
        test_fail_if (new_tonemap == NULL);

        /* Verify updated values in the new tone map */
        for (i = old_opti_cursor; i < bl.opti_table_cursor; i++)
        {
            u32 *word_ptr_old = NULL;
            u32 *word_ptr_new = NULL;
            tmp = (u16 *) blk_table_get (bl.opti_table, i);
            u8 mod_old = tonemap_get_tone (tm,
                                           *tmp,
                                           &word_ptr_old);
            u8 mod_new = tonemap_get_tone (new_tonemap,
                                           *tmp,
                                           &word_ptr_new);
            test_fail_if (mod_old != 1);
            test_fail_if (mod_new != 2);

            bits_per_symbol += CE_BIT_PER_MOD[mod_new];
            bits_per_symbol -= CE_BIT_PER_MOD[mod_old];
        }

        /* Check that bits per symbol is ok. */
        test_fail_if (tm->bits_per_symbol + bits_per_symbol !=
                      new_tonemap->bits_per_symbol);

        /* Cleanup */
        ce_rx_bitloading_uninit (&bl);
        tonemap_free (tm);
        tonemap_free (new_tonemap);
    } test_end;

    /**
    * Test for creating a new tone map based on old one with some
    * adjustments done using the optimization table.
    * This test verify that we correctly compute a new tone when we have
    * to do a Tone Map -.
    */
    test_begin (t, "Verify TM- in a classic case")
    {
        ce_rx_bl_tone_map_update_status_t out = 0;
        u64 ber_target = 0;
        ce_rx_bitloading_t bl;
        uint tone_en = 0;
        tonemap_t *new_tonemap = NULL;
        tonemap_t *tm = NULL;
        tonemask_info_t ti;

        uint i = 0;
        uint old_opti_cursor = 0;
        uint bits_per_symbol = 0;
        u16 *tmp;

        /* Preparing input data and supposed output. */
        ti.carrier_nb = tonemask_default (ti.tonemask);
        tm = tonemap_alloc ();
        tone_en = ti.carrier_nb;
        ce_rx_bitloading_init (&bl);
        bl.opti_table = blk_table_init (sizeof (u16), tone_en);
        uint pos_opti = 0;
        uint tone_index_real = 0;
#define TONEMAP_WRITE_OPEN {
#define TONEMAP_WRITE_CLOSE }
        TONEMAP_WRITE_BEGIN (tm, ti.tonemask)
        {
            tmp = (u16 *) blk_table_get (bl.opti_table, pos_opti++);
            *tmp = tone_index_real++;
            TONEMAP_WRITE_MOD (1);
            tm->bits_per_symbol += CE_BIT_PER_MOD[1];
        }
        TONEMAP_WRITE_CLOSE
        else
        TONEMAP_WRITE_OPEN
        {
            tone_index_real++;
        }
        TONEMAP_WRITE_END;
#undef TONEMAP_WRITE_OPEN
#undef TONEMAP_WRITE_CLOSE
        bl.opti_table_cursor = tone_en / 2;
        old_opti_cursor = bl.opti_table_cursor;
        /* Produce a TM- operation. */
        ber_target = 100;
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_FAST] = 120;
        bl.ber_sliding_mean[CE_RX_BL_BER_SLIDING_MEAN_SLOW] = 123;

        /* Test. */
        out = ce_rx_bl_tone_map_update_compute_new_tonemap (&bl,
                                                            ber_target,
                                                            tm,
                                                            tone_en,
                                                            &new_tonemap);

        /* Check basic errors. */
        if ((int) bl.opti_table_cursor - (int) ce_rx_bl_tone_map_update_count (
                CE_RX_BL_TONE_MAP_UPDATE_MINUS,
                ber_target, (u64 *) bl.ber_sliding_mean) >= 0)
        {
            test_fail_if (out != CE_RX_BL_TONE_MAP_UPDATE_STATUS_OK);
            test_fail_if (bl.opti_table_cursor != old_opti_cursor -
                ce_rx_bl_tone_map_update_count (CE_RX_BL_TONE_MAP_UPDATE_MINUS
                , ber_target, (u64 *) bl.ber_sliding_mean));
        }
        else
        {
            test_fail_if (out !=
                CE_RX_BL_TONE_MAP_UPDATE_STATUS_OUT_OF_RANGE_MIN);
            test_fail_if (bl.opti_table_cursor != tone_en - 1);
        }
        test_fail_if (new_tonemap == NULL);

        /* Verify updated values in the new tone map */
        for (i = bl.opti_table_cursor;
             i < old_opti_cursor;
             i++)
        {
            u32 *word_ptr_old = NULL;
            u32 *word_ptr_new = NULL;
            tmp = (u16 *) blk_table_get (bl.opti_table, i);
            u8 mod_old = tonemap_get_tone (tm,
                                           *tmp,
                                           &word_ptr_old);
            u8 mod_new = tonemap_get_tone (new_tonemap,
                                           *tmp,
                                           &word_ptr_new);

            test_fail_if (mod_old != 1);
            test_fail_if (mod_new != 0);

            bits_per_symbol += CE_BIT_PER_MOD[mod_new];
            bits_per_symbol -= CE_BIT_PER_MOD[mod_old];
        }

        /* Check that bits per symbol is ok. */
        test_fail_if (tm->bits_per_symbol + bits_per_symbol !=
            new_tonemap->bits_per_symbol);

        /* Cleanup */
        ce_rx_bitloading_uninit (&bl);
        tonemap_free (tm);
        tonemap_free (new_tonemap);
    } test_end;

    /*
     * Test if the computation of a new tone map after a tone map update
     * operation is correct. Data used here come from vector test.
     */
    test_begin (t, "test tone map creation using tone map update (test vector)")
    {
        u16 t;
        u64 i,j;
        /* Preparing data from test vector. */
        /* Tone map input from
         * bl_iteratif_tm_update_new_tm_IN_1_tone_map_input.data. */
        tonemap_t *tm_in = tonemap_alloc ();
        /* Optimisation table from
         * bl_iteratif_tm_update_new_tm_IN_2_opti_table.data. */
        u16 opti[PHY_CARRIER_NB];
        /* Ber target (and sliding means) input from
         * bl_iteratif_tm_update_new_tm_IN_3_ber_input.data. */
        u64 ber_target;
        u64 means[CE_RX_BL_BER_SLIDING_MEAN_NB];
        /* Cursor input position from
         * bl_iteratif_tm_update_new_tm_IN_4_cursor_input.data. */
        uint curs_in;
        /* Tone map output from
         * bl_iteratif_tm_update_new_tm_OUT_1_tone_map_output.data. */
        tonemap_t *tm_out = tonemap_alloc ();
        /* Cursor output position from
         * bl_iteratif_tm_update_new_tm_OUT_2_cursor_output.data. */
        uint curs_out;
        /* Default tonemask. */
        tonemask_info_t dtmask;
        dtmask.carrier_nb = tonemask_default (dtmask.tonemask);
        /* Variables to check sanity of test vectors. */
        u16 shift_count;
        ce_rx_bl_tone_map_update_actions_t action;
        /* Variables to check function output. */
        tonemap_t *computed_tm;
        ce_rx_bl_tone_map_update_status_t status;
        uint test_curs;
        /* The opposite of CE_BIT_PER_MOD. */
        uint ce_mod_per_bit[CE_BIT_PER_MOD[CE_MOD_COUNT - 1]];
        for (i = 0; i < CE_MOD_COUNT; i++)
            ce_mod_per_bit[CE_BIT_PER_MOD[i]] = i;

        /* For each tone maps, test the computation of the new tone map based
         * on the optimization table and ber parameters. */
        for (t = 0;
             t < bl_iteratif_tm_update_new_tm_IN_1_tone_map_input_width;
             t++)
        {
            /* Fill with data from test vector. */
            /* Input tone map. */
            j = 0;
#define TONEMAP_WRITE_OPEN {
#define TONEMAP_WRITE_CLOSE }
            TONEMAP_WRITE_BEGIN (tm_in, dtmask.tonemask)
            {
                TONEMAP_WRITE_MOD (
                    ce_mod_per_bit[
                    bl_iteratif_tm_update_new_tm_IN_1_tone_map_input[j][t]
                    ]);
                j++;
            }
            TONEMAP_WRITE_CLOSE
            else
            TONEMAP_WRITE_OPEN
            {
                j++;
            }
            TONEMAP_WRITE_END;
#undef TONEMAP_WRITE_OPEN
#undef TONEMAP_WRITE_CLOSE
            /* Optimisation table. */
            for (i = 0;
                 i < bl_iteratif_tm_update_new_tm_IN_2_opti_table_height;
                 i++)
                /* The first 75 carriers are not considered, look
                 * documentation of tonemask_default in
                 * mac/common/src/tonemask.c. The soustracted value is 75 (and
                 * not 74 because the input data begin at 1 (and not 0). */
                opti[i] = bl_iteratif_tm_update_new_tm_IN_2_opti_table[i][t]
                          - (PHY_CARRIER_OFFSET + 1);
            /* Ber target. */
            ber_target = bl_iteratif_tm_update_new_tm_IN_3_ber_input[t][0];
            /* Slow mean. */
            means[CE_RX_BL_BER_SLIDING_MEAN_SLOW] =
                bl_iteratif_tm_update_new_tm_IN_3_ber_input[t][1];
            /* Fast Mean. */
            means[CE_RX_BL_BER_SLIDING_MEAN_FAST] =
                bl_iteratif_tm_update_new_tm_IN_3_ber_input[t][2];
            /* Input Cursor. */
            curs_in = bl_iteratif_tm_update_new_tm_IN_4_cursor_input[0][t];
            test_curs = curs_in;
            /* Output tone map. */
            j = 0;
#define TONEMAP_WRITE_OPEN {
#define TONEMAP_WRITE_CLOSE }
            TONEMAP_WRITE_BEGIN (tm_out, dtmask.tonemask)
            {
                TONEMAP_WRITE_MOD (
                    ce_mod_per_bit[
                    bl_iteratif_tm_update_new_tm_OUT_1_tone_map_output[j][t]
                    ]);
                j++;
            }
            TONEMAP_WRITE_CLOSE
            else
            TONEMAP_WRITE_OPEN
            {
                j++;
            }
            TONEMAP_WRITE_END;
#undef TONEMAP_WRITE_OPEN
#undef TONEMAP_WRITE_CLOSE
            /* Output cursor. */
            curs_out = bl_iteratif_tm_update_new_tm_OUT_2_cursor_output[0][t];

            /* Sanity check: is the shift count is valid ? */
            action = ce_rx_bl_tone_map_update_action (ber_target,
                                                      means);
            shift_count = MAX (curs_in, curs_out) - MIN (curs_in, curs_out);
            test_fail_if (shift_count !=
                          ce_rx_bl_tone_map_update_count (action,
                                                          ber_target,
                                                          means));

            /* Sanity check: is the optimization table valid ?
             * At the cursor position and after, tones should be strictly
             * under a modulation of 10 bits (except the end of the table).
             * Below the cursor, tones should > 0. */
            for (i = 0; i < dtmask.carrier_nb; i++)
            {
                u32 *word_ptr = NULL;
                u8 mod = tonemap_get_tone (tm_in,
                                           opti[i],
                                           &word_ptr);
                if (i < curs_in)
                    test_fail_if (mod == 0);
            }

            /* Fill input data in ce_rx_bitloading_t structure. */
            ce_rx_bitloading_t bl;
            ce_rx_bitloading_init (&bl);
            u16 *tmp;
            for (i = 0; i < CE_RX_BL_BER_SLIDING_MEAN_NB; i++)
                bl.ber_sliding_mean[i] = (s64) means[i];
            bl.opti_table_cursor = test_curs;
            bl.opti_table = blk_table_init (sizeof (u16),
                bl_iteratif_tm_update_new_tm_IN_2_opti_table_height);
            for (i = 0;
                 i < bl_iteratif_tm_update_new_tm_IN_2_opti_table_height;
                 i++)
            {
                tmp = (u16 *) blk_table_get (bl.opti_table, i);
                *tmp = opti[i];
            }

            /* Test function. */
            status = ce_rx_bl_tone_map_update_compute_new_tonemap (
                &bl,
                ber_target,
                tm_in,
                dtmask.carrier_nb,
                &computed_tm);

            /* Re-import data from ce_rx_bitloading_t and free it. */
            test_curs = bl.opti_table_cursor;
            ce_rx_bitloading_uninit (&bl);

            /* Check status. */
            test_fail_if (status == CE_RX_BL_TONE_MAP_UPDATE_STATUS_ERROR);

            /* Check if the new cursor position is well computed. */
            test_fail_if (test_curs != curs_out);

            /* Check if the generated tone map is the same. */
            u32 *word_ptr_out = NULL;
            u32 *word_ptr_in = NULL;
            u32 *word_ptr_cpt = NULL;
            u8 mod_out;
            u8 mod_in;
            u8 mod_cpt;
            for (i = 0; i < dtmask.carrier_nb; i++)
            {
                mod_out = tonemap_get_tone (tm_out,
                                            opti[i],
                                            &word_ptr_out);
                mod_in = tonemap_get_tone (tm_in,
                                           opti[i],
                                           &word_ptr_in);
                mod_cpt = tonemap_get_tone (computed_tm,
                                            opti[i],
                                            &word_ptr_cpt);

                if (action == CE_RX_BL_TONE_MAP_UPDATE_MINUS)
                {
                    if (i >= curs_out && i < curs_in)
                    {
                        test_fail_if (mod_out != 0 && mod_out != mod_in - 1);
                        test_fail_if (mod_cpt != mod_out);
                    }
                    else
                        test_fail_if ((mod_out != mod_in)
                                      || (mod_out != mod_cpt));
                }
                if (action == CE_RX_BL_TONE_MAP_UPDATE_PLUS)
                {
                    if (i < curs_out && i >= curs_in)
                    {
                        test_fail_if (mod_out != CE_MOD_COUNT - 1
                                      && mod_out != mod_in + 1);
                        test_fail_if (mod_cpt != mod_out);
                    }
                    else
                        test_fail_if ((mod_out != mod_in)
                                      || (mod_out != mod_cpt));
                }
            }
            /* Clean. */
            tonemap_free (computed_tm);
        }
        tonemap_free (tm_in);
        tonemap_free (tm_out);
    } test_end;
}

int
test_suite_ce_rx_bl_pber_sliding_mean_update_function (int pber_sliding_mean,
                                                       uint pb_count,
                                                       uint false_pb_count,
                                                       uint supposed_new_pber)
{
    /* Return 0 if test is ok and other value if the test fail.*/
    ce_rx_bitloading_t bt;
    bt.pber_sliding_mean = pber_sliding_mean;
    ce_rx_bl_pber_sliding_mean_update (&bt, pb_count, false_pb_count);
    if (supposed_new_pber == bt.pber_sliding_mean)
        return 0;
    else
        return 1;
}

static void
test_suite_ce_rx_bl_pber_sliding_mean_update (test_t t)
{
    test_begin (t, "test pber sliding mean.")
    {
        test_fail_if (test_suite_ce_rx_bl_pber_sliding_mean_update_function (
                100, 1, 1, 355));
        test_fail_if (test_suite_ce_rx_bl_pber_sliding_mean_update_function (
                10000, 1, 0, 9960));
        test_fail_if (test_suite_ce_rx_bl_pber_sliding_mean_update_function (
                0, 100, 10, 25));
        test_fail_if (test_suite_ce_rx_bl_pber_sliding_mean_update_function (
                65536, MAC_MAX_PB_PER_MPDU, MAC_MAX_PB_PER_MPDU, 65536));
        test_fail_if (test_suite_ce_rx_bl_pber_sliding_mean_update_function (
                65536, MAC_MAX_PB_PER_MPDU, 0, 65280));
    } test_end;
}

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");

    /* 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);

    /* 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);

    /* Test tone map update */
    test_suite_ce_rx_bl_tm_update (t);

    /* Test PBER sliding mean. */
    test_suite_ce_rx_bl_pber_sliding_mean_update (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);
}