| 1 |
;; |
| 2 |
;; testing binary.io |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use gauche.uvector) |
| 7 |
(use gauche.collection) |
| 8 |
(use scheme.list) |
| 9 |
|
| 10 |
(test-start "binary") |
| 11 |
|
| 12 |
;;---------------------------------------------------------- |
| 13 |
(test-section "binary.io") |
| 14 |
|
| 15 |
(use binary.io) |
| 16 |
(test-module 'binary.io) |
| 17 |
|
| 18 |
;; prepare data file |
| 19 |
(sys-unlink "test.o") |
| 20 |
|
| 21 |
(define *u8* |
| 22 |
'(#x01 #x23 #x45 #x67 #x89 #xab #xcd #xef |
| 23 |
#x0f #x1e #x2d #x3c #x4b #x5a #x69 #x78)) |
| 24 |
(define *s8* |
| 25 |
'(#x01 #x23 #x45 #x67 #x-77 #x-55 #x-33 #x-11 |
| 26 |
#x0f #x1e #x2d #x3c #x4b #x5a #x69 #x78)) |
| 27 |
(define *u16be* '(#x0123 #x4567 #x89ab #xcdef #x0f1e #x2d3c #x4b5a #x6978)) |
| 28 |
(define *u16le* '(#x2301 #x6745 #xab89 #xefcd #x1e0f #x3c2d #x5a4b #x7869)) |
| 29 |
(define *s16be* '(#x0123 #x4567 #x-7655 #x-3211 #x0f1e #x2d3c #x4b5a #x6978)) |
| 30 |
(define *s16le* '(#x2301 #x6745 #x-5477 #x-1033 #x1e0f #x3c2d #x5a4b #x7869)) |
| 31 |
(define *u32be* '(#x01234567 #x89abcdef #x0f1e2d3c #x4b5a6978)) |
| 32 |
(define *u32le* '(#x67452301 #xefcdab89 #x3c2d1e0f #x78695a4b)) |
| 33 |
(define *s32be* '(#x01234567 #x-76543211 #x0f1e2d3c #x4b5a6978)) |
| 34 |
(define *s32le* '(#x67452301 #x-10325477 #x3c2d1e0f #x78695a4b)) |
| 35 |
(define *u64be* '(#x0123456789abcdef #x0f1e2d3c4b5a6978)) |
| 36 |
(define *u64le* '(#xefcdab8967452301 #x78695a4b3c2d1e0f)) |
| 37 |
(define *s64be* '(#x0123456789abcdef #x0f1e2d3c4b5a6978)) |
| 38 |
(define *s64le* '(#x-1032547698badcff #x78695a4b3c2d1e0f)) |
| 39 |
|
| 40 |
(define *bytes-str* |
| 41 |
#*"\x01\x23\x45\x67\x89\xab\xcd\xef\x0f\x1e\x2d\x3c\x4b\x5a\x69\x78") |
| 42 |
|
| 43 |
(with-output-to-file "test.o" |
| 44 |
(lambda () |
| 45 |
(for-each write-byte *u8*))) |
| 46 |
|
| 47 |
(define (test-read-binary label data proc . endian) |
| 48 |
(test* label data |
| 49 |
(call-with-input-file "test.o" |
| 50 |
(cut port->list (cut apply proc <> endian) <>)))) |
| 51 |
|
| 52 |
(test-read-binary "read-u8" *u8* read-u8) |
| 53 |
(test-read-binary "read-s8" *s8* read-s8) |
| 54 |
|
| 55 |
(test-read-binary "read-u16 be" |
| 56 |
*u16be* read-u16 'big-endian) |
| 57 |
(test-read-binary "read-u16 le" |
| 58 |
*u16le* read-u16 'little-endian) |
| 59 |
(test-read-binary "read-s16 be" |
| 60 |
*s16be* read-s16 'big-endian) |
| 61 |
(test-read-binary "read-s16 le" |
| 62 |
*s16le* read-s16 'little-endian) |
| 63 |
|
| 64 |
(test-read-binary "read-u32 be" |
| 65 |
*u32be* read-u32 'big-endian) |
| 66 |
(test-read-binary "read-u32 le" |
| 67 |
*u32le* read-u32 'little-endian) |
| 68 |
(test-read-binary "read-s32 be" |
| 69 |
*s32be* read-s32 'big-endian) |
| 70 |
(test-read-binary "read-s32 le" |
| 71 |
*s32le* read-s32 'little-endian) |
| 72 |
|
| 73 |
(test-read-binary "read-u64 be" |
| 74 |
*u64be* read-u64 'big-endian) |
| 75 |
(test-read-binary "read-u64 le" |
| 76 |
*u64le* read-u64 'little-endian) |
| 77 |
(test-read-binary "read-s64 be" |
| 78 |
*s64be* read-s64 'big-endian) |
| 79 |
(test-read-binary "read-s64 le" |
| 80 |
*s64le* read-s64 'little-endian) |
| 81 |
|
| 82 |
(parameterize ((default-endian 'big-endian)) |
| 83 |
(test-read-binary "read-u64 (default - be)" |
| 84 |
*u64be* read-u64)) |
| 85 |
(parameterize ((default-endian 'little-endian)) |
| 86 |
(test-read-binary "read-u64 (default - le)" |
| 87 |
*u64le* read-u64)) |
| 88 |
|
| 89 |
(define (test-write-binary label data proc . endian) |
| 90 |
(test* label *bytes-str* |
| 91 |
(string-complete->incomplete |
| 92 |
(call-with-output-string |
| 93 |
(lambda (p) |
| 94 |
(for-each (cut apply proc <> p endian) data)))))) |
| 95 |
|
| 96 |
(test-write-binary "write-u8" *u8* write-u8) |
| 97 |
(test-write-binary "write-s8" *s8* write-s8) |
| 98 |
|
| 99 |
(test-write-binary "write-u16 be" |
| 100 |
*u16be* write-u16 'big-endian) |
| 101 |
(test-write-binary "write-u16 le" |
| 102 |
*u16le* write-u16 'little-endian) |
| 103 |
(test-write-binary "write-s16 be" |
| 104 |
*s16be* write-s16 'big-endian) |
| 105 |
(test-write-binary "write-s16 le" |
| 106 |
*s16le* write-s16 'little-endian) |
| 107 |
|
| 108 |
(test-write-binary "write-u32 be" |
| 109 |
*u32be* write-u32 'big-endian) |
| 110 |
(test-write-binary "write-u32 le" |
| 111 |
*u32le* write-u32 'little-endian) |
| 112 |
(test-write-binary "write-s32 be" |
| 113 |
*s32be* write-s32 'big-endian) |
| 114 |
(test-write-binary "write-s32 le" |
| 115 |
*s32le* write-s32 'little-endian) |
| 116 |
|
| 117 |
(test-write-binary "write-u64 be" |
| 118 |
*u64be* write-u64 'big-endian) |
| 119 |
(test-write-binary "write-u64 le" |
| 120 |
*u64le* write-u64 'little-endian) |
| 121 |
(test-write-binary "write-s64 be" |
| 122 |
*s64be* write-s64 'big-endian) |
| 123 |
(test-write-binary "write-s64 le" |
| 124 |
*s64le* write-s64 'little-endian) |
| 125 |
|
| 126 |
(define (test-write-binary-range label proc min min-1 max max+1) |
| 127 |
(test* label '(#t #f #t #f) |
| 128 |
(map (lambda (val) |
| 129 |
(with-error-handler |
| 130 |
(^e #f) |
| 131 |
(lambda () |
| 132 |
(and (call-with-output-string (cut proc val <>)) |
| 133 |
#t)))) |
| 134 |
(list min min-1 max max+1)))) |
| 135 |
|
| 136 |
(test-write-binary-range "write-u8" write-u8 |
| 137 |
0 -1 255 256) |
| 138 |
(test-write-binary-range "write-s8" write-s8 |
| 139 |
-128 -129 127 128) |
| 140 |
(test-write-binary-range "write-u16" write-u16 |
| 141 |
0 -1 #xffff #x10000) |
| 142 |
(test-write-binary-range "write-s16" write-s16 |
| 143 |
#x-8000 #x-8001 #x7fff #x8000) |
| 144 |
(test-write-binary-range "write-u32" write-u32 |
| 145 |
0 -1 #xffffffff #x100000000) |
| 146 |
(test-write-binary-range "write-s32" write-s32 |
| 147 |
#x-80000000 #x-80000001 #x7fffffff #x80000000) |
| 148 |
(test-write-binary-range "write-u64" write-u64 |
| 149 |
0 -1 #xffffffffffffffff #x10000000000000000) |
| 150 |
(test-write-binary-range "write-s64" write-s64 |
| 151 |
#x-8000000000000000 #x-8000000000000001 |
| 152 |
#x7fffffffffffffff #x8000000000000000) |
| 153 |
|
| 154 |
(define (test-flonum-rw label reader writer bits) |
| 155 |
(test* label (string-complete->incomplete bits) |
| 156 |
(let ((val (with-input-from-string bits (cut reader)))) |
| 157 |
(and (real? val) |
| 158 |
(string-complete->incomplete |
| 159 |
(with-output-to-string (cut writer val))))))) |
| 160 |
|
| 161 |
(define (test-flonum-rw-f16 bvec endian) |
| 162 |
(test-flonum-rw (format "read/write-f16 (~a)" endian) |
| 163 |
(cut read-f16 #f endian) |
| 164 |
(cut write-f16 <> #f endian) |
| 165 |
(u8vector->string bvec))) |
| 166 |
|
| 167 |
(define (test-flonum-rw-f32 bvec endian) |
| 168 |
(test-flonum-rw (format "read/write-f32 (~a)" endian) |
| 169 |
(cut read-f32 #f endian) |
| 170 |
(cut write-f32 <> #f endian) |
| 171 |
(u8vector->string bvec))) |
| 172 |
|
| 173 |
(define (test-flonum-rw-f64 bvec endian) |
| 174 |
(test-flonum-rw (format "read/write-f64 (~a)" endian) |
| 175 |
(cut read-f64 #f endian) |
| 176 |
(cut write-f64 <> #f endian) |
| 177 |
(u8vector->string bvec))) |
| 178 |
|
| 179 |
(define (test-flonum-rw-driver tester bytes-list) |
| 180 |
(for-each (lambda (bytes) |
| 181 |
(let ((be-vec (coerce-to <u8vector> bytes)) |
| 182 |
(le-vec (coerce-to <u8vector> (reverse bytes)))) |
| 183 |
(tester be-vec 'big-endian) |
| 184 |
(tester le-vec 'little-endian))) |
| 185 |
bytes-list)) |
| 186 |
|
| 187 |
;; f16 - half float |
| 188 |
(test-flonum-rw-driver |
| 189 |
test-flonum-rw-f16 |
| 190 |
(append |
| 191 |
'((#x00 #x00) ; 0.0 |
| 192 |
(#x04 #x00) ; normalized |
| 193 |
(#x04 #x01) |
| 194 |
(#x04 #x02) |
| 195 |
(#x3c #x00) ; 1.0 |
| 196 |
(#x7b #xff) |
| 197 |
(#x7c #x00) ; +inf |
| 198 |
|
| 199 |
(#x80 #x00) ; -0.0 |
| 200 |
(#x84 #x00) ; normalized |
| 201 |
(#xbc #x00) ; -1.0 |
| 202 |
(#xfb #xff) |
| 203 |
(#xfc #x00) ; -inf |
| 204 |
|
| 205 |
(#x00 #x01) ; +denormalized |
| 206 |
(#x00 #x02) ; |
| 207 |
(#x00 #x03) ; |
| 208 |
(#x80 #x01) ; -denormalized |
| 209 |
(#x80 #x02) ; |
| 210 |
(#x80 #x03) ; |
| 211 |
))) |
| 212 |
|
| 213 |
;; f32 - ieee single float |
| 214 |
;; NB: Alpha chip doesn't support denormalized floats, so we exclude |
| 215 |
;; them from the test. |
| 216 |
(test-flonum-rw-driver |
| 217 |
test-flonum-rw-f32 |
| 218 |
(append |
| 219 |
'((#x00 #x00 #x00 #x00) ;; 0.0 |
| 220 |
(#x00 #x80 #x00 #x00) ;; normalized |
| 221 |
(#x00 #x80 #x00 #x01) |
| 222 |
(#x00 #x80 #x00 #x02) |
| 223 |
(#x3f #x80 #x00 #x00) ;; 1.0 |
| 224 |
(#x7f #x7f #xff #xff) |
| 225 |
(#x7f #x80 #x00 #x00) ;; +inf |
| 226 |
|
| 227 |
(#x80 #x00 #x00 #x00) ;;-0.0 |
| 228 |
(#x80 #x80 #x00 #x00) ;; normalized |
| 229 |
(#xbf #x80 #x00 #x00) ;; -1.0 |
| 230 |
(#xff #x7f #xff #xff) |
| 231 |
(#xff #x80 #x00 #x00) ;; -inf |
| 232 |
) |
| 233 |
(if (#/alpha/ (gauche-architecture)) |
| 234 |
'() |
| 235 |
'((#x00 #x00 #x00 #x01) ;; +denormalized |
| 236 |
(#x00 #x00 #x00 #x02) |
| 237 |
(#x00 #x00 #x00 #x03) |
| 238 |
(#x80 #x00 #x00 #x01) ;; -denormalized |
| 239 |
(#x80 #x00 #x00 #x02) |
| 240 |
(#x80 #x00 #x00 #x03) |
| 241 |
)))) |
| 242 |
|
| 243 |
(test-flonum-rw-driver |
| 244 |
test-flonum-rw-f64 |
| 245 |
'((#x00 #x00 #x00 #x00 #x00 #x00 #x00 #x00) ;; 0.0 |
| 246 |
(#x00 #x00 #x00 #x00 #x00 #x00 #x00 #x01) ;; denormalized |
| 247 |
(#x00 #x00 #x00 #x00 #x00 #x00 #x00 #x02) |
| 248 |
(#x00 #x00 #x00 #x00 #x00 #x00 #x00 #x03) |
| 249 |
(#x00 #x10 #x00 #x00 #x00 #x00 #x00 #x00) ;; normalized |
| 250 |
(#x00 #x10 #x00 #x00 #x00 #x00 #x00 #x01) |
| 251 |
(#x3f #xf0 #x00 #x00 #x00 #x00 #x00 #x00) ;; 1.0 |
| 252 |
(#x7f #xef #xff #xff #xff #xff #xff #xff) |
| 253 |
(#x7f #xf0 #x00 #x00 #x00 #x00 #x00 #x00) ;; +inf |
| 254 |
|
| 255 |
(#x80 #x00 #x00 #x00 #x00 #x00 #x00 #x00) ;;-0.0 |
| 256 |
(#x80 #x00 #x00 #x00 #x00 #x00 #x00 #x01) ;; denormalized |
| 257 |
(#x80 #x00 #x00 #x00 #x00 #x00 #x00 #x02) |
| 258 |
(#x80 #x10 #x00 #x00 #x00 #x00 #x00 #x00) ;; normalized |
| 259 |
(#xbf #xf0 #x00 #x00 #x00 #x00 #x00 #x00) ;; -1.0 |
| 260 |
(#xff #xef #xff #xff #xff #xff #xff #xff) |
| 261 |
(#xff #xf0 #x00 #x00 #x00 #x00 #x00 #x00) ;; -inf |
| 262 |
)) |
| 263 |
|
| 264 |
(test* "read-f16 (be)" 1.0 |
| 265 |
(with-input-from-string #*"\x3c\x00" |
| 266 |
(cut read-f16 #f 'big-endian))) |
| 267 |
(test* "read-f32 (be)" 1.0 |
| 268 |
(with-input-from-string #*"\x3f\x80\x00\x00" |
| 269 |
(cut read-f32 #f 'big-endian))) |
| 270 |
(test* "read-f64 (be)" 1.0 |
| 271 |
(with-input-from-string #*"\x3f\xf0\x00\x00\x00\x00\x00\x00" |
| 272 |
(cut read-f64 #f 'big-endian))) |
| 273 |
(test* "read-f16 (le)" 1.0 |
| 274 |
(with-input-from-string #*"\x00\x3c" |
| 275 |
(cut read-f16 #f 'little-endian))) |
| 276 |
(test* "read-f32 (le)" 1.0 |
| 277 |
(with-input-from-string #*"\x00\x00\x80\x3f" |
| 278 |
(cut read-f32 #f 'little-endian))) |
| 279 |
(test* "read-f64 (le)" 1.0 |
| 280 |
(with-input-from-string #*"\x00\x00\x00\x00\x00\x00\xf0\x3f" |
| 281 |
(cut read-f64 #f 'little-endian))) |
| 282 |
(test* "read-f16 (arm)" 1.0 |
| 283 |
(with-input-from-string #*"\x00\x3c" |
| 284 |
(cut read-f16 #f 'arm-little-endian))) |
| 285 |
(test* "read-f32 (arm)" 1.0 |
| 286 |
(with-input-from-string #*"\x00\x00\x80\x3f" |
| 287 |
(cut read-f32 #f 'arm-little-endian))) |
| 288 |
(test* "read-f64 (arm)" 1.0 |
| 289 |
(with-input-from-string #*"\x00\x00\xf0\x3f\x00\x00\x00\x00" |
| 290 |
(cut read-f64 #f 'arm-little-endian))) |
| 291 |
|
| 292 |
|
| 293 |
;; uvector reader |
| 294 |
|
| 295 |
(test* "get-u8" '(1 2 254 255) |
| 296 |
(map (cut get-u8 '#u8(1 2 254 255) <>) (iota 4))) |
| 297 |
(test* "get-u8" (test-error) (get-u8 '#u8(1) -1)) |
| 298 |
(test* "get-u8" (test-error) (get-u8 '#u8(1) 1)) |
| 299 |
(test* "get-s8" '(1 2 -2 -1) |
| 300 |
(map (cut get-s8 '#u8(1 2 254 255) <>) (iota 4))) |
| 301 |
(test* "get-u16 be" '(#x0102 #x02fe #xfeff) |
| 302 |
(map (cut get-u16 '#u8(1 2 254 255) <> 'big-endian) |
| 303 |
'(0 1 2))) |
| 304 |
(test* "get-u16 le" '(#x0201 #xfe02 #xfffe) |
| 305 |
(map (cut get-u16 '#u8(1 2 254 255) <> 'little-endian) |
| 306 |
'(0 1 2))) |
| 307 |
(test* "get-u16 bound" (test-error) |
| 308 |
(get-u16 '#u8(1 2) -1)) |
| 309 |
(test* "get-u16 bound" (test-error) |
| 310 |
(get-u16 '#u8(1 2) 1)) |
| 311 |
(test* "get-s16 be" `(#x0102 #x02fe ,(- #xfeff #x10000)) |
| 312 |
(map (cut get-s16 '#u8(1 2 254 255) <> 'big-endian) |
| 313 |
'(0 1 2))) |
| 314 |
(test* "get-s16 le" `(#x0201 ,(- #xfe02 #x10000) ,(- #xfffe #x10000)) |
| 315 |
(map (cut get-s16 '#u8(1 2 254 255) <> 'little-endian) |
| 316 |
'(0 1 2))) |
| 317 |
|
| 318 |
(test* "get-u32 be" |
| 319 |
'(#x0102feff #x02feff03 #xfeff0304 #xff0304fc #x0304fcfd) |
| 320 |
(map (cut get-u32 |
| 321 |
'#u8(#x01 #x02 #xfe #xff #x03 #x04 #xfc #xfd) |
| 322 |
<> 'big-endian) |
| 323 |
(iota 5))) |
| 324 |
(test* "get-u32 le" |
| 325 |
'(#xfffe0201 #x03fffe02 #x0403fffe #xfc0403ff #xfdfc0403) |
| 326 |
(map (cut get-u32 |
| 327 |
'#u8(#x01 #x02 #xfe #xff #x03 #x04 #xfc #xfd) |
| 328 |
<> 'little-endian) |
| 329 |
(iota 5))) |
| 330 |
(test* "get-s32 be" |
| 331 |
`(#x0102feff #x02feff03 ,(- #xfeff0304 (expt 2 32)) |
| 332 |
,(- #xff0304fc (expt 2 32)) #x0304fcfd) |
| 333 |
(map (cut get-s32 |
| 334 |
'#u8(#x01 #x02 #xfe #xff #x03 #x04 #xfc #xfd) |
| 335 |
<> 'big-endian) |
| 336 |
(iota 5))) |
| 337 |
(test* "get-s32 le" |
| 338 |
`(,(- #xfffe0201 (expt 2 32)) #x03fffe02 #x0403fffe |
| 339 |
,(- #xfc0403ff (expt 2 32)) ,(- #xfdfc0403 (expt 2 32))) |
| 340 |
(map (cut get-s32 |
| 341 |
'#u8(#x01 #x02 #xfe #xff #x03 #x04 #xfc #xfd) |
| 342 |
<> 'little-endian) |
| 343 |
(iota 5))) |
| 344 |
|
| 345 |
(test* "get-u64 be" |
| 346 |
'(#x01ff02fe03fd04fc #xff02fe03fd04fc05) |
| 347 |
(map (cut get-u64 |
| 348 |
'#u8(#x01 #xff #x02 #xfe #x03 #xfd #x04 #xfc #x05) |
| 349 |
<> 'big-endian) |
| 350 |
'(0 1))) |
| 351 |
(test* "get-u64 le" |
| 352 |
'(#xfc04fd03fe02ff01 #x05fc04fd03fe02ff) |
| 353 |
(map (cut get-u64 |
| 354 |
'#u8(#x01 #xff #x02 #xfe #x03 #xfd #x04 #xfc #x05) |
| 355 |
<> 'little-endian) |
| 356 |
'(0 1))) |
| 357 |
(test* "get-s64 be" |
| 358 |
`(#x01ff02fe03fd04fc ,(- #xff02fe03fd04fc05 (expt 2 64))) |
| 359 |
(map (cut get-s64 |
| 360 |
'#u8(#x01 #xff #x02 #xfe #x03 #xfd #x04 #xfc #x05) |
| 361 |
<> 'big-endian) |
| 362 |
'(0 1))) |
| 363 |
(test* "get-s64 le" |
| 364 |
`(,(- #xfc04fd03fe02ff01 (expt 2 64)) #x05fc04fd03fe02ff) |
| 365 |
(map (cut get-s64 |
| 366 |
'#u8(#x01 #xff #x02 #xfe #x03 #xfd #x04 #xfc #x05) |
| 367 |
<> 'little-endian) |
| 368 |
'(0 1))) |
| 369 |
|
| 370 |
(test* "get-f16 be" '(1.0 -1.0) |
| 371 |
(map (cut get-f16 |
| 372 |
'#u8(#x3c #x00 #xbc #x00) |
| 373 |
<> 'big-endian) |
| 374 |
'(0 2))) |
| 375 |
(test* "get-f16 le" '(1.0 -1.0) |
| 376 |
(map (cut get-f16 |
| 377 |
'#u8(#x00 #x3c #x00 #xbc) |
| 378 |
<> 'little-endian) |
| 379 |
'(0 2))) |
| 380 |
(test* "get-f32 be" '(1.0 -1.0) |
| 381 |
(map (cut get-f32 |
| 382 |
'#u8(#x3f #x80 #x00 #x00 #xbf #x80 #x00 #x00) |
| 383 |
<> 'big-endian) |
| 384 |
'(0 4))) |
| 385 |
(test* "get-f32 le" '(1.0 -1.0) |
| 386 |
(map (cut get-f32 |
| 387 |
'#u8(#x00 #x00 #x80 #x3f #x00 #x00 #x80 #xbf) |
| 388 |
<> 'little-endian) |
| 389 |
'(0 4))) |
| 390 |
(test* "get-f64 be" '(1.0 -1.0) |
| 391 |
(map (cut get-f64 |
| 392 |
'#u8(#x3f #xf0 #x00 #x00 #x00 #x00 #x00 #x00 |
| 393 |
#xbf #xf0 #x00 #x00 #x00 #x00 #x00 #x00) |
| 394 |
<> 'big-endian) |
| 395 |
'(0 8))) |
| 396 |
(test* "get-f64 le" '(1.0 -1.0) |
| 397 |
(map (cut get-f64 |
| 398 |
'#u8(#x00 #x00 #x00 #x00 #x00 #x00 #xf0 #x3f |
| 399 |
#x00 #x00 #x00 #x00 #x00 #x00 #xf0 #xbf) |
| 400 |
<> 'little-endian) |
| 401 |
'(0 8))) |
| 402 |
(test* "get-f64 arm" '(1.0 -1.0) |
| 403 |
(map (cut get-f64 |
| 404 |
'#u8(#x00 #x00 #xf0 #x3f #x00 #x00 #x00 #x00 |
| 405 |
#x00 #x00 #xf0 #xbf #x00 #x00 #x00 #x00) |
| 406 |
<> 'arm-little-endian) |
| 407 |
'(0 8))) |
| 408 |
|
| 409 |
(test* "get-uint" |
| 410 |
'(#xff #xff #xfffe #xfeff #xfffe00 #x00feff #xfe0000 #x0000fe |
| 411 |
#xfe00000003 #x03000000fe #x0000000300 #x0003000000) |
| 412 |
(let1 v '#u8(#xff #xfe 0 0 0 3 0 0 0 0) |
| 413 |
(list (get-uint 1 v 0 'big-endian) |
| 414 |
(get-uint 1 v 0 'little-endian) |
| 415 |
(get-uint 2 v 0 'big-endian) |
| 416 |
(get-uint 2 v 0 'little-endian) |
| 417 |
(get-uint 3 v 0 'big-endian) |
| 418 |
(get-uint 3 v 0 'little-endian) |
| 419 |
(get-uint 3 v 1 'big-endian) |
| 420 |
(get-uint 3 v 1 'little-endian) |
| 421 |
(get-uint 5 v 1 'big-endian) |
| 422 |
(get-uint 5 v 1 'little-endian) |
| 423 |
(get-uint 5 v 2 'big-endian) |
| 424 |
(get-uint 5 v 2 'little-endian)))) |
| 425 |
|
| 426 |
(test* "get-sint" |
| 427 |
'(-1 -1 -2 -257 -512 #x00feff -131072 #x0000fe |
| 428 |
-8589934589 #x03000000fe #x0000000300 #x0003000000) |
| 429 |
(let1 v '#u8(#xff #xfe 0 0 0 3 0 0 0 0) |
| 430 |
(list (get-sint 1 v 0 'big-endian) |
| 431 |
(get-sint 1 v 0 'little-endian) |
| 432 |
(get-sint 2 v 0 'big-endian) |
| 433 |
(get-sint 2 v 0 'little-endian) |
| 434 |
(get-sint 3 v 0 'big-endian) |
| 435 |
(get-sint 3 v 0 'little-endian) |
| 436 |
(get-sint 3 v 1 'big-endian) |
| 437 |
(get-sint 3 v 1 'little-endian) |
| 438 |
(get-sint 5 v 1 'big-endian) |
| 439 |
(get-sint 5 v 1 'little-endian) |
| 440 |
(get-sint 5 v 2 'big-endian) |
| 441 |
(get-sint 5 v 2 'little-endian)))) |
| 442 |
|
| 443 |
(test* "put-u8!" '#u8(0 1 254 255) |
| 444 |
(let1 v (make-u8vector 4) |
| 445 |
(put-u8! v 3 255) |
| 446 |
(put-u8! v 2 254) |
| 447 |
(put-u8! v 1 1) |
| 448 |
(put-u8! v 0 0) |
| 449 |
v)) |
| 450 |
(test* "put-s8!" '#u8(0 1 254 255) |
| 451 |
(let1 v (make-u8vector 4) |
| 452 |
(put-s8! v 3 -1) |
| 453 |
(put-s8! v 2 -2) |
| 454 |
(put-s8! v 1 1) |
| 455 |
(put-s8! v 0 0) |
| 456 |
v)) |
| 457 |
(test* "put-u16! be" '#u8(1 2 0 254 255) |
| 458 |
(let1 v (make-u8vector 5 0) |
| 459 |
(put-u16! v 3 #xfeff 'big-endian) |
| 460 |
(put-u16! v 0 #x0102 'big-endian) |
| 461 |
v)) |
| 462 |
(test* "put-u16! le" '#u8(2 1 0 255 254) |
| 463 |
(let1 v (make-u8vector 5 0) |
| 464 |
(put-u16! v 3 #xfeff 'little-endian) |
| 465 |
(put-u16! v 0 #x0102 'little-endian) |
| 466 |
v)) |
| 467 |
(test* "put-s16! be" '#u8(1 2 0 254 255) |
| 468 |
(let1 v (make-u8vector 5 0) |
| 469 |
(put-s16! v 3 (- #xfeff #x10000) 'big-endian) |
| 470 |
(put-s16! v 0 #x0102 'big-endian) |
| 471 |
v)) |
| 472 |
(test* "put-s16! le" '#u8(2 1 0 255 254) |
| 473 |
(let1 v (make-u8vector 5 0) |
| 474 |
(put-s16! v 3 (- #xfeff #x10000) 'little-endian) |
| 475 |
(put-s16! v 0 #x0102 'little-endian) |
| 476 |
v)) |
| 477 |
|
| 478 |
(test* "put-u32! be" '#u8(1 2 3 4 0 252 253 254 255) |
| 479 |
(let1 v (make-u8vector 9 0) |
| 480 |
(put-u32! v 5 #xfcfdfeff 'big-endian) |
| 481 |
(put-u32! v 0 #x01020304 'big-endian) |
| 482 |
v)) |
| 483 |
(test* "put-u32! le" '#u8(4 3 2 1 0 255 254 253 252) |
| 484 |
(let1 v (make-u8vector 9 0) |
| 485 |
(put-u32! v 5 #xfcfdfeff 'little-endian) |
| 486 |
(put-u32! v 0 #x01020304 'little-endian) |
| 487 |
v)) |
| 488 |
(test* "put-s32! be" '#u8(1 2 3 4 0 252 253 254 255) |
| 489 |
(let1 v (make-u8vector 9 0) |
| 490 |
(put-s32! v 5 (- #xfcfdfeff (expt 2 32)) 'big-endian) |
| 491 |
(put-s32! v 0 #x01020304 'big-endian) |
| 492 |
v)) |
| 493 |
(test* "put-s32! le" '#u8(4 3 2 1 0 255 254 253 252) |
| 494 |
(let1 v (make-u8vector 9 0) |
| 495 |
(put-s32! v 5 (- #xfcfdfeff (expt 2 32)) 'little-endian) |
| 496 |
(put-s32! v 0 #x01020304 'little-endian) |
| 497 |
v)) |
| 498 |
|
| 499 |
(test* "put-u64! be" |
| 500 |
'#u8(1 2 3 4 5 6 7 8 0 248 249 250 251 252 253 254 255) |
| 501 |
(let1 v (make-u8vector 17 0) |
| 502 |
(put-u64! v 9 #xf8f9fafbfcfdfeff 'big-endian) |
| 503 |
(put-u64! v 0 #x0102030405060708 'big-endian) |
| 504 |
v)) |
| 505 |
(test* "put-u64! le" |
| 506 |
'#u8(8 7 6 5 4 3 2 1 0 255 254 253 252 251 250 249 248) |
| 507 |
(let1 v (make-u8vector 17 0) |
| 508 |
(put-u64! v 9 #xf8f9fafbfcfdfeff 'little-endian) |
| 509 |
(put-u64! v 0 #x0102030405060708 'little-endian) |
| 510 |
v)) |
| 511 |
(test* "put-s64! be" |
| 512 |
'#u8(1 2 3 4 5 6 7 8 0 248 249 250 251 252 253 254 255) |
| 513 |
(let1 v (make-u8vector 17 0) |
| 514 |
(put-s64! v 9 (- #xf8f9fafbfcfdfeff (expt 2 64)) 'big-endian) |
| 515 |
(put-s64! v 0 #x0102030405060708 'big-endian) |
| 516 |
v)) |
| 517 |
(test* "put-s64! le" |
| 518 |
'#u8(8 7 6 5 4 3 2 1 0 255 254 253 252 251 250 249 248) |
| 519 |
(let1 v (make-u8vector 17 0) |
| 520 |
(put-s64! v 9 (- #xf8f9fafbfcfdfeff (expt 2 64)) 'little-endian) |
| 521 |
(put-s64! v 0 #x0102030405060708 'little-endian) |
| 522 |
v)) |
| 523 |
|
| 524 |
(test* "put-f16! be" '#u8(#x3c #x00 0 #xbc #x00) |
| 525 |
(let1 v (make-u8vector 5 0) |
| 526 |
(put-f16! v 0 1.0 'big-endian) |
| 527 |
(put-f16! v 3 -1.0 'big-endian) |
| 528 |
v)) |
| 529 |
(test* "put-f16! le" '#u8(#x00 #x3c 0 #x00 #xbc) |
| 530 |
(let1 v (make-u8vector 5 0) |
| 531 |
(put-f16! v 0 1.0 'little-endian) |
| 532 |
(put-f16! v 3 -1.0 'little-endian) |
| 533 |
v)) |
| 534 |
|
| 535 |
(test* "put-f32! be" |
| 536 |
'#u8(#x3f #x80 #x00 #x00 0 #xbf #x80 #x00 #x00) |
| 537 |
(let1 v (make-u8vector 9 0) |
| 538 |
(put-f32! v 0 1.0 'big-endian) |
| 539 |
(put-f32! v 5 -1.0 'big-endian) |
| 540 |
v)) |
| 541 |
(test* "put-f32! le" |
| 542 |
'#u8(#x00 #x00 #x80 #x3f 0 #x00 #x00 #x80 #xbf) |
| 543 |
(let1 v (make-u8vector 9 0) |
| 544 |
(put-f32! v 0 1.0 'little-endian) |
| 545 |
(put-f32! v 5 -1.0 'little-endian) |
| 546 |
v)) |
| 547 |
|
| 548 |
(test* "put-f64! be" |
| 549 |
'#u8(#x3f #xf0 #x00 #x00 #x00 #x00 #x00 #x00 0 |
| 550 |
#xbf #xf0 #x00 #x00 #x00 #x00 #x00 #x00) |
| 551 |
(let1 v (make-u8vector 17 0) |
| 552 |
(put-f64! v 0 1.0 'big-endian) |
| 553 |
(put-f64! v 9 -1.0 'big-endian) |
| 554 |
v)) |
| 555 |
(test* "put-f64! le" |
| 556 |
'#u8(#x00 #x00 #x00 #x00 #x00 #x00 #xf0 #x3f 0 |
| 557 |
#x00 #x00 #x00 #x00 #x00 #x00 #xf0 #xbf) |
| 558 |
(let1 v (make-u8vector 17 0) |
| 559 |
(put-f64! v 0 1.0 'little-endian) |
| 560 |
(put-f64! v 9 -1.0 'little-endian) |
| 561 |
v)) |
| 562 |
(test* "put-f64! arm" |
| 563 |
'#u8(#x00 #x00 #xf0 #x3f #x00 #x00 #x00 #x00 0 |
| 564 |
#x00 #x00 #xf0 #xbf #x00 #x00 #x00 #x00) |
| 565 |
(let1 v (make-u8vector 17 0) |
| 566 |
(put-f64! v 0 1.0 'arm-little-endian) |
| 567 |
(put-f64! v 9 -1.0 'arm-little-endian) |
| 568 |
v)) |
| 569 |
|
| 570 |
(test* "put-uint!" |
| 571 |
'#u8(0 #xff #xff 0 0 #xff #xfe #xfe #xff 0) |
| 572 |
(rlet1 v (make-u8vector 10) |
| 573 |
(put-uint! 2 v 0 #xff 'big-endian) |
| 574 |
(put-uint! 2 v 2 #xff 'little-endian) |
| 575 |
(put-uint! 3 v 4 #xfffe 'big-endian) |
| 576 |
(put-uint! 3 v 7 #xfffe 'little-endian))) |
| 577 |
|
| 578 |
(test* "put-sint!" |
| 579 |
'#u8(#xff #xfe #xfe #xff #xff #xfe 00 00 #xfe #xff) |
| 580 |
(rlet1 v (make-u8vector 10) |
| 581 |
(put-sint! 2 v 0 -2 'big-endian) |
| 582 |
(put-sint! 2 v 2 -2 'little-endian) |
| 583 |
(put-sint! 3 v 4 -512 'big-endian) |
| 584 |
(put-sint! 3 v 7 -512 'little-endian))) |
| 585 |
|
| 586 |
;;---------------------------------------------------------- |
| 587 |
(test-section "binary.ftype") |
| 588 |
|
| 589 |
(use binary.ftype) |
| 590 |
(test-module 'binary.ftype) |
| 591 |
|
| 592 |
(define *fobject-storage* |
| 593 |
'#u8(#x80 #x01 #x02 #x03 #x04 #x05 #x06 #x07 |
| 594 |
#x08 #x09 #x0a #x0b #x0c #x0d #x0e #x0f |
| 595 |
#x10 #x11 #x12 #x13 #x14 #x15 #x16 #x17 |
| 596 |
#x18 #x19 #x1a #x1b #x1c #x1d #x1e #x1f |
| 597 |
#x20 #x21 #x22 #x23 #x24 #x25 #x26 #x27 |
| 598 |
#x28 #x29 #x2a #x2b #x2c #x2d #x2e #x2f |
| 599 |
#x30 #x31 #x32 #x33 #x34 #x35 #x36 #x37 |
| 600 |
#x38 #x39 #x3a #x3b #x3c #x3d #x3e #x3f |
| 601 |
)) |
| 602 |
|
| 603 |
;; procedural interface |
| 604 |
(let ([ft1 (make-fstruct-type 'ft1 |
| 605 |
`((a ,ftype:schar) |
| 606 |
(b ,ftype:uchar) |
| 607 |
(c ,ftype:short) |
| 608 |
(d ,ftype:schar) |
| 609 |
(e ,ftype:short) |
| 610 |
(f ,ftype:schar) |
| 611 |
(g ,ftype:int) |
| 612 |
(h ,ftype:schar) |
| 613 |
(i ,ftype:long)) |
| 614 |
#f #f)] |
| 615 |
[ft2 (make-fstruct-type 'ft2 ;fully packed |
| 616 |
`((a ,ftype:schar) |
| 617 |
(b ,ftype:uchar) |
| 618 |
(c ,ftype:short) |
| 619 |
(d ,ftype:schar) |
| 620 |
(e ,ftype:short) |
| 621 |
(f ,ftype:schar) |
| 622 |
(g ,ftype:int) |
| 623 |
(h ,ftype:schar) |
| 624 |
(i ,ftype:long)) |
| 625 |
#f 0)]) |
| 626 |
(define (read-all-slots ft endian :optional (off 0)) |
| 627 |
(parameterize ([default-endian endian]) |
| 628 |
(map (cut fobject-ref/uv ft <> *fobject-storage* off) |
| 629 |
'(a b c d e f g h i)))) |
| 630 |
|
| 631 |
(test* "big endian, natural alignment" |
| 632 |
`(-128 1 #x0203 4 #x0607 8 #x0c0d0e0f #x10 |
| 633 |
,(case (ftype-size ftype:long) |
| 634 |
[(4) #x14151617] |
| 635 |
[(8) #x18191a1b1c1d1e1f])) |
| 636 |
(read-all-slots ft1 'big-endian)) |
| 637 |
(test* "little endian, natural alignment" |
| 638 |
`(-128 1 #x0302 4 #x0706 8 #x0f0e0d0c #x10 |
| 639 |
,(case (ftype-size ftype:long) |
| 640 |
[(4) #x17161514] |
| 641 |
[(8) #x1f1e1d1c1b1a1918])) |
| 642 |
(read-all-slots ft1 'little-endian)) |
| 643 |
(test* "with offset" |
| 644 |
`(8 9 #x0a0b 12 #x0e0f 16 #x14151617 24 |
| 645 |
,(case (ftype-size ftype:long) |
| 646 |
[(4) #x1c1d1e1f] |
| 647 |
[(8) #x2021222324252627])) |
| 648 |
(read-all-slots ft1 'big-endian 8)) |
| 649 |
|
| 650 |
(test* "big endian, packed" |
| 651 |
`(-128 1 #x0203 4 #x0506 7 #x08090a0b #x0c |
| 652 |
,(case (ftype-size ftype:long) |
| 653 |
[(4) #x0d0e0f10] |
| 654 |
[(8) #x0d0e0f1011121314])) |
| 655 |
(read-all-slots ft2 'big-endian)) |
| 656 |
(test* "little endian, packed" |
| 657 |
`(-128 1 #x0302 4 #x0605 7 #x0b0a0908 #x0c |
| 658 |
,(case (ftype-size ftype:long) |
| 659 |
[(4) #x100f0e0d] |
| 660 |
[(8) #x14131211100f0e0d])) |
| 661 |
(read-all-slots ft2 'little-endian)) |
| 662 |
) |
| 663 |
|
| 664 |
;; define-fstruct-type macro |
| 665 |
(define-fstruct-type ft3 #t #t |
| 666 |
((a ftype:schar) |
| 667 |
(b ftype:uchar) |
| 668 |
(c ftype:short) |
| 669 |
(d ftype:schar) |
| 670 |
(e ftype:short) |
| 671 |
(f ftype:schar) |
| 672 |
(g ftype:int) |
| 673 |
(h ftype:schar) |
| 674 |
(i ftype:long))) |
| 675 |
|
| 676 |
(let ([fs #f]) |
| 677 |
(test* "ft3 ctor and pred" #t |
| 678 |
(begin |
| 679 |
(set! fs (make-ft3 :a -1 :b 2 :c #x0304 :d 5 |
| 680 |
:e -1 :f 6 :g #x0708090a |
| 681 |
:h 7 :i #x0b0c0d0e)) |
| 682 |
(ft3? fs))) |
| 683 |
(test* "ft3 store" |
| 684 |
(case (default-endian) |
| 685 |
[(big-endian) |
| 686 |
(case (ftype-size ftype:long) |
| 687 |
[(4) '#u8(#xff 2 3 4 5 0 #xff #xff 6 0 0 0 7 8 9 10 |
| 688 |
7 0 0 0 11 12 13 14)] |
| 689 |
[(8) '#u8(#xff 2 3 4 5 0 #xff #xff 6 0 0 0 7 8 9 10 |
| 690 |
7 0 0 0 0 0 0 0 0 0 0 0 11 12 13 14)])] |
| 691 |
[(little-endian arm-little-endian) |
| 692 |
(case (ftype-size ftype:long) |
| 693 |
[(4) '#u8(#xff 2 4 3 5 0 #xff #xff 6 0 0 0 10 9 8 7 |
| 694 |
7 0 0 0 14 13 12 11)] |
| 695 |
[(8) '#u8(#xff 2 4 3 5 0 #xff #xff 6 0 0 0 10 9 8 7 |
| 696 |
7 0 0 0 0 0 0 0 14 13 12 11 0 0 0 0)])]) |
| 697 |
(fobject-storage fs)) |
| 698 |
(test* "ft3 init w/offset" (fobject-storage fs) |
| 699 |
(let1 v (make-u8vector (+ 5 (ftype-size ft3))) |
| 700 |
(init-ft3! v 5 |
| 701 |
:a -1 :b 2 :c #x0304 :d 5 |
| 702 |
:e -1 :f 6 :g #x0708090a |
| 703 |
:h 7 :i #x0b0c0d0e) |
| 704 |
(uvector-alias <u8vector> v 5))) |
| 705 |
) |
| 706 |
|
| 707 |
;; fstruct with bitfield |
| 708 |
(define-fstruct-type ft3 #t #t |
| 709 |
((a ftype:uchar) |
| 710 |
(b0 (bitfield 3)) |
| 711 |
(b1 (bitfield 1)) |
| 712 |
(b2 (bitfield 6)) |
| 713 |
(c ftype:int32))) |
| 714 |
|
| 715 |
;;---------------------------------------------------------- |
| 716 |
(test-section "binary.pack") |
| 717 |
|
| 718 |
(use binary.pack) |
| 719 |
(test-module 'binary.pack) |
| 720 |
|
| 721 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 722 |
;; test helpers |
| 723 |
|
| 724 |
(define (test-error title expected thunk) |
| 725 |
(test title #t |
| 726 |
(lambda () |
| 727 |
(with-error-handler |
| 728 |
(lambda (err) |
| 729 |
(and (cond ((regexp? expected) |
| 730 |
(rxmatch expected (condition-message err ""))) |
| 731 |
((procedure? expected) |
| 732 |
(expected err)) |
| 733 |
(else |
| 734 |
(equal? expected err))) |
| 735 |
#t)) |
| 736 |
thunk)))) |
| 737 |
|
| 738 |
(define (unpack-pack fmt ls) |
| 739 |
(unpack fmt :from-string (pack fmt ls :to-string? #t))) |
| 740 |
|
| 741 |
(define (test-unpack-pack name fmt ls) |
| 742 |
(test name ls (cut unpack-pack fmt ls))) |
| 743 |
|
| 744 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 745 |
;; start the perl tests |
| 746 |
|
| 747 |
(test "ascii integer medley" #t |
| 748 |
(lambda () |
| 749 |
(let ((fmt "c2 x5 C C x s d i l a6") |
| 750 |
(ls `(1 -100 127 128 32767 ,(/ 987.654321098 100.0) 12345 123456 "abcdef"))) |
| 751 |
(equal? ls (unpack fmt :from-string (pack fmt ls :to-string? #t)))))) |
| 752 |
|
| 753 |
;; % is unpack only, so it probably doesn't belong |
| 754 |
|
| 755 |
; (for-each |
| 756 |
; (lambda (x) |
| 757 |
; (test "counting bits" (caddr x) |
| 758 |
; (lambda () (unpack (car x) :from-string (cadr x))))) |
| 759 |
; '(("%32B*" "\001\002\004\010\020\040\100\200\377" 16) |
| 760 |
; ("%32b69" "\001\002\004\010\020\040\100\200\017" 12) |
| 761 |
; ("%32B69" "\001\002\004\010\020\040\100\200\017" 9))) |
| 762 |
|
| 763 |
(test-unpack-pack "maxint" "I" '(#xFFFFFFFF)) |
| 764 |
|
| 765 |
(define x-ls `(5 130 256 560 32000 3097152 268435455 1073741844 |
| 766 |
,(expt 2 33) 4503599627365785 |
| 767 |
23728385234614992549757750638446)) |
| 768 |
|
| 769 |
(define x (string-complete->incomplete (pack "w*" x-ls :to-string? #t))) |
| 770 |
|
| 771 |
(define y (string-complete->incomplete |
| 772 |
(pack "H*" (list (string-append "0581028200843081fa0081bd8440ffffff7f8480808014A0808" |
| 773 |
"0800087ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e")) |
| 774 |
:to-string? #t))) |
| 775 |
|
| 776 |
(test "ber pack" x (lambda () y)) |
| 777 |
|
| 778 |
(for-each |
| 779 |
(lambda (a b) (test "ber unpack" a (lambda () b))) |
| 780 |
x-ls (unpack "w*" :from-string y)) |
| 781 |
|
| 782 |
(for-each |
| 783 |
(lambda (template) |
| 784 |
(test (string-append "unpack-pack lengths: " template) 2 |
| 785 |
(lambda () |
| 786 |
(length (unpack-pack (string-append template "*") '(12 34)))))) |
| 787 |
'("c" "C" "i" "I" "s" "S" "l" "L" "n" "N" "v" "V" "f" "d" "q" "Q")) |
| 788 |
|
| 789 |
;; test the ascii template types (A, a, Z) |
| 790 |
|
| 791 |
(for-each |
| 792 |
(lambda (x) |
| 793 |
(test (format #f "ascii ~a template: ~a" |
| 794 |
(if (eq? (car x) 'p) "pack" "unpack") (cadr x)) |
| 795 |
(cadddr x) |
| 796 |
(lambda () |
| 797 |
(if (equal? (car x) 'u) |
| 798 |
(car (unpack (cadr x) :from-string (caddr x))) |
| 799 |
(pack (cadr x) (list (caddr x)) :to-string? #t) |
| 800 |
)))) |
| 801 |
'((p "A*" "foo\0bar\0 " "foo\0bar\0 ") |
| 802 |
(p "A11" "foo\0bar\0 " "foo\0bar\0 ") |
| 803 |
(u "A*" "foo\0bar \0" "foo\0bar") |
| 804 |
(u "A8" "foo\0bar \0" "foo\0bar") |
| 805 |
(p "a*" "foo\0bar\0 " "foo\0bar\0 ") |
| 806 |
(p "a11" "foo\0bar\0 " "foo\0bar\0 \0\0") |
| 807 |
(u "a*" "foo\0bar \0" "foo\0bar \0") |
| 808 |
(u "a8" "foo\0bar \0" "foo\0bar ") |
| 809 |
(p "Z*" "foo\0bar\0 " "foo\0bar\0 \0") |
| 810 |
(p "Z11" "foo\0bar\0 " "foo\0bar\0 \0\0") |
| 811 |
(p "Z3" "foo" "fo\0") |
| 812 |
(u "Z*" "foo\0bar \0" "foo") |
| 813 |
(u "Z8" "foo\0bar \0" "foo") |
| 814 |
;; added more basic tests (cross-referenced with Perl) |
| 815 |
(p "A*" "foo-bar" "foo-bar") |
| 816 |
(p "A11" "foo-bar" "foo-bar ") |
| 817 |
(u "A*" "foo-bar" "foo-bar") |
| 818 |
(u "A8" "foo-bar" "foo-bar") |
| 819 |
(p "a*" "foo-bar" "foo-bar") |
| 820 |
(p "a11" "foo-bar" "foo-bar\0\0\0\0") |
| 821 |
(u "a*" "foo-bar" "foo-bar") |
| 822 |
(u "a8" "foo-bar" "foo-bar\0") |
| 823 |
(p "Z*" "foo-bar" "foo-bar\0") |
| 824 |
(p "Z11" "foo-bar" "foo-bar\0\0\0\0") |
| 825 |
(p "Z3" "foo" "fo\0") |
| 826 |
(u "Z*" "foo" "foo") |
| 827 |
(u "Z8" "foo" "foo") |
| 828 |
;; added binary and hex tests (cross-referenced with Perl) |
| 829 |
(p "B8" "01000001" "A") |
| 830 |
(u "B8" "A" "01000001") |
| 831 |
(p "b8" "10000010" "A") |
| 832 |
(u "b8" "A" "10000010") |
| 833 |
(p "B*" "0100001001000011" "BC") |
| 834 |
(u "B*" "BC" "0100001001000011") |
| 835 |
(p "b*" "1100001001000010" "CB") |
| 836 |
(u "b*" "CB" "1100001001000010") |
| 837 |
(p "B*" "01000010010000110" "BC\0") |
| 838 |
(p "b*" "11000010010000100" "CB\0") |
| 839 |
(p "H2" "41" "A") |
| 840 |
(u "H2" "A" "41") |
| 841 |
(p "h2" "14" "A") |
| 842 |
(u "h2" "A" "14") |
| 843 |
(p "H*" "78797a" "xyz") |
| 844 |
(u "H*" "xyz" "78797a") |
| 845 |
(p "h*" "8797a7" "xyz") |
| 846 |
(u "h*" "xyz" "8797a7") |
| 847 |
)) |
| 848 |
|
| 849 |
;; pack nvNV byteorders |
| 850 |
|
| 851 |
(for-each |
| 852 |
(lambda (x) |
| 853 |
(test (string-append "pack " (car x) " byteorders") (cadr x) |
| 854 |
(lambda () |
| 855 |
(string-complete->incomplete (pack (car x) (cddr x) :to-string? #t))))) |
| 856 |
'(("n" #*"\xde\xad" #xdead) |
| 857 |
("v" #*"\xad\xde" #xdead) |
| 858 |
("N" #*"\xde\xad\xbe\xef" #xdeadbeef) |
| 859 |
("V" #*"\xef\xbe\xad\xde" #xdeadbeef))) |
| 860 |
|
| 861 |
(for-each |
| 862 |
(lambda (op) |
| 863 |
(for-each |
| 864 |
(lambda (i) |
| 865 |
(test* (format "pack/unpack ~A ~D" op i) (list i) |
| 866 |
(unpack op :from-string (pack op (list i) :to-string? #t)))) |
| 867 |
'(0 1 2 3 127 128 1023 1024 65534 65535))) |
| 868 |
'("n" "N" "v" "V")) |
| 869 |
|
| 870 |
(for-each |
| 871 |
(lambda (op) |
| 872 |
(for-each |
| 873 |
(lambda (i) |
| 874 |
(test* (format "pack/unpack ~A ~D" op i) (list i) |
| 875 |
(unpack op :from-string (pack op (list i) :to-string? #t)))) |
| 876 |
'(65536 16777215 16777216 4294967295))) |
| 877 |
'("N" "V")) |
| 878 |
|
| 879 |
;; object tests |
| 880 |
|
| 881 |
(test* "unpack o" '((1 (2 (3)))) |
| 882 |
(unpack "o" :from-string "(1 (2 (3)))")) |
| 883 |
|
| 884 |
(test* "pack o" "(1 (2 (3)))" |
| 885 |
(pack "o" '((1 (2 (3)))) :to-string? #t)) |
| 886 |
|
| 887 |
;; interesting, equivalent regexps aren't equal? even if they print the |
| 888 |
;; same |
| 889 |
;;(test* "unpack o" '((1 (2 (3))) "string" #/re?ge[x]p/) |
| 890 |
;; (unpack "o*" :from-string "(1 (2 (3)))\"string\"#/re?ge[x]p/")) |
| 891 |
|
| 892 |
(test* "pack o" "(1 (2 (3)))\"string\"#/re?ge[x]p/" |
| 893 |
(pack "o*" '((1 (2 (3))) "string" #/re?ge[x]p/) :to-string? #t)) |
| 894 |
|
| 895 |
;; grouping tests |
| 896 |
|
| 897 |
(test* "pack ()" "AB" |
| 898 |
(pack "(C C)" '(65 66) :to-string? #t)) |
| 899 |
|
| 900 |
(test* "unpack ()" '(65 66) |
| 901 |
(unpack "(C C)" :from-string "AB")) |
| 902 |
|
| 903 |
(test* "pack <>" "AB" |
| 904 |
(pack "<C C>" '((65 66)) :to-string? #t)) |
| 905 |
|
| 906 |
(test* "unpack <>" '((65 66)) |
| 907 |
(unpack "<C C>" :from-string "AB")) |
| 908 |
|
| 909 |
(test* "pack <>" "\x02ABCD" |
| 910 |
(pack "C/<C C>" '((65 66) (67 68)) :to-string? #t)) |
| 911 |
|
| 912 |
(test* "unpack <>" '((65 66) (67 68)) |
| 913 |
(unpack "C/<C C>" :from-string "\x02ABCD")) |
| 914 |
|
| 915 |
(test* "pack <>" "\x01AB" |
| 916 |
(pack "C/<C C>" '((65 66)) :to-string? #t)) |
| 917 |
|
| 918 |
(test* "unpack <>" '((65 66)) |
| 919 |
(unpack "C/<C C>" :from-string "\x01AB")) |
| 920 |
|
| 921 |
(test* "pack <>" "ABCD" |
| 922 |
(pack "<C C> 2" '((65 66) (67 68)) :to-string? #t)) |
| 923 |
|
| 924 |
(test* "unpack <>" '((65 66) (67 68)) |
| 925 |
(unpack "<C C> 2" :from-string "ABCD")) |
| 926 |
|
| 927 |
;; / tests |
| 928 |
|
| 929 |
(test-error "unpack / with no numeric type" |
| 930 |
#/unknown pack template/ |
| 931 |
(cut unpack "/a*" :from-string "hello")) |
| 932 |
|
| 933 |
(test* "unpack C/a*" '("yes") |
| 934 |
(unpack "C/a*" :from-string #*"\x03yes")) |
| 935 |
|
| 936 |
(test* "pack C/a*" "\x03yes" |
| 937 |
(pack "C/a*" '("yes") :to-string? #t)) |
| 938 |
|
| 939 |
(test* "unpack C/A*" '("yes") |
| 940 |
(unpack "C/A*" :from-string #*"\x03yes")) |
| 941 |
|
| 942 |
(test* "pack C/A*" "\x03yes" |
| 943 |
(pack "C/A*" '("yes") :to-string? #t)) |
| 944 |
|
| 945 |
(test* "unpack C/C*" '(65 66 67) |
| 946 |
(unpack "C/C*" :from-string "\x03ABC")) |
| 947 |
|
| 948 |
(test* "pack C/C*" "\x03ABC" |
| 949 |
(pack "C/C*" '(65 66 67) :to-string? #t)) |
| 950 |
|
| 951 |
(test* "unpack w/C*" '(65 66 67) |
| 952 |
(unpack "w/C*" :from-string "\x03ABC")) |
| 953 |
|
| 954 |
(test* "pack w/C*" "\x03ABC" |
| 955 |
(pack "w/C*" '(65 66 67) :to-string? #t)) |
| 956 |
|
| 957 |
;; not sure about supporting implicit type conversions |
| 958 |
;;(test* "pack / medley" "\x01A\x01B003ok \x03yes\x04z\x00" |
| 959 |
;; (pack "C/B* C/H* a3/A C/a* C/Z" '("01000001" "42" "ok" "yes" "z") :to-string? #t)) |
| 960 |
|
| 961 |
(test* "unpack / medley" '("01000001" "42" "ok" "yes" "z") |
| 962 |
(unpack "C/B* C/H* a3/A C/a* C/Z" :from-string "\x01A\x01B003ok \x03yes\x04z\x00abc")) |
| 963 |
|
| 964 |
;; @ tests |
| 965 |
|
| 966 |
(test* "pack fixed @" "a\0\0\0z" |
| 967 |
(pack "a1@4a1" '("a" "z") :to-string? #t)) |
| 968 |
|
| 969 |
(test* "pack fixed @ again" "a\0\0\0z" |
| 970 |
(pack "a1@4a1" '("a" "z") :to-string? #t)) |
| 971 |
|
| 972 |
(test* "unpack fixed @" '("a" "z") |
| 973 |
(unpack "a1@4a1" :from-string "a\0\0\0z")) |
| 974 |
|
| 975 |
(test* "pack variable @" "a\0\0\0z" |
| 976 |
(pack "a*@4a1" '("a" "z") :to-string? #t)) |
| 977 |
|
| 978 |
(test* "unpack fixed @" '("a" "z") |
| 979 |
(unpack "C/a @4 a1" :from-string "\x01a\0\0z")) |
| 980 |
|
| 981 |
(test* "pack twice @" "a\0\0\0mn\0\0\0z" |
| 982 |
(pack "a1@4a1a1@9a1" '("a" "m" "n" "z") :to-string? #t)) |
| 983 |
|
| 984 |
(test* "unpack twice @" '("a" "m" "n" "z") |
| 985 |
(unpack "a1@4a1a1@9a1" :from-string "a\0\0\0mn\0\0\0z")) |
| 986 |
|
| 987 |
;; These two fail in 0.9.1. Reported by ryoakg. |
| 988 |
(test* "V*; a bug of peeking 1" |
| 989 |
(unfold (pa$ <= 16) (^_`(,(* 16 _))) (pa$ + 1) 0 (^_'())) |
| 990 |
(map (.$ (cute unpack "V*" :from-string <>) |
| 991 |
u8vector->string |
| 992 |
(cute u8vector <> 0 0 0)) |
| 993 |
(iota 16 0 16))) |
| 994 |
(test* "V*: a bug of peeking 2" |
| 995 |
'(#x01010101 #x01010101 #x000000d0 #x01010101 #x01010101) |
| 996 |
(unpack "V*" :from-string |
| 997 |
#*"\x01\x01\x01\x01\ |
| 998 |
\x01\x01\x01\x01\ |
| 999 |
\xd0\x00\x00\x00\ |
| 1000 |
\x01\x01\x01\x01\ |
| 1001 |
\x01\x01\x01\x01")) |
| 1002 |
|
| 1003 |
(test-end) |