Section 2.4: Operations on 16-bit Integers.
[guile-r6rs-libs.git] / tests / bytevector.test
blob2f6c63484e4cbf90fbae89a1343b3bfbd5477cd9
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,s16}-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                    65023)
152            (equal? (bytevector-s16-ref b 14 (endianness little))
153                    -513)
154            (equal? (bytevector-u16-ref b 14 (endianness big))
155                    65533)
156            (equal? (bytevector-s16-ref b 14 (endianness big))
157                    -3))))
159   (pass-if "bytevector-{u16,s16}-ref"
160     (let ((b (make-bytevector 4)))
161       (bytevector-u16-set! b 0 44444 (endianness little))
162       (and (equal? (bytevector-u16-ref b 0 (endianness little))
163                    44444)
164            (equal? (bytevector-s16-ref b 0 (endianness little))
165                    (- 44444 65536)))))
167   (pass-if "bytevector-native-{u16,s16}-{ref,set!}"
168     (let ((b (make-bytevector 4)))
169       (bytevector-u16-native-set! b 0 44444)
170       (and (equal? (bytevector-u16-native-ref b 0)
171                    44444)
172            (equal? (bytevector-s16-native-ref b 0)
173                    (- 44444 65536))))))
176 ;;; Local Variables:
177 ;;; coding: latin-1
178 ;;; mode: scheme
179 ;;; End:
181 ;;; arch-tag: 1f440f36-3002-4659-8bab-4aeafdc5398e