Anonymous user
Assertions in design by contract: Difference between revisions
m
→{{header|REXX}}: added/changed some comments and whitespace, simplified the program, used a template for the output section.
m (→{{header|Phix}}: added syntax colouring the hard way) |
m (→{{header|REXX}}: added/changed some comments and whitespace, simplified the program, used a template for the output section.) |
||
Line 569:
=={{header|REXX}}==
This is just a simple method of ''assertion''; more informative messages could be added in the '''assertion''' routine.
<br>A '''return''' statement could've been used instead of an '''exit''' statement to continue processing.
<lang rexx>/*REXX program demonstrates a method on how to use assertions in design by contract.*/
parse arg top . /*obtain optional argument from the CL.*/
if top=='' | top=="," then top=
do #=1 for 666 /*repeat for a devilish number of times*/
a= random(1, top)
b= random(1, a)
c= a -
say
call assert date('Weekday') \== "Monday"
call assert time('H')<9 | time("H")>15
call assert c>0 /*The value of C must be positive. */
end /*#*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
assert: if arg(1) then return 1; say /*if true, then assertion has passed. */
say 'assertion failed on line ' sigL " with: " subword(space(sourceline(sigl)),
say; say '# index= ' #
say 'ASSERT is exiting.';
/*──────────────────────────────────────────────────────────────────────────────────────*/
<pre>
a= 88 b= 77 c=
a= 33 b= 9 c=
a= 64 b= 9 c=
a= 6 b= 4 c=
a= 74 b= 49 c=
a= 58 b= 48 c=
a= 53 b= 1 c=
a= 36 b= 9 c=
▲ a= 1 b= 1 c= 0
assertion failed on line 13 with: assert c>0 /*The value of C must be positive. */
# index=
ASSERT is exiting.
</pre>
|