-- This program tests every big Endian conversion of thirty-two bit words to octets, and vice versa. -- Copyright (C) 2024 Prince Trippy . -- This program is free software: you can redistribute it and/or modify it under the terms of the -- GNU Affero General Public License version 3 as published by the Free Software Foundation. -- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without -- even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -- See the GNU Affero General Public License for more details. -- You should have received a copy of the GNU Affero General Public License along with this program. -- If not, see . -- This library requires Ada 1995 support, mainly due to the Interfaces types used, but also others. with Interfaces, Ada.Text_IO, Ada.Command_Line, System, Unchecked_Conversion; procedure Word_And_Octet_Test is type Octet_Array is array (1 .. 4) of Interfaces.Unsigned_8; pragma Pack(Octet_Array); for Octet_Array'Size use Interfaces.Unsigned_32'Size; for Octet_Array'Alignment use Interfaces.Unsigned_32'Alignment; function To_Octets (From : in Interfaces.Unsigned_32) return Octet_Array is function Word_To_Octets is new Unchecked_Conversion(Source => Interfaces.Unsigned_32, Target => Octet_Array); O : Octet_Array; begin O := Word_To_Octets(From); case System.Default_Bit_Order is when System.High_Order_First => null; when System.Low_Order_First => O := (O(4), O(3), O(2), O(1)); end case; return O; end To_Octets; function To_Word (From : in Octet_Array) return Interfaces.Unsigned_32 is function Octets_To_Word is new Unchecked_Conversion(Source => Octet_Array, Target => Interfaces.Unsigned_32); A : Octet_Array := From; begin case System.Default_Bit_Order is when System.High_Order_First => null; when System.Low_Order_First => A := (A(4), A(3), A(2), A(1)); end case; return Octets_To_Word(A); end To_Word; use type Interfaces.Unsigned_32; W : Interfaces.Unsigned_32 := 0; begin Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure); for A in Interfaces.Unsigned_8'Range loop for B in Interfaces.Unsigned_8'Range loop for C in Interfaces.Unsigned_8'Range loop for D in Interfaces.Unsigned_8'Range loop if To_Word((A, B, C, D)) /= W or To_Octets(W) /= (A, B, C, D) then Ada.Text_IO.Put_Line("Failure:" & Interfaces.Unsigned_32'Image(W) & ", (" & Interfaces.Unsigned_8'Image(A) & "," & Interfaces.Unsigned_8'Image(B) & "," & Interfaces.Unsigned_8'Image(C) & "," & Interfaces.Unsigned_8'Image(D) & ")"); return; end if; W := W + 1; end loop; end loop; end loop; end loop; Ada.Text_IO.Put_Line("Success"); Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Success); end Word_And_Octet_Test; .