diff --git a/pitfall/xenomorph/private/buffer.rkt b/pitfall/xenomorph/private/buffer.rkt index 05cb619f..2190f582 100644 --- a/pitfall/xenomorph/private/buffer.rkt +++ b/pitfall/xenomorph/private/buffer.rkt @@ -21,9 +21,9 @@ A Restructure RBuffer object is separate. (define-subclass RestructureBase (RBuffer [len #xffff]) - (define/override (decode stream [parent #f]) - (define decoded-len (resolve-length len stream parent)) - (send stream readBuffer decoded-len)) + (define/override (decode port [parent #f]) + (define decoded-len (resolve-length len port parent)) + (read-bytes decoded-len port)) (define/override (size [val #f] [parent #f]) (when val (unless (bytes? val) @@ -32,12 +32,14 @@ A Restructure RBuffer object is separate. (bytes-length val) (resolve-length len val parent))) - (define/override (encode stream buf [parent #f]) + (define/override (encode port buf [parent #f]) (unless (bytes? buf) (raise-argument-error 'Buffer:encode "bytes" buf)) + (define op (or port (open-output-bytes))) (when (NumberT? len) - (send len encode stream (length buf))) - (send stream writeBuffer buf))) + (send len encode op (length buf))) + (write-bytes buf op) + (unless port (get-output-bytes op)))) (define-subclass RBuffer (BufferT)) diff --git a/pitfall/xenomorph/test/bitfield-test.rkt b/pitfall/xenomorph/test/bitfield-test.rkt index 9565e762..0bfedcff 100644 --- a/pitfall/xenomorph/test/bitfield-test.rkt +++ b/pitfall/xenomorph/test/bitfield-test.rkt @@ -21,44 +21,27 @@ https://github.com/mbutterick/restructure/blob/master/test/Bitfield.coffee (match-define (list JACK KACK LACK MACK NACK OACK PACK QUACK) (map (curry arithmetic-shift 1) (range 8))) -; ; it 'should have the right size', -> -; bitfield.size().should.equal 1 - (check-equal? (size bitfield) 1) -; ; it 'should decode', -> -; stream = new DecodeStream new Buffer [JACK | MACK | PACK | NACK | QUACK] -; bitfield.decode(stream).should.deep.equal -; Jack: yes, Kack: no, Lack: no, Mack: yes, Nack: yes, Oack: no, Pack: yes, Quack: yes - -(let ([stream (+DecodeStream (+Buffer (list (bitwise-ior JACK MACK PACK NACK QUACK))))]) - (check-equal? (decode bitfield stream) (mhasheq 'Quack #t - 'Nack #t - 'Lack #f - 'Oack #f - 'Pack #t - 'Mack #t - 'Jack #t - 'Kack #f))) - - -; -; it 'should encode', (done) -> -; stream = new EncodeStream -; stream.pipe concat (buf) -> -; buf.should.deep.equal new Buffer [JACK | MACK | PACK | NACK | QUACK] -; done() -; -; bitfield.encode stream, Jack: yes, Kack: no, Lack: no, Mack: yes, Nack: yes, Oack: no, Pack: yes, Quack: yes - -(check-equal? (encode bitfield #f (mhasheq 'Quack #t +(parameterize ([current-input-port (open-input-bytes (bytes (bitwise-ior JACK MACK PACK NACK QUACK)))]) + (check-equal? (decode bitfield) (mhasheq 'Quack #t 'Nack #t 'Lack #f 'Oack #f 'Pack #t 'Mack #t 'Jack #t - 'Kack #f)) - (+Buffer (list (bitwise-ior JACK MACK PACK NACK QUACK)))) + 'Kack #f))) + +; it 'should encode', (done) -> +(check-equal? (encode bitfield (mhasheq 'Quack #t + 'Nack #t + 'Lack #f + 'Oack #f + 'Pack #t + 'Mack #t + 'Jack #t + 'Kack #f) #f) + (bytes (bitwise-ior JACK MACK PACK NACK QUACK))) diff --git a/pitfall/xenomorph/test/buffer-test.rkt b/pitfall/xenomorph/test/buffer-test.rkt index 336e46c8..9859a8e5 100644 --- a/pitfall/xenomorph/test/buffer-test.rkt +++ b/pitfall/xenomorph/test/buffer-test.rkt @@ -9,80 +9,35 @@ https://github.com/mbutterick/restructure/blob/master/test/Buffer.coffee ;describe 'Buffer', -> ; describe 'decode', -> ; it 'should decode', -> -; stream = new DecodeStream new Buffer [0xab, 0xff, 0x1f, 0xb6] -; buf = new BufferT(2) -; buf.decode(stream).should.deep.equal new Buffer [0xab, 0xff] -; buf.decode(stream).should.deep.equal new Buffer [0x1f, 0xb6] +(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff #x1f #xb6))]) + (define buf (+BufferT 2)) + (check-equal? (decode buf) (bytes #xab #xff)) + (check-equal? (decode buf) (bytes #x1f #xb6))) -(let ([stream (+DecodeStream (+Buffer '(#xab #xff #x1f #xb6)))] - [buf (+BufferT 2)]) - (check-equal? (decode buf stream) (+Buffer '(#xab #xff))) - (check-equal? (decode buf stream) (+Buffer '(#x1f #xb6)))) - -; ; it 'should decode with parent key length', -> -; stream = new DecodeStream new Buffer [0xab, 0xff, 0x1f, 0xb6] -; buf = new BufferT('len') -; buf.decode(stream, len: 3).should.deep.equal new Buffer [0xab, 0xff, 0x1f] -; buf.decode(stream, len: 1).should.deep.equal new Buffer [0xb6] - -(let ([stream (+DecodeStream (+Buffer '(#xab #xff #x1f #xb6)))] - [buf (+BufferT 'len)]) - (check-equal? (decode buf stream (hash 'len 3)) (+Buffer '(#xab #xff #x1f))) - (check-equal? (decode buf stream (hash 'len 1)) (+Buffer '(#xb6)))) +(parameterize ([current-input-port (open-input-bytes (bytes #xab #xff #x1f #xb6))]) + (define buf (+BufferT 'len)) + (check-equal? (decode buf #:parent (hash 'len 3)) (bytes #xab #xff #x1f)) + (check-equal? (decode buf #:parent (hash 'len 1)) (bytes #xb6))) -; ; describe 'size', -> ; it 'should return size', -> -; buf = new BufferT(2) -; buf.size(new Buffer [0xab, 0xff]).should.equal 2 - -(let ([buf (+BufferT 2)]) - (check-equal? (size buf (+Buffer '(#xab #xff))) 2)) +(check-equal? (size (+BufferT 2) (bytes #xab #xff)) 2) -; -; it 'should use defined length if no value given', -> -; array = new BufferT 10 -; array.size().should.equal 10 -(let ([array (+BufferT 10)]) - (check-equal? (size array) 10)) +; it 'should use defined length if no value given', ->x +(check-equal? (size (+BufferT 10)) 10) -; ; describe 'encode', -> ; it 'should encode', (done) -> -; stream = new EncodeStream -; stream.pipe concat (buf) -> -; buf.should.deep.equal new Buffer [0xab, 0xff, 0x1f, 0xb6] -; done() -; -; buf = new BufferT(2) -; buf.encode stream, new Buffer [0xab, 0xff] -; buf.encode stream, new Buffer [0x1f, 0xb6] -; stream.end() -; - -(let ([stream (+EncodeStream)] - [buf (+BufferT 2)]) - (encode buf stream (+Buffer '(#xab #xff))) - (encode buf stream (+Buffer '(#x1f #xb6))) - (check-equal? (dump stream) (+Buffer '(#xab #xff #x1f #xb6)))) +(let ([buf (+BufferT 2)]) + (check-equal? (bytes-append + (encode buf (bytes #xab #xff) #f) + (encode buf (bytes #x1f #xb6) #f)) (bytes #xab #xff #x1f #xb6))) ; it 'should encode length before buffer', (done) -> -; stream = new EncodeStream -; stream.pipe concat (buf) -> -; buf.should.deep.equal new Buffer [2, 0xab, 0xff] -; done() -; -; buf = new BufferT(uint8) -; buf.encode stream, new Buffer [0xab, 0xff] -; stream.end() - -(let ([stream (+EncodeStream)] - [buf (+BufferT uint8)]) - (encode buf stream (+Buffer '(#xab #xff))) - (check-equal? (dump stream) (+Buffer '(2 #xab #xff)))) \ No newline at end of file +(check-equal? (encode (+BufferT uint8) (bytes #xab #xff) #f) (bytes 2 #xab #xff)) \ No newline at end of file