main
Matthew Butterick 6 years ago
parent 268190290a
commit c1d81f4169

@ -13,94 +13,94 @@ https://github.com/mbutterick/restructure/blob/master/test/Array.coffee
|#
(test-case
"decode fixed length"
"array: decode fixed length"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint8 #:length 4)) '(1 2 3 4))))
(test-case
"decode with post-decode"
"array: decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xa (+xarray #:type uint8 #:length 4 #:post-decode (λ (val) (map (λ (x) (* 2 x)) val))))
(check-equal? (decode xa) '(2 4 6 8))))
(test-case
"decode fixed number of bytes"
"array: decode fixed number of bytes"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint16be #:length 4 #:count-bytes #t)) '(258 772))))
(test-case
"decode length from parent key"
"array: decode length from parent key"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (send (+xarray #:type uint8 #:length 'len) xxdecode (current-input-port) (mhash 'len 4)) '(1 2 3 4))))
(test-case
"decode byte count from parent key"
"array: decode byte count from parent key"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (send (+xarray #:type uint16be #:length 'len #:count-bytes #t) xxdecode (current-input-port) (mhash 'len 4)) '(258 772))))
(test-case
"decode length as number before array"
"array: decode length as number before array"
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint8 #:length uint8)) '(1 2 3 4))))
(test-case
"decode byte count as number before array"
"array: decode byte count as number before array"
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint16be #:length uint8 #:count-bytes #t)) '(258 772))))
(test-case
"decode length from function"
"array: decode length from function"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint8 #:length (λ _ 4))) '(1 2 3 4))))
(test-case
"decode byte count from function"
"array: decode byte count from function"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (decode (+xarray #:type uint16be #:length (λ _ 4) #:count-bytes #t)) '(258 772))))
(test-case
"decode to the end of parent if no length given"
"array: decode to the end of parent if no length given"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (send (+xarray #:type uint8) xxdecode (current-input-port) (mhash '_length 4 '_startOffset 0)) '(1 2 3 4))))
(test-case
"decode to the end of the stream if parent exists, but its length is 0"
"array: decode to the end of the stream if parent exists, but its length is 0"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(check-equal? (send (+xarray #:type uint8) xxdecode (current-input-port) (mhash '_length 0 '_startOffset 0)) '(1 2 3 4 5))))
(test-case
"decode to the end of the stream if no parent and length is given"
"array: decode to the end of the stream if no parent and length is given"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4))])
(check-equal? (decode (+xarray #:type uint8)) '(1 2 3 4 ))))
(test-case
"use array length"
"array: use array length"
(check-equal? (size (+xarray #:type uint8 #:length 10) '(1 2 3 4)) 4))
(test-case
"add size of length field before string"
"array: add size of length field before string"
(check-equal? (size (+xarray #:type uint8 #:length uint8) '(1 2 3 4)) 5))
(test-case
"use defined length if no value given"
"array: use defined length if no value given"
(check-equal? (size (+xarray #:type uint8 #:length 10)) 10))
(test-case
"encode using array length"
"array: encode using array length"
(check-equal? (encode (+xarray #:type uint8 #:length 10) '(1 2 3 4) #f) (bytes 1 2 3 4)))
(test-case
"encode with pre-encode"
"array: encode with pre-encode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xa (+xarray #:type uint8 #:length 4 #:pre-encode (λ (val) (map (λ (x) (* 2 x)) val))))
(check-equal? (encode xa '(1 2 3 4) #f) (bytes 2 4 6 8))))
(test-case
"encode length as number before array"
"array: encode length as number before array"
(check-equal? (encode (+xarray #:type uint8 #:length uint8) '(1 2 3 4) #f) (bytes 4 1 2 3 4)))
(test-case
"add pointers after array if length is encoded at start"
"array: add pointers after array if length is encoded at start"
(check-equal? (encode (+xarray #:type (+xpointer #:offset-type uint8
#:type uint8)
#:length uint8) '(1 2 3 4) #f) (bytes 4 5 6 7 8 1 2 3 4)))

@ -18,11 +18,11 @@ https://github.com/mbutterick/restructure/blob/master/test/Bitfield.coffee
(map (λ (x) (arithmetic-shift 1 x)) (range 8)))
(test-case
"bitfield should have the right size"
"bitfield: should have the right size"
(check-equal? (size bitfield) 1))
(test-case
"bitfield should decode"
"bitfield: should decode"
(parameterize ([current-input-port (open-input-bytes (bytes (bitwise-ior JACK MACK PACK NACK QUACK)))])
(check-equal? (decode bitfield) (mhasheq 'Quack #t
'Nack #t
@ -34,7 +34,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Bitfield.coffee
'Kack #f))))
(test-case
"bitfield should decode with post-decode"
"bitfield: should decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes (bitwise-ior JACK MACK PACK NACK QUACK)))])
(define bitfield (+xbitfield uint8 '(Jack Kack Lack Mack Nack Oack Pack Quack) #:post-decode (λ (fh) (hash-set! fh 'foo 42) fh)))
(check-equal? (decode bitfield) (mhasheq 'Quack #t
@ -48,7 +48,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Bitfield.coffee
'foo 42))))
(test-case
"bitfield should encode"
"bitfield: should encode"
(check-equal? (encode bitfield (mhasheq 'Quack #t
'Nack #t
'Lack #f
@ -60,7 +60,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Bitfield.coffee
(bytes (bitwise-ior JACK MACK PACK NACK QUACK))))
(test-case
"bitfield should encode with pre-encode"
"bitfield: should encode with pre-encode"
(define bitfield (+xbitfield uint8 '(Jack Kack Lack Mack Nack Oack Pack Quack)
#:pre-encode (λ (fh)
(hash-set! fh 'Jack #f)

@ -11,47 +11,47 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee
|#
(test-case
"buffer should decode"
"buffer: should decode"
(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff #x1f #xb6))])
(define buf (+xbuffer #:length 2))
(check-equal? (decode buf) (bytes #xab #xff))
(check-equal? (decode buf) (bytes #x1f #xb6))))
(test-case
"buffer should error on invalid length"
"buffer: should error on invalid length"
(check-exn exn:fail:contract? (λ () (+xbuffer #:length #true))))
(test-case
"buffer should decode with post-decode"
"buffer: should decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff #x1f #xb6))])
(define buf (+xbuffer #:length 2 #:post-decode (λ (val) (bytes 1 2))))
(check-equal? (decode buf) (bytes 1 2))
(check-equal? (decode buf) (bytes 1 2))))
(test-case
"buffer should decode with parent key length"
"buffer: should decode with parent key length"
(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff #x1f #xb6))])
(define buf (+xbuffer #:length 'len))
(check-equal? (decode buf #:parent (hash 'len 3)) (bytes #xab #xff #x1f))
(check-equal? (decode buf #:parent (hash 'len 1)) (bytes #xb6))))
(test-case
"size should return size"
"buffer: hould return size"
(check-equal? (size (+xbuffer #:length 2) (bytes #xab #xff)) 2))
(test-case
"size should use defined length if no value given"
"buffer: hould use defined length if no value given"
(check-equal? (size (+xbuffer #:length 10)) 10))
(test-case
"encode should encode"
"buffer: should encode"
(let ([buf (+xbuffer 2)])
(check-equal? (bytes-append
(encode buf (bytes #xab #xff) #f)
(encode buf (bytes #x1f #xb6) #f)) (bytes #xab #xff #x1f #xb6))))
(test-case
"encode should encode with pre-encode"
"buffer: should encode with pre-encode"
(let ()
(define buf (+xbuffer 2 #:pre-encode (λ (val) (bytes 1 2))))
(check-equal? (bytes-append
@ -59,5 +59,5 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee
(encode buf (bytes #x1f #xb6) #f)) (bytes 1 2 1 2))))
(test-case
"encode should encode length before buffer"
"buffer: should encode length before buffer"
(check-equal? (encode (+xbuffer #:length uint8) (bytes #xab #xff) #f) (bytes 2 #xab #xff)))

@ -14,26 +14,26 @@ https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
#:values '("foo" "bar" "baz")))
(test-case
"should error with invalid type"
"enum: should error with invalid type"
(check-exn exn:fail:contract? (λ () (+xenum 42))))
(test-case
"should error with invalid values"
"enum: should error with invalid values"
(check-exn exn:fail:contract? (λ () (+xenum #:values 42))))
(test-case
"should have the right size"
"enum: should have the right size"
(check-equal? (size e) 1))
(test-case
"decode should decode"
"enum: decode should decode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 0))])
(check-equal? (decode e) "bar")
(check-equal? (decode e) "baz")
(check-equal? (decode e) "foo")))
(test-case
"decode should decode with post-decode"
"enum: decode should decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 0))])
(define e2 (+xenum #:type uint8
#:values '("foo" "bar" "baz")
@ -43,7 +43,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
(check-equal? (decode e2) "foobar")))
(test-case
"encode should encode"
"enum: encode should encode"
(parameterize ([current-output-port (open-output-bytes)])
(encode e "bar")
(encode e "baz")
@ -51,7 +51,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 1 2 0))))
(test-case
"encode should encode with pre-encode"
"enum: encode should encode with pre-encode"
(parameterize ([current-output-port (open-output-bytes)])
(define e2 (+xenum #:type uint8
#:values '("foo" "bar" "baz")
@ -62,5 +62,5 @@ https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 0 0 0))))
(test-case
"should throw on unknown option"
"enum: should throw on unknown option"
(check-exn exn:fail:contract? (λ () (encode e "unknown" (open-output-bytes)))))

@ -14,7 +14,7 @@ https://github.com/mbutterick/restructure/blob/master/test/LazyArray.coffee
|#
(test-case
"decode should decode items lazily"
"lazy-array: decode should decode items lazily"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4))
(define arr (decode xla))
@ -26,7 +26,7 @@ https://github.com/mbutterick/restructure/blob/master/test/LazyArray.coffee
(check-equal? (stream-ref arr 3) 4)))
(test-case
"decode should decode items lazily with post-decode"
"lazy-array: decode should decode items lazily with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4 #:post-decode (λ (str) (stream-map (λ (i) (* 2 i)) str))))
(define arr (decode xla))
@ -39,35 +39,35 @@ https://github.com/mbutterick/restructure/blob/master/test/LazyArray.coffee
(check-equal? (stream-ref arr 3) 8)))
(test-case
"should be able to convert to an array"
"lazy-array: should be able to convert to an array"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4))
(define arr (decode xla))
(check-equal? (stream->list arr) '(1 2 3 4))))
(test-case
"decode should decode length as number before array"
"lazy-array: decode should decode length as number before array"
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(define xla (+xlazy-array uint8 uint8))
(define arr (decode xla))
(check-equal? (stream->list arr) '(1 2 3 4))))
(test-case
"size should work with xlazy-arrays"
"lazy-array: size should work with xlazy-arrays"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4))
(define arr (decode xla))
(check-equal? (size xla arr) 4)))
(test-case
"encode should work with xlazy-arrays"
"lazy-array: encode should work with xlazy-arrays"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4))
(define arr (decode xla))
(check-equal? (encode xla arr #f) (bytes 1 2 3 4))))
(test-case
"encode should work with xlazy-arrays with pre-encode"
"lazy-array: encode should work with xlazy-arrays with pre-encode"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define xla (+xlazy-array uint8 4 #:pre-encode (λ (str) (stream-map (λ (val) (* 2 val)) str))))
(define arr (decode xla))

@ -10,7 +10,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
|#
(test-case
"uint8: decode, size, encode"
"number: uint8: decode, size, encode"
(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff))])
(check-equal? (decode uint8) #xab)
(check-equal? (decode uint8) #xff))
@ -21,7 +21,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
(check-equal? (get-output-bytes port) (bytes #xab #xff))))
(test-case
"uint8: decode with post-decode, size, encode with pre-encode"
"number: uint8: decode with post-decode, size, encode with pre-encode"
(define myuint8 (+xint 1 #:signed #f
#:post-decode (λ (val) #xdeadbeef)
#:pre-encode (λ (val) #xcc)))
@ -35,56 +35,56 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
(check-equal? (get-output-bytes port) (bytes #xcc #xcc))))
(test-case
"uint16 is the same endianness as the platform"
"number: uint16 is the same endianness as the platform"
(check-equal? (decode uint16 (bytes 0 1))
(decode (if (system-big-endian?) uint16be uint16le) (bytes 0 1))))
(test-case
"uint16be: decode, size, encode"
"number: uint16be: decode, size, encode"
(check-equal? (decode uint16be (open-input-bytes (bytes #xab #xff))) #xabff)
(check-equal? (size uint16be) 2)
(check-equal? (encode uint16be #xabff #f) (bytes #xab #xff)))
(test-case
"uint16le: decode, size, encode"
"number: uint16le: decode, size, encode"
(check-equal? (decode uint16le (open-input-bytes (bytes #xff #xab))) #xabff)
(check-equal? (size uint16le) 2)
(check-equal? (encode uint16le #xabff #f) (bytes #xff #xab)))
(test-case
"uint24 is the same endianness as the platform"
"number: uint24 is the same endianness as the platform"
(check-equal? (decode uint24 (bytes 0 1 2))
(decode (if (system-big-endian?) uint24be uint24le) (bytes 0 1 2))))
(test-case
"uint24be: decode, size, encode"
"number: uint24be: decode, size, encode"
(check-equal? (decode uint24be (open-input-bytes (bytes #xff #xab #x24))) #xffab24)
(check-equal? (size uint24be) 3)
(check-equal? (encode uint24be #xffab24 #f) (bytes #xff #xab #x24)))
(test-case
"uint24le: decode, size, encode"
"number: uint24le: decode, size, encode"
(check-equal? (decode uint24le (open-input-bytes (bytes #x24 #xab #xff))) #xffab24)
(check-equal? (size uint24le) 3)
(check-equal? (encode uint24le #xffab24 #f) (bytes #x24 #xab #xff)))
(test-case
"uint32 is the same endianness as the platform"
"number: uint32 is the same endianness as the platform"
(check-equal? (decode uint32 (bytes 0 1 2 3))
(decode (if (system-big-endian?) uint32be uint32le) (bytes 0 1 2 3))))
(test-case
"uint32be: decode, size, encode"
"number: uint32be: decode, size, encode"
(check-equal? (decode uint32be (open-input-bytes (bytes #xff #xab #x24 #xbf))) #xffab24bf)
(check-equal? (size uint32be) 4)
(check-equal? (encode uint32be #xffab24bf #f) (bytes #xff #xab #x24 #xbf)))
(test-case
"uint32le: decode, size, encode"
"number: uint32le: decode, size, encode"
(check-equal? (decode uint32le (open-input-bytes (bytes #xbf #x24 #xab #xff))) #xffab24bf)
(check-equal? (size uint32le) 4)
(check-equal? (encode uint32le #xffab24bf #f) (bytes #xbf #x24 #xab #xff)))
(test-case
"int8: decode, size, encode"
"number: int8: decode, size, encode"
(let ([port (open-input-bytes (bytes #x7f #xff))])
(check-equal? (decode int8 port) 127)
(check-equal? (decode int8 port) -1))
@ -95,11 +95,11 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
(check-equal? (get-output-bytes port) (bytes #x7f #xff))))
(test-case
"int32 is the same endianness as the platform"
"number: int32 is the same endianness as the platform"
(check-equal? (decode int16 (bytes 0 1))
(decode (if (system-big-endian?) int16be int16le) (bytes 0 1))))
(test-case
"int16be: decode, size, encode"
"number: int16be: decode, size, encode"
(let ([port (open-input-bytes (bytes #xff #xab))])
(check-equal? (decode int16be port) -85))
(check-equal? (size int16be) 2)
@ -108,105 +108,105 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
(check-equal? (get-output-bytes port) (bytes #xff #xab))))
(test-case
"int16le: decode, size, encode"
"number: int16le: decode, size, encode"
(check-equal? (decode int16le (open-input-bytes (bytes #xab #xff))) -85)
(check-equal? (size int16le) 2)
(check-equal? (encode int16le -85 #f) (bytes #xab #xff)))
(test-case
"int24 is the same endianness as the platform"
"number: int24 is the same endianness as the platform"
(check-equal? (decode int24 (bytes 0 1 2))
(decode (if (system-big-endian?) int24be int24le) (bytes 0 1 2))))
(test-case
"int24be: decode, size, encode"
"number: int24be: decode, size, encode"
(check-equal? (decode int24be (open-input-bytes (bytes #xff #xab #x24))) -21724)
(check-equal? (size int24be) 3)
(check-equal? (encode int24be -21724 #f) (bytes #xff #xab #x24)))
(test-case
"int24le: decode, size, encode"
"number: int24le: decode, size, encode"
(check-equal? (decode int24le (open-input-bytes (bytes #x24 #xab #xff))) -21724)
(check-equal? (size int24le) 3)
(check-equal? (encode int24le -21724 #f) (bytes #x24 #xab #xff)))
(test-case
"int32 is the same endianness as the platform"
"number: int32 is the same endianness as the platform"
(check-equal? (decode int32 (bytes 0 1 2 3))
(decode (if (system-big-endian?) int32be int32le) (bytes 0 1 2 3))))
(test-case
"int32be: decode, size, encode"
"number: int32be: decode, size, encode"
(check-equal? (decode int32be (open-input-bytes (bytes #xff #xab #x24 #xbf))) -5561153)
(check-equal? (size int32be) 4)
(check-equal? (encode int32be -5561153 #f) (bytes #xff #xab #x24 #xbf)))
(test-case
"int32le: decode, size, encode"
"number: int32le: decode, size, encode"
(check-equal? (decode int32le (open-input-bytes (bytes #xbf #x24 #xab #xff))) -5561153)
(check-equal? (size int32le) 4)
(check-equal? (encode int32le -5561153 #f) (bytes #xbf #x24 #xab #xff)))
(test-case
"float is the same endianness as the platform"
"number: float is the same endianness as the platform"
(check-equal? (decode float (bytes 0 1 2 3))
(decode (if (system-big-endian?) floatbe floatle) (bytes 0 1 2 3))))
(test-case
"floatbe: decode, size, encode"
"number: floatbe: decode, size, encode"
(check-= (decode floatbe (open-input-bytes (bytes #x43 #x7a #x8c #xcd))) 250.55 0.01)
(check-equal? (size floatbe) 4)
(check-equal? (encode floatbe 250.55 #f) (bytes #x43 #x7a #x8c #xcd)))
(test-case
"floatle: decode, size, encode"
"number: floatle: decode, size, encode"
(check-= (decode floatle (open-input-bytes (bytes #xcd #x8c #x7a #x43))) 250.55 0.01)
(check-equal? (size floatle) 4)
(check-equal? (encode floatle 250.55 #f) (bytes #xcd #x8c #x7a #x43)))
(test-case
"double is the same endianness as the platform"
"number: double is the same endianness as the platform"
(check-equal? (decode double (bytes 0 1 2 3 4 5 6 7))
(decode (if (system-big-endian?) doublebe doublele) (bytes 0 1 2 3 4 5 6 7))))
(test-case
"doublebe: decode, size, encode"
"number: doublebe: decode, size, encode"
(check-equal? (decode doublebe (open-input-bytes (bytes #x40 #x93 #x4a #x3d #x70 #xa3 #xd7 #x0a))) 1234.56)
(check-equal? (size doublebe) 8)
(check-equal? (encode doublebe 1234.56 #f) (bytes #x40 #x93 #x4a #x3d #x70 #xa3 #xd7 #x0a)))
(test-case
"doublele: decode, size, encode"
"number: doublele: decode, size, encode"
(check-equal? (decode doublele (open-input-bytes (bytes #x0a #xd7 #xa3 #x70 #x3d #x4a #x93 #x40))) 1234.56)
(check-equal? (size doublele) 8)
(check-equal? (encode doublele 1234.56 #f) (bytes #x0a #xd7 #xa3 #x70 #x3d #x4a #x93 #x40)))
(test-case
"fixed16 is the same endianness as the platform"
"number: fixed16 is the same endianness as the platform"
(check-equal? (decode fixed16 (bytes 0 1))
(decode (if (system-big-endian?) fixed16be fixed16le) (bytes 0 1))))
(test-case
"fixed16be: decode, size, encode"
"number: fixed16be: decode, size, encode"
(check-= (decode fixed16be (open-input-bytes (bytes #x19 #x57))) 25.34 0.01)
(check-equal? (size fixed16be) 2)
(check-equal? (encode fixed16be 25.34 #f) (bytes #x19 #x57)))
(test-case
"fixed16le: decode, size, encode"
"number: fixed16le: decode, size, encode"
(check-= (decode fixed16le (open-input-bytes (bytes #x57 #x19))) 25.34 0.01)
(check-equal? (size fixed16le) 2)
(check-equal? (encode fixed16le 25.34 #f) (bytes #x57 #x19)))
(test-case
"fixed32 is the same endianness as the platform"
"number: fixed32 is the same endianness as the platform"
(check-equal? (decode fixed32 (bytes 0 1 2 3))
(decode (if (system-big-endian?) fixed32be fixed32le) (bytes 0 1 2 3))))
(test-case
"fixed32be: decode, size, encode"
"number: fixed32be: decode, size, encode"
(check-= (decode fixed32be (open-input-bytes (bytes #x00 #xfa #x8c #xcc))) 250.55 0.01)
(check-equal? (size fixed32be) 4)
(check-equal? (encode fixed32be 250.55 #f) (bytes #x00 #xfa #x8c #xcc)))
(test-case
"fixed32le: decode, size, encode"
"number: fixed32le: decode, size, encode"
(check-= (decode fixed32le (open-input-bytes (bytes #xcc #x8c #xfa #x00))) 250.55 0.01)
(check-equal? (size fixed32le) 4)
(check-equal? (encode fixed32le 250.55 #f) (bytes #xcc #x8c #xfa #x00)))

@ -12,104 +12,104 @@ https://github.com/mbutterick/restructure/blob/master/test/Optional.coffee
|#
(test-case
"decode should not decode when condition is falsy"
"optional: decode should not decode when condition is falsy"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8 #:condition #f))
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0)))
(test-case
"decode with post-decode"
"optional: decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8 #:condition #f #:post-decode (λ (val) 42)))
(check-equal? (decode optional) 42)
(check-equal? (pos (current-input-port)) 0)))
(test-case
"decode should not decode when condition is a function and falsy"
"optional: decode should not decode when condition is a function and falsy"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8 #:condition (λ _ #f)))
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0)))
(test-case
"decode should decode when condition is omitted"
"optional: decode should decode when condition is omitted"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
"decode should decode when condition is truthy"
"optional: decode should decode when condition is truthy"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8 #:condition #t))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
"decode should decode when condition is a function and truthy"
"optional: decode should decode when condition is a function and truthy"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+xoptional #:type uint8 #:condition (λ _ #t)))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1)))
(test-case
"size"
"optional: size"
(check-equal? (size (+xoptional #:type uint8 #:condition #f)) 0))
(test-case
"size should return 0 when condition is a function and falsy"
"optional: size should return 0 when condition is a function and falsy"
(check-equal? (size (+xoptional #:type uint8 #:condition (λ _ #f))) 0))
(test-case
"size should return given type size when condition is omitted"
"optional: size should return given type size when condition is omitted"
(check-equal? (size (+xoptional #:type uint8)) 1))
(test-case
"size should return given type size when condition is truthy"
"optional: size should return given type size when condition is truthy"
(check-equal? (size (+xoptional #:type uint8 #:condition #t)) 1))
(test-case
"size should return given type size when condition is a function and truthy"
"optional: size should return given type size when condition is a function and truthy"
(check-equal? (size (+xoptional #:type uint8 #:condition (λ _ #t))) 1))
(test-case
"encode should not encode when condition is falsy"
"optional: encode should not encode when condition is falsy"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8 #:condition #f))
(encode optional 128)
(check-equal? (get-output-bytes (current-output-port)) (bytes))))
(test-case
"encode with pre-encode"
"optional: encode with pre-encode"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8 #:pre-encode (λ (val) 42)))
(encode optional 128)
(check-equal? (get-output-bytes (current-output-port)) (bytes 42))))
(test-case
"encode should not encode when condition is a function and falsy"
"optional: encode should not encode when condition is a function and falsy"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8 #:condition (λ _ #f)))
(encode optional 128)
(check-equal? (get-output-bytes (current-output-port)) (bytes))))
(test-case
"encode should encode when condition is omitted"
"optional: encode should encode when condition is omitted"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8))
(encode optional 128)
(check-equal? (get-output-bytes (current-output-port)) (bytes 128))))
(test-case
"encode should encode when condition is truthy"
"optional: encode should encode when condition is truthy"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8 #:condition #t))
(encode optional 128)
(check-equal? (get-output-bytes (current-output-port)) (bytes 128))))
(test-case
"encode should encode when condition is a function and truthy"
"optional: encode should encode when condition is a function and truthy"
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+xoptional #:type uint8 #:condition (λ _ #t)))
(encode optional 128)

@ -16,86 +16,86 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
|#
(test-case
"decode should handle null pointers"
"pointer: decode should handle null pointers"
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(check-false (decode (+xpointer) #:parent (mhash '_startOffset 50)))))
(test-case
"decode should use local offsets from start of parent by default"
"pointer: decode should use local offsets from start of parent by default"
(parameterize ([current-input-port (open-input-bytes (bytes 1 53))])
(check-equal? (decode (+xpointer) #:parent (mhash '_startOffset 0)) 53)))
(test-case
"decode should support immediate offsets"
"pointer: decode should support immediate offsets"
(parameterize ([current-input-port (open-input-bytes (bytes 1 53))])
(check-equal? (decode (+xpointer #:relative-to 'immediate)) 53)))
(test-case
"decode should support offsets relative to the parent"
"pointer: decode should support offsets relative to the parent"
(parameterize ([current-input-port (open-input-bytes (bytes 0 0 1 53))])
(pos (current-input-port) 2)
(check-equal? (decode (+xpointer #:relative-to 'parent) #:parent (mhash 'parent (mhash '_startOffset 2))) 53)))
(test-case
"decode should support global offsets"
"pointer: decode should support global offsets"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 4 0 0 0 53))])
(pos (current-input-port) 2)
(check-equal? (decode (+xpointer #:relative-to 'global) #:parent (mhash 'parent (mhash 'parent (mhash '_startOffset 2))))
53)))
(test-case
"decode should support returning pointer if there is no decode type"
"pointer: decode should support returning pointer if there is no decode type"
(parameterize ([current-input-port (open-input-bytes (bytes 4))])
(check-equal? (decode (+xpointer uint8 'void) #:parent (mhash '_startOffset 0)) 4)))
(test-case
"decode should support decoding pointers lazily"
"pointer: decode should support decoding pointers lazily"
(parameterize ([current-input-port (open-input-bytes (bytes 1 53))])
(define res (decode (+xstruct 'ptr (+xpointer #:lazy #t))))
(check-true (promise? (dict-ref res 'ptr)))
(check-equal? (force (dict-ref res 'ptr)) 53)))
(test-case
"size"
"pointer: size"
(let ([parent (mhash 'pointerSize 0)])
(check-equal? (size (+xpointer) 10 #:parent parent) 1)
(check-equal? (dict-ref parent 'pointerSize) 1)))
(test-case
"size should add to immediate pointerSize"
"pointer: size should add to immediate pointerSize"
(let ([parent (mhash 'pointerSize 0)])
(check-equal? (size (+xpointer #:relative-to 'immediate) 10 #:parent parent) 1)
(check-equal? (dict-ref parent 'pointerSize) 1)))
(test-case
"size should add to parent pointerSize"
"pointer: size should add to parent pointerSize"
(let ([parent (mhash 'parent (mhash 'pointerSize 0))])
(check-equal? (size (+xpointer #:relative-to 'parent) 10 #:parent parent) 1)
(check-equal? (dict-ref* parent 'parent 'pointerSize) 1)))
(test-case
"size should add to global pointerSize"
"pointer: size should add to global pointerSize"
(let ([parent (mhash 'parent (mhash 'parent (mhash 'parent (mhash 'pointerSize 0))))])
(check-equal? (size (+xpointer #:relative-to 'global) 10 #:parent parent) 1)
(check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointerSize) 1)))
(test-case
"size should handle void pointers"
"pointer: size should handle void pointers"
(let ([parent (mhash 'pointerSize 0)])
(check-equal? (size (+xpointer uint8 'void) (+xvoid-pointer uint8 50) #:parent parent) 1)
(check-equal? (dict-ref parent 'pointerSize) 1)))
(test-case
"size should throw if no type and not a void pointer"
"pointer: size should throw if no type and not a void pointer"
(let ([parent (mhash 'pointerSize 0)])
(check-exn exn:fail:contract? (λ () (size (+xpointer uint8 'void) 30 #:parent parent)))))
(test-case
"size should return a fixed size without a value"
"pointer: size should return a fixed size without a value"
(check-equal? (size (+xpointer)) 1))
(test-case
"encode should handle null pointers"
"pointer: encode should handle null pointers"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'pointerSize 0
'startOffset 0
@ -106,7 +106,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 0))))
(test-case
"encode should handle local offsets"
"pointer: encode should handle local offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'pointerSize 0
'startOffset 0
@ -120,7 +120,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case
"encode should handle immediate offsets"
"pointer: encode should handle immediate offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'pointerSize 0
'startOffset 0
@ -134,7 +134,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 0))))
(test-case
"encode should handle offsets relative to parent"
"pointer: encode should handle offsets relative to parent"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'parent (mhash 'pointerSize 0
'startOffset 3
@ -148,7 +148,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 2))))
(test-case
"encode should handle global offsets"
"pointer: encode should handle global offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'parent
(mhash 'parent
@ -165,7 +165,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 5))))
(test-case
"encode should support void pointers"
"pointer: encode should support void pointers"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'pointerSize 0
'startOffset 0
@ -177,7 +177,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case
"encode should throw if not a void pointer instance"
"pointer: encode should throw if not a void pointer instance"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'pointerSize 0
'startOffset 0

@ -12,36 +12,36 @@ https://github.com/mbutterick/restructure/blob/master/test/Reserved.coffee
|#
(test-case
"size should have a default count of 1"
"reserved: size should have a default count of 1"
(check-equal? (size (+xreserved uint8)) 1))
(test-case
"size should allow custom counts and types"
"reserved: size should allow custom counts and types"
(check-equal? (size (+xreserved uint16be 10)) 20))
(test-case
"should decode"
"reserved: should decode"
(parameterize ([current-input-port (open-input-bytes (bytes 0 0))])
(define reserved (+xreserved uint16be))
(check-equal? (decode reserved) (void))
(check-equal? (pos (current-input-port)) 2)))
(test-case
"should decode with post-decode"
"reserved: should decode with post-decode"
(parameterize ([current-input-port (open-input-bytes (bytes 0 0))])
(define reserved (+xreserved uint16be #:post-decode (λ (val) 42)))
(check-equal? (decode reserved) 42)
(check-equal? (pos (current-input-port)) 2)))
(test-case
"should encode"
"reserved: should encode"
(parameterize ([current-output-port (open-output-bytes)])
(define reserved (+xreserved uint16be))
(encode reserved #f)
(check-equal? (get-output-bytes (current-output-port)) (bytes 0 0))))
(test-case
"should encode with pre-encode"
"reserved: should encode with pre-encode"
(parameterize ([current-output-port (open-output-bytes)])
(define reserved (+xreserved uint32be #:pre-encode (λ (val) 42)))
(encode reserved #f)

@ -13,112 +13,112 @@ https://github.com/mbutterick/restructure/blob/master/test/String.coffee
|#
(test-case
"decode fixed length"
"string: decode fixed length"
(parameterize ([current-input-port (open-input-bytes #"testing")])
(check-equal? (decode (+xstring 7)) "testing")))
(test-case
"decode fixed length with post-decode"
"string: decode fixed length with post-decode"
(parameterize ([current-input-port (open-input-bytes #"testing")])
(define xs (+xstring 7 #:post-decode (λ (val) "ring a ding")))
(check-equal? (decode xs) "ring a ding")))
(test-case
"decode length from parent key"
"string: decode length from parent key"
(parameterize ([current-input-port (open-input-bytes #"testing")])
(check-equal? (send (+xstring 'len) xxdecode (current-input-port) (mhash 'len 7)) "testing")))
(test-case
"decode length as number before string"
"string: decode length as number before string"
(parameterize ([current-input-port (open-input-bytes #"\x07testing")])
(check-equal? (send (+xstring uint8) xxdecode (current-input-port) (mhash 'len 7)) "testing")))
(test-case
"decode utf8"
"string: decode utf8"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+xstring 4 'utf8)) "🍻")))
(test-case
"decode encoding computed from function"
"string: decode encoding computed from function"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+xstring 4 (λ _ 'utf8))) "🍻")))
(test-case
"decode null-terminated string and read past terminator"
"string: decode null-terminated string and read past terminator"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻\x00"))])
(check-equal? (decode (+xstring #f 'utf8)) "🍻")
(check-equal? (pos (current-input-port)) 5)))
(test-case
"decode remainder of buffer when null-byte missing"
"string: decode remainder of buffer when null-byte missing"
(parameterize ([current-input-port (open-input-bytes (string->bytes/utf-8 "🍻"))])
(check-equal? (decode (+xstring #f 'utf8)) "🍻")))
(test-case
"size should use string length"
"string: size should use string length"
(check-equal? (size (+xstring 7) "testing") 7))
(test-case
"size should use correct encoding"
"string: size should use correct encoding"
(check-equal? (size (+xstring 10 'utf8) "🍻") 4))
(test-case
"size should use encoding from function"
"string: size should use encoding from function"
(check-equal? (size (+xstring 10 (λ _ 'utf8)) "🍻") 4))
(test-case
"should add size of length field before string"
"string: should add size of length field before string"
(check-equal? (size (+xstring uint8 'utf8) "🍻") 5))
; todo: it "should work with utf16be encoding"
(test-case
"size should take null-byte into account"
"string: size should take null-byte into account"
(check-equal? (size (+xstring #f 'utf8) "🍻") 5))
(test-case
"size should use defined length if no value given"
"string: size should use defined length if no value given"
(check-equal? (size (+xstring 10)) 10))
(test-case
"encode using string length"
"string: encode using string length"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring 7) "testing")
(check-equal? (get-output-bytes (current-output-port)) #"testing")))
(test-case
"encode using string length and pre-encode"
"string: encode using string length and pre-encode"
(parameterize ([current-output-port (open-output-bytes)])
(define xs (+xstring 7 #:pre-encode (λ (val) (list->string (reverse (string->list val))))))
(encode xs "testing")
(check-equal? (get-output-bytes (current-output-port)) #"gnitset")))
(test-case
"encode length as number before string"
"string: encode length as number before string"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring uint8) "testing")
(check-equal? (get-output-bytes (current-output-port)) #"\x07testing")))
(test-case
"encode length as number before string utf8"
"string: encode length as number before string utf8"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring uint8 'utf8) "testing 😜")
(check-equal? (get-output-bytes (current-output-port)) (string->bytes/utf-8 "\x0ctesting 😜"))))
(test-case
"encode utf8"
"string: encode utf8"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring 4 'utf8) "🍻" )
(check-equal? (get-output-bytes (current-output-port)) (string->bytes/utf-8 "🍻"))))
(test-case
"encode encoding computed from function"
"string: encode encoding computed from function"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring 4 (λ _ 'utf8)) "🍻")
(check-equal? (get-output-bytes (current-output-port)) (string->bytes/utf-8 "🍻"))))
(test-case
"encode null-terminated string"
"string: encode null-terminated string"
(parameterize ([current-output-port (open-output-bytes)])
(encode (+xstring #f 'utf8) "🍻" )
(check-equal? (get-output-bytes (current-output-port)) (string->bytes/utf-8 "🍻\x00"))))

@ -15,14 +15,14 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
|#
(test-case
"decode into an object"
"struct: decode into an object"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal?
(decode (+xstruct 'name (+xstring #:length uint8) 'age uint8))
(mhasheq 'name "roxyb" 'age 21))))
(test-case
"decode with process hook"
"struct: decode with process hook"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x20")])
(define struct (+xstruct #:post-decode (λ (o) (dict-set! o 'canDrink (>= (dict-ref o 'age) 21)) o)
'name (+xstring #:length uint8) 'age uint8))
@ -30,40 +30,40 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(mhasheq 'name "roxyb" 'age 32 'canDrink #t))))
(test-case
"decode supports function keys"
"struct: decode supports function keys"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x20")])
(define struct (+xstruct 'name (+xstring #:length uint8) 'age uint8 'canDrink (λ (o) (>= (dict-ref o 'age) 21))))
(check-equal? (decode struct)
(mhasheq 'name "roxyb" 'age 32 'canDrink #t))))
(test-case
"compute the correct size"
"struct: compute the correct size"
(check-equal? (size (+xstruct 'name (+xstring #:length uint8) 'age uint8)
(hasheq 'name "roxyb" 'age 32)) 7))
(test-case
"compute the correct size with pointers"
"struct: compute the correct size with pointers"
(check-equal? (size (+xstruct 'name (+xstring #:length uint8)
'age uint8
'ptr (+xpointer #:type (+xstring #:length uint8)))
(mhash 'name "roxyb" 'age 21 'ptr "hello")) 14))
(test-case
"get the correct size when no value is given"
"struct: get the correct size when no value is given"
(check-equal? (size (+xstruct 'name (+xstring 4) 'age uint8)) 5))
(test-case
"throw when getting non-fixed length size and no value is given"
"struct: throw when getting non-fixed length size and no value is given"
(check-exn exn:fail:contract? (λ () (size (+xstruct 'name (+xstring #:length uint8) 'age uint8)))))
(test-case
"encode objects to buffers"
"struct: encode objects to buffers"
(parameterize ([current-input-port (open-input-bytes #"\x05roxyb\x15")])
(check-equal? (decode (+xstruct 'name (+xstring #:length uint8) 'age uint8))
(mhasheq 'name "roxyb" 'age 21))))
(test-case
"support pre-encode hook"
"struct: support pre-encode hook"
(parameterize ([current-output-port (open-output-bytes)])
(define struct (+xstruct #:pre-encode (λ (val)
(dict-set! val 'nameLength (string-length (dict-ref val 'name))) val)
@ -74,7 +74,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(check-equal? (get-output-bytes (current-output-port)) #"\x05roxyb\x15")))
(test-case
"encode pointer data after structure"
"struct: encode pointer data after structure"
(parameterize ([current-output-port (open-output-bytes)])
(define struct (+xstruct 'name (+xstring #:length uint8)
'age uint8

@ -16,7 +16,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
|#
(test-case
"decode should get version from number type"
"versioned struct: decode should get version from number type"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -30,7 +30,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (decode vstruct) (mhasheq 'name "roxyb 🤘" 'age 21 'version 1 'gender 0)))))
(test-case
"decode should throw for unknown version"
"versioned struct: decode should throw for unknown version"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -42,7 +42,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-exn exn:fail:contract? (λ () (decode vstruct))))))
(test-case
"decode should support common header block"
"versioned struct: decode should support common header block"
(let ([vstruct (+xversioned-struct uint8
(dictify
'header (dictify 'age uint8
@ -63,7 +63,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'gender 0)))))
(test-case
"decode should support parent version key"
"versioned struct: decode should support parent version key"
(let ([vstruct (+xversioned-struct 'version
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -79,7 +79,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(mhasheq 'name "roxyb 🤘" 'age 21 'version 1 'gender 0)))))
(test-case
"decode should support sub versioned structs"
"versioned struct: decode should support sub versioned structs"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -99,22 +99,22 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (decode vstruct #:parent (mhash 'version 0))
(mhasheq 'name "ice cream" 'isDessert 1 'version 1)))))
#;(test-case
"decode should support process hook"
(let ([vstruct (+xversioned-struct uint8
(test-case
"versioned struct: decode should support process hook"
(let ([vstruct (+xversioned-struct #:post-decode (λ (val) (dict-set! val 'processed "true") val)
uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
'age uint8)
1 (+xstruct 'name (+xstring #:length uint8 #:encoding 'utf8)
'age uint8
'gender uint8)))])
(set-post-decode! vstruct (λ (val) (dict-set! val 'processed "true") val))
(parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")])
(check-equal? (decode vstruct)
(mhasheq 'name "roxyb" 'processed "true" 'age 21 'version 0)))))
(test-case
"size should compute the correct size"
"versioned struct: size should compute the correct size"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -131,7 +131,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'version 1)) 14)))
(test-case
"size should throw for unknown version"
"versioned struct: size should throw for unknown version"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -142,7 +142,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-exn exn:fail:contract? (λ () (size vstruct (mhasheq 'name "roxyb" 'age 21 'version 5))))))
(test-case
"size should support common header block"
"versioned struct: size should support common header block"
(let ([struct (+xversioned-struct uint8
(dictify
'header (dictify 'age uint8
@ -154,7 +154,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (size struct (mhasheq 'name "roxyb 🤘" 'gender 0 'age 21 'alive 1 'version 1)) 15)))
(test-case
"size should compute the correct size with pointers"
"versioned struct: size should compute the correct size with pointers"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -169,7 +169,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
'ptr "hello")) 15)))
(test-case
"size should throw if no value is given"
"versioned struct: size should throw if no value is given"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -180,7 +180,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-exn exn:fail:contract? (λ () (size vstruct)))))
(test-case
"encode should encode objects to buffers"
"versioned struct: encode should encode objects to buffers"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -194,7 +194,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (get-output-bytes op) (string->bytes/utf-8 "\x00\x05roxyb\x15\x01\x0aroxyb 🤘\x15\x00"))))
(test-case
"encode should throw for unknown version"
"versioned struct: encode should throw for unknown version"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -206,7 +206,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-exn exn:fail:contract? (λ () (encode vstruct op (mhasheq 'name "roxyb" 'age 21 'version 5))))))
(test-case
"encode should support common header block"
"versioned struct: encode should support common header block"
(let ([vstruct (+xversioned-struct uint8
(dictify
'header (dictify 'age uint8
@ -220,7 +220,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (get-output-bytes op) (string->bytes/utf-8 "\x00\x15\x01\x05roxyb\x01\x15\x01\x0aroxyb 🤘\x00"))))
(test-case
"encode should encode pointer data after structure"
"versioned struct: encode should encode pointer data after structure"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)
@ -235,7 +235,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe
(check-equal? (get-output-bytes op) (string->bytes/utf-8 "\x01\x05roxyb\x15\x09\x05hello"))))
#;(test-case
"encode should support preEncode hook"
"versioned struct: encode should support preEncode hook"
(let ([vstruct (+xversioned-struct uint8
(dictify
0 (dictify 'name (+xstring #:length uint8 #:encoding 'ascii)

Loading…
Cancel
Save