-- CXA4032.A
--
--                             Grant of Unlimited Rights
--
--     Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
--     F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
--     unlimited rights in the software and documentation contained herein.
--     Unlimited rights are defined in DFAR 252.227-7013(a)(19).  By making
--     this public release, the Government intends to confer upon all
--     recipients unlimited rights  equal to those held by the Government.
--     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 procedures defined in package Ada.Strings.Unbounded
--      are available, and that they produce correct results. Specifically,
--      check the procedures Replace_Slice, Insert, Overwrite, Delete,
--      Trim (2 versions), Head, and Tail.
--
-- TEST DESCRIPTION:
--      This test demonstrates the uses of many of the procedures defined
--      in package Ada.Strings.Unbounded for use with unbounded strings.
--      The test simulates how unbounded strings could be processed in a
--      user environment, using the procedures provided in this package.
--
--      This test, when taken in conjunction with tests CXA4010, CXA4011,
--      CXA4030, and CXA4031 will constitute a test of all the functionality
--      contained in package Ada.Strings.Unbounded.  This test uses a variety
--      of the procedures defined in the unbounded string package in ways
--      typical of common usage.
--
--
-- CHANGE HISTORY:
--      02 Mar 95   SAIC    Initial prerelease version.
--
--!

with Report;
with Ada.Strings;
with Ada.Strings.Maps;
with Ada.Strings.Maps.Constants;
with Jwa.Strings;
with Ada.Exceptions; use Ada.Exceptions;

procedure Test_Jwa_CXA4032 is
begin

   Report.Test ("CXA4032", "Check that the subprograms defined in "        &
                           "package Ada.Strings.Unbounded are available, " &
                           "and that they produce correct results");

   Test_Block:
   declare

      package JS renames Jwa.Strings;
      use JS;
      use Ada.Strings;

      TC_Null_String : constant String := "";
      TC_String_5    : String(1..5)    := "ABCDE";

      TC_Unb_String  : JS.String_8 :=
                         JS.To_String_8("Test String");

   begin

      -- Procedure Replace_Slice

      begin                                           -- Low > Source'Last+1
         JS.Replace_Slice(Source => TC_Unb_String,
                           Low    => JS.Length(TC_Unb_String) + 2,
                           High   => JS.Length(TC_Unb_String),
                           By     => TC_String_5);
         Report.Failed("Index_Error not raised by Replace_Slice when Low " &
                       "> Source'Last+1");
      exception
         when Index_Error => null;  -- OK, expected exception.
         when others      =>
            Report.Failed("Unexpected exception raised by Replace_Slice" &
                          "when Low > Source'Last+1");
      end;

      -- High >= Low

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Replace_Slice(TC_Unb_String, 5, 5, TC_String_5);

      if TC_Unb_String /= JS.To_String_8("TestABCDEString") then
         Report.Failed("Incorrect results from Replace_Slice - 1");
      end if;

      JS.Replace_Slice(TC_Unb_String, 1, 4, TC_String_5);

      if TC_Unb_String /= JS.To_String_8("ABCDEABCDEString") then
         Report.Failed("Incorrect results from Replace_Slice - 2");
      end if;

      JS.Replace_Slice(TC_Unb_String,
                        11,
                        JS.Length(TC_Unb_String),
                        TC_Null_String);

      if TC_Unb_String /= JS.To_String_8("ABCDEABCDE") then
         Report.Failed("Incorrect results from Replace_Slice - 3");
      end if;

      -- High < Low

      JS.Replace_Slice(TC_Unb_String, Low => 4, High => 1, By => "xxx");

      if TC_Unb_String /= JS.To_String_8("ABCxxxDEABCDE") then
         Report.Failed("Incorrect results from Replace_Slice - 4");
      end if;

      JS.Replace_Slice(TC_Unb_String, Low => 1, High => 0, By => "yyy");

      if TC_Unb_String /= JS.To_String_8("yyyABCxxxDEABCDE") then
         Report.Failed("Incorrect results from Replace_Slice - 5");
      end if;

      JS.Replace_Slice(TC_Unb_String,
                        JS.Length(TC_Unb_String) + 1,
                        JS.Length(TC_Unb_String),
                        By => "zzz");

      if TC_Unb_String /= JS.To_String_8("yyyABCxxxDEABCDEzzz") then
         Report.Failed("Incorrect results from Replace_Slice - 6");
      end if;


      -- Procedure Insert

      TC_Unb_String := JS.To_String_8("Test String");

      begin                  -- Before not in Source'First..Source'Last + 1
         JS.Insert(Source   => TC_Unb_String,
                    Before   => JS.Length(TC_Unb_String) + 2,
                    New_Item => TC_String_5);
         Report.Failed("Index_Error not raised by Insert when Before " &
                       "not in the range Source'First..Source'Last+1");
      exception
         when Index_Error => null;  -- OK, expected exception.
         when others      =>
            Report.Failed
              ("Unexpected exception raised by Insert when Before not in " &
               "the range Source'First..Source'Last+1");
      end;

      JS.Insert(TC_Unb_String, 1, "**");

      if TC_Unb_String /= JS.To_String_8("**Test String") then
         Report.Failed("Incorrect results from Insert - 1");
      end if;

      JS.Insert(TC_Unb_String, JS.Length(TC_Unb_String)+1, "**");

      if TC_Unb_String /= JS.To_String_8("**Test String**") then
         Report.Failed("Incorrect results from Insert - 2");
      end if;

      JS.Insert(TC_Unb_String, 8, "---");

      if TC_Unb_String /= JS.To_String_8("**Test ---String**") then
         Report.Failed("Incorrect results from Insert - 3");
      end if;

      JS.Insert(TC_Unb_String, 3, TC_Null_String);

      if TC_Unb_String /= JS.To_String_8("**Test ---String**") then
         Report.Failed("Incorrect results from Insert - 4");
      end if;


      -- Procedure Overwrite

      begin                -- Position not in Source'First..Source'Last + 1
         JS.Overwrite(Source   => TC_Unb_String,
                       Position => JS.Length(TC_Unb_String) + 2,
                       New_Item => TC_String_5);
         Report.Failed("Index_Error not raised by Overwrite when Position " &
                       "not in the range Source'First..Source'Last+1");
      exception
         when Index_Error => null;  -- OK, expected exception.
         when others      =>
            Report.Failed
              ("Unexpected exception raised by Overwrite when Position not " &
               "in the range Source'First..Source'Last+1");
      end;

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Overwrite(Source   => TC_Unb_String,
                    Position => 1,
                    New_Item => "XXXX");

      if TC_Unb_String /= JS.To_String_8("XXXX String") then
         Report.Failed("Incorrect results from Overwrite - 1");
      end if;

      JS.Overwrite(TC_Unb_String, JS.Length(TC_Unb_String)+1, "**");

      if TC_Unb_String /= JS.To_String_8("XXXX String**") then
         Report.Failed("Incorrect results from Overwrite - 2");
      end if;

      JS.Overwrite(TC_Unb_String, 3, TC_Null_String);

      if TC_Unb_String /= JS.To_String_8("XXXX String**") then
         Report.Failed("Incorrect results from Overwrite - 3");
      end if;

      JS.Overwrite(TC_Unb_String, 1, "abcdefghijklmn");

      if TC_Unb_String /= JS.To_String_8("abcdefghijklmn") then
         Report.Failed("Incorrect results from Overwrite - 4");
      end if;


      -- Procedure Delete

      TC_Unb_String := JS.To_String_8("Test String");

      -- From > Through  (No change to Source)

      JS.Delete(Source  => TC_Unb_String,
                 From    => JS.Length(TC_Unb_String),
                 Through => JS.Length(TC_Unb_String)-1);

      if TC_Unb_String /= JS.To_String_8("Test String") then
         Report.Failed("Incorrect results from Delete - 1");
      end if;

      JS.Delete(TC_Unb_String, 1, 0);

      if TC_Unb_String /= JS.To_String_8("Test String") then
         Report.Failed("Incorrect results from Delete - 2");
      end if;

      -- From <= Through

      JS.Delete(TC_Unb_String, 1, 5);

      if TC_Unb_String /= JS.To_String_8("String") then
         Report.Failed("Incorrect results from Delete - 3");
      end if;

      JS.Delete(TC_Unb_String, 3, 3);

      if TC_Unb_String /= JS.To_String_8("Sting") then
         Report.Failed("Incorrect results from Delete - 4");
      end if;


      -- Procedure Trim

      TC_Unb_String := JS.To_String_8("No Spaces");

      JS.Trim(Source => TC_Unb_String, Side => Ada.Strings.Both);

      if TC_Unb_String /= JS.To_String_8("No Spaces") then
         Report.Failed("Incorrect results from Trim - 1");
      end if;

      TC_Unb_String := JS.To_String_8("   Leading Spaces   ");

      JS.Trim(TC_Unb_String, Ada.Strings.Left);

      if TC_Unb_String /= JS.To_String_8("Leading Spaces   ") then
         Report.Failed("Incorrect results from Trim - 2");
      end if;

      TC_Unb_String := JS.To_String_8("   Ending Spaces   ");

      JS.Trim(TC_Unb_String, Ada.Strings.Right);

      if TC_Unb_String /= JS.To_String_8("   Ending Spaces") then
         Report.Failed("Incorrect results from Trim - 3");
      end if;

      TC_Unb_String :=
        JS.To_String_8("    Spaces   on  both  ends     ");

      JS.Trim(TC_Unb_String, Ada.Strings.Both);

      if TC_Unb_String /=
         JS.To_String_8("Spaces   on  both  ends")
      then
         Report.Failed("Incorrect results from Trim - 4");
      end if;


      -- Procedure Trim (with Character Set parameters)

      TC_Unb_String := JS.To_String_8("lowerCASEletters");

      JS.Trim(Source => TC_Unb_String,
               Left   => Ada.Strings.Maps.Constants.Lower_Set,
               Right  => Ada.Strings.Maps.Constants.Lower_Set);

      if TC_Unb_String /= JS.To_String_8("CASE") then
         Report.Failed("Incorrect results from Trim with Sets - 1");
      end if;

      TC_Unb_String := JS.To_String_8("lowerCASEletters");

      JS.Trim(TC_Unb_String,
               Ada.Strings.Maps.Constants.Upper_Set,
               Ada.Strings.Maps.Constants.Upper_Set);

      if TC_Unb_String /= JS.To_String_8("lowerCASEletters") then
         Report.Failed("Incorrect results from Trim with Sets - 2");
      end if;

      TC_Unb_String := JS.To_String_8("012abcdefghGFEDCBA789ab");

      JS.Trim(TC_Unb_String,
               Ada.Strings.Maps.Constants.Hexadecimal_Digit_Set,
               Ada.Strings.Maps.Constants.Hexadecimal_Digit_Set);

      if TC_Unb_String /= JS.To_String_8("ghG") then
         Report.Failed("Incorrect results from Trim with Sets - 3");
      end if;


      -- Procedure Head

      -- Count <= Source'Length

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Head(Source => TC_Unb_String,
               Count  => 0,
               Pad    => '*');

      if TC_Unb_String /= JS.Null_String_8 then
         Report.Failed("Incorrect results from Head - 1");
      end if;

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Head(Source => TC_Unb_String,
               Count  => 4,
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("Test") then
         Report.Failed("Incorrect results from Head - 2");
      end if;

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Head(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String),
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("Test String") then
         Report.Failed("Incorrect results from Head - 3");
      end if;

      -- Count > Source'Length

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Head(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String) + 4,
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("Test String****") then
         Report.Failed("Incorrect results from Head - 4");
      end if;

      TC_Unb_String := JS.Null_String_8;

      JS.Head(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String) + 3,
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("***") then
         Report.Failed("Incorrect results from Head - 5");
      end if;


      -- Procedure Tail

      -- Count <= Source'Length

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Tail(Source => TC_Unb_String,
               Count  => 0,
               Pad    => '*');

      if TC_Unb_String /= JS.Null_String_8 then
         Report.Failed("Incorrect results from Tail - 1");
      end if;

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Tail(Source => TC_Unb_String,
               Count  => 6,
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("String") then
         Report.Failed("Incorrect results from Tail - 2");
      end if;

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Tail(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String),
               Pad    => '*');

      if TC_Unb_String /= JS.To_String_8("Test String") then
         Report.Failed("Incorrect results from Tail - 3");
      end if;

      -- Count > Source'Length

      TC_Unb_String := JS.To_String_8("Test String");

      JS.Tail(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String) + 5,
               Pad    => 'x');

      if TC_Unb_String /= JS.To_String_8("xxxxxTest String") then
         Report.Failed("Incorrect results from Tail - 4");
      end if;

      TC_Unb_String := JS.Null_String_8;

      JS.Tail(Source => TC_Unb_String,
               Count  => JS.Length(TC_Unb_String) + 3,
               Pad    => 'X');

      if TC_Unb_String /= JS.To_String_8("XXX") then
         Report.Failed("Incorrect results from Tail - 5");
      end if;


   exception
      when E : others =>
         Report.Failed ("Exception raised in Test_Block: " &
                        Exception_Information (E));
   end Test_Block;

   Report.Result;

end Test_Jwa_CXA4032;
