1 ;;; arithmetic-fixnums.test --- Test suite for R6RS (rnrs arithmetic bitwise)
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-suite test-r6rs-arithmetic-fixnums)
21 :use-module ((rnrs arithmetic fixnums) :version (6))
22 :use-module ((rnrs conditions) :version (6))
23 :use-module ((rnrs exceptions) :version (6))
24 :use-module (test-suite lib))
26 (with-test-prefix "fixnum?"
27 (pass-if "fixnum? is #t for fixnums" (fixnum? 0))
29 (pass-if "fixnum? is #f for non-fixnums" (not (fixnum? 'foo)))
31 (pass-if "fixnum? is #f for non-fixnum numbers"
32 (and (not (fixnum? 1.0)) (not (fixnum? (+ (greatest-fixnum) 1))))))
34 (with-test-prefix "fx=?"
35 (pass-if "fx=? is #t for eqv inputs" (fx=? 3 3 3))
37 (pass-if "fx=? is #f for non-eqv inputs" (not (fx=? 1 2 3))))
39 (with-test-prefix "fx>?"
40 (pass-if "fx>? is #t for monotonically > inputs" (fx>? 3 2 1))
42 (pass-if "fx>? is #f for non-monotonically > inputs" (not (fx>? 1 2 3))))
44 (with-test-prefix "fx<?"
45 (pass-if "fx<? is #t for monotonically < inputs" (fx<? 1 2 3))
47 (pass-if "fx<? is #t for non-monotonically < inputs" (not (fx<? 3 2 1))))
49 (with-test-prefix "fx>=?"
50 (pass-if "fx>=? is #t for monotonically > or = inputs" (fx>=? 3 2 2 1))
52 (pass-if "fx>=? is #f for non-monotonically > or = inputs"
55 (with-test-prefix "fx<=?"
56 (pass-if "fx<=? is #t for monotonically < or = inputs" (fx<=? 1 2 2 3))
58 (pass-if "fx<=? is #f for non-monotonically < or = inputs"
61 (with-test-prefix "fxzero?"
62 (pass-if "fxzero? is #t for zero" (fxzero? 0))
64 (pass-if "fxzero? is #f for non-zero fixnums"
65 (and (not (fxzero? 1)) (not (fxzero? -1)))))
67 (with-test-prefix "fxpositive?"
68 (pass-if "fxpositive? is #t for positive fixnums" (fxpositive? 1))
70 (pass-if "fxpositive? is #f for non-positive fixnums"
71 (and (not (fxpositive? -1))
72 (not (fxpositive? 0)))))
74 (with-test-prefix "fxnegative?"
75 (pass-if "fxnegative? is #t for negative fixnums" (fxnegative? -1))
77 (pass-if "fxnegative? is #f for non-negative fixnums"
78 (and (not (fxnegative? 1))
79 (not (fxnegative? 0)))))
81 (with-test-prefix "fxodd?"
82 (pass-if "fxodd? is #t for odd fixnums" (fxodd? 1))
84 (pass-if "fxodd? is #f for even fixnums" (not (fxodd? 2))))
86 (with-test-prefix "fxeven?"
87 (pass-if "fxeven? is #t for even fixnums" (fxeven? 2))
89 (pass-if "fxeven? is #f for odd fixnums" (not (fxeven? 1))))
91 (with-test-prefix "fxmax" (pass-if "simple" (fx=? (fxmax 1 3 2) 3)))
93 (with-test-prefix "fxmin" (pass-if "simple" (fx=? (fxmin -1 0 2) -1)))
95 (with-test-prefix "fx+"
96 (pass-if "simple" (fx=? (fx+ 1 2) 3))
98 (pass-if "&implementation-restriction on non-fixnum result"
99 (guard (condition ((implementation-restriction-violation? condition) #t)
101 (begin (fx+ (greatest-fixnum) 1) #f))))
103 (with-test-prefix "fx*"
104 (pass-if "simple" (fx=? (fx* 2 3) 6))
106 (pass-if "&implementation-restriction on non-fixnum result"
107 (guard (condition ((implementation-restriction-violation? condition) #t)
109 (begin (fx* (greatest-fixnum) 2) #f))))
111 (with-test-prefix "fx-"
112 (pass-if "unary fx- negates argument" (fx=? (fx- 1) -1))
114 (pass-if "simple" (fx=? (fx- 3 2) 1))
116 (pass-if "&assertion on non-fixnum result"
117 (guard (condition ((assertion-violation? condition) #t) (else #f))
118 (fx- (least-fixnum) 1))))
120 (with-test-prefix "fxdiv-and-mod"
122 (call-with-values (lambda () (fxdiv-and-mod 123 10))
124 (or (and (fx=? d 12) (fx=? m 3))
125 (throw 'unresolved))))))
127 (with-test-prefix "fxdiv"
128 (pass-if "simple" (or (fx=? (fxdiv -123 10) -13) (throw 'unresolved))))
130 (with-test-prefix "fxmod"
131 (pass-if "simple" (or (fx=? (fxmod -123 10) 7) (throw 'unresolved))))
133 (with-test-prefix "fxdiv0-and-mod0"
135 (call-with-values (lambda () (fxdiv0-and-mod0 -123 10))
137 (or (and (fx=? d 12) (fx=? m -3))
138 (throw 'unresolved))))))
140 (with-test-prefix "fxdiv0"
141 (pass-if "simple" (or (fx=? (fxdiv0 -123 10) 12) (throw 'unresolved))))
143 (with-test-prefix "fxmod0"
144 (pass-if "simple" (or (fx=? (fxmod0 -123 10) -3) (throw 'unresolved))))
147 ;; Without working div and mod implementations and without any example results
148 ;; from the spec, I have no idea what the results of these functions should
151 (with-test-prefix "fx+/carry" (pass-if "simple" (throw 'unresolved)))
153 (with-test-prefix "fx-/carry" (pass-if "simple" (throw 'unresolved)))
155 (with-test-prefix "fx*/carry" (pass-if "simple" (throw 'unresolved)))
157 (with-test-prefix "fxnot" (pass-if "simple" (fx=? (fxnot 3) -4)))
159 (with-test-prefix "fxand" (pass-if "simple" (fx=? (fxand 5 6) 4)))
161 (with-test-prefix "fxior" (pass-if "simple" (fx=? (fxior 2 4) 6)))
163 (with-test-prefix "fxxor" (pass-if "simple" (fx=? (fxxor 5 4) 1)))
165 (with-test-prefix "fxif" (pass-if "simple" (fx=? (fxif 5 3 4) 1)))
167 (with-test-prefix "fxbit-count" (pass-if "simple" (fx=? (fxbit-count 5) 2)))
169 (with-test-prefix "fxlength" (pass-if "simple" (fx=? (fxlength 5) 3)))
171 (with-test-prefix "fxfirst-bit-set"
173 (and (eqv? (fxfirst-bit-set 1) 0)
174 (eqv? (fxfirst-bit-set -4) 2)))
176 (pass-if "fxfirst-bit-set is -1 on zero"
177 (and (eqv? (fxfirst-bit-set 0) -1))))
179 (with-test-prefix "fxbit-set?"
180 (pass-if "fxbit-set? is #t on index of set bit" (fxbit-set? 5 2))
182 (pass-if "fxbit-set? is #f on index of unset bit" (not (fxbit-set? 5 1))))
184 (with-test-prefix "fxcopy-bit" (pass-if "simple" (fx=? (fxcopy-bit 2 2 7) 6)))
186 (with-test-prefix "fxbit-field"
187 (pass-if "simple" (fx=? (fxbit-field 50 1 4) 1)))
189 (with-test-prefix "fxcopy-bit-field"
190 (pass-if "simple" (fx=? (fxcopy-bit-field 255 2 6 10) 235)))
192 (with-test-prefix "fxarithmetic-shift"
194 (and (fx=? (fxarithmetic-shift -6 -1) -3)
195 (fx=? (fxarithmetic-shift -5 -1) -3)
196 (fx=? (fxarithmetic-shift -4 -1) -2)
197 (fx=? (fxarithmetic-shift -3 -1) -2)
198 (fx=? (fxarithmetic-shift -2 -1) -1)
199 (fx=? (fxarithmetic-shift -1 -1) -1))))
201 (with-test-prefix "fxarithmetic-shift-left"
202 (pass-if "simple" (fx=? (fxarithmetic-shift-left -6 -1) -3)))
204 (with-test-prefix "fxarithmetic-shift-right"
205 (pass-if "simple" (fx=? (fxarithmetic-shift-right -6 1) -3)))
207 (with-test-prefix "fxrotate-bit-field"
208 (pass-if "simple" (fx=? (fxrotate-bit-field 227 2 6 2) 203)))
210 (with-test-prefix "fxreverse-bit-field"
211 (pass-if "simple" (fx=? (fxreverse-bit-field 82 1 4) 88)))