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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
-- C391003.A
--
-- Grant of Unlimited Rights
--
-- 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 a type can be extended at a different level than the
-- parent type. Check that subprograms overridden for such an extension
-- can be dispatched to, and that the overridden routine can access
-- local variables of the subprogram.
--
-- TEST DESCRIPTION:
-- This test checks that Ada 95 rule of the third sentence of 3.9.1(3)
-- is not enforced for more recent Ada implementations, as it was
-- repealed by AI95-00344-01. It also checks some consequences of that
-- change.
--
-- We declare a root tagged private type Acccount to represent a bank
-- account, and some associated primitive operations, in a visible
-- library package. (This type is based on the one in test C392008.) We
-- also define a Transfer operation that takes two accounts and an amount
-- in that package. That operation is implemented using dispatching calls
-- on the primitive operations of type Account.
--
-- We declare a package containing a normal extension of the type,
-- Interest_Checking. This overrides some of the operations of Account.
--
-- We then define a package containing a wire-transfer subprogram, which
-- extends Account to create a temporary account to hold funds before
-- they are transmitted. We then transfer funds from the base account
-- to the temporary account using Transfer.
--
-- Finally, we have a main program where we create various accounts and
-- try various wire-transfers.
--
-- The following hierarchy of tagged types and primitive operations is
-- utilized in this test:
--
-- package Bank
-- type Account (root)-----------------------------------------------\
-- | |
-- | Operations |
-- | proc Deposit |
-- | proc Withdrawal |
-- | func Balance |
-- | proc Service_Charge |
-- | proc Add_Interest |
-- | proc Open |
-- | |
-- package Interest_Checking |
-- type Account (extended from Bank.Account) |
-- | |
-- \-Operations |
-- proc Deposit (inherited) |
-- func Balance (inherited) |
-- proc Service_Charge (inherited) |
-- proc Add_Interest (overridden) |
-- proc Open (overridden) |
-- proc Withdrawal (overridden) |
-- |
-- procedure Wire_Transfer----------------------------------------------/
-- type Transfer_Account (extended from Bank.Account)
-- |
-- \-Operations
-- proc Deposit (overridden)
-- proc Withdrawal (inherited)
-- func Balance (inherited)
-- proc Service_Charge (inherited)
-- proc Add_Interest (inherited)
-- proc Open (inherited)
--
-- CHANGE HISTORY:
-- 08 Jan 2015 RLB Created test.
-- 13 Mar 2015 RLB Eliminated overlong line.
--
--!
----------------------------------------------------------------- C391003_0
package C391003_0 is -- package Bank
type Dollar_Amount is range -300_00..300_00;
type Account is tagged private;
-- Primitive operations.
procedure Deposit (A : in out Account;
X : in Dollar_Amount);
procedure Withdrawal (A : in out Account;
X : in Dollar_Amount);
function Balance (A : in Account) return Dollar_Amount;
procedure Service_Charge (A : in out Account);
procedure Add_Interest (A : in out Account);
procedure Open (A : in out Account);
procedure Transfer (Source, Target : in out Account'Class;
Amount : Dollar_Amount);
private
type Account is tagged
record
Current_Balance: Dollar_Amount;
end record;
end C391003_0;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
package body C391003_0 is
-- Primitive operations for type Account.
procedure Deposit (A : in out Account;
X : in Dollar_Amount) is
begin
A.Current_Balance := A.Current_Balance + X;
end Deposit;
procedure Withdrawal(A : in out Account;
X : in Dollar_Amount) is
begin
A.Current_Balance := A.Current_Balance - X;
end Withdrawal;
function Balance (A : in Account) return Dollar_Amount is
begin
return (A.Current_Balance);
end Balance;
procedure Service_Charge (A : in out Account) is
begin
A.Current_Balance := A.Current_Balance - 5_00;
end Service_Charge;
procedure Add_Interest (A : in out Account) is
Interest_On_Account : Dollar_Amount := 0_00;
begin
A.Current_Balance := A.Current_Balance + Interest_On_Account;
end Add_Interest;
procedure Open (A : in out Account) is
Initial_Deposit : Dollar_Amount := 00_00;
begin
A.Current_Balance := Initial_Deposit;
end Open;
procedure Transfer (Source, Target : in out Account'Class;
Amount : Dollar_Amount) is
Transfer_Fee : constant Dollar_Amount := 2_00;
begin
Withdrawal (Source, Amount); -- Dispatching call.
Withdrawal (Source, Transfer_Fee); -- Dispatching call.
Deposit (Target, Amount); -- Dispatching call.
end Transfer;
end C391003_0;
----------------------------------------------------------------- C391003_1
with C391003_0; -- with Bank;
package C391003_1 is -- package Interest_Checking
package Bank renames C391003_0;
subtype Interest_Rate is Bank.Dollar_Amount range 0..100; -- was digits 4;
Current_Rate : Interest_Rate := 0_02;
type Account is new Bank.Account with
record
Overdraft_Fee : Bank.Dollar_Amount;
Rate : Interest_Rate;
end record;
overriding
procedure Add_Interest (A : in out Account);
overriding
procedure Open (A : in out Account);
overriding
procedure Withdrawal (A : in out Account;
X : in Bank.Dollar_Amount);
-- Inherited primitive operations (from Bank.Account)
-- procedure Deposit (A : in out Account;
-- X : in Bank.Dollar_Amount);
-- function Balance (A : in Account) return Bank.Dollar_Amount;
-- procedure Service_Charge (A : in out Account);
end C391003_1;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
package body C391003_1 is
-- Overridden primitive operations.
procedure Add_Interest (A : in out Account) is
Interest_On_Account : Bank.Dollar_Amount
:= Bank.Dollar_Amount( Bank."*"( A.Balance, A.Rate ));
begin
Bank.Deposit (Bank.Account(A), Interest_On_Account);
end Add_Interest;
procedure Open (A : in out Account) is
Initial_Deposit : Bank.Dollar_Amount := 20_00;
Check_Guarantee : Bank.Dollar_Amount := 10_00;
begin
Bank.Open (Bank.Account(A));
A.Overdraft_Fee := Check_Guarantee;
A.Rate := Current_Rate;
Bank.Deposit (Bank.Account(A), Initial_Deposit);
end Open;
procedure Withdrawal (A : in out Account;
X : in Bank.Dollar_Amount) is
use type Bank.Dollar_Amount;
begin
if X < A.Balance then
Bank.Withdrawal (Bank.Account(A), X);
else -- apply overdraft fee.
Bank.Withdrawal (Bank.Account(A), X);
Bank.Withdrawal (Bank.Account(A), A.Overdraft_Fee);
end if;
end Withdrawal;
end C391003_1;
----------------------------------------------------------------- C391003_2
with C391003_0; -- with Bank;
package C391003_2 is -- package Wire_Transfers
package Bank renames C391003_0;
type Account_Kind is (Basic, Premium, Platinum);
Transfer_Failed : exception;
procedure Wire_Transfer (Source : in out Bank.Account'Class;
Amount : in Bank.Dollar_Amount;
Kind : in Account_Kind);
end C391003_2;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
with Report;
package body C391003_2 is
use type Bank.Dollar_Amount;
procedure Wire_Transfer (Source : in out Bank.Account'Class;
Amount : in Bank.Dollar_Amount;
Kind : in Account_Kind) is
Minimum_Xfer : Bank.Dollar_Amount;
Maximum_Xfer : Bank.Dollar_Amount;
type Transfer_Account is new Bank.Account with null record;
overriding
procedure Deposit (A : in out Transfer_Account;
X : in Bank.Dollar_Amount) is
begin
Bank.Deposit (Bank.Account(A), X);
if A.Balance < Minimum_Xfer then
raise Transfer_Failed with "Too little transferred";
elsif A.Balance > Maximum_Xfer then
--raise Transfer_Failed with "Too much transferred";
Report.Failed ("Too much transferred, limit=" &
Bank.Dollar_Amount'Image(Maximum_Xfer) &
"; Balance=" & Bank.Dollar_Amount'Image(A.Balance));
else
null; -- Transmit money via ACH.
end if;
end Deposit;
Xfer_Acct : Transfer_Account;
begin
case Kind is
when Basic =>
Minimum_Xfer := 25_00;
Maximum_Xfer := 100_00;
when Premium =>
Minimum_Xfer := 12_00;
Maximum_Xfer := 200_00;
when Platinum =>
Minimum_Xfer := 10_00;
Maximum_Xfer := 300_00;
end case;
Open (Xfer_Acct); -- Zeros balance.
Bank.Transfer (Source => Source,
Target => Xfer_Acct,
Amount => Amount);
if Balance (Xfer_Acct) /= Amount then
Report.Failed ("Wrong balance transferred - Balance=" &
Bank.Dollar_Amount'Image(Balance (Xfer_Acct)));
end if;
end Wire_Transfer;
end C391003_2;
------------------------------------------------------------------- C391003
with C391003_0; use C391003_0; -- package Bank
with C391003_1; use C391003_1; -- package Interest_Checking;
with C391003_2; use C391003_2; -- package Wire_Transfer;
with Report;
with Ada.Exceptions;
procedure C391003 is
package Bank renames C391003_0;
package Interest_Checking renames C391003_1;
package Wire_Transfers renames C391003_2;
B_Acct : Bank.Account;
IC_Acct, IC_Acct2 : Interest_Checking.Account;
begin
Report.Test ("C391003", "Check that a type can be extended at a " &
"different level than the parent type. Check " &
"that subprograms overridden for such an " &
"extension can be dispatched to, and that the " &
"overridden routine can access local variables " &
"of the subprogram");
Open (B_Acct);
Deposit (B_Acct, 250_00);
Open (IC_Acct);
Deposit (IC_Acct, 30_00); --+20_00 initial deposit.
Open (IC_Acct2);
Deposit (IC_Acct2, 280_00); --+20_00 initial deposit.
-- Try a wire transfer for a platinum account:
Wire_Transfer (IC_Acct, Amount => 23_00, Kind => Platinum);
if Balance (IC_Acct) /= 25_00 then
Report.Failed ("Wrong balance in account after transfer");
end if;
-- Try a wire transfer for a premium account:
Wire_Transfer (IC_Acct2, Amount => 200_00, Kind => Premium);
if Balance (IC_Acct2) /= 98_00 then
Report.Failed ("Wrong balance in account after transfer");
end if;
-- Try a too-small transfer for a basic account:
begin
Wire_Transfer (B_Acct, Amount => 14_00, Kind => Basic);
-- Should raise Transfer_Failed.
Report.Failed ("Failed transfer not detected (probably did not " &
"dispatch to overridden Deposit)");
exception
when Wire_Transfers.Transfer_Failed => null;
end;
Report.Result;
exception
when Err:others =>
Report.Failed ("Stray exception: " &
Ada.Exceptions.Exception_Information (Err));
end C391003;
|