fed72ebb0214e3288a67c5d715991a8b8962a6eb
[bpt/guile.git] / test-suite / tests / r6rs-arithmetic-fixnums.test
1 ;;; arithmetic-fixnums.test --- Test suite for R6RS (rnrs arithmetic bitwise)
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-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))
25
26 (with-test-prefix "fixnum?"
27 (pass-if "fixnum? is #t for fixnums" (fixnum? 0))
28
29 (pass-if "fixnum? is #f for non-fixnums" (not (fixnum? 'foo)))
30
31 (pass-if "fixnum? is #f for non-fixnum numbers"
32 (and (not (fixnum? 1.0)) (not (fixnum? (+ (greatest-fixnum) 1))))))
33
34 (with-test-prefix "fx=?"
35 (pass-if "fx=? is #t for eqv inputs" (fx=? 3 3 3))
36
37 (pass-if "fx=? is #f for non-eqv inputs" (not (fx=? 1 2 3))))
38
39 (with-test-prefix "fx>?"
40 (pass-if "fx>? is #t for monotonically > inputs" (fx>? 3 2 1))
41
42 (pass-if "fx>? is #f for non-monotonically > inputs" (not (fx>? 1 2 3))))
43
44 (with-test-prefix "fx<?"
45 (pass-if "fx<? is #t for monotonically < inputs" (fx<? 1 2 3))
46
47 (pass-if "fx<? is #t for non-monotonically < inputs" (not (fx<? 3 2 1))))
48
49 (with-test-prefix "fx>=?"
50 (pass-if "fx>=? is #t for monotonically > or = inputs" (fx>=? 3 2 2 1))
51
52 (pass-if "fx>=? is #f for non-monotonically > or = inputs"
53 (not (fx>=? 1 2 3))))
54
55 (with-test-prefix "fx<=?"
56 (pass-if "fx<=? is #t for monotonically < or = inputs" (fx<=? 1 2 2 3))
57
58 (pass-if "fx<=? is #f for non-monotonically < or = inputs"
59 (not (fx<=? 3 2 1))))
60
61 (with-test-prefix "fxzero?"
62 (pass-if "fxzero? is #t for zero" (fxzero? 0))
63
64 (pass-if "fxzero? is #f for non-zero fixnums"
65 (and (not (fxzero? 1)) (not (fxzero? -1)))))
66
67 (with-test-prefix "fxpositive?"
68 (pass-if "fxpositive? is #t for positive fixnums" (fxpositive? 1))
69
70 (pass-if "fxpositive? is #f for non-positive fixnums"
71 (and (not (fxpositive? -1))
72 (not (fxpositive? 0)))))
73
74 (with-test-prefix "fxnegative?"
75 (pass-if "fxnegative? is #t for negative fixnums" (fxnegative? -1))
76
77 (pass-if "fxnegative? is #f for non-negative fixnums"
78 (and (not (fxnegative? 1))
79 (not (fxnegative? 0)))))
80
81 (with-test-prefix "fxodd?"
82 (pass-if "fxodd? is #t for odd fixnums" (fxodd? 1))
83
84 (pass-if "fxodd? is #f for even fixnums" (not (fxodd? 2))))
85
86 (with-test-prefix "fxeven?"
87 (pass-if "fxeven? is #t for even fixnums" (fxeven? 2))
88
89 (pass-if "fxeven? is #f for odd fixnums" (not (fxeven? 1))))
90
91 (with-test-prefix "fxmax" (pass-if "simple" (fx=? (fxmax 1 3 2) 3)))
92
93 (with-test-prefix "fxmin" (pass-if "simple" (fx=? (fxmin -1 0 2) -1)))
94
95 (with-test-prefix "fx+"
96 (pass-if "simple" (fx=? (fx+ 1 2) 3))
97
98 (pass-if "&implementation-restriction on non-fixnum result"
99 (guard (condition ((implementation-restriction-violation? condition) #t)
100 (else #f))
101 (begin (fx+ (greatest-fixnum) 1) #f))))
102
103 (with-test-prefix "fx*"
104 (pass-if "simple" (fx=? (fx* 2 3) 6))
105
106 (pass-if "&implementation-restriction on non-fixnum result"
107 (guard (condition ((implementation-restriction-violation? condition) #t)
108 (else #f))
109 (begin (fx* (greatest-fixnum) 2) #f))))
110
111 (with-test-prefix "fx-"
112 (pass-if "unary fx- negates argument" (fx=? (fx- 1) -1))
113
114 (pass-if "simple" (fx=? (fx- 3 2) 1))
115
116 (pass-if "&assertion on non-fixnum result"
117 (guard (condition ((assertion-violation? condition) #t) (else #f))
118 (fx- (least-fixnum) 1))))
119
120 (with-test-prefix "fxdiv-and-mod"
121 (pass-if "simple"
122 (call-with-values (lambda () (fxdiv-and-mod 123 10))
123 (lambda (d m)
124 (or (and (fx=? d 12) (fx=? m 3))
125 (throw 'unresolved))))))
126
127 (with-test-prefix "fxdiv"
128 (pass-if "simple" (or (fx=? (fxdiv -123 10) -13) (throw 'unresolved))))
129
130 (with-test-prefix "fxmod"
131 (pass-if "simple" (or (fx=? (fxmod -123 10) 7) (throw 'unresolved))))
132
133 (with-test-prefix "fxdiv0-and-mod0"
134 (pass-if "simple"
135 (call-with-values (lambda () (fxdiv0-and-mod0 -123 10))
136 (lambda (d m)
137 (or (and (fx=? d 12) (fx=? m -3))
138 (throw 'unresolved))))))
139
140 (with-test-prefix "fxdiv0"
141 (pass-if "simple" (or (fx=? (fxdiv0 -123 10) 12) (throw 'unresolved))))
142
143 (with-test-prefix "fxmod0"
144 (pass-if "simple" (or (fx=? (fxmod0 -123 10) -3) (throw 'unresolved))))
145
146
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
149 ;; be. -juliang
150
151 (with-test-prefix "fx+/carry" (pass-if "simple" (throw 'unresolved)))
152
153 (with-test-prefix "fx-/carry" (pass-if "simple" (throw 'unresolved)))
154
155 (with-test-prefix "fx*/carry" (pass-if "simple" (throw 'unresolved)))
156
157 (with-test-prefix "fxnot" (pass-if "simple" (fx=? (fxnot 3) -4)))
158
159 (with-test-prefix "fxand" (pass-if "simple" (fx=? (fxand 5 6) 4)))
160
161 (with-test-prefix "fxior" (pass-if "simple" (fx=? (fxior 2 4) 6)))
162
163 (with-test-prefix "fxxor" (pass-if "simple" (fx=? (fxxor 5 4) 1)))
164
165 (with-test-prefix "fxif" (pass-if "simple" (fx=? (fxif 5 3 4) 1)))
166
167 (with-test-prefix "fxbit-count" (pass-if "simple" (fx=? (fxbit-count 5) 2)))
168
169 (with-test-prefix "fxlength" (pass-if "simple" (fx=? (fxlength 5) 3)))
170
171 (with-test-prefix "fxfirst-bit-set"
172 (pass-if "simple"
173 (and (eqv? (fxfirst-bit-set 1) 0)
174 (eqv? (fxfirst-bit-set -4) 2)))
175
176 (pass-if "fxfirst-bit-set is -1 on zero"
177 (and (eqv? (fxfirst-bit-set 0) -1))))
178
179 (with-test-prefix "fxbit-set?"
180 (pass-if "fxbit-set? is #t on index of set bit" (fxbit-set? 5 2))
181
182 (pass-if "fxbit-set? is #f on index of unset bit" (not (fxbit-set? 5 1))))
183
184 (with-test-prefix "fxcopy-bit" (pass-if "simple" (fx=? (fxcopy-bit 2 2 7) 6)))
185
186 (with-test-prefix "fxbit-field"
187 (pass-if "simple" (fx=? (fxbit-field 50 1 4) 1)))
188
189 (with-test-prefix "fxcopy-bit-field"
190 (pass-if "simple" (fx=? (fxcopy-bit-field 255 2 6 10) 235)))
191
192 (with-test-prefix "fxarithmetic-shift"
193 (pass-if "simple"
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))))
200
201 (with-test-prefix "fxarithmetic-shift-left"
202 (pass-if "simple" (fx=? (fxarithmetic-shift-left -6 -1) -3)))
203
204 (with-test-prefix "fxarithmetic-shift-right"
205 (pass-if "simple" (fx=? (fxarithmetic-shift-right -6 1) -3)))
206
207 (with-test-prefix "fxrotate-bit-field"
208 (pass-if "simple" (fx=? (fxrotate-bit-field 227 2 6 2) 203)))
209
210 (with-test-prefix "fxreverse-bit-field"
211 (pass-if "simple" (fx=? (fxreverse-bit-field 82 1 4) 88)))