| 1 |
;;;; bytevectors.test --- R6RS bytevectors. -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2009-2015, 2018, 2021, 2023 Free Software Foundation, Inc. |
| 4 |
;;;; |
| 5 |
;;;; Ludovic Courtès |
| 6 |
;;;; |
| 7 |
;;;; This library is free software; you can redistribute it and/or |
| 8 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 9 |
;;;; License as published by the Free Software Foundation; either |
| 10 |
;;;; version 3 of the License, or (at your option) any later version. |
| 11 |
;;;; |
| 12 |
;;;; This library is distributed in the hope that it will be useful, |
| 13 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
;;;; Lesser General Public License for more details. |
| 16 |
;;;; |
| 17 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 18 |
;;;; License along with this library; if not, write to the Free Software |
| 19 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
|
| 21 |
(define-module (test-bytevector) |
| 22 |
#:use-module (test-suite lib) |
| 23 |
#:use-module (system base compile) |
| 24 |
#:use-module (rnrs bytevectors) |
| 25 |
#:use-module (rnrs bytevectors gnu) |
| 26 |
#:use-module ((system foreign) #:select (sizeof size_t)) |
| 27 |
#:use-module (srfi srfi-1) |
| 28 |
#:use-module (srfi srfi-4)) |
| 29 |
|
| 30 |
(define exception:decoding-error |
| 31 |
(cons 'decoding-error "input (locale conversion|decoding) error")) |
| 32 |
|
| 33 |
;;; Some of the tests in here are examples taken from the R6RS Standard |
| 34 |
;;; Libraries document. |
| 35 |
|
| 36 |
|
| 37 |
(with-test-prefix/c&e "2.2 General Operations" |
| 38 |
|
| 39 |
(pass-if "native-endianness" |
| 40 |
(not (not (memq (native-endianness) '(big little))))) |
| 41 |
|
| 42 |
(pass-if "make-bytevector" |
| 43 |
(and (bytevector? (make-bytevector 20)) |
| 44 |
(bytevector? (make-bytevector 20 3)))) |
| 45 |
|
| 46 |
(pass-if "bytevector-length" |
| 47 |
(= (bytevector-length (make-bytevector 20)) 20)) |
| 48 |
|
| 49 |
(pass-if "bytevector=?" |
| 50 |
(and (bytevector=? (make-bytevector 20 7) |
| 51 |
(make-bytevector 20 7)) |
| 52 |
(not (bytevector=? (make-bytevector 20 7) |
| 53 |
(make-bytevector 20 0))))) |
| 54 |
|
| 55 |
;; This failed prior to Guile 2.0.12. |
| 56 |
;; See <http://bugs.gnu.org/19027>. |
| 57 |
(pass-if-equal "bytevector-fill! with fill 255" |
| 58 |
#vu8(255 255 255 255) |
| 59 |
(let ((bv (make-bytevector 4))) |
| 60 |
(bytevector-fill! bv 255) |
| 61 |
bv)) |
| 62 |
|
| 63 |
;; This is a Guile-specific extension. |
| 64 |
(pass-if-equal "bytevector-fill! with fill -128" |
| 65 |
#vu8(128 128 128 128) |
| 66 |
(let ((bv (make-bytevector 4))) |
| 67 |
(bytevector-fill! bv -128) |
| 68 |
bv)) |
| 69 |
|
| 70 |
;; This is a Guile-specific extension. |
| 71 |
(pass-if-equal "bytevector-fill! range arguments I" |
| 72 |
#vu8(0 0 1 1 1) |
| 73 |
(let ((bv (make-bytevector 5 0))) |
| 74 |
(bytevector-fill! bv 1 2) |
| 75 |
bv)) |
| 76 |
|
| 77 |
;; This is a Guile-specific extension. |
| 78 |
(pass-if-equal "bytevector-fill! range arguments II" |
| 79 |
#vu8(0 0 1 1 0) |
| 80 |
(let ((bv (make-bytevector 5 0))) |
| 81 |
(bytevector-fill! bv 1 2 4) |
| 82 |
bv)) |
| 83 |
|
| 84 |
(pass-if "bytevector-copy! overlapping" |
| 85 |
;; See <http://debbugs.gnu.org/10070>. |
| 86 |
(let ((b (u8-list->bytevector '(1 2 3 4 5 6 7 8)))) |
| 87 |
(bytevector-copy! b 0 b 3 4) |
| 88 |
(bytevector->u8-list b) |
| 89 |
(bytevector=? b #vu8(1 2 3 1 2 3 4 8))))) |
| 90 |
|
| 91 |
|
| 92 |
(with-test-prefix/c&e "2.3 Operations on Bytes and Octets" |
| 93 |
|
| 94 |
(pass-if "bytevector-{u8,s8}-ref" |
| 95 |
(equal? '(-127 129 -1 255) |
| 96 |
(let ((b1 (make-bytevector 16 -127)) |
| 97 |
(b2 (make-bytevector 16 255))) |
| 98 |
(list (bytevector-s8-ref b1 0) |
| 99 |
(bytevector-u8-ref b1 0) |
| 100 |
(bytevector-s8-ref b2 0) |
| 101 |
(bytevector-u8-ref b2 0))))) |
| 102 |
|
| 103 |
(pass-if "bytevector-{u8,s8}-set!" |
| 104 |
(equal? '(-126 130 -10 246) |
| 105 |
(let ((b (make-bytevector 16 -127))) |
| 106 |
|
| 107 |
(bytevector-s8-set! b 0 -126) |
| 108 |
(bytevector-u8-set! b 1 246) |
| 109 |
|
| 110 |
(list (bytevector-s8-ref b 0) |
| 111 |
(bytevector-u8-ref b 0) |
| 112 |
(bytevector-s8-ref b 1) |
| 113 |
(bytevector-u8-ref b 1))))) |
| 114 |
|
| 115 |
(pass-if "bytevector->u8-list" |
| 116 |
(let ((lst '(1 2 3 128 150 255))) |
| 117 |
(equal? lst |
| 118 |
(bytevector->u8-list |
| 119 |
(let ((b (make-bytevector 6))) |
| 120 |
(for-each (lambda (i v) |
| 121 |
(bytevector-u8-set! b i v)) |
| 122 |
(iota 6) |
| 123 |
lst) |
| 124 |
b))))) |
| 125 |
|
| 126 |
(pass-if "u8-list->bytevector" |
| 127 |
(let ((lst '(1 2 3 128 150 255))) |
| 128 |
(equal? lst |
| 129 |
(bytevector->u8-list (u8-list->bytevector lst))))) |
| 130 |
|
| 131 |
(pass-if-exception "u8-list->bytevector [invalid argument type]" |
| 132 |
exception:wrong-type-arg |
| 133 |
(u8-list->bytevector 'not-a-list)) |
| 134 |
|
| 135 |
(pass-if-exception "u8-list->bytevector [circular list]" |
| 136 |
exception:wrong-type-arg |
| 137 |
(u8-list->bytevector (circular-list 1 2 3))) |
| 138 |
|
| 139 |
(pass-if "bytevector-uint-{ref,set!} [small]" |
| 140 |
(let ((b (make-bytevector 15))) |
| 141 |
(bytevector-uint-set! b 0 #x1234 |
| 142 |
(endianness little) 2) |
| 143 |
(equal? (bytevector-uint-ref b 0 (endianness big) 2) |
| 144 |
#x3412))) |
| 145 |
|
| 146 |
(pass-if "bytevector-uint-set! [large]" |
| 147 |
(let ((b (make-bytevector 16))) |
| 148 |
(bytevector-uint-set! b 0 (- (expt 2 128) 3) |
| 149 |
(endianness little) 16) |
| 150 |
(equal? (bytevector->u8-list b) |
| 151 |
'(253 255 255 255 255 255 255 255 |
| 152 |
255 255 255 255 255 255 255 255)))) |
| 153 |
|
| 154 |
(pass-if "bytevector-uint-{ref,set!} [large]" |
| 155 |
(let ((b (make-bytevector 120))) |
| 156 |
(bytevector-uint-set! b 0 (- (expt 2 128) 3) |
| 157 |
(endianness little) 16) |
| 158 |
(equal? (bytevector-uint-ref b 0 (endianness little) 16) |
| 159 |
#xfffffffffffffffffffffffffffffffd))) |
| 160 |
|
| 161 |
(pass-if "bytevector-sint-ref [small]" |
| 162 |
(let ((b (u8-list->bytevector '(#xff #xf0 #xff)))) |
| 163 |
(equal? (bytevector-sint-ref b 0 (endianness big) 2) |
| 164 |
(bytevector-sint-ref b 1 (endianness little) 2) |
| 165 |
-16))) |
| 166 |
|
| 167 |
(pass-if "bytevector-sint-ref [large]" |
| 168 |
(let ((b (make-bytevector 50))) |
| 169 |
(bytevector-uint-set! b 0 (- (expt 2 128) 3) |
| 170 |
(endianness little) 16) |
| 171 |
(equal? (bytevector-sint-ref b 0 (endianness little) 16) |
| 172 |
-3))) |
| 173 |
|
| 174 |
(pass-if "bytevector-sint-set! [small]" |
| 175 |
(let ((b (make-bytevector 3))) |
| 176 |
(bytevector-sint-set! b 0 -16 (endianness big) 2) |
| 177 |
(bytevector-sint-set! b 1 -16 (endianness little) 2) |
| 178 |
(equal? (bytevector->u8-list b) |
| 179 |
'(#xff #xf0 #xff)))) |
| 180 |
|
| 181 |
(pass-if "equal?" |
| 182 |
(let ((bv1 (u8-list->bytevector (iota 123))) |
| 183 |
(bv2 (u8-list->bytevector (iota 123)))) |
| 184 |
(equal? bv1 bv2)))) |
| 185 |
|
| 186 |
|
| 187 |
(with-test-prefix/c&e "2.4 Operations on Integers of Arbitrary Size" |
| 188 |
|
| 189 |
(pass-if "bytevector->sint-list" |
| 190 |
(let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2)))) |
| 191 |
(equal? (bytevector->sint-list b (endianness little) 2) |
| 192 |
'(513 -253 513 513)))) |
| 193 |
|
| 194 |
(pass-if "bytevector->uint-list" |
| 195 |
(let ((b (u8-list->bytevector '(2 1 255 3 2 1 2 1)))) |
| 196 |
(equal? (bytevector->uint-list b (endianness big) 2) |
| 197 |
'(513 65283 513 513)))) |
| 198 |
|
| 199 |
(pass-if "bytevector->uint-list [empty]" |
| 200 |
(let ((b (make-bytevector 0))) |
| 201 |
(null? (bytevector->uint-list b (endianness big) 2)))) |
| 202 |
|
| 203 |
(pass-if-exception "bytevector->sint-list [out-of-range]" |
| 204 |
exception:out-of-range |
| 205 |
(bytevector->sint-list (make-bytevector 6) (endianness little) -1)) |
| 206 |
|
| 207 |
(pass-if-exception "bytevector->uint-list [out-of-range]" |
| 208 |
exception:out-of-range |
| 209 |
(bytevector->uint-list (make-bytevector 6) (endianness little) 0)) |
| 210 |
|
| 211 |
(pass-if-exception "bytevector->uint-list [word size doesn't divide length]" |
| 212 |
exception:wrong-type-arg |
| 213 |
(bytevector->uint-list (make-bytevector 6) (endianness little) 4)) |
| 214 |
|
| 215 |
(pass-if "{sint,uint}-list->bytevector" |
| 216 |
(let ((b1 (sint-list->bytevector '(513 -253 513 513) |
| 217 |
(endianness little) 2)) |
| 218 |
(b2 (uint-list->bytevector '(513 65283 513 513) |
| 219 |
(endianness little) 2)) |
| 220 |
(b3 (u8-list->bytevector '(1 2 3 255 1 2 1 2)))) |
| 221 |
(and (bytevector=? b1 b2) |
| 222 |
(bytevector=? b2 b3)))) |
| 223 |
|
| 224 |
(pass-if "sint-list->bytevector [limits]" |
| 225 |
(bytevector=? (sint-list->bytevector '(-32768 32767) |
| 226 |
(endianness big) 2) |
| 227 |
(let ((bv (make-bytevector 4))) |
| 228 |
(bytevector-u8-set! bv 0 #x80) |
| 229 |
(bytevector-u8-set! bv 1 #x00) |
| 230 |
(bytevector-u8-set! bv 2 #x7f) |
| 231 |
(bytevector-u8-set! bv 3 #xff) |
| 232 |
bv))) |
| 233 |
|
| 234 |
(pass-if-exception "sint-list->bytevector [invalid argument type]" |
| 235 |
exception:wrong-type-arg |
| 236 |
(sint-list->bytevector 'not-a-list (endianness big) 2)) |
| 237 |
|
| 238 |
(pass-if-exception "uint-list->bytevector [invalid argument type]" |
| 239 |
exception:wrong-type-arg |
| 240 |
(uint-list->bytevector 'not-a-list (endianness big) 2)) |
| 241 |
|
| 242 |
(pass-if-exception "sint-list->bytevector [circular list]" |
| 243 |
exception:wrong-type-arg |
| 244 |
(sint-list->bytevector (circular-list 1 2 3) (endianness big) |
| 245 |
2)) |
| 246 |
|
| 247 |
(pass-if-exception "uint-list->bytevector [circular list]" |
| 248 |
exception:wrong-type-arg |
| 249 |
(uint-list->bytevector (circular-list 1 2 3) (endianness big) |
| 250 |
2)) |
| 251 |
|
| 252 |
(pass-if-exception "sint-list->bytevector [out-of-range]" |
| 253 |
exception:out-of-range |
| 254 |
(sint-list->bytevector (list 0 0 (expt 2 16)) (endianness big) |
| 255 |
2)) |
| 256 |
|
| 257 |
(pass-if-exception "uint-list->bytevector [out-of-range]" |
| 258 |
exception:out-of-range |
| 259 |
(uint-list->bytevector '(0 -1) (endianness big) 2))) |
| 260 |
|
| 261 |
|
| 262 |
(with-test-prefix/c&e "2.5 Operations on 16-Bit Integers" |
| 263 |
|
| 264 |
(pass-if "bytevector-u16-ref" |
| 265 |
(let ((b (u8-list->bytevector |
| 266 |
'(255 255 255 255 255 255 255 255 |
| 267 |
255 255 255 255 255 255 255 253)))) |
| 268 |
(and (equal? (bytevector-u16-ref b 14 (endianness little)) |
| 269 |
#xfdff) |
| 270 |
(equal? (bytevector-u16-ref b 14 (endianness big)) |
| 271 |
#xfffd)))) |
| 272 |
|
| 273 |
(pass-if "bytevector-s16-ref" |
| 274 |
(let ((b (u8-list->bytevector |
| 275 |
'(255 255 255 255 255 255 255 255 |
| 276 |
255 255 255 255 255 255 255 253)))) |
| 277 |
(and (equal? (bytevector-s16-ref b 14 (endianness little)) |
| 278 |
-513) |
| 279 |
(equal? (bytevector-s16-ref b 14 (endianness big)) |
| 280 |
-3)))) |
| 281 |
|
| 282 |
(pass-if "bytevector-s16-ref [unaligned]" |
| 283 |
(let ((b (u8-list->bytevector '(#xff #xf0 #xff)))) |
| 284 |
(equal? (bytevector-s16-ref b 1 (endianness little)) |
| 285 |
-16))) |
| 286 |
|
| 287 |
(pass-if "bytevector-{u16,s16}-ref" |
| 288 |
(let ((b (make-bytevector 2))) |
| 289 |
(bytevector-u16-set! b 0 44444 (endianness little)) |
| 290 |
(and (equal? (bytevector-u16-ref b 0 (endianness little)) |
| 291 |
44444) |
| 292 |
(equal? (bytevector-s16-ref b 0 (endianness little)) |
| 293 |
(- 44444 65536))))) |
| 294 |
|
| 295 |
(pass-if "bytevector-native-{u16,s16}-{ref,set!}" |
| 296 |
(let ((b (make-bytevector 2))) |
| 297 |
(bytevector-u16-native-set! b 0 44444) |
| 298 |
(and (equal? (bytevector-u16-native-ref b 0) |
| 299 |
44444) |
| 300 |
(equal? (bytevector-s16-native-ref b 0) |
| 301 |
(- 44444 65536))))) |
| 302 |
|
| 303 |
(pass-if "bytevector-s16-{ref,set!} [unaligned]" |
| 304 |
(let ((b (make-bytevector 3))) |
| 305 |
(bytevector-s16-set! b 1 -77 (endianness little)) |
| 306 |
(equal? (bytevector-s16-ref b 1 (endianness little)) |
| 307 |
-77)))) |
| 308 |
|
| 309 |
|
| 310 |
(with-test-prefix/c&e "2.6 Operations on 32-bit Integers" |
| 311 |
|
| 312 |
(pass-if "bytevector-u32-ref" |
| 313 |
(let ((b (u8-list->bytevector |
| 314 |
'(255 255 255 255 255 255 255 255 |
| 315 |
255 255 255 255 255 255 255 253)))) |
| 316 |
(and (equal? (bytevector-u32-ref b 12 (endianness little)) |
| 317 |
#xfdffffff) |
| 318 |
(equal? (bytevector-u32-ref b 12 (endianness big)) |
| 319 |
#xfffffffd)))) |
| 320 |
|
| 321 |
(pass-if "bytevector-s32-ref" |
| 322 |
(let ((b (u8-list->bytevector |
| 323 |
'(255 255 255 255 255 255 255 255 |
| 324 |
255 255 255 255 255 255 255 253)))) |
| 325 |
(and (equal? (bytevector-s32-ref b 12 (endianness little)) |
| 326 |
-33554433) |
| 327 |
(equal? (bytevector-s32-ref b 12 (endianness big)) |
| 328 |
-3)))) |
| 329 |
|
| 330 |
(pass-if "bytevector-{u32,s32}-ref" |
| 331 |
(let ((b (make-bytevector 4))) |
| 332 |
(bytevector-u32-set! b 0 2222222222 (endianness little)) |
| 333 |
(and (equal? (bytevector-u32-ref b 0 (endianness little)) |
| 334 |
2222222222) |
| 335 |
(equal? (bytevector-s32-ref b 0 (endianness little)) |
| 336 |
(- 2222222222 (expt 2 32)))))) |
| 337 |
|
| 338 |
(pass-if "bytevector-{u32,s32}-native-{ref,set!}" |
| 339 |
(let ((b (make-bytevector 4))) |
| 340 |
(bytevector-u32-native-set! b 0 2222222222) |
| 341 |
(and (equal? (bytevector-u32-native-ref b 0) |
| 342 |
2222222222) |
| 343 |
(equal? (bytevector-s32-native-ref b 0) |
| 344 |
(- 2222222222 (expt 2 32))))))) |
| 345 |
|
| 346 |
|
| 347 |
(with-test-prefix/c&e "2.7 Operations on 64-bit Integers" |
| 348 |
|
| 349 |
(pass-if "bytevector-u64-ref" |
| 350 |
(let ((b (u8-list->bytevector |
| 351 |
'(255 255 255 255 255 255 255 255 |
| 352 |
255 255 255 255 255 255 255 253)))) |
| 353 |
(and (equal? (bytevector-u64-ref b 8 (endianness little)) |
| 354 |
#xfdffffffffffffff) |
| 355 |
(equal? (bytevector-u64-ref b 8 (endianness big)) |
| 356 |
#xfffffffffffffffd)))) |
| 357 |
|
| 358 |
(pass-if "bytevector-s64-ref" |
| 359 |
(let ((b (u8-list->bytevector |
| 360 |
'(255 255 255 255 255 255 255 255 |
| 361 |
255 255 255 255 255 255 255 253)))) |
| 362 |
(and (equal? (bytevector-s64-ref b 8 (endianness little)) |
| 363 |
-144115188075855873) |
| 364 |
(equal? (bytevector-s64-ref b 8 (endianness big)) |
| 365 |
-3)))) |
| 366 |
|
| 367 |
(pass-if "bytevector-{u64,s64}-ref" |
| 368 |
(let ((b (make-bytevector 8)) |
| 369 |
(big 9333333333333333333)) |
| 370 |
(bytevector-u64-set! b 0 big (endianness little)) |
| 371 |
(and (equal? (bytevector-u64-ref b 0 (endianness little)) |
| 372 |
big) |
| 373 |
(equal? (bytevector-s64-ref b 0 (endianness little)) |
| 374 |
(- big (expt 2 64)))))) |
| 375 |
|
| 376 |
(pass-if "bytevector-{u64,s64}-native-{ref,set!}" |
| 377 |
(let ((b (make-bytevector 8)) |
| 378 |
(big 9333333333333333333)) |
| 379 |
(bytevector-u64-native-set! b 0 big) |
| 380 |
(and (equal? (bytevector-u64-native-ref b 0) |
| 381 |
big) |
| 382 |
(equal? (bytevector-s64-native-ref b 0) |
| 383 |
(- big (expt 2 64)))))) |
| 384 |
|
| 385 |
(pass-if "ref/set! with zero" |
| 386 |
(let ((b (make-bytevector 8))) |
| 387 |
(bytevector-s64-set! b 0 -1 (endianness big)) |
| 388 |
(bytevector-u64-set! b 0 0 (endianness big)) |
| 389 |
(= 0 (bytevector-u64-ref b 0 (endianness big))))) |
| 390 |
|
| 391 |
(pass-if-exception "bignum out of range" |
| 392 |
exception:out-of-range |
| 393 |
(bytevector-u64-set! (make-bytevector 8) 0 (expt 2 64) (endianness big)))) |
| 394 |
|
| 395 |
|
| 396 |
(with-test-prefix/c&e "2.8 Operations on IEEE-754 Representations" |
| 397 |
|
| 398 |
(pass-if "single, little endian" |
| 399 |
;; http://bugs.gnu.org/11310 |
| 400 |
(let ((b (make-bytevector 4))) |
| 401 |
(bytevector-ieee-single-set! b 0 1.0 (endianness little)) |
| 402 |
(equal? #vu8(0 0 128 63) b))) |
| 403 |
|
| 404 |
(pass-if "single, big endian" |
| 405 |
;; http://bugs.gnu.org/11310 |
| 406 |
(let ((b (make-bytevector 4))) |
| 407 |
(bytevector-ieee-single-set! b 0 1.0 (endianness big)) |
| 408 |
(equal? #vu8(63 128 0 0) b))) |
| 409 |
|
| 410 |
(pass-if "bytevector-ieee-single-native-{ref,set!}" |
| 411 |
(let ((b (make-bytevector 4)) |
| 412 |
(number 3.00)) |
| 413 |
(bytevector-ieee-single-native-set! b 0 number) |
| 414 |
(equal? (bytevector-ieee-single-native-ref b 0) |
| 415 |
number))) |
| 416 |
|
| 417 |
(pass-if "bytevector-ieee-single-{ref,set!}" |
| 418 |
(let ((b (make-bytevector 8)) |
| 419 |
(number 3.14)) |
| 420 |
(bytevector-ieee-single-set! b 0 number (endianness little)) |
| 421 |
(bytevector-ieee-single-set! b 4 number (endianness big)) |
| 422 |
(equal? (bytevector-ieee-single-ref b 0 (endianness little)) |
| 423 |
(bytevector-ieee-single-ref b 4 (endianness big))))) |
| 424 |
|
| 425 |
(pass-if "bytevector-ieee-single-{ref,set!} [unaligned]" |
| 426 |
(let ((b (make-bytevector 9)) |
| 427 |
(number 3.14)) |
| 428 |
(bytevector-ieee-single-set! b 1 number (endianness little)) |
| 429 |
(bytevector-ieee-single-set! b 5 number (endianness big)) |
| 430 |
(equal? (bytevector-ieee-single-ref b 1 (endianness little)) |
| 431 |
(bytevector-ieee-single-ref b 5 (endianness big))))) |
| 432 |
|
| 433 |
(pass-if "double, little endian" |
| 434 |
;; http://bugs.gnu.org/11310 |
| 435 |
(let ((b (make-bytevector 8))) |
| 436 |
(bytevector-ieee-double-set! b 0 1.0 (endianness little)) |
| 437 |
(equal? #vu8(0 0 0 0 0 0 240 63) b))) |
| 438 |
|
| 439 |
(pass-if "double, big endian" |
| 440 |
;; http://bugs.gnu.org/11310 |
| 441 |
(let ((b (make-bytevector 8))) |
| 442 |
(bytevector-ieee-double-set! b 0 1.0 (endianness big)) |
| 443 |
(equal? #vu8(63 240 0 0 0 0 0 0) b))) |
| 444 |
|
| 445 |
(pass-if "bytevector-ieee-double-native-{ref,set!}" |
| 446 |
(let ((b (make-bytevector 8)) |
| 447 |
(number 3.14)) |
| 448 |
(bytevector-ieee-double-native-set! b 0 number) |
| 449 |
(equal? (bytevector-ieee-double-native-ref b 0) |
| 450 |
number))) |
| 451 |
|
| 452 |
(pass-if "bytevector-ieee-double-{ref,set!}" |
| 453 |
(let ((b (make-bytevector 16)) |
| 454 |
(number 3.14)) |
| 455 |
(bytevector-ieee-double-set! b 0 number (endianness little)) |
| 456 |
(bytevector-ieee-double-set! b 8 number (endianness big)) |
| 457 |
(equal? (bytevector-ieee-double-ref b 0 (endianness little)) |
| 458 |
(bytevector-ieee-double-ref b 8 (endianness big)))))) |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
;; Default to the C locale for the following tests. |
| 463 |
(when (defined? 'setlocale) |
| 464 |
(setlocale LC_ALL "C")) |
| 465 |
|
| 466 |
|
| 467 |
(with-test-prefix "2.9 Operations on Strings" |
| 468 |
|
| 469 |
(pass-if "string->utf8" |
| 470 |
(let* ((str "hello, world") |
| 471 |
(utf8 (string->utf8 str))) |
| 472 |
(and (bytevector? utf8) |
| 473 |
(= (bytevector-length utf8) |
| 474 |
(string-length str)) |
| 475 |
(equal? (string->list str) |
| 476 |
(map integer->char (bytevector->u8-list utf8)))))) |
| 477 |
|
| 478 |
(pass-if "string->utf8 [latin-1]" |
| 479 |
(let* ((str "hé, ça va bien ?") |
| 480 |
(utf8 (string->utf8 str))) |
| 481 |
(and (bytevector? utf8) |
| 482 |
(= (bytevector-length utf8) |
| 483 |
(+ 2 (string-length str)))))) |
| 484 |
|
| 485 |
(pass-if "string->utf16" |
| 486 |
(let* ((str "hello, world") |
| 487 |
(utf16 (string->utf16 str))) |
| 488 |
(and (bytevector? utf16) |
| 489 |
(= (bytevector-length utf16) |
| 490 |
(* 2 (string-length str))) |
| 491 |
(equal? (string->list str) |
| 492 |
(map integer->char |
| 493 |
(bytevector->uint-list utf16 |
| 494 |
(endianness big) 2)))))) |
| 495 |
|
| 496 |
(pass-if "string->utf16 [little]" |
| 497 |
(let* ((str "hello, world") |
| 498 |
(utf16 (string->utf16 str (endianness little)))) |
| 499 |
(and (bytevector? utf16) |
| 500 |
(= (bytevector-length utf16) |
| 501 |
(* 2 (string-length str))) |
| 502 |
(equal? (string->list str) |
| 503 |
(map integer->char |
| 504 |
(bytevector->uint-list utf16 |
| 505 |
(endianness little) 2)))))) |
| 506 |
|
| 507 |
|
| 508 |
(pass-if "string->utf32" |
| 509 |
(let* ((str "hello, world") |
| 510 |
(utf32 (string->utf32 str))) |
| 511 |
(and (bytevector? utf32) |
| 512 |
(= (bytevector-length utf32) |
| 513 |
(* 4 (string-length str))) |
| 514 |
(equal? (string->list str) |
| 515 |
(map integer->char |
| 516 |
(bytevector->uint-list utf32 |
| 517 |
(endianness big) 4)))))) |
| 518 |
|
| 519 |
(pass-if "string->utf32 [Greek]" |
| 520 |
(let* ((str "Ἄνεμοι") |
| 521 |
(utf32 (string->utf32 str))) |
| 522 |
(and (bytevector? utf32) |
| 523 |
(equal? (bytevector->uint-list utf32 (endianness big) 4) |
| 524 |
'(#x1f0c #x3bd #x3b5 #x3bc #x3bf #x3b9))))) |
| 525 |
|
| 526 |
(pass-if "string->utf32 [little]" |
| 527 |
(let* ((str "hello, world") |
| 528 |
(utf32 (string->utf32 str (endianness little)))) |
| 529 |
(and (bytevector? utf32) |
| 530 |
(= (bytevector-length utf32) |
| 531 |
(* 4 (string-length str))) |
| 532 |
(equal? (string->list str) |
| 533 |
(map integer->char |
| 534 |
(bytevector->uint-list utf32 |
| 535 |
(endianness little) 4)))))) |
| 536 |
|
| 537 |
(pass-if "utf8->string" |
| 538 |
(let* ((utf8 (u8-list->bytevector (map char->integer |
| 539 |
(string->list "hello, world")))) |
| 540 |
(str (utf8->string utf8))) |
| 541 |
(and (string? str) |
| 542 |
(= (string-length str) |
| 543 |
(bytevector-length utf8)) |
| 544 |
(equal? (string->list str) |
| 545 |
(map integer->char (bytevector->u8-list utf8)))))) |
| 546 |
|
| 547 |
(pass-if "utf8->string [latin-1]" |
| 548 |
(let* ((utf8 (string->utf8 "hé, ça va bien ?")) |
| 549 |
(str (utf8->string utf8))) |
| 550 |
(and (string? str) |
| 551 |
(= (string-length str) |
| 552 |
(- (bytevector-length utf8) 2))))) |
| 553 |
|
| 554 |
(pass-if-equal "utf8->string [replacement character]" |
| 555 |
'(104 105 65533) |
| 556 |
(map char->integer |
| 557 |
(string->list (utf8->string #vu8(104 105 239 191 189))))) |
| 558 |
|
| 559 |
(pass-if-exception "utf8->string [invalid encoding]" |
| 560 |
exception:decoding-error |
| 561 |
(utf8->string #vu8(104 105 239 191 50))) |
| 562 |
|
| 563 |
(pass-if "utf16->string" |
| 564 |
(let* ((utf16 (uint-list->bytevector (map char->integer |
| 565 |
(string->list "hello, world")) |
| 566 |
(endianness big) 2)) |
| 567 |
(str (utf16->string utf16))) |
| 568 |
(and (string? str) |
| 569 |
(= (* 2 (string-length str)) |
| 570 |
(bytevector-length utf16)) |
| 571 |
(equal? (string->list str) |
| 572 |
(map integer->char |
| 573 |
(bytevector->uint-list utf16 (endianness big) |
| 574 |
2)))))) |
| 575 |
|
| 576 |
(pass-if "utf16->string [little]" |
| 577 |
(let* ((utf16 (uint-list->bytevector (map char->integer |
| 578 |
(string->list "hello, world")) |
| 579 |
(endianness little) 2)) |
| 580 |
(str (utf16->string utf16 (endianness little)))) |
| 581 |
(and (string? str) |
| 582 |
(= (* 2 (string-length str)) |
| 583 |
(bytevector-length utf16)) |
| 584 |
(equal? (string->list str) |
| 585 |
(map integer->char |
| 586 |
(bytevector->uint-list utf16 (endianness little) |
| 587 |
2)))))) |
| 588 |
(pass-if "utf32->string" |
| 589 |
(let* ((utf32 (uint-list->bytevector (map char->integer |
| 590 |
(string->list "hello, world")) |
| 591 |
(endianness big) 4)) |
| 592 |
(str (utf32->string utf32))) |
| 593 |
(and (string? str) |
| 594 |
(= (* 4 (string-length str)) |
| 595 |
(bytevector-length utf32)) |
| 596 |
(equal? (string->list str) |
| 597 |
(map integer->char |
| 598 |
(bytevector->uint-list utf32 (endianness big) |
| 599 |
4)))))) |
| 600 |
|
| 601 |
(pass-if "utf32->string [little]" |
| 602 |
(let* ((utf32 (uint-list->bytevector (map char->integer |
| 603 |
(string->list "hello, world")) |
| 604 |
(endianness little) 4)) |
| 605 |
(str (utf32->string utf32 (endianness little)))) |
| 606 |
(and (string? str) |
| 607 |
(= (* 4 (string-length str)) |
| 608 |
(bytevector-length utf32)) |
| 609 |
(equal? (string->list str) |
| 610 |
(map integer->char |
| 611 |
(bytevector->uint-list utf32 (endianness little) |
| 612 |
4))))))) |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
(with-test-prefix "Datum Syntax" |
| 617 |
|
| 618 |
(pass-if "empty" |
| 619 |
(equal? (with-input-from-string "#vu8()" read) |
| 620 |
(make-bytevector 0))) |
| 621 |
|
| 622 |
(pass-if "simple" |
| 623 |
(equal? (with-input-from-string "#vu8(1 2 3 4 5)" read) |
| 624 |
(u8-list->bytevector '(1 2 3 4 5)))) |
| 625 |
|
| 626 |
(pass-if ">127" |
| 627 |
(equal? (with-input-from-string "#vu8(0 255 127 128)" read) |
| 628 |
(u8-list->bytevector '(0 255 127 128)))) |
| 629 |
|
| 630 |
(pass-if "self-evaluating?" |
| 631 |
(self-evaluating? (make-bytevector 1))) |
| 632 |
|
| 633 |
(pass-if "self-evaluating" |
| 634 |
(equal? (eval (with-input-from-string "#vu8(1 2 3 4 5)" read) |
| 635 |
(current-module)) |
| 636 |
(u8-list->bytevector '(1 2 3 4 5)))) |
| 637 |
|
| 638 |
(pass-if "quoted" |
| 639 |
(equal? (eval (with-input-from-string "'#vu8(1 2 3 4 5)" read) |
| 640 |
(current-module)) |
| 641 |
(u8-list->bytevector '(1 2 3 4 5)))) |
| 642 |
|
| 643 |
(pass-if "literal simple" |
| 644 |
(equal? #vu8(1 2 3 4 5) |
| 645 |
(u8-list->bytevector '(1 2 3 4 5)))) |
| 646 |
|
| 647 |
(pass-if "literal >127" |
| 648 |
(equal? #vu8(0 255 127 128) |
| 649 |
(u8-list->bytevector '(0 255 127 128)))) |
| 650 |
|
| 651 |
(pass-if "literal quoted" |
| 652 |
(equal? '#vu8(1 2 3 4 5) |
| 653 |
(u8-list->bytevector '(1 2 3 4 5)))) |
| 654 |
|
| 655 |
(pass-if-exception "incorrect prefix" |
| 656 |
exception:read-error |
| 657 |
(with-input-from-string "#vi8(1 2 3)" read)) |
| 658 |
|
| 659 |
(pass-if-exception "extraneous space" |
| 660 |
exception:read-error |
| 661 |
(with-input-from-string "#vu8 (1 2 3)" read)) |
| 662 |
|
| 663 |
(pass-if-exception "negative integers" |
| 664 |
exception:out-of-range |
| 665 |
(with-input-from-string "#vu8(-1 -2 -3)" read)) |
| 666 |
|
| 667 |
(pass-if-exception "out-of-range integers" |
| 668 |
exception:out-of-range |
| 669 |
(with-input-from-string "#vu8(0 256)" read))) |
| 670 |
|
| 671 |
|
| 672 |
(with-test-prefix "bytevector-slice" |
| 673 |
|
| 674 |
(pass-if-exception "wrong size" |
| 675 |
exception:out-of-range |
| 676 |
(let ((b #vu8(1 2 3))) |
| 677 |
(bytevector-slice b 1 3))) |
| 678 |
|
| 679 |
(pass-if-equal "slices" |
| 680 |
(list #vu8(1 2) #vu8(2 3) |
| 681 |
#vu8(1) #vu8(2) #vu8(3)) |
| 682 |
(let ((b #vu8(1 2 3))) |
| 683 |
(list (bytevector-slice b 0 2) |
| 684 |
(bytevector-slice b 1) |
| 685 |
(bytevector-slice b 0 1) |
| 686 |
(bytevector-slice b 1 1) |
| 687 |
(bytevector-slice b 2)))) |
| 688 |
|
| 689 |
(pass-if-exception "immutable flag preserved" |
| 690 |
exception:wrong-type-arg |
| 691 |
(compile '(begin |
| 692 |
(use-modules (rnrs bytevectors) |
| 693 |
(rnrs bytevectors gnu)) |
| 694 |
|
| 695 |
;; The literal bytevector below is immutable. |
| 696 |
(let ((bv #vu8(1 2 3))) |
| 697 |
(bytevector-u8-set! (bytevector-slice bv 1) 0 0))) |
| 698 |
|
| 699 |
;; Disable optimizations to invoke the full-blown |
| 700 |
;; 'scm_bytevector_u8_set_x' procedure, which checks for |
| 701 |
;; the SCM_F_BYTEVECTOR_IMMUTABLE flag. |
| 702 |
#:optimization-level 0 |
| 703 |
#:to 'value)) |
| 704 |
|
| 705 |
(pass-if-exception "size + offset overflows" |
| 706 |
exception:out-of-range |
| 707 |
(let ((size_t-max (expt 2 (* 8 (sizeof size_t))))) |
| 708 |
;; Without overflow checks, this would read arbitrary memory. |
| 709 |
(bytevector-slice #vu8(1 2 3) (- size_t-max 10) 10))) |
| 710 |
|
| 711 |
(pass-if-equal "slice of f32vector" |
| 712 |
'(8 2) |
| 713 |
(let* ((v #f32(1.1 1.2 3.14)) |
| 714 |
(s (bytevector-slice v 4))) |
| 715 |
(and (= (f32vector-ref s 0) |
| 716 |
(f32vector-ref v 1)) |
| 717 |
(list (bytevector-length s) |
| 718 |
(f32vector-length s))))) |
| 719 |
|
| 720 |
(pass-if-equal "unaligned offset for f32vector" |
| 721 |
10 |
| 722 |
(let* ((v #f32(1.1 1.2 3.14)) |
| 723 |
(s (bytevector-slice v 2))) |
| 724 |
(and (not (f32vector? s)) |
| 725 |
(bytevector-length s)))) |
| 726 |
|
| 727 |
(pass-if-equal "unaligned size for f32vector" |
| 728 |
1 |
| 729 |
(let* ((v #f32(1.1 1.2 3.14)) |
| 730 |
(s (bytevector-slice v 0 1))) |
| 731 |
(and (not (f32vector? s)) |
| 732 |
(bytevector-length s))))) |
| 733 |
|
| 734 |
|
| 735 |
(with-test-prefix "Arrays" |
| 736 |
|
| 737 |
(pass-if "array?" |
| 738 |
(array? #vu8(1 2 3))) |
| 739 |
|
| 740 |
(pass-if "array-length" |
| 741 |
(equal? (iota 16) |
| 742 |
(map array-length |
| 743 |
(map make-bytevector (iota 16))))) |
| 744 |
|
| 745 |
(pass-if "array-ref" |
| 746 |
(let ((bv #vu8(255 127))) |
| 747 |
(and (= 255 (array-ref bv 0)) |
| 748 |
(= 127 (array-ref bv 1))))) |
| 749 |
|
| 750 |
(pass-if-exception "array-ref [index out-of-range]" |
| 751 |
exception:out-of-range |
| 752 |
(let ((bv #vu8(1 2))) |
| 753 |
(array-ref bv 2))) |
| 754 |
|
| 755 |
(pass-if "array-set!" |
| 756 |
(let ((bv (make-bytevector 2))) |
| 757 |
(array-set! bv 255 0) |
| 758 |
(array-set! bv 77 1) |
| 759 |
(equal? '(255 77) |
| 760 |
(bytevector->u8-list bv)))) |
| 761 |
|
| 762 |
(pass-if-exception "array-set! [index out-of-range]" |
| 763 |
exception:out-of-range |
| 764 |
(let ((bv (make-bytevector 2))) |
| 765 |
(array-set! bv 0 2))) |
| 766 |
|
| 767 |
(pass-if-exception "array-set! [value out-of-range]" |
| 768 |
exception:out-of-range |
| 769 |
(let ((bv (make-bytevector 2))) |
| 770 |
(array-set! bv 256 0))) |
| 771 |
|
| 772 |
(pass-if "array-type" |
| 773 |
(eq? 'vu8 (array-type #vu8()))) |
| 774 |
|
| 775 |
(pass-if "array-contents" |
| 776 |
(let ((bv (u8-list->bytevector (iota 10)))) |
| 777 |
(eq? bv (array-contents bv)))) |
| 778 |
|
| 779 |
(pass-if "array-ref" |
| 780 |
(let ((bv (u8-list->bytevector (iota 10)))) |
| 781 |
(equal? (iota 10) |
| 782 |
(map (lambda (i) (array-ref bv i)) |
| 783 |
(iota 10))))) |
| 784 |
|
| 785 |
(pass-if "array-set!" |
| 786 |
(let ((bv (make-bytevector 10))) |
| 787 |
(for-each (lambda (i) |
| 788 |
(array-set! bv i i)) |
| 789 |
(iota 10)) |
| 790 |
(equal? (iota 10) |
| 791 |
(bytevector->u8-list bv)))) |
| 792 |
|
| 793 |
(pass-if "make-typed-array" |
| 794 |
(let ((bv (make-typed-array 'vu8 77 33))) |
| 795 |
(equal? bv (u8-list->bytevector (make-list 33 77))))) |
| 796 |
|
| 797 |
(pass-if-exception "make-typed-array [out-of-range]" |
| 798 |
exception:out-of-range |
| 799 |
(make-typed-array 'vu8 256 77))) |
| 800 |
|
| 801 |
|
| 802 |
(with-test-prefix "uniform-array->bytevector" |
| 803 |
|
| 804 |
(pass-if "bytevector" |
| 805 |
(let ((bv #vu8(0 1 128 255))) |
| 806 |
(equal? bv (uniform-array->bytevector bv)))) |
| 807 |
|
| 808 |
(pass-if "empty bitvector" |
| 809 |
(let ((bv (uniform-array->bytevector (make-bitvector 0)))) |
| 810 |
(equal? bv #vu8()))) |
| 811 |
|
| 812 |
(pass-if "bitvector < 8" |
| 813 |
(let ((bv (uniform-array->bytevector (make-bitvector 4 #t)))) |
| 814 |
(= (bytevector-length bv) 4))) |
| 815 |
|
| 816 |
(pass-if "bitvector == 8" |
| 817 |
(let ((bv (uniform-array->bytevector (make-bitvector 8 #t)))) |
| 818 |
(= (bytevector-length bv) 4))) |
| 819 |
|
| 820 |
(pass-if "bitvector > 8" |
| 821 |
(let ((bv (uniform-array->bytevector (make-bitvector 9 #t)))) |
| 822 |
(= (bytevector-length bv) 4))) |
| 823 |
|
| 824 |
(pass-if "bitvector == 32" |
| 825 |
(let ((bv (uniform-array->bytevector (make-bitvector 32 #t)))) |
| 826 |
(= (bytevector-length bv) 4))) |
| 827 |
|
| 828 |
(pass-if "bitvector > 32" |
| 829 |
(let ((bv (uniform-array->bytevector (make-bitvector 33 #t)))) |
| 830 |
(= (bytevector-length bv) 8)))) |
| 831 |
|
| 832 |
|
| 833 |
(with-test-prefix "srfi-4 homogeneous numeric vectors as bytevectors" |
| 834 |
|
| 835 |
;; This failed prior to Guile 2.0.12. |
| 836 |
;; See <http://bugs.gnu.org/18866>. |
| 837 |
(pass-if-equal "bytevector-copy on srfi-4 arrays" |
| 838 |
(make-bytevector 8 #xFF) |
| 839 |
(bytevector-copy (make-u32vector 2 #xFFFFFFFF)))) |
| 840 |
|
| 841 |
;;; Local Variables: |
| 842 |
;;; eval: (put 'with-test-prefix/c&e 'scheme-indent-function 1) |
| 843 |
;;; End: |