-- CXAC007.A -- -- 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 ACAA 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. -- -- Notice -- -- The ACAA has created and maintains the Ada Conformity Assessment Test -- Suite for the purpose of conformity assessments conducted in accordance -- with the International Standard ISO/IEC 18009 - Ada: Conformity -- assessment of a language processor. This test suite should not be used -- to make claims of conformance unless used in accordance with -- ISO/IEC 18009 and any applicable ACAA procedures. -- --* -- -- OBJECTIVE: -- Check that Stream_IO is preelaborated. -- -- TEST DESCRIPTION: -- This test is designed to demonstrate using a logger in a preelaborated -- package. The logger uses Stream_IO to write to the log as it is the -- only Ada IO package that can be used in a preelaborated package. -- -- After writing the log, we close it and read it to verify the expected -- contents. -- -- APPLICABILITY CRITERIA: -- This test is applicable to all implementations capable of supporting -- external Stream_IO files. -- -- -- CHANGE HISTORY: -- 16 Nov 2013 GRB Initial version. -- 27 Nov 2013 RLB Added headers. -- 18 Apr 2014 RLB Renamed, revised test as original version was -- illegal. -- 19 May 2014 RLB Added missing name to Legal_File_Name (default -- is nonsense as it is called before Test). -- --! with Ada.Streams.Stream_IO; package CXAC007_Logger with Preelaborate is procedure Log_Item (To : in String; Item : in String); -- Write an item to the log file named To. -- Note: We have to pass the file name here since it is generated by -- the Report package, which is not Preelaborated. In a real program, -- the name would most likely come from a constant or function declared -- in this package rather than being passed. end CXAC007_Logger; with Ada.Streams.Stream_IO; package body CXAC007_Logger is procedure Log_Item (To : in String; Item : in String) is File : Ada.Streams.Stream_IO.File_Type; use type Ada.Streams.Stream_Element_Offset; Buffer : Ada.Streams.Stream_Element_Array(1 .. Item'Length + 2); begin Ada.Streams.Stream_IO.Open (File, Ada.Streams.Stream_IO.Append_File, To); for I in Item'range loop Buffer(Ada.Streams.Stream_Element_Offset(I - Item'First)+1) := Ada.Streams.Stream_Element(Character'Pos(Item(I))); end loop; Buffer(Buffer'Last-1) := 13; Buffer(Buffer'Last ) := 10; Ada.Streams.Stream_IO.Write (File, Buffer); Ada.Streams.Stream_IO.Close (File); end Log_Item; end CXAC007_Logger; --==================================================================-- package CXAC007_Test with Preelaborate is procedure Do_Something (Log_Name : in String); -- A placeholder for the real application. end CXAC007_Test; with CXAC007_Logger; package body CXAC007_Test is procedure Do_Something (Log_Name : in String) is begin CXAC007_Logger.Log_Item (Log_Name, "Doing"); null; CXAC007_Logger.Log_Item (Log_Name, "Do more"); end Do_Something; end CXAC007_Test; --==================================================================-- with CXAC007_Logger; with CXAC007_Test; with Ada.Streams.Stream_IO; with Report; procedure CXAC007 is Log_File_Name : constant String := Report.Legal_File_Name (Nam => "CXAC007"); -- A file name that will be used for the log file. This file will -- be written. begin Report.Test ("CXAC007", "Check that Stream_IO is preelaborated"); Test_for_Stream_IO_Support: declare Test_File : Ada.Streams.Stream_IO.File_Type; begin -- If an implementation does not support Stream_IO in a particular -- environment, the exception Use_Error or Name_Error will be raised on -- calls to various Stream_IO operations. This block statement -- encloses a call to Create, which should produce an exception in a -- non-supportive environment. These exceptions will be handled to -- produce a Not_Applicable result. Ada.Streams.Stream_IO.Create (Test_File, Ada.Streams.Stream_IO.Out_File, Log_File_Name); Ada.Streams.Stream_IO.Close (Test_File); exception when Ada.Streams.Stream_IO.Use_Error | Ada.Streams.Stream_IO.Name_Error => Report.Not_Applicable ( "Files not supported - Create as Out_File for Stream_IO" ); Report.Comment ("File_Name is " & Log_File_Name); goto Not_Applicable; end Test_for_Stream_IO_Support; -- Generate a few log items: CXAC007_Logger.Log_Item (Log_File_Name, "Start"); CXAC007_Test.Do_Something (Log_File_Name); CXAC007_Logger.Log_Item (Log_File_Name, "End"); -- Read the log file and verify correct contents: declare Log_File : Ada.Streams.Stream_IO.File_Type; procedure Check (Data : in String) is Buffer : Ada.Streams.Stream_Element_Array(1 .. 12); Last : Ada.Streams.Stream_Element_Offset; use type Ada.Streams.Stream_Element_Offset; use type Ada.Streams.Stream_Element; begin Ada.Streams.Stream_IO.Read (Log_File, Buffer(1..Data'Length+2), Last); if Last /= Data'Length+2 then Report.Failed ("Wrong length read, premature end of file for " & Data); end if; if Buffer(Data'Length+1) /= 13 then Report.Failed ("Missing CR for " & Data); end if; if Buffer(Data'Length+2) /= 10 then Report.Failed ("Missing LF for " & Data); end if; for I in Data'range loop if Buffer(Ada.Streams.Stream_Element_Offset(I - Data'First)+1) /= Ada.Streams.Stream_Element(Character'Pos(Data(I))) then Report.Failed ("Wrong character for " & Data); exit; end if; end loop; end Check; begin Ada.Streams.Stream_IO.Open (Log_File, Ada.Streams.Stream_IO.In_File, Log_File_Name); Check ("Start"); Check ("Doing"); Check ("Do more"); Check ("End"); Ada.Streams.Stream_IO.Delete (Log_File); -- Clean up after ourselves. end; <> Report.Result; end CXAC007;