a2lfile 3.4.0

read, modify and write a2l files
Documentation
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
use crate::{
    A2lError, ItemList,
    module::{AnyCompuTab, AnyObject, AnyTypedef},
    specification::*,
};
use std::collections::HashMap;

#[derive(Debug, Clone, Copy, PartialEq)]
enum CharacteristicWrapper<'a> {
    Characteristic(&'a Characteristic),
    TypedefCharacteristic(&'a TypedefCharacteristic),
}

#[must_use]
// check the cross references between various elements
pub fn check(a2l_file: &A2lFile) -> Vec<A2lError> {
    let mut results = Vec::new();

    for module in &a2l_file.project.module {
        let compu_tabs = module.compu_tabs();
        let objects = module.objects();
        let typedefs = module.typedefs();

        for axis_pts in &module.axis_pts {
            check_axis_pts(axis_pts, module, &objects, &mut results);
        }

        for t_axis in &module.typedef_axis {
            check_typedef_axis(t_axis, module, &objects, &mut results);
        }

        for characteristic in &module.characteristic {
            check_characteristic(characteristic, module, &objects, &mut results);
        }

        for t_characteristic in &module.typedef_characteristic {
            // a TYPEDEF_CHARACTERISTIC is directly used if it is used in an INSTANCE
            // indirect use is if it is used as a STRUCTURE_COMPONENT in a TYPEDEF_STRUCTURE
            // directly used TYPEDEF_CHARACTERISTICs cannot use THIS
            let is_directly_used = module
                .instance
                .iter()
                .any(|instance| instance.type_ref == t_characteristic.name);
            // build a list of TYPEDEF_STRUCTUREs that use this TYPEDEF_CHARACTERISTIC as a STRUCTURE_COMPONENT
            // indirectly used TYPEDEF_CHARACTERISTICs can use THIS in their AXIS_DESCRs to refer to the
            // containing structure
            let containing_structures: Vec<&TypedefStructure> = module
                .typedef_structure
                .iter()
                .filter(|ts| {
                    ts.structure_component
                        .iter()
                        .any(|sc| sc.component_type == t_characteristic.name)
                })
                .collect();
            check_typedef_characteristic(
                t_characteristic,
                module,
                &objects,
                is_directly_used,
                &containing_structures,
                &mut results,
            );
        }

        for compu_method in &module.compu_method {
            check_compu_method(compu_method, module, &compu_tabs, &mut results);
        }

        for function in &module.function {
            check_function(function, module, &objects, &mut results);
        }

        for group in &module.group {
            check_group(group, module, &objects, &mut results);
        }
        check_group_structure(&module.group, &mut results);

        for measurement in &module.measurement {
            check_measurement(measurement, module, &mut results);
        }

        for t_measurement in &module.typedef_measurement {
            check_typedef_measurement(t_measurement, module, &mut results);
        }

        for transformer in &module.transformer {
            check_transformer(transformer, module, &objects, &mut results);
        }

        for instance in &module.instance {
            check_instance(instance, &typedefs, &mut results);
        }

        for typedef_structure in &module.typedef_structure {
            check_typedef_structure(typedef_structure, &typedefs, &mut results);
        }
    }

    results
}

fn check_axis_descr(
    idx: usize,
    parent_name: &str,
    axis_descr: &AxisDescr,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    let line = axis_descr.get_line();
    if axis_descr.input_quantity != "NO_INPUT_QUANTITY"
        && !objects.contains_key(&axis_descr.input_quantity)
        && module
            .find_instance_component(&axis_descr.input_quantity)
            .is_none()
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
            source_name: parent_name.to_string(),
            source_line: line,
            target_type: "MEASUREMENT".to_string(),
            target_name: axis_descr.input_quantity.to_string(),
        });
    }

    if axis_descr.conversion != "NO_COMPU_METHOD"
        && !module.compu_method.contains_key(&axis_descr.conversion)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
            source_name: parent_name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: axis_descr.conversion.to_string(),
        });
    }

    // type is (COM_AXIS, RES_AXIS) XOR AXIS_PTS_REF is present
    match (axis_descr.attribute, axis_descr.axis_pts_ref.is_some()) {
        (AxisDescrAttribute::ComAxis, false) | (AxisDescrAttribute::ResAxis, false) => {
            log_msgs.push(A2lError::ContentError {
                item_name: parent_name.to_string(),
                blockname: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                line,
                description: format!("{} requires an AXIS_PTS_REF.", axis_descr.attribute),
            });
        }
        (AxisDescrAttribute::StdAxis, true) | (AxisDescrAttribute::FixAxis, true) => {
            log_msgs.push(A2lError::ContentError {
                item_name: parent_name.to_string(),
                blockname: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                line,
                description: format!("{} does not use AXIS_PTS_REF.", axis_descr.attribute),
            });
        }
        // other combinations are valid
        _ => {}
    }

    // CURVE_AXIS_REF is only allowed for AXIS_DESCR of type CURVE_AXIS
    if axis_descr.attribute != AxisDescrAttribute::CurveAxis && axis_descr.curve_axis_ref.is_some()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: parent_name.to_string(),
            blockname: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
            line,
            description: "Only AXIS_DESCR of type CURVE_AXIS can have a CURVE_AXIS_REF."
                .to_string(),
        });
    } else if axis_descr.attribute == AxisDescrAttribute::CurveAxis
        && axis_descr.curve_axis_ref.is_none()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: parent_name.to_string(),
            blockname: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
            line,
            description: "AXIS_DESCR of type CURVE_AXIS must have a CURVE_AXIS_REF.".to_string(),
        });
    }
}

// check axis_pts_ref and curve_axis_ref, which both have the special feature
// of potentially referencing structure components using THIS.
#[allow(clippy::too_many_arguments)]
fn check_axis_descr_refs(
    module: &Module,
    idx: usize,
    parent_name: &str,
    axis_descr: &AxisDescr,
    objects: &ItemList<AnyObject>,
    is_directly_used: bool,
    containing_structures: &[&TypedefStructure],
    log_msgs: &mut Vec<A2lError>,
) {
    // AXIS_DESCR.AXIS_PTS_REF
    if let Some(axis_pts_ref) = &axis_descr.axis_pts_ref {
        let apr_line = axis_pts_ref.get_line();
        if !is_directly_used
            && !containing_structures.is_empty()
            && axis_pts_ref.axis_points.starts_with("THIS.")
        {
            let structure_component = axis_pts_ref.axis_points.strip_prefix("THIS.").unwrap();
            // is there any containing structure which does not have a component with the given name?
            if !is_valid_structure_component(structure_component, containing_structures) {
                // if so, the THIS reference is invalid
                log_msgs.push(A2lError::CrossReferenceError {
                    source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                    source_name: parent_name.to_string(),
                    source_line: apr_line,
                    target_type: "STRUCTURE_COMPONENT".to_string(),
                    target_name: structure_component.to_string(),
                });
            }
        } else if !objects.contains_key(&axis_pts_ref.axis_points)
            && module
                .find_instance_component(&axis_pts_ref.axis_points)
                .is_none()
        {
            log_msgs.push(A2lError::CrossReferenceError {
                source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                source_name: parent_name.to_string(),
                source_line: apr_line,
                target_type: "AXIS_PTS".to_string(),
                target_name: axis_pts_ref.axis_points.to_string(),
            });
        }
    }

    // AXIS_DESCR.CURVE_AXIS_REF
    if let Some(curve_axis_ref) = &axis_descr.curve_axis_ref {
        let car_line = curve_axis_ref.get_line();
        if !is_directly_used
            && !containing_structures.is_empty()
            && curve_axis_ref.curve_axis.starts_with("THIS.")
        {
            let structure_component = curve_axis_ref.curve_axis.strip_prefix("THIS.").unwrap();
            // is there any containing structure which does not have a component with the given name?
            if !is_valid_structure_component(structure_component, containing_structures) {
                // if so, the THIS reference is invalid
                log_msgs.push(A2lError::CrossReferenceError {
                    source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                    source_name: parent_name.to_string(),
                    source_line: car_line,
                    target_type: "STRUCTURE_COMPONENT".to_string(),
                    target_name: structure_component.to_string(),
                });
            }
        } else if !objects.contains_key(&curve_axis_ref.curve_axis)
            && module
                .find_instance_component(&curve_axis_ref.curve_axis)
                .is_none()
        {
            log_msgs.push(A2lError::CrossReferenceError {
                source_type: format!("AXIS_DESCR[{idx}] of CHARACTERISTIC"),
                source_name: parent_name.to_string(),
                source_line: car_line,
                target_type: "CHARACTERISTIC".to_string(),
                target_name: curve_axis_ref.curve_axis.to_string(),
            });
        }
    }
}

fn is_valid_structure_component(
    structure_component: &str,
    containing_structures: &[&TypedefStructure],
) -> bool {
    // all containing structures must have a structure_component with the given name
    containing_structures.iter().all(|td_struct| {
        td_struct
            .structure_component
            .iter()
            .any(|sc| sc.name == structure_component)
    })
}

fn check_typedef_axis(
    t_axis: &TypedefAxis,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = t_axis.get_name();
    let line = t_axis.get_line();

    if t_axis.input_quantity != "NO_INPUT_QUANTITY"
        && !objects.contains_key(&t_axis.input_quantity)
        && module
            .find_instance_component(&t_axis.input_quantity)
            .is_none()
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "TYPEDEF_AXIS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "MEASUREMENT".to_string(),
            target_name: t_axis.input_quantity.to_string(),
        });
    }

    if !module.record_layout.contains_key(&t_axis.record_layout) {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "TYPEDEF_AXIS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "RECORD_LAYOUT".to_string(),
            target_name: t_axis.record_layout.to_string(),
        });
    }

    if t_axis.conversion != "NO_COMPU_METHOD"
        && !module.compu_method.contains_key(&t_axis.conversion)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "TYPEDEF_AXIS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: t_axis.conversion.to_string(),
        });
    }
}

fn check_axis_pts(
    axis_pts: &AxisPts,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = &axis_pts.name;
    let line = axis_pts.get_line();

    if axis_pts.conversion != "NO_COMPU_METHOD"
        && !module.compu_method.contains_key(&axis_pts.conversion)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "AXIS_PTS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: axis_pts.conversion.to_string(),
        });
    }

    if axis_pts.input_quantity != "NO_INPUT_QUANTITY"
        && !objects.contains_key(&axis_pts.input_quantity)
        && module
            .find_instance_component(&axis_pts.input_quantity)
            .is_none()
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "AXIS_PTS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "MEASUREMENT".to_string(),
            target_name: axis_pts.input_quantity.to_string(),
        });
    }

    if let Some(rl) = module.record_layout.get(&axis_pts.deposit_record) {
        if let Some(axis_pts_x) = &rl.axis_pts_x {
            let opt_compu_method = module.compu_method.get(&axis_pts.conversion);
            let calculated_limits = calc_compu_method_limits(opt_compu_method, axis_pts_x.datatype);
            let existing_limits = (axis_pts.lower_limit, axis_pts.upper_limit);
            if !check_limits_valid(existing_limits, calculated_limits) {
                log_msgs.push(A2lError::LimitCheckError {
                    item_name: name.to_string(),
                    blockname: "AXIS_PTS".to_string(),
                    line,
                    lower_limit: axis_pts.lower_limit,
                    upper_limit: axis_pts.upper_limit,
                    calculated_lower_limit: calculated_limits.0,
                    calculated_upper_limit: calculated_limits.1,
                });
            }
        } else {
            log_msgs.push(A2lError::ContentError {
                item_name: name.to_string(),
                blockname: "AXIS_PTS".to_string(),
                line,
                description: format!(
                    "Referenced RECORD_LAYOUT {} does not have AXIS_PTS_X.",
                    axis_pts.deposit_record
                ),
            });
        }
    } else {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "AXIS_PTS".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "RECORD_LAYOUT".to_string(),
            target_name: axis_pts.deposit_record.to_string(),
        });
    }

    check_function_list(&axis_pts.function_list, module, log_msgs);
    check_ref_memory_segment(&axis_pts.ref_memory_segment, module, log_msgs);
}

fn check_characteristic(
    characteristic: &Characteristic,
    module: &Module,
    objects: &ItemList<AnyObject>,
    results: &mut Vec<A2lError>,
) {
    check_characteristic_common(
        CharacteristicWrapper::Characteristic(characteristic),
        module,
        objects,
        true, // all CHARACTERISTICs are directly used, indirect use only exists for TYPEDEF_CHARACTERISTICs
        &[],
        results,
    );

    if let Some(comparison_quantity) = &characteristic.comparison_quantity {
        let cqline = comparison_quantity.get_line();
        if !objects.contains_key(&comparison_quantity.name)
            && module
                .find_instance_component(&comparison_quantity.name)
                .is_none()
        {
            results.push(A2lError::CrossReferenceError {
                source_type: "CHARACTERISTIC".to_string(),
                source_name: characteristic.name.to_string(),
                source_line: cqline,
                target_type: "MEASUREMENT".to_string(),
                target_name: comparison_quantity.name.to_string(),
            });
        }
    }

    if let Some(dependent_characteristic) = &characteristic.dependent_characteristic {
        let depline = dependent_characteristic.get_line();
        check_reference_list(
            "DEPENDENT_CHARACTERISTIC",
            "CHARACTERISTIC",
            depline,
            &dependent_characteristic.characteristic_list,
            Some(module),
            objects,
            results,
        );
    }

    if let Some(map_list) = &characteristic.map_list {
        let ml_line = map_list.get_line();
        check_reference_list(
            "MAP_LIST",
            "CHARACTERISTIC",
            ml_line,
            &map_list.name_list,
            Some(module),
            objects,
            results,
        );
    }

    if let Some(virtual_characteristic) = &characteristic.virtual_characteristic {
        let vc_line = virtual_characteristic.get_line();
        check_reference_list(
            "VIRTUAL_CHARACTERISTIC",
            "CHARACTERISTIC",
            vc_line,
            &virtual_characteristic.characteristic_list,
            Some(module),
            objects,
            results,
        );
    }

    check_function_list(&characteristic.function_list, module, results);
    check_ref_memory_segment(&characteristic.ref_memory_segment, module, results);
}

fn check_typedef_characteristic(
    t_characteristic: &TypedefCharacteristic,
    module: &Module,
    objects: &ItemList<AnyObject>,
    is_directly_used: bool,
    containing_structures: &[&TypedefStructure],
    log_msgs: &mut Vec<A2lError>,
) {
    check_characteristic_common(
        CharacteristicWrapper::TypedefCharacteristic(t_characteristic),
        module,
        objects,
        is_directly_used,
        containing_structures,
        log_msgs,
    );
}

// check the items that are shared between CHARACTERISTIC and TYPEDEF_CHARACTERISTIC
fn check_characteristic_common(
    characteristic: CharacteristicWrapper,
    module: &Module,
    objects: &ItemList<AnyObject>,
    is_directly_used: bool,
    containing_structures: &[&TypedefStructure],
    log_msgs: &mut Vec<A2lError>,
) {
    let kind = characteristic.kind();
    let name = characteristic.name();
    let line = characteristic.line();

    if characteristic.conversion() != "NO_COMPU_METHOD"
        && !module
            .compu_method
            .contains_key(characteristic.conversion())
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: kind.to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: characteristic.conversion().to_string(),
        });
    }

    for (idx, axis_descr) in characteristic.axis_descr().iter().enumerate() {
        check_axis_descr(idx, name, axis_descr, module, objects, log_msgs);
        check_axis_descr_refs(
            module,
            idx,
            name,
            axis_descr,
            objects,
            is_directly_used,
            containing_structures,
            log_msgs,
        );
    }

    let mut expected_axis_count = match characteristic.characteristic_type() {
        CharacteristicType::Value | CharacteristicType::ValBlk | CharacteristicType::Ascii => 0,
        CharacteristicType::Curve => 1,
        CharacteristicType::Map => 2,
        CharacteristicType::Cuboid => 3,
        CharacteristicType::Cube4 => 4,
        CharacteristicType::Cube5 => 5,
    };
    // If the characteristic is a CUBOID and has a MAP_LIST, then it only has one axis, while the referenced MAPs each provide the other two axes.
    if characteristic.characteristic_type() == CharacteristicType::Cuboid
        && characteristic.map_list().is_some()
    {
        expected_axis_count = 1; // MAP_LIST defines the x and y axis, only the z axis remains
    }
    if characteristic.axis_descr().len() != expected_axis_count {
        log_msgs.push(A2lError::ContentError {
            item_name: name.to_string(),
            blockname: kind.to_string(),
            line,
            description: format!(
                "Expected {expected_axis_count} AXIS_DESCR for type {}, found {}",
                characteristic.characteristic_type(),
                characteristic.axis_descr().len()
            ),
        });
    }

    let rl_name = characteristic.record_layout();
    if let Some(record_layout) = module.record_layout.get(rl_name) {
        if let Some(fnc_values) = &record_layout.fnc_values {
            // check the limits of the characteristic based on the compu method
            // Not all characteristics have a compu method, since it can be NO_COMPU_METHOD
            let opt_compu_method = module.compu_method.get(characteristic.conversion());
            let calculated_limits = calc_compu_method_limits(opt_compu_method, fnc_values.datatype);
            let existing_limits = (characteristic.lower_limit(), characteristic.upper_limit());
            if !check_limits_valid(existing_limits, calculated_limits) {
                log_msgs.push(A2lError::LimitCheckError {
                    item_name: name.to_string(),
                    blockname: kind.to_string(),
                    line,
                    lower_limit: characteristic.lower_limit(),
                    upper_limit: characteristic.upper_limit(),
                    calculated_lower_limit: calculated_limits.0,
                    calculated_upper_limit: calculated_limits.1,
                });
            }
        } else {
            log_msgs.push(A2lError::ContentError {
                item_name: name.to_string(),
                blockname: kind.to_string(),
                line,
                description: format!(
                    "Referenced RECORD_LAYOUT {rl_name} does not have FNC_VALUES."
                ),
            });
        }

        // make an array of references to the axis_pts fields in the record layout, some or all of which may be None
        let axis_refs = [
            &record_layout.axis_pts_x,
            &record_layout.axis_pts_y,
            &record_layout.axis_pts_z,
            &record_layout.axis_pts_4,
            &record_layout.axis_pts_5,
        ];
        let axis_pts_names = ["X", "Y", "Z", "4", "5"];
        for (idx, axis_descr) in characteristic.axis_descr().iter().enumerate() {
            if axis_descr.attribute == AxisDescrAttribute::StdAxis {
                // an STD_AXIS must be described by the record layout - should this also apply to CURVE_AXIS?
                if let Some(axis_pts_dim) = axis_refs[idx] {
                    // the compu method is optional, it could be set to NO_COMPU_METHOD
                    let opt_compu_method = module.compu_method.get(&axis_descr.conversion);
                    let calculated_limits =
                        calc_compu_method_limits(opt_compu_method, axis_pts_dim.datatype);
                    let existing_limits = (axis_descr.lower_limit, axis_descr.upper_limit);
                    if !check_limits_valid(existing_limits, calculated_limits) {
                        let ad_line = axis_descr.get_line();
                        log_msgs.push(A2lError::LimitCheckError {
                            item_name: name.to_string(),
                            blockname: "AXIS_DESCR".to_string(),
                            line: ad_line,
                            lower_limit: axis_descr.lower_limit,
                            upper_limit: axis_descr.upper_limit,
                            calculated_lower_limit: calculated_limits.0,
                            calculated_upper_limit: calculated_limits.1,
                        });
                    }
                } else {
                    log_msgs.push(A2lError::ContentError {
                        item_name: name.to_string(),
                        blockname: kind.to_string(),
                        line,
                        description: format!(
                            "Referenced RECORD_LAYOUT {rl_name} does not have AXIS_PTS_{}.",
                            axis_pts_names[idx]
                        ),
                    });
                }
            }
        }
    } else {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: kind.to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "RECORD_LAYOUT".to_string(),
            target_name: rl_name.to_string(),
        });
    }
}

fn check_compu_method(
    compu_method: &CompuMethod,
    module: &Module,
    compu_tabs: &ItemList<AnyCompuTab>,
    log_msgs: &mut Vec<A2lError>,
) {
    let line = compu_method.get_line();
    let uses_table = compu_method.conversion_type == ConversionType::TabIntp
        || compu_method.conversion_type == ConversionType::TabNointp
        || compu_method.conversion_type == ConversionType::TabVerb;

    // a COMPU_METHOD with conversion type TAB_* must have a COMPU_TAB_REF, others must not have it
    if uses_table && compu_method.compu_tab_ref.is_none() {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: format!(
                "COMPU_METHOD with conversion type {} must have a COMPU_TAB_REF.",
                compu_method.conversion_type
            ),
        });
    } else if !uses_table && compu_method.compu_tab_ref.is_some() {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: format!(
                "COMPU_METHOD with conversion type {} must not have a COMPU_TAB_REF.",
                compu_method.conversion_type
            ),
        });
    }

    // a COMPU_METHOD with conversion type LINEAR must have COEFFS_LINEAR
    if compu_method.conversion_type == ConversionType::Linear
        && compu_method.coeffs_linear.is_none()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: "COMPU_METHOD with conversion type LINEAR must have COEFFS_LINEAR."
                .to_string(),
        });
    } else if compu_method.conversion_type != ConversionType::Linear
        && compu_method.coeffs_linear.is_some()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: format!(
                "COMPU_METHOD with conversion type {} must not have COEFFS_LINEAR.",
                compu_method.conversion_type
            ),
        });
    }

    // a COMPU_METHOD with conversion type RATIONAL must have COEFFS
    if compu_method.conversion_type == ConversionType::RatFunc && compu_method.coeffs.is_none() {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: "COMPU_METHOD with conversion type RATIONAL must have COEFFS.".to_string(),
        });
    } else if compu_method.conversion_type != ConversionType::RatFunc
        && compu_method.coeffs.is_some()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: format!(
                "COMPU_METHOD with conversion type {} must not have COEFFS.",
                compu_method.conversion_type
            ),
        });
    }

    // a COMPU_METHOD with conversion type FORM must have FORMULA
    if compu_method.conversion_type == ConversionType::Form && compu_method.formula.is_none() {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: "COMPU_METHOD with conversion type FORM must have FORMULA.".to_string(),
        });
    } else if compu_method.conversion_type != ConversionType::Form && compu_method.formula.is_some()
    {
        log_msgs.push(A2lError::ContentError {
            item_name: compu_method.name.to_string(),
            blockname: "COMPU_METHOD".to_string(),
            line,
            description: format!(
                "COMPU_METHOD with conversion type {} must not have FORMULA.",
                compu_method.conversion_type
            ),
        });
    }

    if let Some(compu_tab_ref) = &compu_method.compu_tab_ref
        && !compu_tabs.contains_key(&compu_tab_ref.conversion_table)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "COMPU_METHOD".to_string(),
            source_name: compu_method.name.to_string(),
            source_line: line,
            target_type: "COMPU_TAB".to_string(),
            target_name: compu_tab_ref.conversion_table.to_string(),
        });
    }

    if let Some(ref_unit) = &compu_method.ref_unit
        && !module.unit.contains_key(&ref_unit.unit)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "COMPU_METHOD".to_string(),
            source_name: compu_method.name.to_string(),
            source_line: line,
            target_type: "UNIT".to_string(),
            target_name: ref_unit.unit.to_string(),
        });
    }

    if let Some(status_string_ref) = &compu_method.status_string_ref
        && !compu_tabs.contains_key(&status_string_ref.conversion_table)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "COMPU_METHOD".to_string(),
            source_name: compu_method.name.to_string(),
            source_line: line,
            target_type: "COMPU_VTAB".to_string(),
            target_name: status_string_ref.conversion_table.to_string(),
        });
    }
}

fn check_function(
    function: &Function,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    if let Some(in_measurement) = &function.in_measurement {
        let line = in_measurement.get_line();
        check_reference_list(
            "IN_MEASUREMENT",
            "MEASUREMENT",
            line,
            &in_measurement.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(loc_measurement) = &function.loc_measurement {
        let line = loc_measurement.get_line();
        check_reference_list(
            "LOC_MEASUREMENT",
            "MEASUREMENT",
            line,
            &loc_measurement.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(out_measurement) = &function.out_measurement {
        let line = out_measurement.get_line();
        check_reference_list(
            "OUT_MEASUREMENT",
            "MEASUREMENT",
            line,
            &out_measurement.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(def_characteristic) = &function.def_characteristic {
        let line = def_characteristic.get_line();
        check_reference_list(
            "DEF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &def_characteristic.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(ref_characteristic) = &function.ref_characteristic {
        let line = ref_characteristic.get_line();
        check_reference_list(
            "REF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &ref_characteristic.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(sub_function) = &function.sub_function {
        let line = sub_function.get_line();
        check_reference_list(
            "SUB_FUNCTION",
            "FUNCTION",
            line,
            &sub_function.identifier_list,
            None, // functions can't be instance components, so no need to pass module here
            &module.function,
            log_msgs,
        );
    }
}

fn check_function_list(
    opt_function_list: &Option<FunctionList>,
    module: &Module,
    log_msgs: &mut Vec<A2lError>,
) {
    if let Some(function_list) = opt_function_list {
        let line = function_list.get_line();
        check_reference_list(
            "FUNCTION_LIST",
            "FUNCTION",
            line,
            &function_list.name_list,
            None, // functions can't be instance components, so no need to pass module here
            &module.function,
            log_msgs,
        );
    }
}

fn check_group(
    group: &Group,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    if let Some(ref_characteristic) = &group.ref_characteristic {
        let line = ref_characteristic.get_line();
        check_reference_list(
            "REF_CHARACTERISTIC",
            "CHARACTERISTIC",
            line,
            &ref_characteristic.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(ref_measurement) = &group.ref_measurement {
        let line = ref_measurement.get_line();
        check_reference_list(
            "REF_MEASUREMENT",
            "MEASUREMENT",
            line,
            &ref_measurement.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(function_list) = &group.function_list {
        let line = function_list.get_line();
        check_reference_list(
            "FUNCTION_LIST",
            "FUNCTION",
            line,
            &function_list.name_list,
            None, // functions can't be instance components, so no need to pass module here
            &module.function,
            log_msgs,
        );
    }

    if let Some(sub_group) = &group.sub_group {
        let line = sub_group.get_line();
        check_reference_list(
            "SUB_GROUP",
            "GROUP",
            line,
            &sub_group.identifier_list,
            None, // groups can't be instance components, so no need to pass module here
            &module.group,
            log_msgs,
        );
    }
}

fn check_measurement(measurement: &Measurement, module: &Module, log_msgs: &mut Vec<A2lError>) {
    let name = &measurement.name;
    let line = measurement.get_line();

    if measurement.conversion != "NO_COMPU_METHOD"
        && !module.compu_method.contains_key(&measurement.conversion)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "MEASUREMENT".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: measurement.conversion.to_string(),
        });
    }

    let opt_compu_method = module.compu_method.get(&measurement.conversion);
    let calculated_limits = calc_compu_method_limits(opt_compu_method, measurement.datatype);
    let existing_limits = (measurement.lower_limit, measurement.upper_limit);
    if !check_limits_valid(existing_limits, calculated_limits) {
        log_msgs.push(A2lError::LimitCheckError {
            item_name: name.to_string(),
            blockname: "MEASUREMENT".to_string(),
            line,
            lower_limit: measurement.lower_limit,
            upper_limit: measurement.upper_limit,
            calculated_lower_limit: calculated_limits.0,
            calculated_upper_limit: calculated_limits.1,
        });
    }

    check_ref_memory_segment(&measurement.ref_memory_segment, module, log_msgs);
    check_function_list(&measurement.function_list, module, log_msgs);
}

fn check_typedef_measurement(
    t_measurement: &TypedefMeasurement,
    module: &Module,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = &t_measurement.name;
    let line = t_measurement.get_line();

    if t_measurement.conversion != "NO_COMPU_METHOD"
        && !module.compu_method.contains_key(&t_measurement.conversion)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "TYPEDEF_MEASUREMENT".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "COMPU_METHOD".to_string(),
            target_name: t_measurement.conversion.to_string(),
        });
    }

    let opt_compu_method = module.compu_method.get(&t_measurement.conversion);
    let (lower_limit, upper_limit) =
        calc_compu_method_limits(opt_compu_method, t_measurement.datatype);
    if lower_limit > t_measurement.lower_limit || upper_limit < t_measurement.upper_limit {
        log_msgs.push(A2lError::LimitCheckError {
            item_name: name.to_string(),
            blockname: "TYPEDEF_MEASUREMENT".to_string(),
            line,
            lower_limit: t_measurement.lower_limit,
            upper_limit: t_measurement.upper_limit,
            calculated_lower_limit: lower_limit,
            calculated_upper_limit: upper_limit,
        });
    }
}

fn check_transformer(
    transformer: &Transformer,
    module: &Module,
    objects: &ItemList<AnyObject>,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = &transformer.name;
    let line = transformer.get_line();

    if transformer.inverse_transformer != "NO_INVERSE_TRANSFORMER"
        && !module
            .transformer
            .contains_key(&transformer.inverse_transformer)
    {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "TRANSFORMER".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "inverse TRANSFORMER".to_string(),
            target_name: transformer.inverse_transformer.to_string(),
        });
    }

    if let Some(transformer_in_objects) = &transformer.transformer_in_objects {
        let line = transformer_in_objects.get_line();
        check_reference_list(
            "TRANSFORMER_IN_OBJECTS",
            "CHARACTERISTIC / BLOB / INSTANCE",
            line,
            &transformer_in_objects.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }

    if let Some(transformer_out_objects) = &transformer.transformer_out_objects {
        let line = transformer_out_objects.get_line();
        check_reference_list(
            "TRANSFORMER_OUT_OBJECTS",
            "CHARACTERISTIC / BLOB / INSTANCE",
            line,
            &transformer_out_objects.identifier_list,
            Some(module),
            objects,
            log_msgs,
        );
    }
}

fn check_ref_memory_segment(
    opt_ref_memory_segment: &Option<RefMemorySegment>,
    module: &Module,
    log_msgs: &mut Vec<A2lError>,
) {
    if let Some(ref_memory_segment) = opt_ref_memory_segment {
        let line = ref_memory_segment.get_line();

        let valid_ref = module
            .mod_par
            .as_ref()
            .map(|mod_par| {
                mod_par
                    .memory_segment
                    .contains_key(&ref_memory_segment.name)
            })
            .unwrap_or(false);

        if !valid_ref {
            log_msgs.push(A2lError::CrossReferenceError {
                source_type: "REF_MEMORY_SEGMENT".to_string(),
                source_name: ref_memory_segment.name.to_string(),
                source_line: line,
                target_type: "MEMORY_SEGMENT".to_string(),
                target_name: ref_memory_segment.name.to_string(),
            });
        }
    }
}

fn check_reference_list<T: A2lObjectName>(
    container_type: &str,
    ref_type: &str,
    line: u32,
    identifier_list: &[String],
    opt_module: Option<&Module>,
    map: &ItemList<T>,
    log_msgs: &mut Vec<A2lError>,
) {
    for ident in identifier_list {
        if map.get(ident).is_none()
            && opt_module
                .and_then(|m| m.find_instance_component(ident))
                .is_none()
        {
            log_msgs.push(A2lError::CrossReferenceError {
                source_type: container_type.to_string(),
                source_name: ident.to_string(),
                source_line: line,
                target_type: ref_type.to_string(),
                target_name: ident.to_string(),
            });
        }
    }
}

struct GroupInfo<'a> {
    is_root: bool,
    parents: Vec<&'a str>,
}

fn check_group_structure(grouplist: &ItemList<Group>, log_msgs: &mut Vec<A2lError>) {
    let mut groupinfo: HashMap<String, GroupInfo> = HashMap::new();

    for group in grouplist {
        groupinfo.insert(
            group.name.clone(),
            GroupInfo {
                is_root: group.root.is_some(),
                parents: Vec::new(),
            },
        );
    }

    for group in grouplist {
        if let Some(sub_group) = &group.sub_group {
            for sg in &sub_group.identifier_list {
                if let Some(gi) = groupinfo.get_mut(sg) {
                    gi.parents.push(&group.name);
                } else {
                    // nonexistent sub-group
                    log_msgs.push(A2lError::CrossReferenceError {
                        source_type: "GROUP".to_string(),
                        source_name: group.name.clone(),
                        source_line: group.get_line(),
                        target_type: "GROUP".to_string(),
                        target_name: sg.clone(),
                    });
                }
            }
        }
    }

    for group in grouplist {
        let gi = groupinfo
            .get(&group.name)
            .expect("all groups should be in the groupinfo map");
        if gi.is_root && gi.parents.len() > 1 {
            log_msgs.push(A2lError::GroupStructureError {
                group_name: group.name.clone(),
                line: group.get_line(),
                description: format!(
                    "has the ROOT attribute, but is also referenced as a sub-group by GROUPs {}",
                    gi.parents.join(", ")
                ),
            });
        } else if gi.is_root && gi.parents.len() == 1 {
            log_msgs.push(A2lError::GroupStructureError {
                group_name: group.name.clone(),
                line: group.get_line(),
                description: format!(
                    "has the ROOT attribute, but is also referenced as a sub-group by GROUP {}",
                    gi.parents[0]
                ),
            });
        } else if !gi.is_root && gi.parents.len() > 1 {
            log_msgs.push(A2lError::GroupStructureError {
                group_name: group.name.clone(),
                line: group.get_line(),
                description: format!(
                    "is referenced as a sub-group by multiple groups: {}",
                    gi.parents.join(", ")
                ),
            });
        } else if !gi.is_root && gi.parents.is_empty() {
            log_msgs.push(A2lError::GroupStructureError {
                group_name: group.name.clone(),
                line: group.get_line(),
                description: "does not have the ROOT attribute, and is not referenced as a sub-group by any other group".to_string(),
            });
        }
    }
}

fn check_instance(
    instance: &Instance,
    typedefs: &ItemList<AnyTypedef>,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = &instance.name;
    let line = instance.get_line();

    if !typedefs.contains_key(&instance.type_ref) {
        log_msgs.push(A2lError::CrossReferenceError {
            source_type: "INSTANCE".to_string(),
            source_name: name.to_string(),
            source_line: line,
            target_type: "TYPEDEF_<x>".to_string(),
            target_name: instance.type_ref.to_string(),
        });
    }
}

fn check_typedef_structure(
    typedef_structure: &TypedefStructure,
    typedefs: &ItemList<AnyTypedef>,
    log_msgs: &mut Vec<A2lError>,
) {
    let name = &typedef_structure.name;
    let line = typedef_structure.get_line();

    for sc in &typedef_structure.structure_component {
        if !typedefs.contains_key(&sc.component_type) {
            log_msgs.push(A2lError::CrossReferenceError {
                source_type: format!("STRUCTURE_COMPONENT {} of TYPEDEF_STRUCTURE", sc.name),
                source_name: name.to_string(),
                source_line: line,
                target_type: "TYPEDEF_<x>".to_string(),
                target_name: sc.component_type.clone(),
            });
        }
    }
}

// calculate the limits of a characteristic based on the compu method
fn calc_compu_method_limits(
    opt_compu_method: Option<&CompuMethod>,
    datatype: DataType,
) -> (f64, f64) {
    let (mut lower_limit, mut upper_limit) = get_datatype_limits(datatype);

    if let Some(cm) = opt_compu_method {
        match cm.conversion_type {
            ConversionType::Form => {
                // In order to handle this textual formula, we would need to parse it and evaluate it.
                // For now, we just allow any value.
                lower_limit = f64::MIN;
                upper_limit = f64::MAX;
            }
            ConversionType::Linear => {
                // for a linear compu method, the limits are physical values
                // f(x)=ax + b; PHYS = f(INT)
                if let Some(c) = &cm.coeffs_linear {
                    if c.a >= 0.0 {
                        lower_limit = c.a * lower_limit + c.b;
                        upper_limit = c.a * upper_limit + c.b;
                    } else {
                        // factor a is negative, so the lower and upper limits are swapped
                        upper_limit = c.a * lower_limit + c.b;
                        lower_limit = c.a * upper_limit + c.b;
                    }
                }
            }
            ConversionType::RatFunc => {
                // f(x)=(ax^2 + bx + c)/(dx^2 + ex + f); INT = f(PHYS)
                if let Some(c) = &cm.coeffs {
                    // we're only handling the simple linear case here
                    if c.a == 0.0 && c.d == 0.0 && c.e == 0.0 && c.f != 0.0 {
                        // now the rational function is reduced to
                        //   y = (bx + c) / f
                        // which can be inverted to
                        //   x = (fy - c) / b
                        // this is rewritten to, to fix the edge case where f is f64::MAX, y > 1, but y/b < 1
                        //   x = (f * (y/b)) - c/b
                        let func = |y: f64| c.f * (y / c.b) - (c.c / c.b);
                        lower_limit = func(lower_limit);
                        upper_limit = func(upper_limit);
                        if lower_limit > upper_limit {
                            std::mem::swap(&mut lower_limit, &mut upper_limit);
                        }
                    } else {
                        // complex formula:
                        // deriving the limits from the coefficients is not implemented here, so allow any value
                        lower_limit = f64::MIN;
                        upper_limit = f64::MAX;
                    }
                }
            }
            ConversionType::Identical
            | ConversionType::TabIntp
            | ConversionType::TabNointp
            | ConversionType::TabVerb => {
                // identical and all table-based compu methods have direct int-to-phys mapping:
                // The compu method does not modify the limits given by the datatype.
            }
        }
    }

    (lower_limit, upper_limit)
}

// Compare the existing limits with the calculated limits
// Returns true if the existing limits are inside the calculated limits
//
// This should be simple, but is made more difficult by precision issues ...
fn check_limits_valid(existing: (f64, f64), calculated: (f64, f64)) -> bool {
    // calculated lower limit should be less than or equal to the existing lower limit
    // calculated upper limit should be greater than or equal to the existing upper limit

    // In order to allow for floating point precision issues and imprecise data entry in the A2L file,
    // we allow for a tolerance of 0.0001% of the calculated value.
    let epsilon_lower = (calculated.0 * 1E-6).abs();
    let epsilon_upper = (calculated.1 * 1E-6).abs();
    (calculated.0 - existing.0) <= epsilon_lower && (existing.1 - calculated.1) <= epsilon_upper
}

fn get_datatype_limits(a2l_datatype: DataType) -> (f64, f64) {
    match a2l_datatype {
        DataType::Ubyte => (0.0, 255.0),
        DataType::Sbyte => (-128.0, 127.0),
        DataType::Uword => (0.0, 65535.0),
        DataType::Sword => (-32768.0, 32767.0),
        DataType::Ulong => (0.0, 4294967295.0),
        DataType::Slong => (-2147483648.0, 2147483647.0),
        DataType::AUint64 => (0.0, 18446744073709551615.0),
        DataType::AInt64 => (-9223372036854775808.0, 9223372036854775807.0),
        DataType::Float16Ieee => (-6.5504e+4_f64, 6.5504e+4_f64),
        DataType::Float32Ieee => (f32::MIN as f64, f32::MAX as f64),
        DataType::Float64Ieee => (f64::MIN, f64::MAX),
    }
}

impl CharacteristicWrapper<'_> {
    fn name(&self) -> &str {
        match self {
            CharacteristicWrapper::Characteristic(c) => &c.name,
            CharacteristicWrapper::TypedefCharacteristic(tc) => &tc.name,
        }
    }

    fn line(&self) -> u32 {
        match self {
            CharacteristicWrapper::Characteristic(c) => c.get_line(),
            CharacteristicWrapper::TypedefCharacteristic(tc) => tc.get_line(),
        }
    }

    fn axis_descr(&self) -> &[AxisDescr] {
        match self {
            CharacteristicWrapper::Characteristic(c) => &c.axis_descr,
            CharacteristicWrapper::TypedefCharacteristic(tc) => &tc.axis_descr,
        }
    }

    fn map_list(&self) -> Option<&MapList> {
        match self {
            CharacteristicWrapper::Characteristic(c) => c.map_list.as_ref(),
            CharacteristicWrapper::TypedefCharacteristic(_) => None,
        }
    }

    fn conversion(&self) -> &str {
        match self {
            CharacteristicWrapper::Characteristic(c) => &c.conversion,
            CharacteristicWrapper::TypedefCharacteristic(tc) => &tc.conversion,
        }
    }

    fn record_layout(&self) -> &str {
        match self {
            CharacteristicWrapper::Characteristic(c) => &c.deposit,
            CharacteristicWrapper::TypedefCharacteristic(tc) => &tc.record_layout,
        }
    }

    fn characteristic_type(&self) -> CharacteristicType {
        match self {
            CharacteristicWrapper::Characteristic(c) => c.characteristic_type,
            CharacteristicWrapper::TypedefCharacteristic(tc) => tc.characteristic_type,
        }
    }

    fn lower_limit(&self) -> f64 {
        match self {
            CharacteristicWrapper::Characteristic(c) => c.lower_limit,
            CharacteristicWrapper::TypedefCharacteristic(tc) => tc.lower_limit,
        }
    }

    fn upper_limit(&self) -> f64 {
        match self {
            CharacteristicWrapper::Characteristic(c) => c.upper_limit,
            CharacteristicWrapper::TypedefCharacteristic(tc) => tc.upper_limit,
        }
    }

    fn kind(&self) -> &str {
        match self {
            CharacteristicWrapper::Characteristic(_) => "CHARACTERISTIC",
            CharacteristicWrapper::TypedefCharacteristic(_) => "TYPEDEF_CHARACTERISTIC",
        }
    }
}

#[cfg(test)]
mod test {
    use crate::*;

    #[test]
    fn check_axis_descr() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" CURVE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR COM_AXIS input_quantity conversion 1 0 100
                    AXIS_PTS_REF axis_points
                    CURVE_AXIS_REF curve_axis
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid input quantity, conversion, axis points ref, and curve axis ref. Additionally, CURVE_AXIS_REF is not allowed for COM_AXIS
        assert_eq!(log_msgs.len(), 5);

        // error: COM_AXIS should have an AXIS_PTS_REF
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" CURVE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR COM_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 1 0 100
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);

        // error: STD_AXIS should not have an AXIS_PTS_REF
        static A2L_TEXT3: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" CURVE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR STD_AXIS meas cm 1 0 100
                    AXIS_PTS_REF axis_points
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin AXIS_PTS axis_points "" 0x1234 NO_INPUT_QUANTITY rl 0 NO_COMPU_METHOD 3 0.0 10.0
            /end AXIS_PTS
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
                AXIS_PTS_X 0 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
            /begin MEASUREMENT meas "" FLOAT32_IEEE cm  1 1.0 0 100
            /end MEASUREMENT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT3, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        // for msg in &log_msgs {
        //     println!("{msg}");
        // }
        assert_eq!(log_msgs.len(), 1);

        // valid input, no errors to report
        static A2L_TEXT4: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" CURVE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR STD_AXIS meas cm 1 0 100
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
                AXIS_PTS_X 1 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
            /begin MEASUREMENT meas "" FLOAT32_IEEE cm  1 1.0 0 100
            /end MEASUREMENT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT4, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid input quantity
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_typedef_axis() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_AXIS typedef_axis_name "" input_quantity record_layout 0 conversion 1 0 100
            /end TYPEDEF_AXIS
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid input quantity, record layout and conversion
        assert_eq!(log_msgs.len(), 3);

        // valid input, no errors to report
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_AXIS typedef_axis_name "" meas rl 0 cm 1 0 100
            /end TYPEDEF_AXIS
            /begin MEASUREMENT meas "" FLOAT32_IEEE cm 1 1.0 0 100
            /end MEASUREMENT
            /begin RECORD_LAYOUT rl /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_axis_pts() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin AXIS_PTS axis_pts_name "" 0x1234 input_qty record_layout 0 conversion 3 0.0 10.0
            /end AXIS_PTS
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid input quantity, record layout (aka deposit_record), and conversion
        assert_eq!(log_msgs.len(), 3);

        // AXIS_PTS_X is missing from the record layout
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin AXIS_PTS axis_pts_name "" 0x1234 NO_INPUT_QUANTITY rl 0 NO_COMPU_METHOD 3 0.0 10.0
            /end AXIS_PTS
            /begin RECORD_LAYOUT rl
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);

        // valid input, no errors to report
        static A2L_TEXT3: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin AXIS_PTS axis_pts_name "" 0x1234 meas rl 0 cm 3 0.0 10.0
            /end AXIS_PTS
            /begin MEASUREMENT meas "" FLOAT32_IEEE cm 1 1.0 0 100
            /end MEASUREMENT
            /begin RECORD_LAYOUT rl
                AXIS_PTS_X 0 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT3, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_characteristic() {
        // invalid conversion, record layout, comparison quantity, memory segment,
        // dependent characteristic, function, map list, and virtual characteristic
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" VALUE 0x1234 rl 0 conversion 0.0 1.0
                COMPARISON_QUANTITY comp_qty
                REF_MEMORY_SEGMENT mem_seg
                /begin DEPENDENT_CHARACTERISTIC "formula"
                    name
                /end DEPENDENT_CHARACTERISTIC
                /begin FUNCTION_LIST
                    name
                /end FUNCTION_LIST
                /begin MAP_LIST
                    name
                /end MAP_LIST
                /begin VIRTUAL_CHARACTERISTIC "virt_char"
                    name
                /end VIRTUAL_CHARACTERISTIC
            /end CHARACTERISTIC
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 8);

        // record layout exists, but lacks FNC_VALUES
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" VALUE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);

        // invalid axis descr
        static A2L_TEXT3: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" VALUE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR STD_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 1 0 100
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin CHARACTERISTIC c2 "" MAP 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
                AXIS_PTS_X 1 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT3, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        // invalid axis descr for c, no axis descr for c2
        assert_eq!(log_msgs.len(), 2);

        // valid input, no errors to report
        static A2L_TEXT4: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" VALUE 0x1234 rl 0 cm 0.0 1.0
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT4, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_typedef_characteristic() {
        // invalid record layout and conversion, additionally the axis descr check reports invalid input quantity and conversion
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_CHARACTERISTIC name "" CURVE record_layout 0 conversion 0 100
                /begin AXIS_DESCR STD_AXIS input_quantity conversion 1 0 100
                /end AXIS_DESCR
            /end TYPEDEF_CHARACTERISTIC
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 4);

        // missing axis descr
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_CHARACTERISTIC name "" CUBOID rl 0 NO_COMPU_METHOD 0 100
            /end TYPEDEF_CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        // invalid axis descr
        assert_eq!(log_msgs.len(), 1);

        // valid input, no errors to report
        static A2L_TEXT3: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_CHARACTERISTIC name "" VALUE rl 0 cm 0 100
            /end TYPEDEF_CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit" /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT3, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_compu_method() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin COMPU_METHOD cm "" TAB_VERB "%4.2" "unit"
                COMPU_TAB_REF compu_tab_ref
                REF_UNIT ref_unit
                STATUS_STRING_REF status_string_ref
            /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid compu tab ref, ref unit, and status string ref
        assert_eq!(log_msgs.len(), 3);

        // valid input, no errors to report
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin COMPU_METHOD cm "" IDENTICAL "%4.2" "unit"
            /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_function() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin FUNCTION f ""
                /begin IN_MEASUREMENT
                    name
                /end IN_MEASUREMENT
                /begin LOC_MEASUREMENT
                    name
                /end LOC_MEASUREMENT
                /begin OUT_MEASUREMENT
                    name
                /end OUT_MEASUREMENT
                /begin DEF_CHARACTERISTIC
                    name
                /end DEF_CHARACTERISTIC
                /begin REF_CHARACTERISTIC
                    name
                /end REF_CHARACTERISTIC
                /begin SUB_FUNCTION
                    name
                /end SUB_FUNCTION
            /end FUNCTION
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid in measurement, loc measurement, out measurement, def characteristic,
        // ref characteristic, and sub function
        assert_eq!(log_msgs.len(), 6);

        // valid input, no errors to report
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin FUNCTION f ""
            /end FUNCTION
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_group() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin GROUP g ""
                /begin REF_CHARACTERISTIC
                    name
                /end REF_CHARACTERISTIC
                /begin REF_MEASUREMENT
                    name
                /end REF_MEASUREMENT
                /begin FUNCTION_LIST
                    name
                /end FUNCTION_LIST
                /begin SUB_GROUP
                    name
                /end SUB_GROUP
            /end GROUP
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid ref characteristic, ref measurement, function list, and sub group
        // additionally the group consistency check reports that the group is not a root group, and refereces a non-existent sub-group
        assert_eq!(log_msgs.len(), 6);
    }

    #[test]
    fn check_group_structure() {
        // grp1 is a root group, but is also referenced as a sub-group by grp2
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin GROUP grp1 "" ROOT
            /end GROUP
            /begin GROUP grp2 "" ROOT
                /begin SUB_GROUP
                    grp1
                /end SUB_GROUP
            /end GROUP
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);

        // grp1 is a root group, but is also referenced as a sub-group by grp2 and grp3
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin GROUP grp1 "" ROOT
            /end GROUP
            /begin GROUP grp2 "" ROOT
                /begin SUB_GROUP
                    grp1
                /end SUB_GROUP
            /end GROUP
            /begin GROUP grp3 "" ROOT
                /begin SUB_GROUP
                    grp1
                /end SUB_GROUP
            /end GROUP
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);

        // grp1 (not a root group) is referenced as a sub-group by grp2 and grp3
        static A2L_TEXT3: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin GROUP grp1 ""
            /end GROUP
            /begin GROUP grp2 "" ROOT
                /begin SUB_GROUP
                    grp1
                /end SUB_GROUP
            /end GROUP
            /begin GROUP grp3 "" ROOT
                /begin SUB_GROUP
                    grp1
                /end SUB_GROUP
            /end GROUP
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT3, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 1);
    }

    #[test]
    fn check_measurement() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin MEASUREMENT m "" FLOAT32_IEEE conversion 1 1.0 0 100
                REF_MEMORY_SEGMENT mem_seg
                /begin FUNCTION_LIST
                    name
                /end FUNCTION_LIST
            /end MEASUREMENT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid conversion, memory segment, and function list
        assert_eq!(log_msgs.len(), 3);
    }

    #[test]
    fn check_typedef_measurement() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_MEASUREMENT tm "" UBYTE conversion 1 1 0 100
            /end TYPEDEF_MEASUREMENT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid conversion
        assert_eq!(log_msgs.len(), 1);
    }

    #[test]
    fn check_transformer() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TRANSFORMER transformer_name "version string" "dll32" "dll64" 1 ON_CHANGE inverse_transformer
                /begin TRANSFORMER_IN_OBJECTS
                    name
                /end TRANSFORMER_IN_OBJECTS
                /begin TRANSFORMER_OUT_OBJECTS
                    name
                /end TRANSFORMER_OUT_OBJECTS
            /end TRANSFORMER
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid inverse transformer, transformer in objects, and transformer out objects
        assert_eq!(log_msgs.len(), 3);

        // valid input, no errors to report
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TRANSFORMER transformer_name "version string" "dll32" "dll64" 1 ON_CHANGE NO_INVERSE_TRANSFORMER
            /end TRANSFORMER
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn check_instance() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin INSTANCE i "" type_ref 0x1234
            /end INSTANCE
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid type ref
        assert_eq!(log_msgs.len(), 1);
    }

    #[test]
    fn check_typedef_structure() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_STRUCTURE ts "" 1
                /begin STRUCTURE_COMPONENT component_name component_type 1
                /end STRUCTURE_COMPONENT
            /end TYPEDEF_STRUCTURE
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);

        // invalid component type
        assert_eq!(log_msgs.len(), 1);
    }

    #[test]
    fn check_calc_limits() {
        // Compu method type FORM: limits are set to f64::MIN and f64::MAX
        let cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::Form,
            "format".to_string(),
            "unit".to_string(),
        );
        let datatype = DataType::Ubyte;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        // FORM conversion type: any value is allowed, because the formula is not evaluated
        assert_eq!(lower_limit, f64::MIN);
        assert_eq!(upper_limit, f64::MAX);

        // Compu method type LINEAR: limits are calculated
        let mut cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::Linear,
            "format".to_string(),
            "unit".to_string(),
        );
        cm.coeffs_linear = Some(CoeffsLinear::new(33.0, 2.0));
        let datatype = DataType::Sbyte;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        // linear conversion type: lower and upper limits are physical values
        assert_eq!(lower_limit, 33.0 * -128.0 + 2.0);
        assert_eq!(upper_limit, 33.0 * 127.0 + 2.0);

        // Compu method type RAT_FUNC, with a restricted formula: limits are calculated
        let mut cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::RatFunc,
            "format".to_string(),
            "unit".to_string(),
        );
        cm.coeffs = Some(Coeffs::new(0.0, 3.5, 4.44, 0.0, 0.0, 2.0));
        let datatype = DataType::Slong;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        // rational function conversion type: lower and upper limits are physical values
        // y = (3.5x + 4.44) / 2 -> x = (2y - 4.44) / 3.5
        assert_eq!(lower_limit, (2.0 * -2147483648.0 - 4.44) / 3.5);
        assert_eq!(upper_limit, (2.0 * 2147483647.0 - 4.44) / 3.5);

        // Compu method type RAT_FUNC, with a restricted formula for f64 values: limits are calculated
        // In this calculation, the value could go to INFINITY if the operations are done in the wrong order
        let mut cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::RatFunc,
            "format".to_string(),
            "unit".to_string(),
        );
        cm.coeffs = Some(Coeffs::new(0.0, -3.5, 4.44, 0.0, 0.0, 2.0));
        let datatype = DataType::Float64Ieee;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        // min and max values are not f64::MIN, f64::MAX, -f64::INFINITY or f64::INFINITY
        assert_ne!(lower_limit, f64::MIN);
        assert_ne!(upper_limit, f64::MAX);
        assert_ne!(lower_limit, -f64::INFINITY);
        assert_ne!(upper_limit, f64::INFINITY);

        // Compu method type RAT_FUNC, with a full formula: limits are set to f64::MIN and f64::MAX
        let mut cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::RatFunc,
            "format".to_string(),
            "unit".to_string(),
        );
        cm.coeffs = Some(Coeffs::new(1.0, 3.5, 4.44, 2.0, 3.0, 4.0));
        let datatype = DataType::Slong;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        assert_eq!(lower_limit, f64::MIN);
        assert_eq!(upper_limit, f64::MAX);

        // Compu method type IDENTICAL: limits are set to the datatype limits
        let cm = CompuMethod::new(
            "".to_string(),
            "".to_string(),
            ConversionType::Identical,
            "format".to_string(),
            "unit".to_string(),
        );
        let datatype = DataType::Float32Ieee;
        let (lower_limit, upper_limit) = super::calc_compu_method_limits(Some(&cm), datatype);
        // identical conversion type: any value is allowed, because the compu method does not modify the limits given by the datatype
        assert_eq!(lower_limit, f32::MIN as f64);
        assert_eq!(upper_limit, f32::MAX as f64);
    }

    #[test]
    fn check_limits() {
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin CHARACTERISTIC c "" CURVE 0x1234 rl 0 cm 0 100000
                /begin AXIS_DESCR STD_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 1 -1E39 1E39
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin AXIS_PTS axis_points "" 0x1234 NO_INPUT_QUANTITY rl2 0 cm2 3 -100000.0 100000.0
            /end AXIS_PTS
            /begin MEASUREMENT m "" ULONG cm3 1 1.0 -1000000 1000000
            /end MEASUREMENT
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 UWORD ROW_DIR DIRECT
                AXIS_PTS_X 0 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
            /begin RECORD_LAYOUT rl2
                AXIS_PTS_X 0 SWORD INDEX_INCR DIRECT
            /end RECORD_LAYOUT
            /begin COMPU_METHOD cm "" LINEAR "%4.2" "unit"
                COEFFS_LINEAR 0.5 0
            /end COMPU_METHOD
            /begin COMPU_METHOD cm2 "" RAT_FUNC "%4.2" "unit"
                COEFFS 0 500 4000 0 0 2.222
            /end COMPU_METHOD
            /begin COMPU_METHOD cm3 "" IDENTICAL "%4.2" "unit"
            /end COMPU_METHOD
        /end MODULE /end PROJECT"#;
        let (mut a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        // invalid limits for each of CHARACTERISTIC, AXIS_DESCR, AXIS_PTS, and MEASUREMENT
        assert_eq!(log_msgs.len(), 4);

        // fix the errors in each item, now no errors should be reported
        a2lfile.project.module[0].characteristic[0].upper_limit = u16::MAX as f64 * 0.5;
        a2lfile.project.module[0].characteristic[0].axis_descr[0].upper_limit = f32::MAX as f64;
        a2lfile.project.module[0].characteristic[0].axis_descr[0].lower_limit = f32::MIN as f64;
        a2lfile.project.module[0].axis_pts[0].upper_limit =
            ((i16::MAX as f64 * 2.222) - 4000.0) / 500.0;
        a2lfile.project.module[0].axis_pts[0].lower_limit =
            ((i16::MIN as f64 * 2.222) - 4000.0) / 500.0;
        a2lfile.project.module[0].measurement[0].lower_limit = u32::MIN as f64;
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }

    #[test]
    fn axis_pts_with_this() {
        // if an AXIS is defined inside a TYPEDEF_STRUCTURE, then the AXIS_PTS_REF may refer to a structure component using THIS.component_name
        // valid THIS reference
        static A2L_TEXT: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_AXIS lookup_axis_type "LookUpTable axis" NO_INPUT_QUANTITY F32 0 NO_COMPU_METHOD 16 -1E32 1E32
            /end TYPEDEF_AXIS
            /begin TYPEDEF_CHARACTERISTIC lookup_values_type "LookUpTable values" MAP F32 0 NO_COMPU_METHOD -1E32 1E32
                /begin AXIS_DESCR COM_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 16 0.0 0.0
                    AXIS_PTS_REF THIS.lookup_axis
                /end AXIS_DESCR
                /begin AXIS_DESCR CURVE_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 16 0.0 0.0
                    CURVE_AXIS_REF THIS.lookup_axis
                /end AXIS_DESCR
            /end TYPEDEF_CHARACTERISTIC
            /begin TYPEDEF_STRUCTURE LookUpTable "" 128 CONSISTENT_EXCHANGE
                /begin STRUCTURE_COMPONENT lookup_axis lookup_axis_type 0 /end STRUCTURE_COMPONENT
                /begin STRUCTURE_COMPONENT lookup_values lookup_values_type 64 /end STRUCTURE_COMPONENT
            /end TYPEDEF_STRUCTURE
            /begin INSTANCE lockup_table "" LookUpTable 0x80010000 /end INSTANCE
            /begin RECORD_LAYOUT F32
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);

        // invalid THIS reference
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_CHARACTERISTIC lookup_values_type "LookUpTable values" CURVE F32 0 NO_COMPU_METHOD -1E32 1E32
                /begin AXIS_DESCR COM_AXIS NO_INPUT_QUANTITY NO_COMPU_METHOD 16 0.0 0.0
                    AXIS_PTS_REF THIS.lookup_axis
                    CURVE_AXIS_REF THIS.lookup_axis
                /end AXIS_DESCR
            /end TYPEDEF_CHARACTERISTIC
            /begin TYPEDEF_STRUCTURE LookUpTable "" 128 CONSISTENT_EXCHANGE
                /begin STRUCTURE_COMPONENT lookup_values lookup_values_type 64 /end STRUCTURE_COMPONENT
            /end TYPEDEF_STRUCTURE
            /begin INSTANCE lockup_table "" LookUpTable 0x80010000 /end INSTANCE
            /begin RECORD_LAYOUT F32
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 3);
    }

    #[test]
    fn check_instance_references() {
        // a CHARACTERISTIC that uses a structure component in an INSTANCE as the input of its AXIS_DESCR
        static A2L_TEXT2: &str = r#"ASAP2_VERSION 1 71 /begin PROJECT p "" /begin MODULE m ""
            /begin TYPEDEF_MEASUREMENT td_measurement "" UBYTE NO_COMPU_METHOD 1 1 0 100
            /end TYPEDEF_MEASUREMENT
            /begin TYPEDEF_STRUCTURE td_struct "" 128 CONSISTENT_EXCHANGE
                /begin STRUCTURE_COMPONENT item td_measurement 1 /end STRUCTURE_COMPONENT
            /end TYPEDEF_STRUCTURE
            /begin INSTANCE inst "" td_struct 0x80010000 /end INSTANCE
            /begin RECORD_LAYOUT F32
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
            /end RECORD_LAYOUT
            /begin CHARACTERISTIC char "" CURVE 0x1234 rl 0 NO_COMPU_METHOD 0.0 1.0
                /begin AXIS_DESCR STD_AXIS inst.item NO_COMPU_METHOD 1 0 100
                /end AXIS_DESCR
            /end CHARACTERISTIC
            /begin RECORD_LAYOUT rl
                FNC_VALUES 0 FLOAT32_IEEE ROW_DIR DIRECT
                AXIS_PTS_X 1 FLOAT32_IEEE INDEX_INCR DIRECT
            /end RECORD_LAYOUT
        /end MODULE /end PROJECT"#;
        let (a2lfile, _) = load_from_string(A2L_TEXT2, None, true).unwrap();
        let log_msgs = super::check(&a2lfile);
        assert_eq!(log_msgs.len(), 0);
    }
}