Skip to content

Commit 970dc56

Browse files
authored
Merge pull request #4 from damaki/topic/add-reflection-tests
Add tests for reflected & normal tables produce equivalent CRCs
2 parents b354514 + 8c627f0 commit 970dc56

5 files changed

Lines changed: 183 additions & 10 deletions

File tree

src/libcrc-generic_nbit_crcs-bitwise.adb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ is
100100
Reason =>
101101
"Statement has effect for different generic instantiations");
102102

103-
if Reflect_Output then
104-
if not Reflected_Polynomial then
103+
if Reflect_Output /= Reflected_Polynomial then
105104

106-
pragma Warnings (GNATprove, On, "statement has no effect");
105+
pragma Warnings (GNATprove, On, "statement has no effect");
107106

108-
CRC := Bit_Reverse_CRC (CRC);
109-
end if;
107+
CRC := Bit_Reverse_CRC (CRC);
110108
end if;
111109

112110
return CRC xor Final_XOR;

src/libcrc-generic_nbit_crcs-table_based.adb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ package body LibCRC.Generic_Nbit_CRCs.Table_Based with SPARK_Mode is
9797
Reason =>
9898
"Statement has effect for different generic instantiations");
9999

100-
if Reflect_Output then
101-
if not CRC_Table_Reflected then
100+
if Reflect_Output /= CRC_Table_Reflected then
102101

103-
pragma Warnings (GNATprove, On, "statement has no effect");
102+
pragma Warnings (GNATprove, On, "statement has no effect");
104103

105-
CRC := Bit_Reverse_CRC (CRC);
106-
end if;
104+
CRC := Bit_Reverse_CRC (CRC);
107105
end if;
108106

109107
return CRC xor Final_XOR;

tests/unit_tests/src/crc_table_test_suites.adb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ with LibCRC.Predefined_Constant_Tables;
1515
with LibCRC.Predefined_Elaborated_Tables;
1616

1717
with Generic_CRC_Table_Tests;
18+
with Reflection_Tests;
1819

1920
package body CRC_Table_Test_Suites
2021
is
@@ -393,6 +394,8 @@ is
393394
Table_64_Poly_259C84CBA6426349_Tests.Add_To_Suite (S.all);
394395
Table_64_Poly_AD93D23594C93659_Tests.Add_To_Suite (S.all);
395396

397+
Reflection_Tests.Add_To_Suite (S.all);
398+
396399
return S;
397400
end Suite;
398401

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
--
2+
-- Copyright 2025 (C) Daniel King
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0
5+
--
6+
with Interfaces; use Interfaces;
7+
with AUnit.Assertions; use AUnit.Assertions;
8+
9+
with LibCRC;
10+
with LibCRC.CRC_32bit;
11+
with LibCRC.Generic_Nbit_CRCs.Table_Based;
12+
with LibCRC.Generic_Nbit_CRCs.Bitwise;
13+
14+
package body Reflection_Tests is
15+
16+
-- Use the CRC-32 polynomial for testing
17+
Test_Polynomial : constant Unsigned_32 := 16#04C11DB7#;
18+
19+
-- Other parameters for testing
20+
Test_Seed : constant Unsigned_32 := 16#12345678#;
21+
Test_Final_XOR : constant Unsigned_32 := 16#87654321#;
22+
23+
Test_Table : aliased constant LibCRC.CRC_32bit.CRC_Table_Type :=
24+
LibCRC.CRC_32bit.Generate_Table (Test_Polynomial);
25+
26+
Test_Table_Reflected :
27+
aliased constant LibCRC.CRC_32bit.CRC_Table_Type :=
28+
LibCRC.CRC_32bit.Generate_Table_Reflected
29+
(LibCRC.CRC_32bit.Bit_Reverse_CRC (Test_Polynomial));
30+
31+
---------------------------
32+
-- Test_Table_Reflection --
33+
---------------------------
34+
35+
-- Test that equivalent CRCs are output for two table-based CRCs that
36+
-- are identical, except one uses a reflected table and the other a
37+
-- normal (non-reflected) table.
38+
--
39+
-- The test is performed for all possible combinations of Reflect_Input
40+
-- and Reflect_Output.
41+
42+
procedure Test_Table_Reflection (T : in out Test_Fixture) is
43+
Test_Data : constant LibCRC.Byte_Array := (1, 2, 3, 4, 5, 255, 254, 253);
44+
45+
begin
46+
for Reflect_Input in Boolean'Range loop
47+
for Reflect_Output in Boolean'Range loop
48+
declare
49+
package Test_CRC is new LibCRC.CRC_32bit.Table_Based
50+
(Seed => Test_Seed,
51+
Final_XOR => Test_Final_XOR,
52+
Reflect_Input => Reflect_Input,
53+
Reflect_Output => Reflect_Output,
54+
CRC_Table_Reflected => False,
55+
CRC_Table => Test_Table'Access);
56+
57+
package Test_CRC_Reflected is new LibCRC.CRC_32bit.Table_Based
58+
(Seed => Test_Seed,
59+
Final_XOR => Test_Final_XOR,
60+
Reflect_Input => Reflect_Input,
61+
Reflect_Output => Reflect_Output,
62+
CRC_Table_Reflected => True,
63+
CRC_Table => Test_Table_Reflected'Access);
64+
65+
CRC_1 : Unsigned_32;
66+
CRC_2 : Unsigned_32;
67+
begin
68+
69+
CRC_1 := Test_CRC.Calculate (Test_Data);
70+
CRC_2 := Test_CRC_Reflected.Calculate (Test_Data);
71+
72+
Assert (CRC_1 = CRC_2,
73+
"CRC mismatch for Reflect_Input = "
74+
& Reflect_Input'Image & ", Reflect_Output = "
75+
& Reflect_Output'Image & ":"
76+
& CRC_1'Image & " !="
77+
& CRC_2'Image);
78+
end;
79+
end loop;
80+
end loop;
81+
end Test_Table_Reflection;
82+
83+
-----------------------------
84+
-- Test_Bitwise_Reflection --
85+
-----------------------------
86+
87+
-- Test that equivalent CRCs are output for two bitwise CRCs that
88+
-- are identical, except one uses a reflected polynomial and the other a
89+
-- normal (non-reflected) polynomial.
90+
--
91+
-- The test is performed for all possible combinations of Reflect_Input
92+
-- and Reflect_Output.
93+
94+
procedure Test_Bitwise_Reflection (T : in out Test_Fixture) is
95+
Test_Data : constant LibCRC.Byte_Array := (1, 2, 3, 4, 5, 255, 254, 253);
96+
97+
begin
98+
for Reflect_Input in Boolean'Range loop
99+
for Reflect_Output in Boolean'Range loop
100+
declare
101+
package Test_CRC is new LibCRC.CRC_32bit.Bitwise
102+
(Polynomial => Test_Polynomial,
103+
Reflected_Polynomial => False,
104+
Seed => Test_Seed,
105+
Final_XOR => Test_Final_XOR,
106+
Reflect_Input => Reflect_Input,
107+
Reflect_Output => Reflect_Output);
108+
109+
package Test_CRC_Reflected is new LibCRC.CRC_32bit.Bitwise
110+
(Polynomial => LibCRC.CRC_32bit.Bit_Reverse_CRC
111+
(Test_Polynomial),
112+
Reflected_Polynomial => True,
113+
Seed => Test_Seed,
114+
Final_XOR => Test_Final_XOR,
115+
Reflect_Input => Reflect_Input,
116+
Reflect_Output => Reflect_Output);
117+
118+
CRC_1 : Unsigned_32;
119+
CRC_2 : Unsigned_32;
120+
begin
121+
122+
CRC_1 := Test_CRC.Calculate (Test_Data);
123+
CRC_2 := Test_CRC_Reflected.Calculate (Test_Data);
124+
125+
Assert (CRC_1 = CRC_2,
126+
"CRC mismatch for Reflect_Input = "
127+
& Reflect_Input'Image & ", Reflect_Output = "
128+
& Reflect_Output'Image & ":"
129+
& CRC_1'Image & " !="
130+
& CRC_2'Image);
131+
end;
132+
end loop;
133+
end loop;
134+
end Test_Bitwise_Reflection;
135+
136+
------------------
137+
-- Add_To_Suite --
138+
------------------
139+
140+
procedure Add_To_Suite (S : in out Test_Suite'Class) is
141+
begin
142+
S.Add_Test (Caller.Create ("Test table reflection equivalence",
143+
Test_Table_Reflection'Access));
144+
S.Add_Test (Caller.Create ("Test bitwise reflection equivalence",
145+
Test_Bitwise_Reflection'Access));
146+
end Add_To_Suite;
147+
148+
end Reflection_Tests;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2025 (C) Daniel King
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0
5+
--
6+
with AUnit.Test_Fixtures;
7+
with AUnit.Test_Suites; use AUnit.Test_Suites;
8+
with AUnit.Test_Caller;
9+
10+
-- These tests verify that the CRC computation produces the same output
11+
-- regardless of whether the reflected table or polynomial is used.
12+
13+
package Reflection_Tests is
14+
15+
type Test_Fixture is new AUnit.Test_Fixtures.Test_Fixture with null record;
16+
17+
procedure Test_Table_Reflection (T : in out Test_Fixture);
18+
procedure Test_Bitwise_Reflection (T : in out Test_Fixture);
19+
20+
procedure Add_To_Suite (S : in out Test_Suite'Class);
21+
22+
private
23+
24+
package Caller is new AUnit.Test_Caller (Test_Fixture);
25+
26+
end Reflection_Tests;

0 commit comments

Comments
 (0)