Implementation and test cases for the R6RS (rnrs arithmetic flonums)
[bpt/guile.git] / test-suite / tests / r6rs-arithmetic-flonums.test
CommitLineData
b01818d7
JG
1;;; arithmetic-flonums.test --- Test suite for R6RS (rnrs arithmetic flonums)
2
3;; Copyright (C) 2010 Free Software Foundation, Inc.
4;;
5;; This library is free software; you can redistribute it and/or
6;; modify it under the terms of the GNU Lesser General Public
7;; License as published by the Free Software Foundation; either
8;; version 3 of the License, or (at your option) any later version.
9;;
10;; This library is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;; Lesser General Public License for more details.
14;;
15;; You should have received a copy of the GNU Lesser General Public
16;; License along with this library; if not, write to the Free Software
17;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18\f
19
20(define-module (test-suite test-r6rs-arithmetic-flonums)
21 :use-module ((rnrs arithmetic flonums) :version (6))
22 :use-module ((rnrs conditions) :version (6))
23 :use-module ((rnrs exceptions) :version (6))
24 :use-module (test-suite lib))
25
26(define fake-pi 3.14159265)
27(define (reasonably-close? x y) (< (abs (- x y)) 0.0000001))
28
29(with-test-prefix "flonum?"
30 (pass-if "flonum? is #t on flonum"
31 (flonum? 1.5))
32
33 (pass-if "flonum? is #f on non-flonum"
34 (not (flonum? 3))))
35
36(with-test-prefix "real->flonum"
37 (pass-if "simple"
38 (flonum? (real->flonum 3))))
39
40(with-test-prefix "fl=?"
41 (pass-if "fl=? is #t for eqv inputs"
42 (fl=? 3.0 3.0 3.0))
43
44 (pass-if "fl=? is #f for non-eqv inputs"
45 (not (fl=? 1.5 0.0 3.0)))
46
47 (pass-if "+inf.0 is fl= to itself"
48 (fl=? +inf.0 +inf.0))
49
50 (pass-if "0.0 and -0.0 are fl="
51 (fl=? 0.0 -0.0)))
52
53(with-test-prefix "fl<?"
54 (pass-if "fl<? is #t for monotonically < inputs"
55 (fl<? 1.0 2.0 3.0))
56
57 (pass-if "fl<? is #f for non-monotonically < inputs"
58 (not (fl<? 2.0 2.0 1.4))))
59
60(with-test-prefix "fl<=?"
61 (pass-if "fl<=? is #t for monotonically < or = inputs"
62 (fl<=? 1.0 1.2 1.2))
63
64 (pass-if "fl<=? is #f non-monotonically < or = inputs"
65 (not (fl<=? 2.0 1.0 0.9))))
66
67(with-test-prefix "fl>?"
68 (pass-if "fl>? is #t for monotonically > inputs"
69 (fl>? 3.0 2.0 1.0))
70
71 (pass-if "fl>? is #f for non-monotonically > inputs"
72 (not (fl>? 1.0 1.0 1.2))))
73
74(with-test-prefix "fl>=?"
75 (pass-if "fl>=? is #t for monotonically > or = inputs"
76 (fl>=? 3.0 2.0 2.0))
77
78 (pass-if "fl>=? is #f for non-monotonically > or = inputs"
79 (not (fl>=? 1.0 1.2 1.2))))
80
81(with-test-prefix "flinteger?"
82 (pass-if "flinteger? is #t on integer flomnums"
83 (flinteger? 1.0))
84
85 (pass-if "flinteger? is #f on non-integer flonums"
86 (not (flinteger? 1.5))))
87
88(with-test-prefix "flzero?"
89 (pass-if "flzero? is #t for 0.0 and -0.0"
90 (and (flzero? 0.0) (flzero? -0.0)))
91
92 (pass-if "flzero? is #f for non-zero flonums"
93 (not (flzero? 1.0))))
94
95(with-test-prefix "flpositive?"
96 (pass-if "flpositive? is #t on positive flonum"
97 (flpositive? 1.0))
98
99 (pass-if "flpositive? is #f on negative flonum"
100 (not (flpositive? -1.0)))
101
102 (pass-if "0.0 and -0.0 are not flpositive"
103 (and (not (flpositive? 0.0)) (not (flpositive? -0.0)))))
104
105(with-test-prefix "flnegative?"
106 (pass-if "flnegative? is #t on negative flonum"
107 (flnegative? -1.0))
108
109 (pass-if "flnegative? is #f on positive flonum"
110 (not (flnegative? 1.0)))
111
112 (pass-if "0.0 and -0.0 are not flnegative"
113 (and (not (flnegative? 0.0)) (not (flnegative? -0.0)))))
114
115(with-test-prefix "flodd?"
116 (pass-if "&assertion raised on non-integer flonum"
117 (guard (condition ((assertion-violation? condition) #t) (else #f))
118 (begin (flodd? 1.5) #f)))
119
120 (pass-if "flodd? is #t on odd flonums"
121 (flodd? 3.0))
122
123 (pass-if "flodd? is #f on even flonums"
124 (not (flodd? 2.0))))
125
126(with-test-prefix "fleven?"
127 (pass-if "&assertion raised on non-integer flonum"
128 (guard (condition ((assertion-violation? condition) #t) (else #f))
129 (begin (fleven? 1.5) #f)))
130
131 (pass-if "fleven? is #t on even flonums"
132 (fleven? 2.0))
133
134 (pass-if "fleven? is #f on odd flonums"
135 (not (fleven? 3.0))))
136
137(with-test-prefix "flfinite?"
138 (pass-if "flfinite? is #t on non-infinite flonums"
139 (flfinite? 2.0))
140
141 (pass-if "flfinite? is #f on infinities"
142 (and (not (flfinite? +inf.0)) (not (flfinite? -inf.0)))))
143
144(with-test-prefix "flinfinite?"
145 (pass-if "flinfinite? is #t on infinities"
146 (and (flinfinite? +inf.0) (flinfinite? -inf.0)))
147
148 (pass-if "flinfinite? is #f on non-infinite flonums"
149 (not (flinfinite? 2.0))))
150
151(with-test-prefix "flnan?"
152 (pass-if "flnan? is #t on NaN and -NaN"
153 (and (flnan? +nan.0) (flnan? -nan.0)))
154
155 (pass-if "flnan? is #f on non-NaN values"
156 (not (flnan? 1.5))))
157
158(with-test-prefix "flmax"
159 (pass-if "simple" (fl=? (flmax 1.0 3.0 2.0) 3.0)))
160
161(with-test-prefix "flmin"
162 (pass-if "simple" (fl=? (flmin -1.0 0.0 2.0) -1.0)))
163
164(with-test-prefix "fl+"
165 (pass-if "simple" (fl=? (fl+ 2.141 1.0 0.1) 3.241)))
166
167(with-test-prefix "fl*"
168 (pass-if "simple" (fl=? (fl* 1.0 2.0 3.0 1.5) 9.0)))
169
170(with-test-prefix "fl-"
171 (pass-if "unary fl- negates argument" (fl=? (fl- 2.0) -2.0))
172
173 (pass-if "simple" (fl=? (fl- 10.5 6.0 0.5) 4.0)))
174
175(with-test-prefix "fl/"
176 (pass-if "unary fl/ returns multiplicative inverse" (fl=? (fl/ 10.0) 0.1))
177
178 (pass-if "simple" (fl=? (fl/ 10.0 2.0 2.0) 2.5)))
179
180(with-test-prefix "flabs"
181 (pass-if "simple" (and (fl=? (flabs -1.0) 1.0) (fl=? (flabs 1.23) 1.23))))
182
183(with-test-prefix "fldiv-and-mod"
184 (pass-if "simple"
185 (call-with-values (lambda () (fldiv-and-mod 5.0 2.0))
186 (lambda (div mod) (fl=? div 2.0) (fl=? mod 1.0)))))
187
188(with-test-prefix "fldiv"
189 (pass-if "simple" (fl=? (fldiv 5.0 2.0) 2.0)))
190
191(with-test-prefix "flmod"
192 (pass-if "simple" (fl=? (flmod 5.0 2.0) 1.0)))
193
194(with-test-prefix "fldiv0-and-mod0"
195 (pass-if "simple"
196 (call-with-values (lambda () (fldiv0-and-mod0 -123.0 10.0))
197 (lambda (div mod)
198 (or (and (fl=? div -12.0) (fl=? mod -3.0))
199 (throw 'unresolved))))))
200
201(with-test-prefix "fldiv0"
202 (pass-if "simple" (or (fl=? (fldiv0 -123.0 10.0) -12.0) (throw 'unresolved))))
203
204(with-test-prefix "flmod0"
205 (pass-if "simple" (or (fl=? (flmod0 -123.0 10.0) -3.0) (throw 'unresolved))))
206
207(with-test-prefix "flnumerator"
208 (pass-if "simple" (fl=? (flnumerator 0.5) 1.0))
209
210 (pass-if "infinities"
211 (and (fl=? (flnumerator +inf.0) +inf.0)
212 (fl=? (flnumerator -inf.0) -inf.0)))
213
214 (pass-if "negative zero" (fl=? (flnumerator -0.0) -0.0)))
215
216(with-test-prefix "fldenominator"
217 (pass-if "simple" (fl=? (fldenominator 0.5) 2.0))
218
219 (pass-if "infinities"
220 (and (fl=? (fldenominator +inf.0) 1.0)
221 (fl=? (fldenominator -inf.0) 1.0)))
222
223 (pass-if "zero" (fl=? (fldenominator 0.0) 1.0)))
224
225(with-test-prefix "flfloor"
226 (pass-if "simple"
227 (and (fl=? (flfloor -4.3) -5.0)
228 (fl=? (flfloor 3.5) 3.0))))
229
230(with-test-prefix "flceiling"
231 (pass-if "simple"
232 (and (fl=? (flceiling -4.3) -4.0)
233 (fl=? (flceiling 3.5) 4.0))))
234
235(with-test-prefix "fltruncate"
236 (pass-if "simple"
237 (and (fl=? (fltruncate -4.3) -4.0)
238 (fl=? (fltruncate 3.5) 3.0))))
239
240(with-test-prefix "flround"
241 (pass-if "simple"
242 (and (fl=? (flround -4.3) -4.0)
243 (fl=? (flround 3.5) 4.0))))
244
245(with-test-prefix "flexp"
246 (pass-if "infinities"
247 (and (fl=? (flexp +inf.0) +inf.0)
248 (fl=? (flexp -inf.0) 0.0))))
249
250(with-test-prefix "fllog"
251 (pass-if "unary fllog returns natural log"
252 (let ((l (fllog 2.718281828459045)))
253 (and (fl<=? 0.9 l) (fl>=? 1.1 l))))
254
255 (pass-if "infinities"
256 (and (fl=? (fllog +inf.0) +inf.0)
257 (flnan? (fllog -inf.0))))
258
259 (pass-if "zeroes" (fl=? (fllog 0.0) -inf.0))
260
261 (pass-if "binary fllog returns log in specified base"
262 (fl=? (fllog 8.0 2.0) 3.0)))
263
264(with-test-prefix "flsin"
265 (pass-if "simple"
266 (and (reasonably-close? (flsin (/ fake-pi 2)) 1.0)
267 (reasonably-close? (flsin (/ fake-pi 6)) 0.5))))
268
269(with-test-prefix "flcos"
270 (pass-if "simple"
271 (and (fl=? (flcos 0.0) 1.0) (reasonably-close? (flcos (/ fake-pi 3)) 0.5))))
272
273(with-test-prefix "fltan"
274 (pass-if "simple"
275 (and (reasonably-close? (fltan (/ fake-pi 4)) 1.0)
276 (reasonably-close? (fltan (/ (* 3 fake-pi) 4)) -1.0))))
277
278(with-test-prefix "flasin"
279 (pass-if "simple"
280 (and (reasonably-close? (flasin 1.0) (/ fake-pi 2))
281 (reasonably-close? (flasin 0.5) (/ fake-pi 6)))))
282
283(with-test-prefix "flacos"
284 (pass-if "simple"
285 (and (fl=? (flacos 1.0) 0.0)
286 (reasonably-close? (flacos 0.5) (/ fake-pi 3)))))
287
288(with-test-prefix "flatan"
289 (pass-if "unary flatan"
290 (and (reasonably-close? (flatan 1.0) (/ fake-pi 4))
291 (reasonably-close? (flatan -1.0) (/ fake-pi -4))))
292
293 (pass-if "infinities"
294 (and (reasonably-close? (flatan -inf.0) -1.5707963267949)
295 (reasonably-close? (flatan +inf.0) 1.5707963267949)))
296
297 (pass-if "binary flatan"
298 (and (reasonably-close? (flatan 3.5 3.5) (/ fake-pi 4)))))
299
300(with-test-prefix "flsqrt"
301 (pass-if "simple" (fl=? (flsqrt 4.0) 2.0))
302
303 (pass-if "infinity" (fl=? (flsqrt +inf.0) +inf.0))
304
305 (pass-if "negative zero" (fl=? (flsqrt -0.0) -0.0)))
306
307(with-test-prefix "flexpt" (pass-if "simple" (fl=? (flexpt 2.0 3.0) 8.0)))
308
309(with-test-prefix "fixnum->flonum"
310 (pass-if "simple" (fl=? (fixnum->flonum 100) 100.0)))