1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 | -- Abstract :
--
-- See spec.
--
-- Copyright (C) 2009, 2010, 2013, 2014 Stephen Leake. All Rights Reserved.
--
-- This library is free software; you can redistribute it and/or
-- modify it under terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 3, or (at
-- your option) any later version. This library is distributed in the
-- hope that it will be useful, but WITHOUT ANY WARRANTY; without even
-- the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-- PURPOSE. See the GNU General Public License for more details. You
-- should have received a copy of the GNU General Public License
-- distributed with this program; see file COPYING. If not, write to
-- the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
-- MA 02111-1307, USA.
--
-- As a special exception, if other files instantiate generics from
-- this unit, or you link this unit with other files to produce an
-- executable, this unit does not by itself cause the resulting
-- executable to be covered by the GNU General Public License. This
-- exception does not however invalidate any other reasons why the
-- executable file might be covered by the GNU Public License.
with AUnit.Assertions;
with Ada.Exceptions;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
package body AUnit.Check is
procedure Gen_Check_Discrete
(Label : in String;
Computed : in Item_Type;
Expected : in Item_Type)
is begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & " got " & Item_Type'Image (Computed) & " expecting " & Item_Type'Image (Expected));
end Gen_Check_Discrete;
procedure Gen_Check_Access
(Label : in String;
Computed : in Item_Access_Type;
Expected : in Item_Access_Type)
is begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & " access values mismatch");
end Gen_Check_Access;
procedure Check
(Label : in String;
Computed : in Boolean;
Expected : in Boolean)
is begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & " got " & Boolean'Image (Computed) & " expecting " & Boolean'Image (Expected));
end Check;
procedure Check
(Label : in String;
Computed : in String;
Expected : in String)
is begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & ASCII.LF &
"got '" & Computed & "'" & ASCII.LF &
"expecting '" & Expected & "'");
end Check;
procedure Check
(Label : in String;
Computed : in Integer;
Expected : in Integer)
is begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & " got " & Integer'Image (Computed) & " expecting " & Integer'Image (Expected));
end Check;
procedure Check
(Label : in String;
Computed : in Ada.Tags.Tag;
Expected : in Ada.Tags.Tag)
is
use Ada.Tags;
begin
Standard.AUnit.Assertions.Assert
(Computed = Expected,
Label & " got " & External_Tag (Computed) & " expecting " & External_Tag (Expected));
end Check;
function Not_In (Line : in Ada.Text_IO.Count; Skip : in Line_Number_Array_Type) return Boolean
is
use type Ada.Text_IO.Count;
begin
for I in Skip'Range loop
if Line = Skip (I) then
return False;
end if;
end loop;
return True;
end Not_In;
function Chomp (Line : String) return String
is
use Ada.Strings.Maps;
EOL_Set : constant Character_Set := To_Set (Character'Val (10) & Character'Val (13));
function Last_Non_Terminating (P : Natural) return Natural
is
-- Return position of the last character in Line that is not one of the line
-- terminating characters, or 0. Starts looking at position P, backwards.
begin
if P >= Line'First then
if Is_In (Line (P), EOL_Set) then
return Last_Non_Terminating (P - 1);
else
return P;
end if;
else
return 0;
end if;
end Last_Non_Terminating;
begin
return Line (Line'First .. Last_Non_Terminating (Line'Last));
end Chomp;
procedure Check_Files
(Label : in String;
Computed_Name : in String;
Expected_Name : in String;
Skip : in Line_Number_Array_Type := (1 .. 0 => 1))
is
use Ada.Strings;
use Ada.Strings.Fixed;
use Ada.Text_IO;
use Standard.AUnit.Assertions;
Computed : File_Type;
Expected : File_Type;
begin
begin
Open (Computed, In_File, Computed_Name);
exception
when E : others =>
Assert
(False,
Label & " file '" & Computed_Name & "' cannot be opened: " & Ada.Exceptions.Exception_Name (E));
end;
begin
Open (Expected, In_File, Expected_Name);
exception
when E : others =>
Assert
(False,
Label & " file '" & Expected_Name & "' cannot be opened: " & Ada.Exceptions.Exception_Name (E));
end;
begin
while not End_Of_File (Expected) and not End_Of_File (Computed) loop
declare
Computed_Line : constant String := Chomp (Get_Line (Computed));
Expected_Line : constant String := Chomp (Get_Line (Expected));
Line_Image : constant String := ":" & Trim (Positive_Count'Image (Line (Expected)), Both);
begin
-- Get_Line advances the line counter beyond the line of interest
if Not_In (Line (Computed) - 1, Skip) then
Check
(Expected_Name & Line_Image & " " & Computed_Name & Line_Image,
Computed_Line,
Expected_Line);
end if;
end;
end loop;
Assert (End_Of_File (Computed), Label & " '" & Computed_Name & "' longer than '" & Expected_Name & "'");
Assert (End_Of_File (Expected), Label & " '" & Expected_Name & "' longer than '" & Computed_Name & "'");
Close (Computed);
Close (Expected);
exception
when Standard.AUnit.Assertions.Assertion_Error =>
Close (Computed);
Close (Expected);
raise;
end;
end Check_Files;
end AUnit.Check;
|