main
Matthew Butterick 7 years ago
parent 01ca66a40e
commit c89971e5b5

@ -51,13 +51,13 @@
(require sugar/debug)
(define-macro-cases ·
[(_ X REF)
#'(let loop ([x X])
(cond
;; dict first, to catch objects that implement gen:dict
[(dict? x) (dict-ref x 'REF #f)]
;; give `send` precedence (presence of method => wants runtime resolution of value)
[(object? x) (or (send-or-false x REF) (get-or-false x REF))]
[else (raise-argument-error '· (format "~a must be object or dict" 'X) x)]))]
(syntax/loc caller-stx (let loop ([x X])
(cond
;; dict first, to catch objects that implement gen:dict
[(dict? x) (dict-ref x 'REF #f)]
;; give `send` precedence (presence of method => wants runtime resolution of value)
[(object? x) (or (send-or-false x REF) (get-or-false x REF))]
[else (raise-argument-error '· (format "~a must be object or dict" 'X) x)])))]
[(_ X REF0 . REFS) #'(· (· X REF0) . REFS)])
#;(module+ test

@ -9,7 +9,6 @@
"optional.rkt"
"pointer.rkt"
"reserved.rkt"
"stream.rkt"
"string.rkt"
"struct.rkt"
"versioned-struct.rkt")

@ -7,37 +7,37 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|#
(define-subclass Streamcoder (ArrayT type [len #f] [length-type 'count])
(define-subclass xenomorph-base% (ArrayT type [len #f] [length-type 'count])
(define/augride (decode stream [parent #f])
(define/augride (decode port [parent #f])
(define ctx (if (NumberT? len)
(mhasheq 'parent parent
'_startOffset (· stream pos)
'_startOffset (pos port)
'_currentOffset 0
'_length len)
parent))
(define decoded-len (resolve-length len stream parent))
(define decoded-len (resolve-length len port parent))
(cond
[(or (not decoded-len) (eq? length-type 'bytes))
(define end-pos (cond
;; decoded-len is byte length
[decoded-len (+ (· stream pos) decoded-len)]
[decoded-len (+ (pos port) decoded-len)]
;; no decoded-len, but parent has length
[(and parent (not (zero? (· parent _length)))) (+ (· parent _startOffset) (· parent _length))]
;; no decoded-len or parent, so consume whole stream
[else (· stream length_)]))
[else +inf.0]))
(for/list ([i (in-naturals)]
#:break (= (· stream pos) end-pos))
(send type decode stream ctx))]
#:break (or (eof-object? (peek-byte port)) (= (pos port) end-pos)))
(send type decode port ctx))]
;; we have decoded-len, which is treated as count of items
[else (for/list ([i (in-range decoded-len)])
(send type decode stream ctx))]))
(send type decode port ctx))]))
(define/augride (size [val #f] [ctx #f])
(when val (unless (countable? val)
(raise-argument-error 'Array:size "list or countable" val)))
(raise-argument-error 'Array:size "countable" val)))
(cond
[val (let-values ([(ctx len-size) (if (NumberT? len)
(values (mhasheq 'parent ctx) (send len size))
@ -55,13 +55,13 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(define (encode-items ctx)
(for ([item (in-list (countable->list array))])
(send type encode port item ctx)))
(send type encode port item ctx)))
(cond
[(NumberT? len) (define ctx (mhash 'pointers null
'startOffset (· port pos)
'startOffset (pos port)
'parent parent))
(ref-set! ctx 'pointerOffset (+ (· port pos) (size array ctx)))
(ref-set! ctx 'pointerOffset (+ (pos port) (size array ctx)))
(send len encode port (length array)) ; encode length at front
(encode-items ctx)
(for ([ptr (in-list (· ctx pointers))]) ; encode pointer data at end
@ -71,8 +71,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(define-values (Array Array? +Array) (values ArrayT ArrayT? +ArrayT))
(test-module
(define A (+Array uint16be 3))
(check-equal? (decode A #"ABCDEFG") '(16706 17220 17734))
(check-equal? (encode A '(16706 17220 17734) #f) #"ABCDEF")
(check-equal? (decode (+Array uint16be 3) #"ABCDEF") '(16706 17220 17734))
(check-equal? (encode (+Array uint16be 3) '(16706 17220 17734) #f) #"ABCDEF")
(check-equal? (size (+Array uint16be) '(1 2 3)) 6)
(check-equal? (size (+Array doublebe) '(1 2 3 4 5)) 40))

@ -1,7 +1,19 @@
#lang racket/base
(require racket/class sugar/class racket/generic racket/private/generic-methods "generic.rkt")
(require racket/class sugar/class racket/generic racket/private/generic-methods "generic.rkt" racket/port)
(require sugar/debug)
(provide (all-defined-out))
(define-generics posable
(pos posable [new-pos])
#:defaults
([port? (define (pos p [new-pos #f]) (when new-pos
(file-position p new-pos))
(file-position p))]))
(define posable<%>
(interface* ()
([(generic-property gen:posable)
(generic-method-table gen:posable
(define (pos o [new-pos #f]) (send o pos new-pos)))])))
(define-generics codable
(decode codable #:parent [parent] [stream])
@ -27,7 +39,10 @@
(define-generics dumpable
(dump dumpable))
(dump dumpable)
#:defaults
([input-port? (define (dump p) (port->bytes p))]
[output-port? (define (dump p) (get-output-bytes p))]))
(define dumpable<%>
(interface* ()
@ -35,6 +50,8 @@
(generic-method-table gen:dumpable
(define (dump o) (send o dump)))])))
(define (symbol-append . syms)
(string->symbol (apply string-append (map symbol->string syms))))
(define xenomorph-base%
(class* object% (codable<%> sizable<%> dumpable<%>)
@ -44,37 +61,41 @@
(define/pubment (decode port [parent #f])
(when parent (unless (indexable? parent)
(raise-argument-error 'Xenomorph "indexable" parent)))
(raise-argument-error (symbol-append (get-class-name) ':decode) "indexable" parent)))
(define ip (cond
[(bytes? port) (open-input-bytes port)]
[(input-port? port) port]
[else (raise-argument-error 'Xenomorph "bytes or input port" port)]))
(post-decode (inner (void) decode ip parent)))
[else (raise-argument-error (symbol-append (get-class-name) ':decode) "bytes or input port" port)]))
(post-decode (inner (void) decode ip parent) port parent))
(define/pubment (encode port val-in [parent #f])
#;(report* port val-in parent)
(define val (pre-encode val-in))
(define val (pre-encode val-in port))
(when parent (unless (indexable? parent)
(raise-argument-error 'Xenomorph "indexable" parent)))
(raise-argument-error (symbol-append (get-class-name) ':encode) "indexable" parent)))
(define op (cond
[(output-port? port) port]
[(not port) (open-output-bytes)]
[else (raise-argument-error 'Xenomorph "output port or #f" port)]))
(define encode-result (inner (void) encode op val parent))
(define encode-result (inner #"" encode op val parent))
(when (bytes? encode-result)
(write-bytes encode-result op))
(when (not port) (get-output-bytes op)))
(define/pubment (size [val #f] [parent #f])
(define/pubment (size [val #f] [parent #f] . _)
(when parent (unless (indexable? parent)
(raise-argument-error 'Xenomorph "indexable" parent)))
(raise-argument-error (symbol-append (get-class-name) ':size) "indexable" parent)))
(define result (inner (void) size val parent))
(when result (unless (and (integer? result) (not (negative? result)))
(raise-argument-error 'Xenomorph "integer" result)))
result)
(cond
[(void? result) 0]
[(and (integer? result) (not (negative? result))) result]
[else (raise-argument-error (symbol-append (get-class-name) ':size) "nonnegative integer" result)]))
(define/public (get-class-name) (define-values (name _) (object-info this))
(or name 'Xenomorph))
(define/public (post-decode val) val)
(define/public (pre-encode val) val)
(define/public (post-decode val . _) val)
(define/public (pre-encode val . _) val)
(define/public (dump) (void))))
(define-class-predicates xenomorph-base%)

@ -25,11 +25,13 @@ https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee
(define bit-int (for/sum ([(flag i) (in-indexed flags)]
#:when (and flag (ref flag-hash flag)))
(arithmetic-shift 1 i)))
(send type encode port bit-int)))
(send type encode port bit-int))
(define/override (get-class-name) 'Bitfield))
(test-module
(require "number.rkt" "stream.rkt")
(require "number.rkt")
(define bfer (+Bitfield uint16be '(bold italic underline #f shadow condensed extended)))
(define bf (send bfer decode #"\0\25"))
(check-equal? (length (ref-keys bf)) 6) ; omits #f flag

@ -19,7 +19,7 @@ A Restructure RBuffer object is separate.
string->bytes/utf-8
list->bytes) xs))
(define-subclass RestructureBase (RBuffer [len #xffff])
(define-subclass xenomorph-base% (RBuffer [len #xffff])
(define/augment (decode port [parent #f])
(define decoded-len (resolve-length len port parent))

@ -7,7 +7,7 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee
|#
(define-subclass Streamcoder (Enum type [options empty])
(define-subclass xenomorph-base% (Enum type [options empty])
(define/augment (decode stream . _)
(define index (send type decode stream))

@ -8,42 +8,47 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
|#
(define (get o i) (send o get i))
(define (LazyArray->list o) (send o to-list))
(define-subclass object% (InnerLazyArray type [len #f] [stream #f] [ctx #f])
(unless stream (raise-argument-error 'LazyArray "stream" stream))
(define starting-pos (· stream pos))
(define-subclass object% (InnerLazyArray type [len #f] [port-in #f] [ctx #f])
(field ([port port] (cond
[(bytes? port-in) (open-input-bytes port-in)]
[(port? port-in) port-in]
[else (raise-argument-error 'LazyArray "port" port)])))
(define starting-pos (pos port))
(define item-cache (mhasheqv)) ; integer-keyed hash, rather than list
(define/public-final (get index)
(unless (<= 0 index (sub1 len))
#;(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index)
(void))
(ref! item-cache index (λ ()
(define orig-pos (· stream pos))
(send stream pos (+ starting-pos (* (send type size #f ctx) index)))
(define new-item (send type decode stream ctx))
(send stream pos orig-pos)
(define orig-pos (pos port))
(pos port (+ starting-pos (* (send type size #f ctx) index)))
(define new-item (send type decode port ctx))
(pos port orig-pos)
new-item)))
(define/public-final (to-list)
(for/list ([i (in-range len)])
(get i))))
(get i))))
(define-subclass ArrayT (LazyArray)
(inherit-field len type)
(define/override (decode stream [parent #f])
(define pos (· stream pos)) ; ! placement matters. `resolve-length` will change `pos`
(define decoded-len (resolve-length len stream parent))
(define/override (decode port [parent #f])
(define starting-pos (pos port)) ; ! placement matters. `resolve-length` will change `pos`
(define decoded-len (resolve-length len port parent))
(let ([parent (if (NumberT? len)
(mhasheq 'parent parent
'_startOffset pos
'_startOffset starting-pos
'_currentOffset 0
'_length len)
parent)])
(define res (+InnerLazyArray type decoded-len stream parent))
(send stream pos (+ (· stream pos) (* decoded-len (send type size #f parent))))
(define res (+InnerLazyArray type decoded-len port parent))
(pos port (+ (pos port) (* decoded-len (send type size #f parent))))
res))
(define/override (size [val #f] [ctx #f])
@ -51,26 +56,23 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(send val to-list)
val) ctx))
(define/override (encode stream val [ctx #f])
(super encode stream (if (InnerLazyArray? val)
(send val to-list)
val) ctx)))
(define/override (encode port val [ctx #f])
(super encode port (if (InnerLazyArray? val)
(send val to-list)
val) ctx)))
(test-module
(require "stream.rkt")
(define bstr #"ABCD1234")
(define ds (+DecodeStream bstr))
(define ds (open-input-bytes bstr))
(define la (+LazyArray uint8 4))
(define ila (send la decode ds))
(check-equal? (send ds pos) 4)
(check-equal? (send ila get 1) 66)
(check-equal? (send ila get 3) 68)
(check-equal? (send ds pos) 4)
(check-equal? (send ila to-list) '(65 66 67 68))
(define la2 (+LazyArray int16be (λ (t) 4)))
(define es (+EncodeStream))
(send la2 encode es '(1 2 3 4))
(check-equal? (send es dump) #"\0\1\0\2\0\3\0\4")
(check-equal? (send (send la2 decode (+DecodeStream #"\0\1\0\2\0\3\0\4")) to-list) '(1 2 3 4)))
(define ila (decode la ds))
(check-equal? (pos ds) 4)
(check-equal? (get ila 1) 66)
(check-equal? (get ila 3) 68)
(check-equal? (pos ds) 4)
(check-equal? (LazyArray->list ila) '(65 66 67 68))
(define la2 (+LazyArray int16be (λ (t) 4)))
(check-equal? (encode la2 '(1 2 3 4) #f) #"\0\1\0\2\0\3\0\4")
(check-equal? (send (decode la2 (open-input-bytes #"\0\1\0\2\0\3\0\4")) to-list) '(1 2 3 4)))

@ -50,10 +50,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(arithmetic-shift b (* 8 i))))
unsigned-int)
(define/override (post-decode unsigned-val)
(define/override (post-decode unsigned-val . _)
(if _signed? (unsigned->signed unsigned-val bits) unsigned-val))
(define/override (pre-encode val)
(define/override (pre-encode val . _)
(exact-if-possible val))
(define/augment (encode port val [parent #f])
@ -75,7 +75,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(define bs (read-bytes byte-size port))
(floating-point-bytes->real bs (eq? endian 'be)))
(define/augment (encode val [parent #f]) ; convert float to int
(define/augment (encode port val [parent #f]) ; convert float to int
(define bs (real->floating-point-bytes val byte-size (eq? endian 'be)))
bs)
@ -95,10 +95,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee
(super-make-object (string->symbol (format "int~a" size)) fixed-endian)
(define _point (arithmetic-shift 1 fracBits))
(define/override (post-decode int)
(define/override (post-decode int . _)
(exact-if-possible (/ int _point 1.0)))
(define/override (pre-encode fixed)
(define/override (pre-encode fixed . _)
(exact-if-possible (floor (* fixed _point)))))
(define-instance fixed16 (make-object Fixed 16))

@ -7,7 +7,7 @@ approximates
https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
|#
(define-subclass Streamcoder (Optional type [condition #t])
(define-subclass xenomorph-base% (Optional type [condition #t])
(define (resolve-condition parent)
(if (procedure? condition)
@ -18,10 +18,9 @@ https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
(when (resolve-condition parent)
(send type decode stream parent)))
(define/override (size [val #f] [parent #f])
(if (resolve-condition parent)
(send type size val parent)
0))
(define/augment (size val parent)
(when (resolve-condition parent)
(send type size val parent)))
(define/augment (encode stream val parent)
(when (resolve-condition parent)

@ -18,7 +18,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[(· ctx parent) => find-top-ctx]
[else ctx]))
(define-subclass RestructureBase (Pointer offset-type type-in [options (mhasheq)])
(define-subclass xenomorph-base% (Pointer offset-type type-in [options (mhasheq)])
(field [type (and (not (eq? type-in 'void)) type-in)])
(define pointer-style (or (· options type) 'local))
(define allow-null (or (· options allowNull) #t))
@ -26,7 +26,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define lazy (· options lazy))
(define relative-getter-or-0 (or (· options relativeTo) (λ (ctx) 0))) ; changed this to a simple lambda
(define/override (decode stream [ctx #f])
(define/augment (decode stream [ctx #f])
(define offset (send offset-type decode stream ctx))
(cond
[(and allow-null (= offset null-value)) #f] ; handle null pointers
@ -56,7 +56,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[else ptr])]))
(define/override (size [val #f] [ctx #f])
(define/augment (size [val #f] [ctx #f])
(let*-values ([(parent) ctx]
[(ctx) (caseq pointer-style
[(local immediate) ctx]
@ -70,7 +70,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(send offset-type size)))
(define/override (encode stream val [ctx #f])
(define/augment (encode stream val [ctx #f])
(if (not val)
(send offset-type encode stream null-value)
(let* ([parent ctx]

@ -102,14 +102,7 @@ https://github.com/mbutterick/restructure/blob/master/src/DecodeStream.coffee
(generic-method-table gen:countable
(define (length o) (get-field length_ o)))])))
(define-generics posable
(pos posable [new-pos]))
(define posable<%>
(interface* ()
([(generic-property gen:posable)
(generic-method-table gen:posable
(define (pos o [new-pos #f]) (send o pos new-pos)))])))
(define DecodeStreamT
(class* PortWrapper

@ -44,17 +44,17 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define/override (dump) _kv)))
(define-subclass Streamcoder (Struct [fields (dictify)])
(field [[_process process] (λ (res stream ctx) res)]
[[_preEncode preEncode] void]) ; store as field so it can be mutated from outside
(define-subclass xenomorph-base% (Struct [fields (dictify)])
(field [[_process process] (λ (res port ctx) res)]
[[_pre-encode pre-encode] (λ (val port) val)]) ; store as field so it can be mutated from outside
(define/overment (process res stream [ctx #f])
(define/overment (post-decode res stream [ctx #f])
(let* ([res (_process res stream ctx)]
[res (inner res process res stream ctx)])
[res (inner res post-decode res stream ctx)])
(unless (dict? res) (raise-result-error 'Struct:process "dict" res))
res))
(define/override (preEncode . args) (apply _preEncode args))
(define/override (pre-encode . args) (apply _pre-encode args))
(unless ((disjoin assocs? Struct?) fields) ; should be Versioned Struct but whatever
(raise-argument-error 'Struct "assocs or Versioned Struct" fields))
@ -62,33 +62,32 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define/augride (decode stream [parent #f] [len 0])
;; _setup and _parse-fields are separate to cooperate with VersionedStruct
(let* ([res (_setup stream parent len)]
[res (_parse-fields stream res fields)]
[res (process res stream)])
[res (_parse-fields stream res fields)])
res))
(define/public-final (_setup stream parent len)
(define/public-final (_setup port parent len)
(define res (make-object StructDictRes)) ; not mere hash
(dict-set*! res 'parent parent
'_startOffset (· stream pos)
'_startOffset (pos port)
'_currentOffset 0
'_length len)
res)
(define/public-final (_parse-fields stream res fields)
(define/public-final (_parse-fields port res fields)
(unless (assocs? fields)
(raise-argument-error '_parse-fields "assocs" fields))
(for/fold ([res res])
([(key type) (in-dict fields)])
(define val (if (procedure? type)
(type res)
(send type decode stream res)))
(send type decode port res)))
(unless (void? val)
(ref-set! res key val))
(ref-set! res '_currentOffset (- (· stream pos) (· res _startOffset)))
(ref-set! res '_currentOffset (- (pos port) (· res _startOffset)))
res))
(define/override (size [val #f] [parent #f] [include-pointers #t])
(define/augride (size [val #f] [parent #f] [include-pointers #t])
(define ctx (mhasheq 'parent parent
'val val
'pointerSize 0))
@ -96,25 +95,25 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(send type size (and val (ref val key)) ctx))
(if include-pointers (· ctx pointerSize) 0)))
(define/augride (encode stream val [parent #f])
(define/augride (encode port val [parent #f])
(unless (dict? val)
(raise-argument-error 'Struct:encode "dict" val))
(send this preEncode val stream) ; preEncode goes first, because it might bring input dict into compliance
(send this pre-encode val port) ; preEncode goes first, because it might bring input dict into compliance
(define ctx (mhash 'pointers empty
'startOffset (· stream pos)
'startOffset (pos port)
'parent parent
'val val
'pointerSize 0))
(ref-set! ctx 'pointerOffset (+ (· stream pos) (size val ctx #f)))
(ref-set! ctx 'pointerOffset (+ (pos port) (size val ctx #f)))
(unless (andmap (λ (key) (memq key (dict-keys val))) (dict-keys fields))
(raise-argument-error 'Struct:encode
(format "dict that contains superset of Struct keys: ~a" (dict-keys fields)) (dict-keys val)))
(for ([(key type) (in-dict fields)])
(send type encode stream (ref val key) ctx))
(send type encode port (ref val key) ctx))
(for ([ptr (in-list (· ctx pointers))])
(send (· ptr type) encode stream (· ptr val) (· ptr parent)))))
(send (· ptr type) encode port (· ptr val) (· ptr parent)))))
(test-module

@ -15,7 +15,7 @@ https://github.com/mbuttrackerick/restructure/blob/master/src/VersionedStruct.co
(unless (and (dict? versions) (andmap (λ (val) (or (dict? val) (Struct? val))) (map cdr versions)))
(raise-argument-error 'VersionedStruct "dict of dicts or Structs" versions))
(inherit _setup _parse-fields process)
(inherit _setup _parse-fields post-decode)
(inherit-field fields)
(field [forced-version #f]
[versionGetter void]
@ -51,7 +51,6 @@ https://github.com/mbuttrackerick/restructure/blob/master/src/VersionedStruct.co
[(VersionedStruct? fields) (send fields decode stream parent)]
[else
(_parse-fields stream res fields)
(process res stream)
res]))
(define/public-final (force-version! version)

@ -14,40 +14,21 @@ https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
(check-equal? (size e) 1)
;
; it 'should decode', ->
; stream = new DecodeStream new Buffer [1, 2, 0]
; e.decode(stream).should.equal 'bar'
; e.decode(stream).should.equal 'baz'
; e.decode(stream).should.equal 'foo'
(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"))
(let ([stream (+DecodeStream (+Buffer '(1 2 0)))])
(check-equal? (decode e stream) "bar")
(check-equal? (decode e stream) "baz")
(check-equal? (decode e stream) "foo"))
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [1, 2, 0]
; done()
;
; e.encode stream, 'bar'
; e.encode stream, 'baz'
; e.encode stream, 'foo'
; stream.end()
(let ([stream (+EncodeStream)])
(encode e stream "bar")
(encode e stream "baz")
(encode e stream "foo")
(check-equal? (send stream dump) (+Buffer '(1 2 0))))
;
(parameterize ([current-output-port (open-output-bytes)])
(encode e "bar")
(encode e "baz")
(encode e "foo")
(check-equal? (dump (current-output-port)) (bytes 1 2 0)))
; it 'should throw on unknown option', ->
; stream = new EncodeStream
; should.throw ->
; e.encode stream, 'unknown'
(check-exn exn:fail:contract? (λ () (encode e (+EncodeStream) "unknown")))
(check-exn exn:fail:contract? (λ () (encode e "unknown" (open-output-bytes))))

@ -8,104 +8,51 @@ https://github.com/mbutterick/restructure/blob/master/test/LazyArray.coffee
;describe 'LazyArray', ->
; describe 'decode', ->
; it 'should decode items lazily', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new LazyArray uint8, 4
;
; arr = array.decode(stream)
; arr.should.not.be.an.instanceof Array
; arr.should.have.length 4
; stream.pos.should.equal 4
;
; arr.get(0).should.equal 1
; arr.get(1).should.equal 2
; arr.get(2).should.equal 3
; arr.get(3).should.equal 4
;
; should.not.exist arr.get(-1)
; should.not.exist arr.get(5)
(let* ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+LazyArray uint8 4)])
(define arr (decode array stream))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define array (+LazyArray uint8 4))
(define arr (decode array))
(check-false (Array? arr))
(check-equal? (ref arr 'len) 4)
(check-equal? (pos stream) 4)
(check-equal? (pos (current-input-port)) 4)
(check-equal? (get arr 0) 1)
(check-equal? (get arr 1) 2)
(check-equal? (get arr 2) 3)
(check-equal? (get arr 3) 4))
;
; it 'should be able to convert to an array', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new LazyArray uint8, 4
;
; arr = array.decode(stream)
; arr.toArray().should.deep.equal [1, 2, 3, 4]
(let* ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+LazyArray uint8 4)])
(define arr (decode array stream))
(check-equal? (send arr to-list) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define array (+LazyArray uint8 4))
(define arr (decode array))
(check-equal? (LazyArray->list arr) '(1 2 3 4)))
;
; it 'should have an inspect method', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new LazyArray uint8, 4
;
; arr = array.decode(stream)
; arr.inspect().should.equal '[ 1, 2, 3, 4 ]'
; [skipped]
#;(let* ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+LazyArray uint8 4)])
(define arr (decode array stream))
(check-equal? (send arr inspect) (format "~a" '(1 2 3 4))))
;
; it 'should decode length as number before array', ->
; stream = new DecodeStream new Buffer [4, 1, 2, 3, 4, 5]
; array = new LazyArray uint8, uint8
; arr = array.decode(stream)
;
; arr.toArray().should.deep.equal [1, 2, 3, 4]
(let* ([stream (+DecodeStream (+Buffer '(4 1 2 3 4 5)))]
[array (+LazyArray uint8 uint8)])
(define arr (decode array stream))
(check-equal? (send arr to-list) '(1 2 3 4)))
(parameterize ([current-input-port (open-input-bytes (bytes 4 1 2 3 4 5))])
(define array (+LazyArray uint8 uint8))
(define arr (decode array))
(check-equal? (LazyArray->list arr) '(1 2 3 4)))
;
; describe 'size', ->
; it 'should work with LazyArrays', ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new LazyArray uint8, 4
; arr = array.decode(stream)
;
; array.size(arr).should.equal 4
(let* ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+LazyArray uint8 4)])
(define arr (decode array stream))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define array (+LazyArray uint8 4))
(define arr (decode array))
(check-equal? (size array arr) 4))
;
; describe 'encode', ->
; it 'should work with LazyArrays', (done) ->
; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5]
; array = new LazyArray uint8, 4
; arr = array.decode(stream)
;
; enc = new EncodeStream
; enc.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [1, 2, 3, 4]
; done()
;
; array.encode(enc, arr)
; enc.end()
(let* ([stream (+DecodeStream (+Buffer '(1 2 3 4 5)))]
[array (+LazyArray uint8 4)])
(define arr (decode array stream))
(check-equal? (encode array #f arr) (+Buffer '(1 2 3 4))))
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 3 4 5))])
(define array (+LazyArray uint8 4))
(define arr (decode array))
(check-equal? (encode array arr #f) (bytes 1 2 3 4)))

@ -9,228 +9,118 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
;describe 'Number', ->
; describe 'uint8', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xab, 0xff]
; uint8.decode(stream).should.equal 0xab
; uint8.decode(stream).should.equal 0xff
;
; it 'should have a size', ->
; uint8.size().should.equal 1
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xab, 0xff]
; done()
;
; uint8.encode(stream, 0xab)
; uint8.encode(stream, 0xff)
; stream.end()
(let ([stream (+DecodeStream (bytes #xab #xff))])
(check-equal? (decode uint8 stream) #xab)
(check-equal? (decode uint8 stream) #xff))
(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff))])
(check-equal? (decode uint8) #xab)
(check-equal? (decode uint8) #xff))
(check-equal? (size uint8) 1)
(let ([stream (+EncodeStream)])
(encode uint8 stream #xab)
(encode uint8 stream #xff)
(check-equal? (dump stream) (bytes #xab #xff)))
(let ([port (open-output-bytes)])
(encode uint8 #xab port)
(encode uint8 #xff port)
(check-equal? (dump port) (bytes #xab #xff)))
;
; describe 'uint16', ->
; it 'is an alias for uint16be', ->
; uint16.should.equal uint16be
;; modified test: `uint16` is the same endianness as the platform
; modified test: `uint16` is the same endianness as the platform
(check-equal? (decode uint16 (bytes 0 1)) (send (if (system-big-endian?)
uint16be
uint16le) decode (bytes 0 1)))
uint16be
uint16le) decode (bytes 0 1)))
;
; describe 'uint16be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xab, 0xff]
; uint16be.decode(stream).should.equal 0xabff
;
; it 'should have a size', ->
; uint16be.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xab, 0xff]
; done()
;
; uint16be.encode(stream, 0xabff)
; stream.end()
(check-equal? (decode uint16be (+DecodeStream (bytes #xab #xff))) #xabff)
(check-equal? (decode uint16be (open-input-bytes (bytes #xab #xff))) #xabff)
(check-equal? (size uint16be) 2)
(check-equal? (encode uint16be #f #xabff) (bytes #xab #xff))
(check-equal? (encode uint16be #xabff #f) (bytes #xab #xff))
;
; describe 'uint16le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab]
; uint16le.decode(stream).should.equal 0xabff
;
; it 'should have a size', ->
; uint16le.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab]
; done()
;
; uint16le.encode(stream, 0xabff)
; stream.end()
(check-equal? (decode uint16le (+DecodeStream (bytes #xff #xab))) #xabff)
(check-equal? (decode uint16le (open-input-bytes (bytes #xff #xab))) #xabff)
(check-equal? (size uint16le) 2)
(check-equal? (encode uint16le #f #xabff) (bytes #xff #xab))
(check-equal? (encode uint16le #xabff #f) (bytes #xff #xab))
;
; describe 'uint24', ->
; it 'is an alias for uint24be', ->
; uint24.should.equal uint24be
;; modified test: `uint24` is the same endianness as the platform
(check-equal? (decode uint24 (bytes 0 1 2)) (send (if (system-big-endian?)
uint24be
uint24le) decode (bytes 0 1 2)))
uint24be
uint24le) decode (bytes 0 1 2)))
;
; describe 'uint24be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab, 0x24]
; uint24be.decode(stream).should.equal 0xffab24
;
; it 'should have a size', ->
; uint24be.size().should.equal 3
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab, 0x24]
; done()
;
; uint24be.encode(stream, 0xffab24)
; stream.end()
(check-equal? (decode uint24be (+DecodeStream (bytes #xff #xab #x24))) #xffab24)
(check-equal? (decode uint24be (open-input-bytes (bytes #xff #xab #x24))) #xffab24)
(check-equal? (size uint24be) 3)
(check-equal? (encode uint24be #f #xffab24) (bytes #xff #xab #x24))
(check-equal? (encode uint24be #xffab24 #f) (bytes #xff #xab #x24))
;
; describe 'uint24le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x24, 0xab, 0xff]
; uint24le.decode(stream).should.equal 0xffab24
;
; it 'should have a size', ->
; uint24le.size().should.equal 3
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x24, 0xab, 0xff]
; done()
;
; uint24le.encode(stream, 0xffab24)
; stream.end()
(check-equal? (decode uint24le (+DecodeStream (bytes #x24 #xab #xff))) #xffab24)
(check-equal? (decode uint24le (open-input-bytes (bytes #x24 #xab #xff))) #xffab24)
(check-equal? (size uint24le) 3)
(check-equal? (encode uint24le #f #xffab24) (bytes #x24 #xab #xff))
(check-equal? (encode uint24le #xffab24 #f) (bytes #x24 #xab #xff))
;
; describe 'uint32', ->
; it 'is an alias for uint32be', ->
; uint32.should.equal uint32be
;; modified test: `uint32` is the same endianness as the platform
(check-equal? (decode uint32 (bytes 0 1 2 3)) (send (if (system-big-endian?)
uint32be
uint32le) decode (bytes 0 1 2 3)))
uint32be
uint32le) decode (bytes 0 1 2 3)))
;
; describe 'uint32be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab, 0x24, 0xbf]
; uint32be.decode(stream).should.equal 0xffab24bf
;
; it 'should have a size', ->
; uint32be.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab, 0x24, 0xbf]
; done()
;
; uint32be.encode(stream, 0xffab24bf)
; stream.end()
(check-equal? (decode uint32be (+DecodeStream (bytes #xff #xab #x24 #xbf))) #xffab24bf)
(check-equal? (decode uint32be (open-input-bytes (bytes #xff #xab #x24 #xbf))) #xffab24bf)
(check-equal? (size uint32be) 4)
(check-equal? (encode uint32be #f #xffab24bf) (bytes #xff #xab #x24 #xbf))
(check-equal? (encode uint32be #xffab24bf #f) (bytes #xff #xab #x24 #xbf))
;
; describe 'uint32le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xbf, 0x24, 0xab, 0xff]
; uint32le.decode(stream).should.equal 0xffab24bf
;
; it 'should have a size', ->
; uint32le.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xbf, 0x24, 0xab, 0xff]
; done()
;
; uint32le.encode(stream, 0xffab24bf)
; stream.end()
(check-equal? (decode uint32le (+DecodeStream (bytes #xbf #x24 #xab #xff))) #xffab24bf)
(check-equal? (decode uint32le (open-input-bytes (bytes #xbf #x24 #xab #xff))) #xffab24bf)
(check-equal? (size uint32le) 4)
(check-equal? (encode uint32le #f #xffab24bf) (bytes #xbf #x24 #xab #xff))
(check-equal? (encode uint32le #xffab24bf #f) (bytes #xbf #x24 #xab #xff))
;
; describe 'int8', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x7f, 0xff]
; int8.decode(stream).should.equal 127
; int8.decode(stream).should.equal -1
;
; it 'should have a size', ->
; int8.size().should.equal 1
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x7f, 0xff]
; done()
;
; int8.encode(stream, 127)
; int8.encode(stream, -1)
; stream.end()
(let ([stream (+DecodeStream (bytes #x7f #xff))])
(check-equal? (decode int8 stream) 127)
(check-equal? (decode int8 stream) -1))
(let ([port (open-input-bytes (bytes #x7f #xff))])
(check-equal? (decode int8 port) 127)
(check-equal? (decode int8 port) -1))
(check-equal? (size int8) 1)
(let ([stream (+EncodeStream)])
(encode int8 stream 127)
(encode int8 stream -1)
(check-equal? (dump stream) (bytes #x7f #xff)))
(let ([port (open-output-bytes)])
(encode int8 127 port)
(encode int8 -1 port)
(check-equal? (dump port) (bytes #x7f #xff)))
;
@ -240,59 +130,34 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
;; modified test: `int16` is the same endianness as the platform
(check-equal? (decode int16 (bytes 0 1)) (send (if (system-big-endian?)
int16be
int16le) decode (bytes 0 1)))
int16be
int16le) decode (bytes 0 1)))
;
; describe 'int16be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab]
; int16be.decode(stream).should.equal -85
;
; it 'should have a size', ->
; int16be.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab]
; done()
;
; int16be.encode(stream, -85)
; stream.end()
(let ([stream (+DecodeStream (bytes #xff #xab))])
(check-equal? (decode int16be stream) -85))
(let ([port (open-input-bytes (bytes #xff #xab))])
(check-equal? (decode int16be port) -85))
(check-equal? (size int16be) 2)
(let ([stream (+EncodeStream)])
(encode int16be stream -85)
(check-equal? (dump stream) (bytes #xff #xab)))
(let ([port (open-output-bytes)])
(encode int16be -85 port)
(check-equal? (dump port) (bytes #xff #xab)))
;
; describe 'int16le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xab, 0xff]
; int16le.decode(stream).should.equal -85
;
; it 'should have a size', ->
; int16le.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xab, 0xff]
; done()
;
; int16le.encode(stream, -85)
; stream.end()
(check-equal? (decode int16le (+DecodeStream (bytes #xab #xff))) -85)
(check-equal? (decode int16le (open-input-bytes (bytes #xab #xff))) -85)
(check-equal? (size int16le) 2)
(check-equal? (encode int16le #f -85) (bytes #xab #xff))
(check-equal? (encode int16le -85 #f) (bytes #xab #xff))
;
@ -302,325 +167,169 @@ https://github.com/mbutterick/restructure/blob/master/test/Number.coffee
;; modified test: `int24` is the same endianness as the platform
(check-equal? (decode int24 (bytes 0 1 2)) (send (if (system-big-endian?)
int24be
int24le) decode (bytes 0 1 2)))
int24be
int24le) decode (bytes 0 1 2)))
;
; describe 'int24be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab, 0x24]
; int24be.decode(stream).should.equal -21724
;
; it 'should have a size', ->
; int24be.size().should.equal 3
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab, 0x24]
; done()
;
; int24be.encode(stream, -21724)
; stream.end()
(check-equal? (decode int24be (+DecodeStream (bytes #xff #xab #x24))) -21724)
(check-equal? (decode int24be (open-input-bytes (bytes #xff #xab #x24))) -21724)
(check-equal? (size int24be) 3)
(check-equal? (encode int24be #f -21724) (bytes #xff #xab #x24))
(check-equal? (encode int24be -21724 #f) (bytes #xff #xab #x24))
;
; describe 'int24le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x24, 0xab, 0xff]
; int24le.decode(stream).should.equal -21724
;
; it 'should have a size', ->
; int24le.size().should.equal 3
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x24, 0xab, 0xff]
; done()
;
; int24le.encode(stream, -21724)
; stream.end()
;
(check-equal? (decode int24le (+DecodeStream (bytes #x24 #xab #xff))) -21724)
(check-equal? (decode int24le (open-input-bytes (bytes #x24 #xab #xff))) -21724)
(check-equal? (size int24le) 3)
(check-equal? (encode int24le #f -21724) (bytes #x24 #xab #xff))
(check-equal? (encode int24le -21724 #f) (bytes #x24 #xab #xff))
; describe 'int32', ->
; it 'is an alias for int32be', ->
; int32.should.equal int32be
;; modified test: `int32` is the same endianness as the platform
; modified test: `int32` is the same endianness as the platform
(check-equal? (decode int32 (bytes 0 1 2 3)) (send (if (system-big-endian?)
int32be
int32le) decode (bytes 0 1 2 3)))
int32be
int32le) decode (bytes 0 1 2 3)))
;
; describe 'int32be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xff, 0xab, 0x24, 0xbf]
; int32be.decode(stream).should.equal -5561153
;
; it 'should have a size', ->
; int32be.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xff, 0xab, 0x24, 0xbf]
; done()
;
; int32be.encode(stream, -5561153)
; stream.end()
(check-equal? (decode int32be (+DecodeStream (bytes #xff #xab #x24 #xbf))) -5561153)
(check-equal? (decode int32be (open-input-bytes (bytes #xff #xab #x24 #xbf))) -5561153)
(check-equal? (size int32be) 4)
(check-equal? (encode int32be #f -5561153) (bytes #xff #xab #x24 #xbf))
(check-equal? (encode int32be -5561153 #f) (bytes #xff #xab #x24 #xbf))
;
; describe 'int32le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xbf, 0x24, 0xab, 0xff]
; int32le.decode(stream).should.equal -5561153
;
; it 'should have a size', ->
; int32le.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xbf, 0x24, 0xab, 0xff]
; done()
;
; int32le.encode(stream, -5561153)
; stream.end()
(check-equal? (decode int32le (+DecodeStream (bytes #xbf #x24 #xab #xff))) -5561153)
(check-equal? (decode int32le (open-input-bytes (bytes #xbf #x24 #xab #xff))) -5561153)
(check-equal? (size int32le) 4)
(check-equal? (encode int32le #f -5561153) (bytes #xbf #x24 #xab #xff))
(check-equal? (encode int32le -5561153 #f) (bytes #xbf #x24 #xab #xff))
;
; describe 'float', ->
; it 'is an alias for floatbe', ->
; float.should.equal floatbe
;; modified test: `float` is the same endianness as the platform
; modified test: `float` is the same endianness as the platform
(check-equal? (decode float (bytes 0 1 2 3)) (send (if (system-big-endian?)
floatbe
floatle) decode (bytes 0 1 2 3)))
floatbe
floatle) decode (bytes 0 1 2 3)))
;
; describe 'floatbe', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x43, 0x7a, 0x8c, 0xcd]
; floatbe.decode(stream).should.be.closeTo 250.55, 0.005
;
; it 'should have a size', ->
; floatbe.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x43, 0x7a, 0x8c, 0xcd]
; done()
;
; floatbe.encode(stream, 250.55)
; stream.end()
(check-= (decode floatbe (+DecodeStream (bytes #x43 #x7a #x8c #xcd))) 250.55 0.01)
(check-= (decode floatbe (open-input-bytes (bytes #x43 #x7a #x8c #xcd))) 250.55 0.01)
(check-equal? (size floatbe) 4)
(check-equal? (encode floatbe #f 250.55) (bytes #x43 #x7a #x8c #xcd))
(check-equal? (encode floatbe 250.55 #f) (bytes #x43 #x7a #x8c #xcd))
;
; describe 'floatle', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xcd, 0x8c, 0x7a, 0x43]
; floatle.decode(stream).should.be.closeTo 250.55, 0.005
;
; it 'should have a size', ->
; floatle.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xcd, 0x8c, 0x7a, 0x43]
; done()
;
; floatle.encode(stream, 250.55)
; stream.end()
(check-= (decode floatle (+DecodeStream (bytes #xcd #x8c #x7a #x43))) 250.55 0.01)
(check-= (decode floatle (open-input-bytes (bytes #xcd #x8c #x7a #x43))) 250.55 0.01)
(check-equal? (size floatle) 4)
(check-equal? (encode floatle #f 250.55) (bytes #xcd #x8c #x7a #x43))
(check-equal? (encode floatle 250.55 #f) (bytes #xcd #x8c #x7a #x43))
;
; describe 'double', ->
; it 'is an alias for doublebe', ->
; double.should.equal doublebe
;; modified test: `double` is the same endianness as the platform
; modified test: `double` is the same endianness as the platform
(check-equal? (decode double (bytes 0 1 2 3 4 5 6 7)) (send (if (system-big-endian?)
doublebe
doublele) decode (bytes 0 1 2 3 4 5 6 7)))
doublebe
doublele) decode (bytes 0 1 2 3 4 5 6 7)))
;
; describe 'doublebe', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a]
; doublebe.decode(stream).should.be.equal 1234.56
;
; it 'should have a size', ->
; doublebe.size().should.equal 8
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a]
; done()
;
; doublebe.encode(stream, 1234.56)
; stream.end()
(check-equal? (decode doublebe (+DecodeStream (bytes #x40 #x93 #x4a #x3d #x70 #xa3 #xd7 #x0a))) 1234.56)
(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 #f 1234.56) (bytes #x40 #x93 #x4a #x3d #x70 #xa3 #xd7 #x0a))
(check-equal? (encode doublebe 1234.56 #f) (bytes #x40 #x93 #x4a #x3d #x70 #xa3 #xd7 #x0a))
;
; describe 'doublele', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40]
; doublele.decode(stream).should.be.equal 1234.56
;
; it 'should have a size', ->
; doublele.size().should.equal 8
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40]
; done()
;
; doublele.encode(stream, 1234.56)
; stream.end()
(check-equal? (decode doublele (+DecodeStream (bytes #x0a #xd7 #xa3 #x70 #x3d #x4a #x93 #x40))) 1234.56)
(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 #f 1234.56) (bytes #x0a #xd7 #xa3 #x70 #x3d #x4a #x93 #x40))
(check-equal? (encode doublele 1234.56 #f) (bytes #x0a #xd7 #xa3 #x70 #x3d #x4a #x93 #x40))
;
; describe 'fixed16', ->
; it 'is an alias for fixed16be', ->
; fixed16.should.equal fixed16be
;; modified test: `fixed16` is the same endianness as the platform
; modified test: `fixed16` is the same endianness as the platform
(check-equal? (decode fixed16 (bytes 0 1)) (send (if (system-big-endian?)
fixed16be
fixed16le) decode (bytes 0 1)))
fixed16be
fixed16le) decode (bytes 0 1)))
;
; describe 'fixed16be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x19, 0x57]
; fixed16be.decode(stream).should.be.closeTo 25.34, 0.005
;
; it 'should have a size', ->
; fixed16be.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x19, 0x57]
; done()
;
; fixed16be.encode(stream, 25.34)
; stream.end()
(check-= (decode fixed16be (+DecodeStream (bytes #x19 #x57))) 25.34 0.01)
(check-= (decode fixed16be (open-input-bytes (bytes #x19 #x57))) 25.34 0.01)
(check-equal? (size fixed16be) 2)
(check-equal? (encode fixed16be #f 25.34) (bytes #x19 #x57))
(check-equal? (encode fixed16be 25.34 #f) (bytes #x19 #x57))
;
; describe 'fixed16le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x57, 0x19]
; fixed16le.decode(stream).should.be.closeTo 25.34, 0.005
;
; it 'should have a size', ->
; fixed16le.size().should.equal 2
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x57, 0x19]
; done()
;
; fixed16le.encode(stream, 25.34)
; stream.end()
(check-= (decode fixed16le (+DecodeStream (bytes #x57 #x19))) 25.34 0.01)
(check-= (decode fixed16le (open-input-bytes (bytes #x57 #x19))) 25.34 0.01)
(check-equal? (size fixed16le) 2)
(check-equal? (encode fixed16le #f 25.34) (bytes #x57 #x19))
(check-equal? (encode fixed16le 25.34 #f) (bytes #x57 #x19))
;
; describe 'fixed32', ->
; it 'is an alias for fixed32be', ->
; fixed32.should.equal fixed32be
; modified test: `fixed32` is the same endianness as the platform
;; modified test: `fixed32` is the same endianness as the platform
(check-equal? (decode fixed32 (bytes 0 1 2 3)) (send (if (system-big-endian?)
fixed32be
fixed32le) decode (bytes 0 1 2 3)))
fixed32be
fixed32le) decode (bytes 0 1 2 3)))
;
; describe 'fixed32be', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0x00, 0xfa, 0x8c, 0xcc]
; fixed32be.decode(stream).should.be.closeTo 250.55, 0.005
;
; it 'should have a size', ->
; fixed32be.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0x00, 0xfa, 0x8c, 0xcc]
; done()
;
; fixed32be.encode(stream, 250.55)
; stream.end()
(check-= (decode fixed32be (+DecodeStream (bytes #x00 #xfa #x8c #xcc))) 250.55 0.01)
(check-= (decode fixed32be (open-input-bytes (bytes #x00 #xfa #x8c #xcc))) 250.55 0.01)
(check-equal? (size fixed32be) 4)
(check-equal? (encode fixed32be #f 250.55) (bytes #x00 #xfa #x8c #xcc))
(check-equal? (encode fixed32be 250.55 #f) (bytes #x00 #xfa #x8c #xcc))
;
; describe 'fixed32le', ->
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0xcc, 0x8c, 0xfa, 0x00]
; fixed32le.decode(stream).should.be.closeTo 250.55, 0.005
;
; it 'should have a size', ->
; fixed32le.size().should.equal 4
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0xcc, 0x8c, 0xfa, 0x00]
; done()
;
; fixed32le.encode(stream, 250.55)
; stream.end()
(check-= (decode fixed32le (+DecodeStream (bytes #xcc #x8c #xfa #x00))) 250.55 0.01)
(check-= (decode fixed32le (open-input-bytes (bytes #xcc #x8c #xfa #x00))) 250.55 0.01)
(check-equal? (size fixed32le) 4)
(check-equal? (encode fixed32le #f 250.55) (bytes #xcc #x8c #xfa #x00))
(check-equal? (encode fixed32le 250.55 #f) (bytes #xcc #x8c #xfa #x00))

@ -8,193 +8,106 @@ https://github.com/mbutterick/restructure/blob/master/test/Optional.coffee
;describe 'Optional', ->
; describe 'decode', ->
; it 'should not decode when condition is falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, false
; should.not.exist optional.decode(stream)
; stream.pos.should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #f)])
(check-equal? (decode optional stream) (void))
(check-equal? (· stream pos) 0))
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+Optional uint8 #f))
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0))
; it 'should not decode when condition is a function and falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> false
; should.not.exist optional.decode(stream)
; stream.pos.should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #f))])
(check-equal? (decode optional stream) (void))
(check-equal? (· stream pos) 0))
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+Optional uint8 (λ _ #f)))
(check-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 0))
;
; it 'should decode when condition is omitted', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8)])
(check-not-equal? (decode optional stream) (void))
(check-equal? (· stream pos) 1))
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+Optional uint8))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1))
;
; it 'should decode when condition is truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, true
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #t)])
(check-not-equal? (decode optional stream) (void))
(check-equal? (· stream pos) 1))
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+Optional uint8 #t))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1))
;
; it 'should decode when condition is a function and truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> true
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(parameterize ([current-input-port (open-input-bytes (bytes 0))])
(define optional (+Optional uint8 (λ _ #t)))
(check-not-equal? (decode optional) (void))
(check-equal? (pos (current-input-port)) 1))
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #t))])
(check-not-equal? (decode optional stream) (void))
(check-equal? (· stream pos) 1))
;
; describe 'size', ->
; it 'should return 0 when condition is falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, false
; optional.size().should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #f)])
(check-equal? (· optional size) 0))
(check-equal? (size (+Optional uint8 #f)) 0)
;
; it 'should return 0 when condition is a function and falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> false
; optional.size().should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #f))])
(check-equal? (· optional size) 0))
(check-equal? (size (+Optional uint8 (λ _ #f))) 0)
;
; it 'should return given type size when condition is omitted', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8)])
(check-equal? (· optional size) 1))
;
(check-equal? (size (+Optional uint8)) 1)
; it 'should return given type size when condition is truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, true
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #t)])
(check-equal? (· optional size) 1))
(check-equal? (size (+Optional uint8 #t)) 1)
;
; it 'should return given type size when condition is a function and truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> true
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #t))])
(check-equal? (· optional size) 1))
(check-equal? (size (+Optional uint8 (λ _ #t))) 1)
;
; describe 'encode', ->
; it 'should not encode when condition is falsy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, false
; stream.pipe concat (buf) ->
; buf.should.deep.equal []
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 #f)])
(encode optional stream 128)
(check-equal? (dump stream) (+Buffer empty)))
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+Optional uint8 #f))
(encode optional 128)
(check-equal? (dump (current-output-port)) (bytes)))
;
; it 'should not encode when condition is a function and falsy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, -> false
; stream.pipe concat (buf) ->
; buf.should.deep.equal []
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 (λ _ #f))])
(encode optional stream 128)
(check-equal? (dump stream) (+Buffer empty)))
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+Optional uint8 (λ _ #f)))
(encode optional 128)
(check-equal? (dump (current-output-port)) (bytes)))
;
; it 'should encode when condition is omitted', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8)])
(encode optional stream 128)
(check-equal? (dump stream) (+Buffer '(128))))
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+Optional uint8))
(encode optional 128)
(check-equal? (dump (current-output-port)) (bytes 128)))
;
; it 'should encode when condition is truthy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, true
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 #t)])
(encode optional stream 128)
(check-equal? (dump stream) (+Buffer '(128))))
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+Optional uint8 #t))
(encode optional 128)
(check-equal? (dump (current-output-port)) (bytes 128)))
;
; it 'should encode when condition is a function and truthy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, -> true
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 (λ _ #t))])
(encode optional stream 128)
(check-equal? (dump stream) (+Buffer '(128))))
(parameterize ([current-output-port (open-output-bytes)])
(define optional (+Optional uint8 (λ _ #t)))
(encode optional 128)
(check-equal? (dump (current-output-port)) (bytes 128)))
Loading…
Cancel
Save