-- CXA4039.A -- -- Grant of Unlimited Rights -- -- The Ada Conformity Assessment Authority (ACAA) holds unlimited -- rights in the software and documentation contained herein. Unlimited -- rights are the same as those granted by the U.S. Government for older -- parts of the Ada Conformity Assessment Test Suite, and are defined -- in DFAR 252.227-7013(a)(19). By making this public release, the ACAA -- intends to confer upon all recipients unlimited rights equal to those -- held by the ACAA. These rights include rights to use, duplicate, -- release or disclose the released technical data and computer software -- in whole or in part, in any manner and for any purpose whatsoever, and -- to have or permit others to do so. -- -- DISCLAIMER -- -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A -- PARTICULAR PURPOSE OF SAID MATERIAL. --* -- -- OBJECTIVE: -- Check that Ada.Strings.Unbounded.Slice raises Index_Error if -- High > Length (Source) or Low > Length (Source) + 1. -- -- Check that the bounds of the result of Ada.Strings.Unbounded.Slice are -- Low and High. Check that the bounds of the result of -- Ada.Strings.Unbounded.To_String are 1 and Length. -- -- Check that Ada.Strings.Wide_Unbounded.Slice raises Index_Error if -- High > Length (Source) or Low > Length (Source) + 1. -- -- Check that the bounds of the result of Ada.Strings.Wide_Unbounded.Slice -- are Low and High. Check that the bounds of the result of -- Ada.Strings.Wide_Unbounded.To_Wide_String are 1 and Length. -- -- TEST DESCRIPTION: -- These objectives primarily test error cases associated with the -- operations Slice, Unbounded_Slice, To_String, and To_Wide_String. These -- operations are very likely to be used for any application that uses -- Unbounded_String or Unbounded_Wide_String objects, and errors are always -- possible, so we don't attempt to define a specific use case for these -- operations. Undetected runtime errors are a significant portability -- problem (this test was inspired by such a problem in the ARG tool -- GD2HTML). -- -- A.4.5(82) says that Slice has the same effect as the function in -- Ada.Strings.Bounded. That means that the checks for Index_Error for Slice -- that were introduced by AI95-0128-1 (Defect Report 8652/0049) and the -- bounds of Slice as clarified in AI95-0238-1 apply here as well. -- -- The Unbounded_Slice function and procedure were introduced by -- AI95-0301-1. -- -- Note that To_String is the only other string returning function in -- Ada.Strings.Unbounded, so we check its bounds here in order to check all -- such bounds explicitly. The bounds are explicitly defined in the -- definition of To_String (A.4.5(77)). -- -- CHANGE HISTORY: -- 07 APR 2022 RLB Created test from similar test CXA4039. -- --! with Ada.Exceptions; use Ada.Exceptions; with Ada.Strings.Unbounded; with Ada.Strings.Wide_Unbounded; use Ada.Strings; with Report; use Report; procedure CXA4039 is package Us renames Ada.Strings.Unbounded; package WUs renames Ada.Strings.Wide_Unbounded; Source : String (Ident_Int (1) .. Ident_Int (30)); Wide_Source : Wide_String (Ident_Int (1) .. Ident_Int (24)); X : Us.Unbounded_String; WX : WUs.Unbounded_Wide_String; begin Test ("CXA4039", "Check that Slice and Unbounded_Slice raises Index_Error " & "appropriately; check the bounds of Slice, To_String, and " & "To_Wide_String; check for Ada.Strings.Unbounded and " & "Ada.Strings.Wide_Unbounded"); -- Fill Source with "ABC..." for I in Source'Range loop Source (I) := Ident_Char (Character'Val (I + Character'Pos ('A') - Source'First)); end loop; -- and W with "ABC..." for I in Wide_Source'Range loop Wide_Source (I) := Ident_Wide_Char (Wide_Character'Val (I + Wide_Character'Pos ('A') - Wide_Source'First)); end loop; X := Us.To_Unbounded_String (Source); begin declare S : constant String := Us.Slice (X, Low => Ident_Int (28), High => Ident_Int (41)); begin Failed ("No exception raised by Slice - 1"); if S = Source then Comment ("Don't optimize S"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 1"); end; begin declare S : constant String := Us.Slice (X, Low => Ident_Int (8), High => Ident_Int (31)); begin Failed ("No exception raised by Slice - 2"); if S = Source then Comment ("Don't optimize S"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 2"); end; begin declare S : constant String := Us.Slice (X, Low => Ident_Int (15), High => Ident_Int (30)); begin if S /= Source(15..30) then Failed ("Wrong result - 3"); end if; if S'First /= 15 or else S'Last /= 30 then Failed ("Wrong bounds - 3"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 3"); end; begin declare S : constant String := Us.Slice (X, Low => Ident_Int (42), High => Ident_Int (28)); begin Failed ("No exception raised by Slice - 4"); if S = Source then Comment ("Don't optimize S"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 4"); end; begin declare S : constant String := Us.Slice (X, Low => Ident_Int (31), High => Ident_Int (28)); begin if S /= "" then Failed ("Wrong result - 5"); end if; if S'First /= 31 or else S'Last /= 28 then Failed ("Wrong bounds - 5"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 5"); end; begin declare S : constant String := Us.Slice (X, Low => Ident_Int (30), High => Ident_Int (30)); begin if S /= Source(30..30) then Failed ("Wrong result - 6"); end if; if S'First /= 30 or else S'Last /= 30 then Failed ("Wrong bounds - 6"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 6"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (20), High => Ident_Int (38)); begin Failed ("No exception raised by Unbounded_Slice - 7"); if Us.To_String(B) = Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 7"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (12), High => Ident_Int (31)); begin Failed ("No exception raised by Unbounded_Slice - 8"); if Us.To_String(B) = Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 8"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (10), High => Ident_Int (30)); S : constant String := Us.To_String (B); begin if S /= Source(10..30) then Failed ("Wrong result - 9"); end if; if S'First /= 1 or else S'Last /= Us.Length(B) then Failed ("Wrong bounds - 9"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 9"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (36), High => Ident_Int (25)); begin Failed ("No exception raised by Unbounded_Slice - 10"); if Us.To_String(B) = Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 10"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (31), High => Ident_Int (25)); S : constant String := Us.To_String (B); begin if S /= "" then Failed ("Wrong result - 11"); end if; if S'First /= 1 or else S'Last /= Us.Length (B) then Failed ("Wrong bounds - 11"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 11"); end; begin declare B : constant Us.Unbounded_String := Us.Unbounded_Slice (X, Low => Ident_Int (30), High => Ident_Int (30)); S : constant String := Us.To_String (B); begin if S /= Source(30..30) then Failed ("Wrong result - 12"); end if; if S'First /= 1 or else S'Last /= Us.Length (B) then Failed ("Wrong bounds - 12"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 12"); end; declare B : Us.Unbounded_String; begin Us.Unbounded_Slice (Source => X, Target => B, Low => Ident_Int (5), High => Ident_Int (32)); Failed ("No exception raised by Unbounded_Slice - 13"); if Us.To_String(B) = Source then Comment ("Don't optimize B"); end if; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 13"); end; declare B : Us.Unbounded_String; begin Us.Unbounded_Slice (Source => X, Target => B, Low => Ident_Int (44), High => Ident_Int (3)); Failed ("No exception raised by Unbounded_Slice - 14"); if Us.To_String(B) = Source then Comment ("Don't optimize B"); end if; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 14"); end; declare B : Us.Unbounded_String; begin Us.Unbounded_Slice (Source => X, Target => B, Low => Ident_Int (18), High => Ident_Int (30)); declare S : constant String := Us.To_String (B); begin if S /= Source(18..30) then Failed ("Wrong result - 15"); end if; if S'First /= 1 or else S'Last /= Us.Length(B) then Failed ("Wrong bounds - 15"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 15"); end; declare B : Us.Unbounded_String; begin Us.Unbounded_Slice (Source => X, Target => B, Low => Ident_Int (31), High => Ident_Int (12)); declare S : constant String := Us.To_String (B); begin if S /= "" then Failed ("Wrong result - 16"); end if; if S'First /= 1 or else S'Last /= Us.Length(B) then Failed ("Wrong bounds - 16"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 16"); end; ---- WX := WUs.To_Unbounded_Wide_String (Wide_Source); begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (21), High => Ident_Int (33)); begin Failed ("No exception raised by Slice - 21"); if W = Wide_Source then Comment ("Don't optimize W"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 21"); end; begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (8), High => Ident_Int (25)); begin Failed ("No exception raised by Slice - 22"); if W = Wide_Source then Comment ("Don't optimize W"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 22"); end; begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (15), High => Ident_Int (24)); begin if W /= Wide_Source(15..24) then Failed ("Wrong result - 23"); end if; if W'First /= 15 or else W'Last /= 24 then Failed ("Wrong bounds - 23"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 23"); end; begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (36), High => Ident_Int (20)); begin Failed ("No exception raised by Slice - 24"); if W = Wide_Source then Comment ("Don't optimize W"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 24"); end; begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (25), High => Ident_Int (21)); begin if W /= "" then Failed ("Wrong result - 25"); end if; if W'First /= 25 or else W'Last /= 21 then Failed ("Wrong bounds - 25"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 25"); end; begin declare W : constant Wide_String := WUs.Slice (WX, Low => Ident_Int (24), High => Ident_Int (24)); begin if W /= Wide_Source(24..24) then Failed ("Wrong result - 26"); end if; if W'First /= 24 or else W'Last /= 24 then Failed ("Wrong bounds - 26"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 26"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (20), High => Ident_Int (38)); begin Failed ("No exception raised by Unbounded_Slice - 27"); if WUs.To_Wide_String(B) = Wide_Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 27"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (12), High => Ident_Int (31)); begin Failed ("No exception raised by Unbounded_Slice - 28"); if WUs.To_Wide_String(B) = Wide_Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 28"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (4), High => Ident_Int (24)); W : constant Wide_String := WUs.To_Wide_String (B); begin if W /= Wide_Source(4..24) then Failed ("Wrong result - 29"); end if; if W'First /= 1 or else W'Last /= WUs.Length(B) then Failed ("Wrong bounds - 29"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 29"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (36), High => Ident_Int (21)); begin Failed ("No exception raised by Unbounded_Slice - 30"); if WUs.To_Wide_String(B) = Wide_Source then Comment ("Don't optimize B"); end if; end; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 30"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (25), High => Ident_Int (20)); W : constant Wide_String := WUs.To_Wide_String (B); begin if W /= "" then Failed ("Wrong result - 31"); end if; if W'First /= 1 or else W'Last /= WUs.Length (B) then Failed ("Wrong bounds - 31"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 31"); end; begin declare B : constant WUs.Unbounded_Wide_String := WUs.Unbounded_Slice (WX, Low => Ident_Int (24), High => Ident_Int (24)); W : constant Wide_String := WUs.To_Wide_String (B); begin if W /= Wide_Source(24..24) then Failed ("Wrong result - 32"); end if; if W'First /= 1 or else W'Last /= WUs.Length (B) then Failed ("Wrong bounds - 32"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 32"); end; declare B : WUs.Unbounded_Wide_String; begin WUs.Unbounded_Slice (Source => WX, Target => B, Low => Ident_Int (5), High => Ident_Int (32)); Failed ("No exception raised by Unbounded_Slice - 33"); if WUs.To_Wide_String(B) = Wide_Source then Comment ("Don't optimize B"); end if; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 33"); end; declare B : WUs.Unbounded_Wide_String; begin WUs.Unbounded_Slice (Source => WX, Target => B, Low => Ident_Int (44), High => Ident_Int (3)); Failed ("No exception raised by Unbounded_Slice - 34"); if WUs.To_Wide_String(B) = Wide_Source then Comment ("Don't optimize B"); end if; exception when Index_Error => null; -- Expected exception. when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 34"); end; declare B : WUs.Unbounded_Wide_String; begin WUs.Unbounded_Slice (Source => WX, Target => B, Low => Ident_Int (18), High => Ident_Int (24)); declare W : constant Wide_String := WUs.To_Wide_String (B); begin if W /= Wide_Source(18..24) then Failed ("Wrong result - 35"); end if; if W'First /= 1 or else W'Last /= WUs.Length(B) then Failed ("Wrong bounds - 35"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 35"); end; declare B : WUs.Unbounded_Wide_String; begin WUs.Unbounded_Slice (Source => WX, Target => B, Low => Ident_Int (25), High => Ident_Int (24)); declare W : constant Wide_String := WUs.To_Wide_String (B); begin if W /= "" then Failed ("Wrong result - 36"); end if; if W'First /= 1 or else W'Last /= WUs.Length(B) then Failed ("Wrong bounds - 36"); end if; end; exception when E: others => Failed ("Exception raised - " & Exception_Name (E) & " - " & Exception_Message (E) & " - 36"); end; Result; end CXA4039;