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 ;
0 commit comments