1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
;;; arithmetic-fixnums.test --- Test suite for R6RS (rnrs arithmetic bitwise)
;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-suite test-r6rs-arithmetic-fixnums)
:use-module ((rnrs arithmetic fixnums) :version (6))
:use-module ((rnrs conditions) :version (6))
:use-module ((rnrs exceptions) :version (6))
:use-module (test-suite lib))
(with-test-prefix "fixnum-width"
(pass-if-equal "consistent with least-fixnum"
(- (expt 2 (- (fixnum-width) 1)))
(least-fixnum))
(pass-if-equal "consistent with greatest-fixnum"
(- (expt 2 (- (fixnum-width) 1)) 1)
(greatest-fixnum)))
(with-test-prefix "fixnum?"
(pass-if "fixnum? is #t for fixnums" (fixnum? 0))
(pass-if "fixnum? is #f for non-fixnums" (not (fixnum? 'foo)))
(pass-if "fixnum? is #f for non-fixnum numbers"
(and (not (fixnum? 1.0)) (not (fixnum? (+ (greatest-fixnum) 1))))))
(with-test-prefix "fx=?"
(pass-if "fx=? is #t for eqv inputs" (fx=? 3 3 3))
(pass-if "fx=? is #f for non-eqv inputs" (not (fx=? 1 2 3))))
(with-test-prefix "fx>?"
(pass-if "fx>? is #t for monotonically > inputs" (fx>? 3 2 1))
(pass-if "fx>? is #f for non-monotonically > inputs" (not (fx>? 1 2 3))))
(with-test-prefix "fx<?"
(pass-if "fx<? is #t for monotonically < inputs" (fx<? 1 2 3))
(pass-if "fx<? is #t for non-monotonically < inputs" (not (fx<? 3 2 1))))
(with-test-prefix "fx>=?"
(pass-if "fx>=? is #t for monotonically > or = inputs" (fx>=? 3 2 2 1))
(pass-if "fx>=? is #f for non-monotonically > or = inputs"
(not (fx>=? 1 2 3))))
(with-test-prefix "fx<=?"
(pass-if "fx<=? is #t for monotonically < or = inputs" (fx<=? 1 2 2 3))
(pass-if "fx<=? is #f for non-monotonically < or = inputs"
(not (fx<=? 3 2 1))))
(with-test-prefix "fxzero?"
(pass-if "fxzero? is #t for zero" (fxzero? 0))
(pass-if "fxzero? is #f for non-zero fixnums"
(and (not (fxzero? 1)) (not (fxzero? -1)))))
(with-test-prefix "fxpositive?"
(pass-if "fxpositive? is #t for positive fixnums" (fxpositive? 1))
(pass-if "fxpositive? is #f for non-positive fixnums"
(and (not (fxpositive? -1))
(not (fxpositive? 0)))))
(with-test-prefix "fxnegative?"
(pass-if "fxnegative? is #t for negative fixnums" (fxnegative? -1))
(pass-if "fxnegative? is #f for non-negative fixnums"
(and (not (fxnegative? 1))
(not (fxnegative? 0)))))
(with-test-prefix "fxodd?"
(pass-if "fxodd? is #t for odd fixnums" (fxodd? 1))
(pass-if "fxodd? is #f for even fixnums" (not (fxodd? 2))))
(with-test-prefix "fxeven?"
(pass-if "fxeven? is #t for even fixnums" (fxeven? 2))
(pass-if "fxeven? is #f for odd fixnums" (not (fxeven? 1))))
(with-test-prefix "fxmax" (pass-if "simple" (fx=? (fxmax 1 3 2) 3)))
(with-test-prefix "fxmin" (pass-if "simple" (fx=? (fxmin -1 0 2) -1)))
(with-test-prefix "fx+"
(pass-if "simple" (fx=? (fx+ 1 2) 3))
(pass-if "&implementation-restriction on non-fixnum result"
(guard (condition ((implementation-restriction-violation? condition) #t)
(else #f))
(begin (fx+ (greatest-fixnum) 1) #f))))
(with-test-prefix "fx*"
(pass-if "simple" (fx=? (fx* 2 3) 6))
(pass-if "&implementation-restriction on non-fixnum result"
(guard (condition ((implementation-restriction-violation? condition) #t)
(else #f))
(begin (fx* (greatest-fixnum) 2) #f))))
(with-test-prefix "fx-"
(pass-if "unary fx- negates argument" (fx=? (fx- 1) -1))
(pass-if "simple" (fx=? (fx- 3 2) 1))
(pass-if "&assertion on non-fixnum result"
(guard (condition ((assertion-violation? condition) #t) (else #f))
(fx- (least-fixnum) 1))))
(with-test-prefix "fxdiv-and-mod"
(pass-if "simple"
(call-with-values (lambda () (fxdiv-and-mod 123 10))
(lambda (d m)
(and (fx=? d 12) (fx=? m 3))))))
(with-test-prefix "fxdiv" (pass-if "simple" (fx=? (fxdiv -123 10) -13)))
(with-test-prefix "fxmod" (pass-if "simple" (fx=? (fxmod -123 10) 7)))
(with-test-prefix "fxdiv0-and-mod0"
(pass-if "simple"
(call-with-values (lambda () (fxdiv0-and-mod0 -123 10))
(lambda (d m)
(and (fx=? d -12) (fx=? m -3))))))
(with-test-prefix "fxdiv0" (pass-if "simple" (fx=? (fxdiv0 -123 10) -12)))
(with-test-prefix "fxmod0" (pass-if "simple" (fx=? (fxmod0 -123 10) -3)))
;; Without working div and mod implementations and without any example results
;; from the spec, I have no idea what the results of these functions should
;; be. -juliang
;; UPDATE: div and mod implementations are now working properly -mhw
(with-test-prefix "fx+/carry" (pass-if "simple" (throw 'unresolved)))
(with-test-prefix "fx-/carry" (pass-if "simple" (throw 'unresolved)))
(with-test-prefix "fx*/carry" (pass-if "simple" (throw 'unresolved)))
(with-test-prefix "fxnot" (pass-if "simple" (fx=? (fxnot 3) -4)))
(with-test-prefix "fxand" (pass-if "simple" (fx=? (fxand 5 6) 4)))
(with-test-prefix "fxior" (pass-if "simple" (fx=? (fxior 2 4) 6)))
(with-test-prefix "fxxor" (pass-if "simple" (fx=? (fxxor 5 4) 1)))
(with-test-prefix "fxif" (pass-if "simple" (fx=? (fxif 5 3 4) 1)))
(with-test-prefix "fxbit-count"
(pass-if "simple" (fx=? (fxbit-count 5) 2))
(pass-if "negative" (fx=? (fxbit-count -5) -2)))
(with-test-prefix "fxlength" (pass-if "simple" (fx=? (fxlength 5) 3)))
(with-test-prefix "fxfirst-bit-set"
(pass-if "simple"
(and (eqv? (fxfirst-bit-set 1) 0)
(eqv? (fxfirst-bit-set -4) 2)))
(pass-if "fxfirst-bit-set is -1 on zero"
(and (eqv? (fxfirst-bit-set 0) -1))))
(with-test-prefix "fxbit-set?"
(pass-if "fxbit-set? is #t on index of set bit" (fxbit-set? 5 2))
(pass-if "fxbit-set? is #f on index of unset bit" (not (fxbit-set? 5 1))))
(with-test-prefix "fxcopy-bit" (pass-if "simple" (fx=? (fxcopy-bit 2 2 1) 6)))
(with-test-prefix "fxbit-field"
(pass-if "simple" (fx=? (fxbit-field 50 1 4) 1)))
(with-test-prefix "fxcopy-bit-field"
(pass-if "simple" (fx=? (fxcopy-bit-field 255 2 6 10) 235)))
(with-test-prefix "fxarithmetic-shift"
(pass-if "simple"
(and (fx=? (fxarithmetic-shift -6 -1) -3)
(fx=? (fxarithmetic-shift -5 -1) -3)
(fx=? (fxarithmetic-shift -4 -1) -2)
(fx=? (fxarithmetic-shift -3 -1) -2)
(fx=? (fxarithmetic-shift -2 -1) -1)
(fx=? (fxarithmetic-shift -1 -1) -1))))
(with-test-prefix "fxarithmetic-shift-left"
(pass-if "simple" (fx=? (fxarithmetic-shift-left -6 -1) -3)))
(with-test-prefix "fxarithmetic-shift-right"
(pass-if "simple" (fx=? (fxarithmetic-shift-right -6 1) -3)))
(with-test-prefix "fxrotate-bit-field"
(pass-if "simple" (fx=? (fxrotate-bit-field 227 2 6 2) 203)))
(with-test-prefix "fxreverse-bit-field"
(pass-if "simple" (fx=? (fxreverse-bit-field 82 1 4) 88)))
|