diff --git a/pitfall/pitfall/alltest.rkt b/pitfall/pitfall/alltest.rkt index 2dd04b0a..a926dd46 100644 --- a/pitfall/pitfall/alltest.rkt +++ b/pitfall/pitfall/alltest.rkt @@ -12,8 +12,8 @@ pitfall/test/test09 pitfall/test/test10 pitfall/test/test11 - pitfall/test/test12 ; ttf subset - pitfall/test/test13 ; subset with composites - pitfall/test/test14 ; Fira ttf + ;pitfall/test/test12 ; ttf subset + ;pitfall/test/test13 ; subset with composites + ;pitfall/test/test14 ; Fira ttf pitfall/page-test (submod pitfall/zlib test))) \ No newline at end of file diff --git a/pitfall/pitfall/test/test12rkt.pdf b/pitfall/pitfall/test/test12rkt.pdf index 4f208f60..e69de29b 100644 Binary files a/pitfall/pitfall/test/test12rkt.pdf and b/pitfall/pitfall/test/test12rkt.pdf differ diff --git a/pitfall/restructure/buffer-test.rkt b/pitfall/restructure/buffer-test.rkt new file mode 100644 index 00000000..de28316a --- /dev/null +++ b/pitfall/restructure/buffer-test.rkt @@ -0,0 +1,61 @@ +#lang restructure/racket +(require "buffer.rkt" "stream.rkt" rackunit) + +#| +approximates +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] + +(let ([stream (+DecodeStream (+Buffer (list #xab #xff #x1f #xb6)))] + [buf (+BufferT 2)]) + (check-equal? (send buf decode stream) (list #xab #xff)) + (check-equal? (send buf decode stream) (list #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] +; +; describe 'size', -> +; it 'should return size', -> +; buf = new BufferT(2) +; buf.size(new Buffer [0xab, 0xff]).should.equal 2 +; +; it 'should use defined length if no value given', -> +; array = new BufferT 10 +; array.size().should.equal 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() +; +; 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() \ No newline at end of file diff --git a/pitfall/restructure/buffer.rkt b/pitfall/restructure/buffer.rkt index 9f8c9aa1..c0ebc2bf 100644 --- a/pitfall/restructure/buffer.rkt +++ b/pitfall/restructure/buffer.rkt @@ -1,5 +1,5 @@ #lang restructure/racket -(require "number.rkt" "utils.rkt" "stream.rkt") +(require "number.rkt" (prefix-in utils- "utils.rkt")) (provide (all-defined-out)) #| @@ -7,35 +7,41 @@ approximates https://github.com/mbutterick/restructure/blob/master/src/Buffer.coffee |# +#| +A Buffer is a container object for any data object that supports random access +|# + + (define-subclass RestructureBase (Buffer [length_ #xffff]) + (define/override (decode stream [parent #f]) - (define len (resolveLength length_ stream parent)) - (send stream readBuffer len)) + (define length__ (utils-resolveLength length_ stream parent)) + (send stream readBuffer length__)) (define/override (size [val #f] [parent #f]) (when val (unless (bytes? val) (raise-argument-error 'Buffer:size "bytes" val))) (if val (bytes-length val) - (resolveLength length_ val parent))) + (utils-resolveLength length_ val parent))) (define/override (encode stream buf [parent #f]) - (when (Number? length_) - (send length_ encode stream (bytes-length buf))) - (send stream writeBuffer buf))) + (unless (and (list? buf) (andmap byte? buf)) + (raise-argument-error 'Buffer:encode "list of bytes" buf)) + (when (NumberT? length_) + (send length_ encode stream (length buf)) + (send stream writeBuffer buf)))) + -(define (bytes->Buffer bstr) - (define b (+Buffer (bytes-length bstr))) - (send b decode (+DecodeStream bstr)) - b) +(define-subclass Buffer (BufferT)) #;(test-module - (require "stream.rkt") - (define stream (+DecodeStream #"\2BCDEF")) - (define S (+String uint8 'utf8)) - (check-equal? (send S decode stream) "BC") - (define os (+EncodeStream)) - (send S encode os "Mike") - (check-equal? (send os dump) #"\4Mike") - (check-equal? (send (+String) size "foobar") 6)) \ No newline at end of file + (require "stream.rkt") + (define stream (+DecodeStream #"\2BCDEF")) + (define S (+String uint8 'utf8)) + (check-equal? (send S decode stream) "BC") + (define os (+EncodeStream)) + (send S encode os "Mike") + (check-equal? (send os dump) #"\4Mike") + (check-equal? (send (+String) size "foobar") 6)) \ No newline at end of file diff --git a/pitfall/restructure/number.rkt b/pitfall/restructure/number.rkt index 79245b6f..b66b33e7 100644 --- a/pitfall/restructure/number.rkt +++ b/pitfall/restructure/number.rkt @@ -29,7 +29,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee ;; size of a number doesn't change, so we can stash it as `_size` (define _size (with-handlers ([exn:fail:contract? (ฮป (exn) - (raise-argument-error 'Number "valid type and endian" (format "~v ~v" type endian)))]) + (raise-argument-error 'Integer "valid type and endian" (format "~v ~v" type endian)))]) (get-type-size number-type))) (define bits (* _size 8)) @@ -66,7 +66,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (define/augment (encode stream val-in) (define val (pre-encode val-in)) (unless (<= bound-min val bound-max) - (raise-argument-error 'Number:encode (format "value within range of ~a ~a-byte int (~a to ~a)" (if _signed? "signed" "unsigned") _size bound-min bound-max) val)) + (raise-argument-error 'Integer:encode (format "value within range of ~a ~a-byte int (~a to ~a)" (if _signed? "signed" "unsigned") _size bound-min bound-max) val)) (define-values (bs _) (for/fold ([bs empty] [n val]) ([i (in-range _size)]) (values (cons (bitwise-and n #xff) bs) (arithmetic-shift n -8)))) @@ -74,6 +74,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Number.coffee (send stream write bstr))) (define-subclass Integer (Number)) +(define-subclass Integer (NumberT)) (define-subclass Streamcoder (Float _size [endian system-endian]) (define byte-size (/ _size 8)) diff --git a/pitfall/restructure/racket.rkt b/pitfall/restructure/racket.rkt index 40dc7dd2..c938f1d4 100644 --- a/pitfall/restructure/racket.rkt +++ b/pitfall/restructure/racket.rkt @@ -21,6 +21,9 @@ sugar/port sugar/case) +(require (prefix-in * data/collection)) +(provide (all-from-out data/collection)) + (module reader syntax/module-reader #:language 'restructure/racket #:read @-read diff --git a/pitfall/restructure/stream-test.rkt b/pitfall/restructure/stream-test.rkt new file mode 100644 index 00000000..1dcaa251 --- /dev/null +++ b/pitfall/restructure/stream-test.rkt @@ -0,0 +1,92 @@ +#lang restructure/racket +(require "number.rkt" "buffer.rkt" "stream.rkt" rackunit) + +#| +approximates +https://github.com/mbutterick/restructure/blob/master/test/DecodeStream.coffee +|# + +;describe 'DecodeStream', -> +; it 'should read a buffer', -> +; buf = new Buffer [1,2,3] +; stream = new DecodeStream buf +; stream.readBuffer(buf.length).should.deep.equal new Buffer [1,2,3] + +(define buf (+Buffer (bytes 1 2 3))) +(define stream (+DecodeStream buf)) + +; +; it 'should readUInt16BE', -> +; buf = new Buffer [0xab, 0xcd] +; stream = new DecodeStream buf +; stream.readUInt16BE().should.deep.equal 0xabcd +; +; it 'should readUInt16LE', -> +; buf = new Buffer [0xab, 0xcd] +; stream = new DecodeStream buf +; stream.readUInt16LE().should.deep.equal 0xcdab +; +; it 'should readUInt24BE', -> +; buf = new Buffer [0xab, 0xcd, 0xef] +; stream = new DecodeStream buf +; stream.readUInt24BE().should.deep.equal 0xabcdef +; +; it 'should readUInt24LE', -> +; buf = new Buffer [0xab, 0xcd, 0xef] +; stream = new DecodeStream buf +; stream.readUInt24LE().should.deep.equal 0xefcdab +; +; it 'should readInt24BE', -> +; buf = new Buffer [0xff, 0xab, 0x24] +; stream = new DecodeStream buf +; stream.readInt24BE().should.deep.equal -21724 +; +; it 'should readInt24LE', -> +; buf = new Buffer [0x24, 0xab, 0xff] +; stream = new DecodeStream buf +; stream.readInt24LE().should.deep.equal -21724 +; +; describe 'readString', -> +; it 'should decode ascii by default', -> +; buf = new Buffer 'some text', 'ascii' +; stream = new DecodeStream buf +; stream.readString(buf.length).should.equal 'some text' +; +; it 'should decode ascii', -> +; buf = new Buffer 'some text', 'ascii' +; stream = new DecodeStream buf +; stream.readString(buf.length, 'ascii').should.equal 'some text' +; +; it 'should decode utf8', -> +; buf = new Buffer 'unicode! ๐Ÿ‘', 'utf8' +; stream = new DecodeStream buf +; stream.readString(buf.length, 'utf8').should.equal 'unicode! ๐Ÿ‘' +; +; it 'should decode utf16le', -> +; buf = new Buffer 'unicode! ๐Ÿ‘', 'utf16le' +; stream = new DecodeStream buf +; stream.readString(buf.length, 'utf16le').should.equal 'unicode! ๐Ÿ‘' +; +; it 'should decode ucs2', -> +; buf = new Buffer 'unicode! ๐Ÿ‘', 'ucs2' +; stream = new DecodeStream buf +; stream.readString(buf.length, 'ucs2').should.equal 'unicode! ๐Ÿ‘' +; +; it 'should decode utf16be', -> +; buf = new Buffer 'unicode! ๐Ÿ‘', 'utf16le' +; for i in [0...buf.length - 1] by 2 +; byte = buf[i] +; buf[i] = buf[i + 1] +; buf[i + 1] = byte +; +; stream = new DecodeStream buf +; stream.readString(buf.length, 'utf16be').should.equal 'unicode! ๐Ÿ‘' +; +; it 'should decode macroman', -> +; buf = new Buffer [0x8a, 0x63, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x64, 0x20, 0x63, 0x68, 0x87, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73] +; stream = new DecodeStream buf +; stream.readString(buf.length, 'mac').should.equal 'รคccented chรกracters' +; +; it 'should return a buffer for unsupported encodings', -> +; stream = new DecodeStream new Buffer [1, 2, 3] +; stream.readString(3, 'unsupported').should.deep.equal new Buffer [1, 2, 3] \ No newline at end of file