From 35c872b18506ad17f1a7dbe63582cb8cac787180 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 23 Jun 2017 15:07:00 -0700 Subject: [PATCH] alltests work except subsets --- pitfall/pitfall/alltest.rkt | 6 +- pitfall/pitfall/test/test12rkt.pdf | Bin 6345 -> 0 bytes pitfall/restructure/buffer-test.rkt | 61 ++++++++++++++++++ pitfall/restructure/buffer.rkt | 44 +++++++------ pitfall/restructure/number.rkt | 5 +- pitfall/restructure/racket.rkt | 3 + pitfall/restructure/stream-test.rkt | 92 ++++++++++++++++++++++++++++ 7 files changed, 187 insertions(+), 24 deletions(-) create mode 100644 pitfall/restructure/buffer-test.rkt create mode 100644 pitfall/restructure/stream-test.rkt 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 4f208f6094af53f09393de72ccf9aae499e9e5e9..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 6345 zcmZ`;ZA@d?c|P~r7-L|t4d%<(9ODnZ%=r4+*TxgZ!+>FzWriWZ%t9D4Yp?-t2TYA8 zn%OAPG|6ryvTf6LD@Ey6$)70Q_D5N@s#qp%Cd#T(|Fo+pX{1JqvVYQzqW)>N-Bi^= zpZDBrV}=c*%Q@%0=bZaK&--x>?*!*F(<9N#FmLBcpIX2tM z+tO)Uem6(^(;k)YOk*QXADmWsL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cb#XB<+n zE63l9KvL$f3Lu zdSPgAI_?c+?xhBz-C<|w+DO+zMjI9ASK#OIUT@#kJKEU#bD_rX{-@RIN!*Am=loF0 z1ABi#{s6vTd_sR0;2&uz12yFay3x7Nx+0ZaXuxc&!t@YKj}8;SU`N`(10cFPTd+kt zwlJY3LdlhTsxGYm;XsMLzTe`(4Z|-$do<_xxHmBy_7A(-R@JBwFB+_UV_@j((Vp$+-QQW|m*1A8 zKU_JIPG3D@8IQQF-}~&y5tG84PZctU(n}b~VF-qN=$XQN`p^`dURd_8@B@A@lRMzk zd^?*vGzUeC7x{yg2tQ|>^VumsZ&R==f`-_*m> zF?kMkzK6~vOX6KGPIShs!|Z!_^OzOb0{ax*gh+G@=f_0n$pB#Ilps< zE4Ik;^ZCNocA3XVqbT2?)-;#hK|$)ph3VPl8^e>^*-|-QvPG`%Z&0->HHsyZ**#-X zB8f$M1&fwu^7|X5!d|(E1=Ob%BWi3E8{f}vp)_WT%#^eD3meyWx9;cZ>$GY@RK-T3 zi6~7@@Mts@xBVX$h{yQhYQXNV zbR(sSK2?W$#vdnJp{#huZ7p`H&Q-8iYD`*fi7A4;XLbu9q*5o`EbQh=MvaoM1NE*A zCCFT1qil?XY-1<82m3xt8s`^@g6xc{+mnlfHb~Bqn@;mweiPf>%(%*_;+_rn#s_Drp*O zG0`L-#VC=}8%;E-J|!6!qvWXGB=spNz0rk=V?jA_cJmje(}4CmH;{D*CNTH0)Jknp`dv|rY351DVMUyfhF<3 r4kC|H{Tj((p;#<)G64FEg;(GMD)F^nM$Q&&B@{Jj3kIidP22t-TBgJG 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