Ада: Ошибка предварительных условий во время теста на другой процедуре - PullRequest
0 голосов
/ 26 октября 2018

Я немного растерялся. Я пытаюсь сделать универсальный пакет для использования массивов. Дело в том, что когда я пытаюсь протестировать Initializer , я получаю сообщение об ошибке: SYSTEM.ASSERTIONS.ASSERT_FAILURE on Get_Element_Indice , и я не понимаю, почему он действует так!

Есть спецификации (хотя бы их часть).

-- Spécification du module Tableau.

generic
Capacite : Integer;   -- Nombre maximal d'éléments qu'un tableau peut contenir
type T_Element is private;  -- Type des éléments du tableau

package Tableaux is

type T_Tableau is limited private;

procedure Initialiser (Tab : out T_Tableau) with
    Post => Get_Taille(Tab)= 0;

function Get_Taille (Tab : in T_Tableau) return Integer with
       Post => Get_Taille'Result >= 0;

function Get_Element_Indice (Tab : in T_Tableau ; Indice : in Integer) return T_Element ;
       Pre => Indice > 0 and Get_Taille(Tab) >= Indice; --This is where the error is located according to gnat's debuger.

procedure Modifier_Element (Tab : in out T_Tableau; Indice : in Integer; N_Valeur : in T_Element) with
        Pre => Get_Taille(Tab) >= Indice and Indice > 0,
        Post => Get_Element_Indice(Tab, Indice) = N_Valeur;

-- There are more procedures but the error is above so I've cleaned them.

private

type T_Tab_Elements is array (1..Capacite) of T_Element;

type T_Tableau is
    record
        Elements : T_Tab_Elements;  -- les éléments du tableau
        Taille: Integer;            -- Nombre d'éléments dans le tableau
    end record;
   end Tableaux;`

Здесь вы можете найти тело:

-- Implantation du module Tableau.
package body Tableaux is

procedure Initialiser (Tab : out T_Tableau) is
begin
    Tab.Taille := 0;
end Initialiser;

function Get_Taille (Tab : in T_Tableau) return Integer is
begin
    Return Tab.Taille;
end Get_Taille;

function Get_Element_Indice (Tab : in T_Tableau ; Indice : in Integer) return T_Element is
begin
       return Tab.Elements(Indice);
end Get_Element_Indice;


-- It goes on ..

end Tableaux;

А вот и тест:

with Tableaux;

-- Programme de test du module Tableaux.
procedure Test_Tableaux is

package Tableaux_Charactere is
        new Tableaux (Capacite => 10, T_Element => Character);
use Tableaux_Charactere;


function Test_Fonction_Element (Element : in Character) return Character is
    pragma Unreferenced (Element); -- Also, if someone could explain why do I need this line it would be great =)
begin
    return 'A';
end Test_Fonction_Element;


 procedure Appliquer_Sur_Chaque_Charactere is new Appliquer_Sur_Chaque(Test_Fonction_Element);


 -- Initialiser un tableau avec "OK" ajoutés dans le tableau vide.
    procedure Initialiser_Avec_OK (Tab : out T_Tableau) is
begin
    Initialiser(Tab);
    Ajouter_Element(Tab, 'O');
    Ajouter_Element(Tab, 'K');
end Initialiser_Avec_OK;


 -- Initialiser un tableau avec "BONJOUR" ajoutés dans le tableau vide.
procedure Initialiser_Avec_BONJOUR (Tab : out T_Tableau) is
begin
    Initialiser (Tab);
    Ajouter_Element(Tab, 'B');
    Ajouter_Element(Tab, 'O');
    Ajouter_Element(Tab, 'N');
    Ajouter_Element(Tab, 'J');
    Ajouter_Element(Tab, 'O');
    Ajouter_Element(Tab, 'U');
    Ajouter_Element(Tab, 'R');
end Initialiser_Avec_BONJOUR;


-- Test de l'initialisation du Tableau
procedure Tester_Initialiser is
    Tab, Tab2 : T_Tableau;
begin
    Put("Test intialiser");
    Initialiser_Avec_BONJOUR(Tab2);
    pragma Assert ( Get_Taille(Tab2) = 7);
    Initialiser(Tab);
    pragma Assert (Get_Taille(Tab) = 0);
end Tester_Initialiser;

begin
Tester_Initialiser; -- Procedure I want to test
-- There are more
 end Test_Tableaux;

Я хочу извиниться за мой сломанный английский, это не мой родной язык. Спасибо за вашу помощь!

1 Ответ

0 голосов
/ 27 октября 2018

Исключением по умолчанию, которое вы получаете при сбое утверждения, является Ada.Assertions.Assertion_Error.

Пакет System.Assertions не является частью стандарта, поэтому, похоже, вы обнаружили ошибку в компиляторе. (Полный вопрос в этом вопросе будет полезен для проверки этого.)

...