diff --git a/xenomorph/xenomorph/redo/reserved.rkt b/xenomorph/xenomorph/redo/reserved.rkt new file mode 100644 index 00000000..82cbaca2 --- /dev/null +++ b/xenomorph/xenomorph/redo/reserved.rkt @@ -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)) \ No newline at end of file diff --git a/xenomorph/xenomorph/redo/test/main.rkt b/xenomorph/xenomorph/redo/test/main.rkt index 6791a71f..19a781bc 100644 --- a/xenomorph/xenomorph/redo/test/main.rkt +++ b/xenomorph/xenomorph/redo/test/main.rkt @@ -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" diff --git a/xenomorph/xenomorph/redo/test/reserved-test.rkt b/xenomorph/xenomorph/redo/test/reserved-test.rkt new file mode 100644 index 00000000..2c422b59 --- /dev/null +++ b/xenomorph/xenomorph/redo/test/reserved-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)))) \ No newline at end of file