source: project/wiki/eggref/5/checks

Last change on this file was 37485, checked in by juergen, 7 months ago
File size: 5.8 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4== Pre- and postconditions made easy
5
6This egg implements some routines, which are outsourced from
7simple-exceptions. In particular macros << and >>, which accept an
8argument or result, checks it against zero or more predicates and
9returns it in case of success unchanged. Otherwise it prints a
10meaningful error message, showing i.a. the offending predicate and the
11argument's or result's name. Some are implemented as macros instead of
12procedures, because I didn't want an extra parameter with the argument's
13or result's name.
14Procedure versions of those macros are given as well.
15In reimplementing those routines, I changed the syntax a bit, so be
16careful, if you used the equally named routines from simple-expressions.
17
18=== Interface
19
20The precondition and postcondition checks are denoted with some
21consecutive symbols < and > respectively. There are macro and procedure
22versions, the latter denoted with a trailing % and needing an additional
23parameter, the name of the value to be checked. All those routines work
24the same, they differ only in the error message, they produce in case
25some predicate returns #f. The routines named with three symbols < or >
26differ from those with two only by an additional parameter naming the
27location of the checks.
28
29==== assert*
30
31<macro>(assert* loc xpr ....)</macro>
32
33checks the expressions xpr .... in sequence and raises an exception
34for the first failing expression with location property loc and
35arguments property the failing expression quoted.
36
37==== named-lambda
38
39<macro>(named-lambda (name . args) xpr . xprs)</macro>
40
41a named version of lambda, which makes recursion possible and helps in
42error messages.
43
44==== <<<
45
46<macro>(<<< loc arg arg? ...)</macro>
47
48precondition test:
49returns arg unchanged only if all predicates arg? return #t on it,
50otherwise an error message with the offending predicate and the
51location loc is printed.
52
53==== <<
54
55<macro>(<< arg arg? ...)</macro>
56
57precondition test:
58returns arg unchanged only if all predicates arg? return #t on it,
59otherwise an error message with the offending predicate is printed.
60
61==== >>>
62
63<macro>(>>> loc result result? ...)</macro>
64
65postcondition test:
66returns result unchanged only if all predicates result? return #t on it,
67otherwise an error message with the offending predicate and the
68location loc is printed.
69
70==== >>
71
72<macro>(>> result result? ...)</macro>
73
74postcondition test:
75passes result unchanged only if all predicates result? return #t on it
76otherwise an error message with the offending predicate is printed.
77
78==== <<<%
79
80<procedure>(<<<% loc name arg arg? ...)</procedure>
81
82precondition test:
83returns arg unchanged only if all predicates arg? return #t on it,
84otherwise an error message with the offending predicate and the
85location loc as well as arg's name is printed.
86
87==== <<%
88
89<procedure>(<<% name arg arg? ...)</procedure>
90
91precondition test:
92returns arg unchanged only if all predicates arg? return #t on it,
93otherwise an error message with the offending predicate as well as
94arg's name is printed.
95
96==== >>>%
97
98<procedure>(>>>% loc name result result? ...)</procedure>
99
100postcondition test:
101returns result unchanged only if all predicates result? return #t on it,
102otherwise an error message with the offending predicate and the
103location loc as well as result's name is printed.
104
105==== >>%
106
107<procedure>(>>% name result result? ...)</procedure>
108
109postcondition test:
110passes result unchanged only if all predicates result? return #t on it
111otherwise an error message with the offending predicate as well as
112result's name is printed.
113
114==== true?
115
116<procedure>(true? xpr)</procedure>
117
118returns always #t
119
120==== false?
121
122<procedure>(false? xpr)</procedure>
123
124returns always #f
125
126=== Examples
127
128(define x 5)
129
130<enscript highlight=scheme>
131
132(import checks)
133
134(condition-case
135  (assert* 'x (integer? x) (even? x))
136  ((exn assert) #f)) ;-> #f
137
138(<< x positive? odd?)
139; -> 5
140
141(<< x zero? odd?)
142; -> error
143
144(<< x positive? even?)
145; -> error
146
147(<<< 'loc x positive? even?)
148; -> argument-exception
149
150(<<<% 'loc 'x x positive? even?)
151; -> result-exception
152
153(condition-case
154  (<<% 'x x integer? even?)
155  ((exn argument) #f)) ;-> #f
156
157</enscript>
158
159== Last update
160
161Mar 29, 2019
162
163== Author
164
165[[/users/juergen-lorenz|Juergen Lorenz]]
166
167== License
168
169 Copyright (c) 2014-2019, Juergen Lorenz
170 All rights reserved.
171
172 Redistribution and use in source and binary forms, with or without
173 modification, are permitted provided that the following conditions are
174 met:
175 
176 Redistributions of source code must retain the above copyright
177 notice, this list of conditions and the following disclaimer.
178 
179 Redistributions in binary form must reproduce the above copyright
180 notice, this list of conditions and the following disclaimer in the
181 documentation and/or other materials provided with the distribution.
182 Neither the name of the author nor the names of its contributors may be
183 used to endorse or promote products derived from this software without
184 specific prior written permission.
185   
186 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
187 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
188 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
189 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
190 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
191 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
192 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
193 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
194 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
195 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
196 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
197
198== Version History
199
200; 1.3 : dependency on simple-exceptions added
201; 1.2 : code cleaned
202; 1.1.0 : documentation of named-lambda fixed
203; 1.1 : procedure versions added
204; 1.0 : extracted and modified from simple-exceptions
205
206
Note: See TracBrowser for help on using the repository browser.