From 27a1591621a9a78fcd7b264fd8ca48d954c7db81 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 27 Jun 2017 11:01:33 -0700 Subject: [PATCH] in array (not working) --- pitfall/restructure/array-test.rkt | 110 ++++++++++++++++++++++++----- pitfall/restructure/array.rkt | 87 +++++++++++++---------- 2 files changed, 143 insertions(+), 54 deletions(-) diff --git a/pitfall/restructure/array-test.rkt b/pitfall/restructure/array-test.rkt index 1da38843..6fdae9ae 100644 --- a/pitfall/restructure/array-test.rkt +++ b/pitfall/restructure/array-test.rkt @@ -6,77 +6,153 @@ approximates https://github.com/mbutterick/restructure/blob/master/test/Array.coffee |# -;; it 'should decode fixed length', -> +;describe 'Array', -> +; describe 'decode', -> +; it 'should decode fixed length', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint8, 4 +; array.decode(stream).should.deep.equal [1, 2, 3, 4] + (let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint8 4)]) (check-equal? (send array decode stream) '(1 2 3 4))) ;; todo -;; it 'should decode fixed amount of bytes', -> -#;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] +; it 'should decode fixed amount of bytes', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint16, 4, 'bytes' +; array.decode(stream).should.deep.equal [258, 772] +(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint16be 4 'bytes)]) (check-equal? (send array decode stream) '(258 772))) ;; todo -;; it 'should decode length from parent key', -> +; it 'should decode length from parent key', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint8, 'len' +; array.decode(stream, len: 4).should.deep.equal [1, 2, 3, 4] +; #;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint8 4 'len)]) (check-equal? (send array decode stream (mhash 'len 4)) '(1 2 3 4))) ;; todo -;; it 'should decode amount of bytes from parent key', -> +; it 'should decode amount of bytes from parent key', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint16, 'len', 'bytes' +; array.decode(stream, len: 4).should.deep.equal [258, 772] #;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint16be 'len 'bytes)]) (check-equal? (send array decode stream (mhash 'len 4)) '(258 772))) ;; todo -;; it 'should decode length as number before array', -> +; it 'should decode length as number before array', -> +; stream = new DecodeStream new Buffer [4, 1, 2, 3, 4, 5] +; array = new ArrayT uint8, uint8 +; array.decode(stream).should.deep.equal [1, 2, 3, 4] #;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint8 uint8)]) (check-equal? (send array decode stream (mhash 'len 4)) '(1 2 3 4))) ;; todo -;; it 'should decode amount of bytes as number before array', -> +; it 'should decode amount of bytes as number before array', -> +; stream = new DecodeStream new Buffer [4, 1, 2, 3, 4, 5] +; array = new ArrayT uint16, uint8, 'bytes' +; array.decode(stream).should.deep.equal [258, 772] #;(let ([stream (+DecodeStream (bytes 4 1 2 3 4 5))] [array (+Array uint16be uint8 'bytes)]) (check-equal? (send array decode stream) '(258 772))) -;; it 'should decode length from function', -> -(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] +; it 'should decode length from function', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint8, -> 4 +; array.decode(stream).should.deep.equal [1, 2, 3, 4] +#;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint8 (λ _ 4))]) (check-equal? (send array decode stream) '(1 2 3 4))) ;; todo -;; it 'should decode amount of bytes from function', -> +; it 'should decode amount of bytes from function', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint16, (-> 4), 'bytes' +; array.decode(stream).should.deep.equal [258, 772] #;(let ([stream (+DecodeStream (bytes 4 1 2 3 4 5))] [array (+Array uint16be (λ _ 4) 'bytes)]) (check-equal? (send array decode stream) '(258 772))) ;; todo -;; it 'should decode to the end of the parent if no length is given', -> +; it 'should decode to the end of the parent if no length is given', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4, 5] +; array = new ArrayT uint8 +; array.decode(stream, _length: 4, _startOffset: 0).should.deep.equal [1, 2, 3, 4] #;(let ([stream (+DecodeStream (bytes 1 2 3 4 5))] [array (+Array uint8)]) (check-equal? (send array decode stream (mhash '_length 4 '_startOffset 0)) '(1 2 3 4))) -;; it 'should decode to the end of the stream if no parent and length is given', -> -(let ([stream (+DecodeStream (bytes 1 2 3 4))] +; it 'should decode to the end of the stream if no parent and length is given', -> +; stream = new DecodeStream new Buffer [1, 2, 3, 4] +; array = new ArrayT uint8 +; array.decode(stream).should.deep.equal [1, 2, 3, 4] +#;(let ([stream (+DecodeStream (bytes 1 2 3 4))] [array (+Array uint8)]) (check-equal? (send array decode stream) '(1 2 3 4))) -;; it 'should use array length', -> +; describe 'size', -> +; it 'should use array length', -> +; array = new ArrayT uint8, 10 +; array.size([1, 2, 3, 4]).should.equal 4 (let ([array (+Array uint8 10)]) (check-equal? (send array size '(1 2 3 4)) 4)) ;; todo -;; it 'should add size of length field before string', -> +; it 'should add size of length field before string', -> +; array = new ArrayT uint8, uint8 +; array.size([1, 2, 3, 4]).should.equal 5 +; #;(let ([array (+Array uint8 uint8)]) (check-equal? (send array size '(1 2 3 4)) 5)) -;; it 'should use defined length if no value given', -> +; it 'should use defined length if no value given', -> +; array = new ArrayT uint8, 10 +; array.size().should.equal 10 (let ([array (+Array uint8 10)]) - (check-equal? (send array size) 10)) \ No newline at end of file + (check-equal? (send array size) 10)) + + + + +; describe 'encode', -> +; it 'should encode using array length', (done) -> +; stream = new EncodeStream +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [1, 2, 3, 4] +; done() +; +; array = new ArrayT uint8, 10 +; array.encode(stream, [1, 2, 3, 4]) +; stream.end() +; +; it 'should encode length as number before array', (done) -> +; stream = new EncodeStream +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [4, 1, 2, 3, 4] +; done() +; +; array = new ArrayT uint8, uint8 +; array.encode(stream, [1, 2, 3, 4]) +; stream.end() +; +; it 'should add pointers after array if length is encoded at start', (done) -> +; stream = new EncodeStream +; stream.pipe concat (buf) -> +; buf.should.deep.equal new Buffer [4, 5, 6, 7, 8, 1, 2, 3, 4] +; done() +; +; array = new ArrayT new Pointer(uint8, uint8), uint8 +; array.encode(stream, [1, 2, 3, 4]) +; stream.end() \ No newline at end of file diff --git a/pitfall/restructure/array.rkt b/pitfall/restructure/array.rkt index d5befa55..aa20001e 100644 --- a/pitfall/restructure/array.rkt +++ b/pitfall/restructure/array.rkt @@ -11,28 +11,41 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee (inherit-field res) (define/augride (decode stream [parent #f]) - (define pos (· stream pos)) + (when parent + (unless (hash? parent) + (raise-argument-error 'Array:decode "hash" parent))) + + (define pos (send stream pos)) + + (define res empty) (define ctx parent) - (define len (cond - ;; explicit length - [length_ (resolveLength length_ stream parent)] - [else ;; implicit length: length of stream divided by size of item - (define num (send stream length)) - (define denom (send type size)) - (unless (andmap (λ (x) (and x (number? x))) (list num denom)) - (raise-argument-error 'Array:decode "valid length and size" (list num denom))) - (floor (/ (send stream length) (send type size)))])) + + (define length__ + (or length_ (resolveLength length_ stream parent))) + + (when (NumberT? length_) + ;; define hidden properties + (hash-set*! (hash-ref ctx 'res) 'parent parent + '_startOffset pos + '_currentOffset 0 + '_length length_)) + + #;(define length__ (cond + ;; explicit length + [length_ (resolveLength length_ stream parent)] + [else ;; implicit length: length of stream divided by size of item + (define num (send stream length)) + (define denom (send type size)) + (unless (andmap (λ (x) (and x (number? x))) (list num denom)) + (raise-argument-error 'Array:decode "valid length and size" (list num denom))) + (floor (/ (send stream length) (send type size)))])) + - (when (Number? length_) - (hash-set! (· ctx res) 'parent parent) - (hash-set! (· ctx res) '_startOffset pos) - (hash-set! (· ctx res) '_currentOffset 0) - (hash-set! (· ctx res) '_length length_)) - (define res (caseq lengthType - [(bytes) (error 'array-decode-bytes-no!)] - [(count) (for/list ([i (in-range len)]) - (send type decode stream ctx))])) + #;(define res (caseq lengthType + [(bytes) (error 'array-decode-bytes-no!)] + [(count) (for/list ([i (in-range length__)]) + (send type decode stream ctx))])) res) (define/override (size [array #f]) @@ -104,23 +117,23 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee (send val toArray) val)))) -(test-module - (define bstr #"ABCD1234") - (define ds (+DecodeStream 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 toArray) '(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")) toArray) '(1 2 3 4)) - - ) +#;(test-module + (define bstr #"ABCD1234") + (define ds (+DecodeStream 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 toArray) '(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")) toArray) '(1 2 3 4)) + + )