From 7fa2496a66a153751f46a213b19377d2dfd0678d Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 22 Jun 2017 17:27:40 -0700 Subject: [PATCH] next: unit tests for restructure --- pitfall/restructure/array-test.rkt | 82 ++++++++++++++++++++++++++++++ pitfall/restructure/pointer.rkt | 6 ++- pitfall/restructure/struct.rkt | 3 +- 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 pitfall/restructure/array-test.rkt diff --git a/pitfall/restructure/array-test.rkt b/pitfall/restructure/array-test.rkt new file mode 100644 index 00000000..1da38843 --- /dev/null +++ b/pitfall/restructure/array-test.rkt @@ -0,0 +1,82 @@ +#lang restructure/racket +(require "array.rkt" "stream.rkt" "number.rkt" rackunit) + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/test/Array.coffee +|# + +;; it 'should decode fixed length', -> +(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))] + [array (+Array uint16be 4 'bytes)]) + (check-equal? (send array decode stream) '(258 772))) + +;; todo +;; it 'should decode length from parent key', -> +#;(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', -> +#;(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', -> +#;(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', -> +#;(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))] + [array (+Array uint8 (λ _ 4))]) + (check-equal? (send array decode stream) '(1 2 3 4))) + +;; todo +;; it 'should decode amount of bytes from function', -> +#;(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', -> +#;(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))] + [array (+Array uint8)]) + (check-equal? (send array decode stream) '(1 2 3 4))) + + +;; it 'should use array length', -> +(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', -> +#;(let ([array (+Array uint8 uint8)]) + (check-equal? (send array size '(1 2 3 4)) 5)) + + +;; it 'should use defined length if no value given', -> +(let ([array (+Array uint8 10)]) + (check-equal? (send array size) 10)) \ No newline at end of file diff --git a/pitfall/restructure/pointer.rkt b/pitfall/restructure/pointer.rkt index 280bb915..7a3a325b 100644 --- a/pitfall/restructure/pointer.rkt +++ b/pitfall/restructure/pointer.rkt @@ -17,7 +17,11 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee (define offset (send offsetType decode stream ctx)) (report scope 'pointer-scope) (define relative (caseq scope - [(local) (or (· ctx res _startOffset) (· ctx _startOffset))] + [(local) (when (and (· ctx res _startOffset) (· ctx _startOffset) + (not (= (· ctx res _startOffset) (· ctx _startOffset)))) + (report* ctx (· ctx res _startOffset) (· ctx _startOffset)) + (error 'bazongas)) + (· ctx _startOffset)] [(parent) (· ctx parent _startOffset)] [(immediate) (- (· stream pos) (send offsetType size))] [(global) diff --git a/pitfall/restructure/struct.rkt b/pitfall/restructure/struct.rkt index 67a57ced..15a46b38 100644 --- a/pitfall/restructure/struct.rkt +++ b/pitfall/restructure/struct.rkt @@ -52,7 +52,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee (if (procedure? type) (type res) (send type decode stream this))) - (hash-set! res key val))) + (hash-set! res key val) + (hash-set! res '_currentOffset (- (· stream pos) (· res _startOffset))))) (define/override (size [input-hash (mhash)] [parent #f] [includePointers #t]) (for/sum ([(key type) (in-dict fields)])