Partial impl. of Section 2.6, "Operations on IEEE-754 Numbers".
[guile-r6rs-libs.git] / tests / bytevector.test
blob0d676746e3e425d4c70caa1e661c7e8ef203cfcd
1 ;;; R6RS Byte Vectors.
2 ;;;
3 ;;; Copyright 2007  Ludovic Courtès <ludovic.courtes@laas.fr>
4 ;;;
5 ;;;
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 2 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; if not, write to the Free Software
18 ;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 (define-module (test-bytevector)
21   :use-module (test-suite lib)
22   :use-module (r6rs bytevector))
24 ;;; Some of the tests in here are examples taken from the R6RS Standard
25 ;;; Libraries document, version R5.92RS.
28 (with-test-prefix "2.1 General Operations"
30   (pass-if "native-endianness"
31     (not (not (memq (native-endianness) '(big little)))))
33   (pass-if "make-bytevector"
34     (and (bytevector? (make-bytevector 20))
35          (bytevector? (make-bytevector 20 3))))
37   (pass-if "bytevector-length"
38     (= (bytevector-length (make-bytevector 20)) 20))
40   (pass-if "bytevector=?"
41     (and (bytevector=? (make-bytevector 20 7)
42                        (make-bytevector 20 7))
43          (not (bytevector=? (make-bytevector 20 7)
44                             (make-bytevector 20 0))))))
47 (with-test-prefix "2.2 Operations on Bytes and Octets"
49   (pass-if "bytevector-{u8,s8}-ref"
50     (equal? '(-127 129 -1 255)
51             (let ((b1 (make-bytevector 16 -127))
52                   (b2 (make-bytevector 16 255)))
53               (list (bytevector-s8-ref b1 0)
54                     (bytevector-u8-ref b1 0)
55                     (bytevector-s8-ref b2 0)
56                     (bytevector-u8-ref b2 0)))))
58   (pass-if "bytevector-{u8,s8}-set!"
59     (equal? '(-126 130 -10 246)
60             (let ((b (make-bytevector 16 -127)))
62               (bytevector-s8-set! b 0 -126)
63               (bytevector-u8-set! b 1 246)
65               (list (bytevector-s8-ref b 0)
66                     (bytevector-u8-ref b 0)
67                     (bytevector-s8-ref b 1)
68                     (bytevector-u8-ref b 1)))))
70   (pass-if "bytevector->u8-list"
71     (let ((lst '(1 2 3 128 150 255)))
72       (equal? lst
73               (bytevector->u8-list
74                (let ((b (make-bytevector 6)))
75                  (for-each (lambda (i v)
76                              (bytevector-u8-set! b i v))
77                            (iota 6)
78                            lst)
79                  b)))))
81   (pass-if "u8-list->bytevector"
82     (let ((lst '(1 2 3 128 150 255)))
83       (equal? lst
84               (bytevector->u8-list (u8-list->bytevector lst)))))
86   (pass-if "bytevector-uint-{ref,set!} [small]"
87     (let ((b (make-bytevector 15)))
88       (bytevector-uint-set! b 0 #x1234
89                             (endianness little) 2)
90       (equal? (bytevector-uint-ref b 0 (endianness big) 2)
91               #x3412)))
93   (pass-if "bytevector-uint-set! [large]"
94     (let ((b (make-bytevector 16)))
95       (bytevector-uint-set! b 0 (- (expt 2 128) 3)
96                             (endianness little) 16)
97       (equal? (bytevector->u8-list b)
98               '(253 255 255 255 255 255 255 255
99                 255 255 255 255 255 255 255 255))))
101   (pass-if "bytevector-uint-{ref,set!} [large]"
102     (let ((b (make-bytevector 120)))
103       (bytevector-uint-set! b 0 (- (expt 2 128) 3)
104                             (endianness little) 16)
105       (equal? (bytevector-uint-ref b 0 (endianness little) 16)
106               #xfffffffffffffffffffffffffffffffd)))
108   (pass-if "bytevector-sint-ref [small]"
109     (let ((b (u8-list->bytevector '(#xff #xf0 #xff))))
110       (equal? (bytevector-sint-ref b 0 (endianness big) 2)
111               (bytevector-sint-ref b 1 (endianness little) 2)
112               -16)))
114   (pass-if "bytevector-sint-ref [large]"
115     (let ((b (make-bytevector 50)))
116       (bytevector-uint-set! b 0 (- (expt 2 128) 3)
117                             (endianness little) 16)
118       (equal? (bytevector-sint-ref b 0 (endianness little) 16)
119               -3))))
122 (with-test-prefix "2.3 Operations on Integers of Arbitrary Size"
124   (pass-if "bytevector->sint-list"
125     (let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
126       (equal? (bytevector->sint-list b (endianness little) 2)
127               '(513 -253 513 513))))
129   (pass-if "bytevector->uint-list"
130     (let ((b (u8-list->bytevector '(2 1 255 3 2 1 2 1))))
131       (equal? (bytevector->uint-list b (endianness big) 2)
132               '(513 65283 513 513))))
134   (pass-if "{sint,uint}-list->bytevector"
135     (let ((b1 (sint-list->bytevector '(513 -253 513 513)
136                                      (endianness little) 2))
137           (b2 (uint-list->bytevector '(513 65283 513 513)
138                                      (endianness little) 2))
139           (b3 (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
140       (and (bytevector=? b1 b2)
141            (bytevector=? b2 b3)))))
144 (with-test-prefix "2.4 Operations on 16-Bit Integers"
146   (pass-if "bytevector-u16-ref"
147     (let ((b (u8-list->bytevector
148               '(255 255 255 255 255 255 255 255
149                 255 255 255 255 255 255 255 253))))
150       (and (equal? (bytevector-u16-ref b 14 (endianness little))
151                    #xfdff)
152            (equal? (bytevector-u16-ref b 14 (endianness big))
153                    #xfffd))))
155   (pass-if "bytevector-s16-ref"
156     (let ((b (u8-list->bytevector
157               '(255 255 255 255 255 255 255 255
158                 255 255 255 255 255 255 255 253))))
159       (and (equal? (bytevector-s16-ref b 14 (endianness little))
160                    -513)
161            (equal? (bytevector-s16-ref b 14 (endianness big))
162                    -3))))
164   (pass-if "bytevector-{u16,s16}-ref"
165     (let ((b (make-bytevector 2)))
166       (bytevector-u16-set! b 0 44444 (endianness little))
167       (and (equal? (bytevector-u16-ref b 0 (endianness little))
168                    44444)
169            (equal? (bytevector-s16-ref b 0 (endianness little))
170                    (- 44444 65536)))))
172   (pass-if "bytevector-native-{u16,s16}-{ref,set!}"
173     (let ((b (make-bytevector 2)))
174       (bytevector-u16-native-set! b 0 44444)
175       (and (equal? (bytevector-u16-native-ref b 0)
176                    44444)
177            (equal? (bytevector-s16-native-ref b 0)
178                    (- 44444 65536))))))
181 (with-test-prefix "2.5 Operations on 32-bit Integers"
183   (pass-if "bytevector-u32-ref"
184     (let ((b (u8-list->bytevector
185               '(255 255 255 255 255 255 255 255
186                 255 255 255 255 255 255 255 253))))
187       (and (equal? (bytevector-u32-ref b 12 (endianness little))
188                    #xfdffffff)
189            (equal? (bytevector-u32-ref b 12 (endianness big))
190                    #xfffffffd))))
192   (pass-if "bytevector-s32-ref"
193     (let ((b (u8-list->bytevector
194               '(255 255 255 255 255 255 255 255
195                 255 255 255 255 255 255 255 253))))
196       (and (equal? (bytevector-s32-ref b 12 (endianness little))
197                    -33554433)
198            (equal? (bytevector-s32-ref b 12 (endianness big))
199                    -3))))
201   (pass-if "bytevector-{u32,s32}-ref"
202     (let ((b (make-bytevector 4)))
203       (bytevector-u32-set! b 0 2222222222 (endianness little))
204       (and (equal? (bytevector-u32-ref b 0 (endianness little))
205                    2222222222)
206            (equal? (bytevector-s32-ref b 0 (endianness little))
207                    (- 2222222222 (expt 2 32))))))
209   (pass-if "bytevector-{u32,s32}-native-{ref,set!}"
210     (let ((b (make-bytevector 4)))
211       (bytevector-u32-native-set! b 0 2222222222)
212       (and (equal? (bytevector-u32-native-ref b 0)
213                    2222222222)
214            (equal? (bytevector-s32-native-ref b 0)
215                    (- 2222222222 (expt 2 32)))))))
218 (with-test-prefix "2.5(bis) Operations on 64-bit Integers"
220   (pass-if "bytevector-u64-ref"
221     (let ((b (u8-list->bytevector
222               '(255 255 255 255 255 255 255 255
223                 255 255 255 255 255 255 255 253))))
224       (and (equal? (bytevector-u64-ref b 8 (endianness little))
225                    #xfdffffffffffffff)
226            (equal? (bytevector-u64-ref b 8 (endianness big))
227                    #xfffffffffffffffd))))
229   (pass-if "bytevector-s64-ref"
230     (let ((b (u8-list->bytevector
231               '(255 255 255 255 255 255 255 255
232                 255 255 255 255 255 255 255 253))))
233       (and (equal? (bytevector-s64-ref b 8 (endianness little))
234                    -144115188075855873)
235            (equal? (bytevector-s64-ref b 8 (endianness big))
236                    -3))))
238   (pass-if "bytevector-{u64,s64}-ref"
239     (let ((b (make-bytevector 8))
240           (big 9333333333333333333))
241       (bytevector-u64-set! b 0 big (endianness little))
242       (and (equal? (bytevector-u64-ref b 0 (endianness little))
243                    big)
244            (equal? (bytevector-s64-ref b 0 (endianness little))
245                    (- big (expt 2 64))))))
247   (pass-if "bytevector-{u64,s64}-native-{ref,set!}"
248     (let ((b (make-bytevector 8))
249           (big 9333333333333333333))
250       (bytevector-u64-native-set! b 0 big)
251       (and (equal? (bytevector-u64-native-ref b 0)
252                    big)
253            (equal? (bytevector-s64-native-ref b 0)
254                    (- big (expt 2 64)))))))
257 (with-test-prefix "2.6 Operations on IEEE-754 Numbers"
259   ;; FIXME: Non-native endianness not currently supported.
261   (pass-if "bytevector-ieee-double-native-{ref,set!}"
262     (let ((b (make-bytevector 8))
263           (number 3.14))
264       (bytevector-ieee-double-native-set! b 0 number)
265       (equal? (bytevector-ieee-double-native-ref b 0)
266               number))))
269 ;;; Local Variables:
270 ;;; coding: latin-1
271 ;;; mode: scheme
272 ;;; End:
274 ;;; arch-tag: 1f440f36-3002-4659-8bab-4aeafdc5398e