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 &nbsp; '''return''' &nbsp; statement could've been used instead of an &nbsp; '''exit''' &nbsp; 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=100 100 /*Not specified? Then use the default.*/
pad_= left('', 9) /*PAD_: contains nine9 blanks for SAY SAYs. passing*/
a= 1 bw= length(top) + 1 /*W: is used c=for aligning the output. 0*/
 
do #=1 for 666 /*repeat for a devilish number of times*/
a= random(1, top) /*generate a random number (1 ──► TOP)*/
b= random(1, a) /* " " " " (1 ──► A)*/
c= a -b b /*compute difference between A and B. */
say pad _'a=' right(a,4 w) pad _"b=" right(b,4 w) pad _'c='right(c, w)_"sum="right(sumABC(a, b, right(c),4 w)
call assert date('Weekday') \== "Monday" /*shouldn't execute this pgm on Monday.*/
call assert time('H')<9 | time("H")>15 /* ··· and not during banking hours.*/
call assert c>0 /*The value of C must be positive. */
sum=my_sub(a, b, c) /*invoke a subroutine to do "stuff". */
say copies(' ',60) 'sum=' sum /*display the sum of A, B, and C. */
end /*#*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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)),32)
say; say '# index= ' #
say 'ASSERT is exiting.'; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
my_subsumABC: returnprocedure; arg(1)parse +arg(2) +arg(3) x,y,z; return x+y+z /*Sum three arguments. Real easy work.*/</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<pre>
a= 12 6 b= 5 b= 5 c= 7 c= 1 sum= 24
a= 88 b= 77 c= 11 sum= 12176
a= 35 88 b= 26 b= 17 c= 9 c= 71 sum= 70
a= 33 b= 9 c= 24 sum= 176 66
a= 90 34 b= 31 b= 12 c= 59 c= 22 sum= 180
a= 64 b= 9 c= 55 sum= 68128
a= 100 82 b= 18 97 c= 64 c= 3 sum= 164
a= 6 b= 4 c= 2 sum= 200 12
a= 85 88 b= 72 b= 52 c= 13 c= 36 sum= 170
a= 74 b= 49 c= 25 sum= 176148
a= 31 17 b= 14 b= 9 c= 17 c= 8sum= 62
a= 58 b= 48 c= 10 sum= 34116
a= 64 29 b= 59 b= 11 c= 5 c= 18 sum= 128
a= 53 b= 1 c= 52 sum= 58106
a= 54 23 b= 22 b= 1 c= 32 c= 22 sum= 108
a= 36 b= 9 c= 27 sum= 46 72
a= 100 2 b= 352 c= c 0 sum= 654
sum= 200
a= 10 b= 8 c= 2
sum= 20
a= 1 b= 1 c= 0
 
assertion failed on line 13 with: assert c>0 /*The value of C must be positive. */
 
# index= 1117
ASSERT is exiting.
</pre>