-- This code tests every big Endian conversion of sixteen bit words to octet arrays, 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 Hextet_And_Octet_Test is type Octet_Array is array (1 .. 2) of Interfaces.Unsigned_8; pragma Pack(Octet_Array); for Octet_Array'Size use Interfaces.Unsigned_16'Size; for Octet_Array'Alignment use Interfaces.Unsigned_16'Alignment; function To_Octets (From : in Interfaces.Unsigned_16) return Octet_Array is function Hextet_To_Octets is new Unchecked_Conversion(Source => Interfaces.Unsigned_16, Target => Octet_Array); O : Octet_Array; begin O := Hextet_To_Octets(From); case System.Default_Bit_Order is when System.High_Order_First => null; when System.Low_Order_First => O := (O(2), O(1)); end case; return O; end To_Octets; function To_Hextet (From : in Octet_Array) return Interfaces.Unsigned_16 is function Octets_To_Hextet is new Unchecked_Conversion(Source => Octet_Array, Target => Interfaces.Unsigned_16); A : Octet_Array := From; begin case System.Default_Bit_Order is when System.High_Order_First => null; when System.Low_Order_First => A := (A(2), A(1)); end case; return Octets_To_Hextet(A); end To_Hextet; use type Interfaces.Unsigned_16; H : Interfaces.Unsigned_16 := 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 if To_Hextet((A, B)) /= H or To_Octets(H) /= (A, B) then Ada.Text_IO.Put_Line("Failure:" & Interfaces.Unsigned_16'Image(H) & ", (" & Interfaces.Unsigned_8'Image(A) & "," & Interfaces.Unsigned_8'Image(B) & ")"); return; end if; H := H + 1; end loop; end loop; Ada.Text_IO.Put_Line("Success"); Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Success); end Hextet_And_Octet_Test; .