❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

Unbounded string `ENCODING_ERROR : bad input at Item`

Why is the following code failing ?

I have picked Characters.Latin_1.Reserved_128 on purpose.

-- File 'print_non_graphic_character.adb'
with Ada.Characters.Latin_1;
with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Print_Non_Graphic_Character is
   Text_String : constant String := "Non Graphic Character: " & Ada.Characters.Latin_1.Reserved_128;
   Text_Unbounded_String : constant Ada.Strings.Unbounded.Unbounded_String :=
      Ada.Strings.Unbounded.To_Unbounded_String (Text_String);
   Ada.Text_IO.Put_Line (Text_String);
   Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (Text_Unbounded_String));
   Ada.Text_IO.Put_Line (Text_Unbounded_String'Image);
end Print_Non_Graphic_Character;

Note that it also fails with:

Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.Unbounded_String'Image (Text_Unbounded_String));
-- File 'print_non_graphic_character.gpr'
project Print_Non_Graphic_Character is
   for Main use ("print_non_graphic_character.adb");
   for Object_Dir use ".objs";

   package Compiler is
      -- "-Og" -- Optimize for debug
      -- "-g" -- Generate debug info
      for Default_Switches ("Ada") use ("-g", "-gnat2020", "-Og");
   end Compiler;

   package Binder is
      for Switches ("Ada") use ("-Es"); --  Symbolic traceback
   end Binder;
end Print_Non_Graphic_Character;

It gives me with GNAT 12.2.0

Non Graphic Character: οΏ½
Non Graphic Character: οΏ½

raised ADA.STRINGS.UTF_ENCODING.ENCODING_ERROR : bad input at Item (25)
0x40e8fb Ada.Strings.Utf_Encoding.Raise_Encoding_Error at a-stuten.adb:126
0x40f38b Ada.Strings.Utf_Encoding.Strings.Decode at a-suenst.adb:163
0x4049a8 Print_Non_Graphic_Character at print_non_graphic_character.adb:13
0x404efa Main at b__print_non_graphic_character.adb:279
0x404658 _start at ???

It is annoying because, when I want to print a record which is commposed with Unbounded_String, I cannot use the 'Image attribute because of the above error and have to print each field and use To_String for Unbounded_String (which is annoying to have when I couple that with generic functions/packages).

AUnit - Usefullness of the `Test_Caller` package

There seem to be two different ways to do the same thing when using the AUnit library:

Using the AUnit.Test_Cases.Test_Case type, creating some function tests, then register each tests with the AUnit.Test_Cases.Register_Tests function. We can then add this Test_Case to a Suite using the function AUnit.Test_Suites.Add_Test.

There some example of this:

(Surprisingly, there no examples using this way in the AUnit repository.)

The other way is to use the AUnit.Test_Fixtures.Test_Fixture type, creating some function tests. These tests can then be added to a Suite using the generic package AUnit.Test_Caller with the functions AUnit.Test_Suites.Add_Test and AUnit.Test_Caller.Create.

I can see way of using Test_Caller in:

The only difference I can see is that, when using the AUnit.Test_Cases.Test_Case, you can override the Set_Up_Case, Set_Up, Tear_Down and Tear_Down_Case functions. While with the AUnit.Test_Fixtures.Test_Fixture you can only override the Set_Up and Tear_Down functions (because the tests are not group under a Test_Case).

Appart from that, I don't really see much difference.

So, what is the use of the AUnit.Test_Fixtures.Test_Fixture type with the generic package AUnit.Test_Caller ? Why would use this over the (simpler ?) AUnit.Test_Cases.Test_Case type ?

Every example I have seen in one format can be transformed into the other (the Set_Up_Case and Tear_Down_Case set appart). Could give an example which use one but cannot be done by the other ?

Ada instanciation of a generic package with an implementation of a limited interface

I have a limited interface defined in my_interface_package.ads.

package My_Interface_Package is

   type My_Interface is limited interface;

   procedure Procedure_1 (Some_Parameter : in out My_Interface) is null;
   procedure Procedure_2 (Some_Parameter_1 : in out My_Interface; Some_Parameter_2 : in String) is abstract;

end My_Interface_Package;

The type My_Class implements this interface (in my_class_package.ads and my_class_package.adb).

with My_Interface_Package;

package My_Class_Package is

   type My_Class is limited new My_Interface_Package.My_Interface with record
      Some_Element : Boolean;
   end record;

   overriding procedure Procedure_1 (Some_Parameter : in out My_Class);
   overriding procedure Procedure_2 (Some_Parameter_1 : in out My_Class; Some_Parameter_2 : in String);

end My_Class_Package;
with Ada.Text_IO;

package body My_Class_Package is

   procedure Procedure_1 (Some_Parameter : in out My_Class) is
      Ada.Text_IO.Put_Line("Inside Procedure_1.");

   procedure Procedure_2 (Some_Parameter_1 : in out My_Class; Some_Parameter_2 : in String) is
      Ada.Text_IO.Put_Line("Inside Procedure_2.");

end My_Class_Package;

I have a generic package My_Generic_Package defined in my_generic_package.ads and my_generic_package.adb. It expects a type My_Generic_Type. I want this type to accept any concrete implementation of the interface My_Interface.

with My_Interface_Package;

   type My_Generic_Type is limited new My_Interface_Package.My_Interface with private; -- The problem seems to be in this declaration.
package My_Generic_Package is

   type My_Generic_Class is record
      Element : My_Generic_Type;
   end record;

   procedure Calling_Procedure_1 (Some_Parameter : in out My_Generic_Class);
   procedure Calling_Procedure_2 (Some_Parameter_1 : in out My_Generic_Class; Some_Parameter_2 : in String);

end My_Generic_Package;
with Ada.Text_IO;

package body My_Generic_Package is

   procedure Calling_Procedure_1 (Some_Parameter : in out My_Generic_Class) is
      Ada.Text_IO.Put_Line("Calling Procedure_1.");

   procedure Calling_Procedure_2 (Some_Parameter_1 : in out My_Generic_Class; Some_Parameter_2 : in String) is
      Ada.Text_IO.Put_Line("Calling Procedure_2.");
      Procedure_2(Some_Parameter_1, Some_Parameter_2);

end My_Generic_Package;

My main.adb:

with Ada.Text_IO;
with My_Class_Package;
with My_Generic_Package;

procedure Main is
   package My_Instanciated_Package is new My_Generic_Package(My_Generic_Type => My_Class_Package.My_Class);
   My_Object : My_Instanciated_Package.My_Generic_Class;
   My_Instanciated_Package.Calling_Procedure_2(My_Object, "some_string");
   Ada.Text_IO.Put_Line("Program terminated.");
end Main;

GNAT 12.2.0 tells me my_generic_package.ads:3:73: missing ";" (he doesn't want the 'with private' part, but even without it, also fail).

I have tried a bunch of other declaration but ultimatly (trying to inspire myself from wikibooks), all failed. Any idea of what's wrong ?