|
|
@ -1,5 +1,5 @@
|
|
|
|
#lang restructure/racket
|
|
|
|
#lang restructure/racket
|
|
|
|
(require "decodestream.rkt")
|
|
|
|
(require "decodestream.rkt" "encodestream.rkt")
|
|
|
|
|
|
|
|
|
|
|
|
;; approximates https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
|
|
|
|
;; approximates https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
|
|
|
|
|
|
|
|
|
|
|
@ -23,36 +23,34 @@
|
|
|
|
(getter-field [size (hash-ref type-sizes fn)])
|
|
|
|
(getter-field [size (hash-ref type-sizes fn)])
|
|
|
|
|
|
|
|
|
|
|
|
(define/override (decode stream [res #f])
|
|
|
|
(define/override (decode stream [res #f])
|
|
|
|
(unless (input-port? stream)
|
|
|
|
(unless (is-a? stream RDecodeStream)
|
|
|
|
(raise-argument-error 'decode "input port" stream))
|
|
|
|
(raise-argument-error 'decode "RDecodeStream" stream))
|
|
|
|
(define bstr (read-bytes-exact size stream))
|
|
|
|
(define bstr (send stream read-bytes size))
|
|
|
|
(if (= 1 size)
|
|
|
|
(if (= 1 size)
|
|
|
|
(bytes-ref bstr 0)
|
|
|
|
(bytes-ref bstr 0)
|
|
|
|
(integer-bytes->integer bstr (unsigned-type? type) (eq? endian 'BE))))
|
|
|
|
(integer-bytes->integer bstr (unsigned-type? type) (eq? endian 'BE))))
|
|
|
|
|
|
|
|
|
|
|
|
(define/override (encode stream val)
|
|
|
|
(define/override (encode stream val)
|
|
|
|
(when stream
|
|
|
|
(when stream
|
|
|
|
(unless (output-port? stream)
|
|
|
|
(unless (is-a? stream REncodeStream)
|
|
|
|
(raise-argument-error 'encode "output port" stream)))
|
|
|
|
(raise-argument-error 'encode "REncodeStream" stream)))
|
|
|
|
(define bstr
|
|
|
|
(define bstr
|
|
|
|
(if (= 1 size)
|
|
|
|
(if (= 1 size)
|
|
|
|
(bytes val)
|
|
|
|
(bytes val)
|
|
|
|
(integer->integer-bytes val size (unsigned-type? type) (eq? endian 'BE))))
|
|
|
|
(integer->integer-bytes val size (unsigned-type? type) (eq? endian 'BE))))
|
|
|
|
(if stream (write-bytes bstr stream) bstr)))
|
|
|
|
(if stream (send stream write bstr) bstr)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(test-module
|
|
|
|
(test-module
|
|
|
|
(let ([o (make-object Number 'UInt16 'LE)]
|
|
|
|
(let ([o (make-object Number 'UInt16 'LE)]
|
|
|
|
[ip (open-input-bytes (bytes 1 2 3 4))]
|
|
|
|
[ip (make-object RDecodeStream (bytes 1 2 3 4))])
|
|
|
|
[op (open-output-bytes)])
|
|
|
|
|
|
|
|
(check-equal? (send o decode ip) 513) ;; 1000 0000 0100 0000
|
|
|
|
(check-equal? (send o decode ip) 513) ;; 1000 0000 0100 0000
|
|
|
|
(check-equal? (send o decode ip) 1027) ;; 1100 0000 0010 0000
|
|
|
|
(check-equal? (send o decode ip) 1027) ;; 1100 0000 0010 0000
|
|
|
|
(check-equal? (send o encode #f 513) (bytes 1 2))
|
|
|
|
(check-equal? (send o encode #f 513) (bytes 1 2))
|
|
|
|
(check-equal? (send o encode #f 1027) (bytes 3 4)))
|
|
|
|
(check-equal? (send o encode #f 1027) (bytes 3 4)))
|
|
|
|
|
|
|
|
|
|
|
|
(let ([o (make-object Number 'UInt16 'BE)]
|
|
|
|
(let ([o (make-object Number 'UInt16 'BE)]
|
|
|
|
[ip (open-input-bytes (bytes 1 2 3 4))]
|
|
|
|
[ip (make-object RDecodeStream (bytes 1 2 3 4))])
|
|
|
|
[op (open-output-bytes)])
|
|
|
|
|
|
|
|
(check-equal? (send o decode ip) 258) ;; 0100 0000 1000 0000
|
|
|
|
(check-equal? (send o decode ip) 258) ;; 0100 0000 1000 0000
|
|
|
|
(check-equal? (send o decode ip) 772) ;; 0010 0000 1100 0000
|
|
|
|
(check-equal? (send o decode ip) 772) ;; 0010 0000 1100 0000
|
|
|
|
(check-equal? (send o encode #f 258) (bytes 1 2))
|
|
|
|
(check-equal? (send o encode #f 258) (bytes 1 2))
|
|
|
|