reserved done

main
Matthew Butterick 6 years ago
parent 7ab69869d1
commit 5e60b94d81

@ -0,0 +1,30 @@
#lang racket/base
(require "helper.rkt" "util.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee
|#
(define (xreserved-decode xo [port-arg (current-input-port)] #:parent [parent #f])
(define port (->input-port port-arg))
(pos port (+ (pos port) (size xo #f parent)))
(void))
(define (xreserved-encode xo val [port-arg (current-output-port)] #:parent [parent #f])
(define port (if (output-port? port-arg) port-arg (open-output-bytes)))
(write-bytes (make-bytes (size xo val parent) 0))
(unless port-arg (get-output-bytes port)))
(define (xreserved-size xo [val #f] [parent #f])
(* (size (xreserved-type xo)) (resolve-length (xreserved-count xo) #f parent)))
(struct xreserved (type count) #:transparent
#:methods gen:xenomorphic
[(define decode xreserved-decode)
(define encode xreserved-encode)
(define size xreserved-size)])
(define (+xreserved type [count 1])
(xreserved type count))

@ -6,9 +6,9 @@
"enum-test.rkt"
;"lazy-array-test.rkt"
"number-test.rkt"
;"optional-test.rkt"
"optional-test.rkt"
"pointer-test.rkt"
;"reserved-test.rkt"
"reserved-test.rkt"
"string-test.rkt"
"struct-test.rkt"
;"versioned-struct-test.rkt"

@ -0,0 +1,32 @@
#lang racket/base
(require rackunit
"../number.rkt"
"../helper.rkt"
"../reserved.rkt")
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/Reserved.coffee
|#
(test-case
"size should have a default count of 1"
(check-equal? (size (+xreserved uint8)) 1))
(test-case
"size should allow custom counts and types"
(check-equal? (size (+xreserved uint16be 10)) 20))
(test-case
"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 encode"
(parameterize ([current-output-port (open-output-bytes)])
(define reserved (+xreserved uint16be))
(encode reserved #f)
(check-equal? (dump (current-output-port)) (bytes 0 0))))
Loading…
Cancel
Save