@@ -71,12 +71,12 @@ Defensive Programming
71
71
72
72
.. code :: Ada
73
73
74
- procedure Push (S : Stack) is
75
- Entry_Length : constant Positive := Length (S );
74
+ procedure Push (The_Stack : Stack) is
75
+ Entry_Length : constant Positive := Length (The_Stack );
76
76
begin
77
- pragma Assert (not Is_Full (S )); -- entry condition
77
+ pragma Assert (not Is_Full (The_Stack )); -- entry condition
78
78
[...]
79
- pragma Assert (Length (S ) = Entry_Length + 1); -- exit condition
79
+ pragma Assert (Length (The_Stack ) = Entry_Length + 1); -- exit condition
80
80
end Push;
81
81
82
82
* Subprogram contracts are an **assertion ** mechanism
@@ -85,15 +85,15 @@ Defensive Programming
85
85
86
86
.. code :: Ada
87
87
88
- procedure Force_Acquire (P : Peripheral) is
88
+ procedure Force_Acquire (Resource : Peripheral) is
89
89
begin
90
- if not Available (P ) then
90
+ if not Available (Resource ) then
91
91
-- Corrective action
92
- Force_Release (P );
93
- pragma Assert (Available (P ));
92
+ Force_Release (Resource );
93
+ pragma Assert (Available (Resource ));
94
94
end if;
95
95
96
- Acquire (P );
96
+ Acquire (Resource );
97
97
end;
98
98
99
99
-----------------------------
@@ -196,17 +196,17 @@ Quiz
196
196
.. code :: Ada
197
197
198
198
-- Convert string to Integer
199
- function From_String ( S : String ) return Integer
199
+ function To_Integer ( S : String ) return Integer
200
200
with Pre => S'Length > 0;
201
201
202
- procedure Do_Something is
203
- I : Integer := From_String ("");
202
+ procedure Print_Something is
203
+ I : Integer := To_Integer ("");
204
204
begin
205
205
Put_Line (I'Image);
206
- end Do_Something ;
206
+ end Print_Something ;
207
207
208
- Assuming :ada: `From_String ` is defined somewhere, what happens
209
- when :ada: `Do_Something ` is run?
208
+ Assuming :ada: `To_Integer ` is defined somewhere, what happens
209
+ when :ada: `Print_Something ` is run?
210
210
211
211
A. "0" is printed
212
212
B. Constraint Error exception
@@ -217,7 +217,7 @@ when :ada:`Do_Something` is run?
217
217
218
218
Explanations
219
219
220
- The call to :ada: `From_String ` will fail its precondition, which is considered
220
+ The call to :ada: `To_Integer ` will fail its precondition, which is considered
221
221
an :ada: `Assertion_Error ` exception.
222
222
223
223
------
@@ -226,25 +226,25 @@ Quiz
226
226
227
227
.. code :: Ada
228
228
229
- function Area (L : Positive; H : Positive) return Positive is
230
- (L * H )
229
+ function Area (Length : Positive; Height : Positive) return Positive is
230
+ (Length * Height )
231
231
with Pre => ?
232
232
233
233
Which pre-condition is necessary for :ada: `Area ` to calculate the correct result for
234
234
all values :ada: `L ` and :ada: `H `
235
235
236
- A. ``L > 0 and H > 0 ``
237
- B. ``L < Positive'Last and H < Positive'Last ``
238
- C. ``L * H in Positive ``
236
+ A. ``Length > 0 and Height > 0 ``
237
+ B. ``Length < Positive'Last and Height < Positive'Last ``
238
+ C. ``Length * Height in Positive ``
239
239
D. :answer: `None of the above `
240
240
241
241
.. container :: animate
242
242
243
243
Explanations
244
244
245
245
A. Parameters are :ada: `Positive `, so this is unnecessary
246
- B. :ada: `L = Positive'Last-1 and H = Positive'Last-1 ` will still cause an overflow
246
+ B. :ada: `Length = Positive'Last-1 and Height = Positive'Last-1 ` will still cause an overflow
247
247
C. Classic trap: the check itself may cause an overflow!
248
248
249
- Preventing an overflow requires using the expression :ada: `Integer'Last / L <= H `
249
+ Preventing an overflow requires using the expression :ada: `Integer'Last / Length <= Height `
250
250
0 commit comments