reorg tests; add enum, reserved, and optional types

main
Matthew Butterick 7 years ago
parent dd82c98ef5
commit 6008236b1e

@ -0,0 +1,23 @@
#lang restructure/racket
(require "stream.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Enum.coffee
|#
(define-subclass Streamcoder (Enum type [options empty])
(define/augment (decode stream . _)
(define index (send type decode stream))
(or (list-ref options index) index))
(define/override (size . _) (send type size))
(define/augment (encode stream val [ctx #f])
(define index (index-of options val))
(unless index
(raise-argument-error 'Enum:encode "valid option" val))
(send type encode stream index)))

@ -14,7 +14,8 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(define/public-final (get index)
(unless (<= 0 index (sub1 len))
(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index))
#;(raise-argument-error 'LazyArray:get (format "index in range 0 to ~a" len) index)
(void))
(ref! item-cache index (λ ()
(define orig-pos (· stream pos))
(send stream pos (+ starting-pos (* (send type size #f ctx) index)))

@ -1,25 +1,16 @@
#lang restructure/racket
(r+p "base.rkt"
"number.rkt"
"struct.rkt"
"versioned-struct.rkt"
"string.rkt"
"array.rkt"
"lazy-array.rkt"
"bitfield.rkt"
"stream.rkt"
"buffer.rkt"
"pointer.rkt")
(test-module
(require "number-test.rkt"
"struct-test.rkt"
"versioned-struct-test.rkt"
"string-test.rkt"
"array-test.rkt"
"lazy-array-test.rkt"
"bitfield-test.rkt"
"stream-test.rkt"
"buffer-test.rkt"
"pointer-test.rkt"))
"enum.rkt"
"lazy-array.rkt"
"number.rkt"
"optional.rkt"
"pointer.rkt"
"reserved.rkt"
"stream.rkt"
"string.rkt"
"struct.rkt"
"versioned-struct.rkt")

@ -0,0 +1,29 @@
#lang restructure/racket
(require "stream.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Optional.coffee
|#
(define-subclass Streamcoder (Optional type [condition #t])
(define (resolve-condition parent)
(if (procedure? condition)
(condition parent)
condition))
(define/augment (decode stream parent)
(when (resolve-condition parent)
(send type decode stream parent)))
(define/override (size [val #f] [parent #f])
(if (resolve-condition parent)
(send type size val parent)
0))
(define/augment (encode stream val parent)
(when (resolve-condition parent)
(send type encode stream val parent))))

@ -40,10 +40,10 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(relative-getter-or-0 ctx)))
(define ptr (+ offset relative))
(cond
[type (define val undefined)
[type (define val (void))
(define (decode-value)
(cond
[(not (eq? val undefined)) val]
[(not (void? val)) val]
[else
(define orig-pos (· stream pos))
(send stream pos ptr)

@ -0,0 +1,21 @@
#lang restructure/racket
(require "stream.rkt" "utils.rkt")
(provide (all-defined-out))
#|
approximates
https://github.com/mbutterick/restructure/blob/master/src/Reserved.coffee
|#
(define-subclass Streamcoder (Reserved type [count 1])
(define/augment (decode stream parent)
(send stream pos (+ (· stream pos) (size #f parent)))
(void))
(define/override (size [val #f] [parent #f])
(* (send type size) (resolve-length count #f parent)))
(define/augment (encode stream val [parent #f])
(send stream fill 0 (size val parent))))

@ -34,7 +34,8 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee
(define/public-final (write val)
(unless (bytes? val)
(raise-argument-error 'EncodeStream:write "bytes" val))
(void (write-bytes val _port)))
(write-bytes val _port)
(void))
(define/public-final (writeBuffer buffer)
(write buffer))
@ -49,25 +50,31 @@ https://github.com/mbutterick/restructure/blob/master/src/EncodeStream.coffee
[(utf16le ucs2 utf8 ascii) (writeBuffer (string->bytes/utf-8 string))
(when (eq? encoding 'utf16le)
(error 'swap-bytes-unimplemented))]
[else (error 'unsupported-string-encoding)])))
[else (error 'unsupported-string-encoding)]))
(define/public (fill val len)
(write (make-bytes len val))))
(test-module
(define es (+EncodeStream))
(check-true (EncodeStream? es))
(send es write #"AB")
(check-equal? (· es pos) 2)
(send es write #"C")
(check-equal? (· es pos) 3)
(send es write #"D")
(check-equal? (· es pos) 4)
(check-exn exn:fail? (λ () (send es write -42)))
(check-exn exn:fail? (λ () (send es write 1)))
(define op (open-output-bytes))
(define es2 (+EncodeStream op))
(send es2 write #"FOOBAR")
(check-equal? (send es2 dump) #"FOOBAR")
(check-equal? (send es2 dump) #"FOOBAR") ; dump can repeat
(check-equal? (get-output-bytes op) #"FOOBAR"))
(define es (+EncodeStream))
(check-true (EncodeStream? es))
(send es write #"AB")
(check-equal? (· es pos) 2)
(send es write #"C")
(check-equal? (· es pos) 3)
(send es write #"D")
(check-equal? (· es pos) 4)
(check-exn exn:fail? (λ () (send es write -42)))
(check-exn exn:fail? (λ () (send es write 1)))
(define op (open-output-bytes))
(define es2 (+EncodeStream op))
(send es2 write #"FOOBAR")
(check-equal? (send es2 dump) #"FOOBAR")
(check-equal? (send es2 dump) #"FOOBAR") ; dump can repeat
(check-equal? (get-output-bytes op) #"FOOBAR")
(define es3 (+EncodeStream))
(send es3 fill 0 10)
(check-equal? (send es3 dump) (make-bytes 10 0)))
#| approximates

@ -1,6 +1,6 @@
#lang restructure/racket
(require racket/dict "stream.rkt" racket/private/generic-methods racket/struct)
(provide (all-defined-out) ref* ref*-set!)
(provide (all-defined-out) ref* ref*-set! (all-from-out racket/dict))
(require (prefix-in d: racket/dict))
#|
@ -82,8 +82,8 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define val (if (procedure? type)
(type res)
(send type decode stream res)))
;; skip PropertyDescriptor maneuver. Only used for lazy pointer
(ref-set! res key val)
(unless (void? val)
(ref-set! res key val))
(ref-set! res '_currentOffset (- (· stream pos) (· res _startOffset)))
res))

@ -1,22 +0,0 @@
#lang restructure/racket
(require "main.rkt")
(define Person
(make-object RStruct
(list (cons 'name (make-object RString uint8 'utf8))
(cons 'age uint8))))
;; decode a person from a buffer
(define stream-in (make-object RDecodeStream #"\4MikeA"))
(define x (send Person decode stream-in))
(test-module
(check-equal? (hash-ref x 'name) "Mike")
(check-equal? (hash-ref x 'age) 65))
;; encode a person from a hash
(define stream-out (make-object REncodeStream))
(send Person encode stream-out (hasheq 'name "Mike" 'age 65))
(test-module
(check-equal? (send stream-out dump) #"\4MikeA"))

@ -1,5 +1,5 @@
#lang restructure/racket
(require "array.rkt" "stream.rkt" "number.rkt" "buffer.rkt" rackunit "pointer.rkt")
#lang restructure/test/racket
#|
approximates

@ -1,5 +1,5 @@
#lang restructure/racket
(require "bitfield.rkt" "number.rkt" "stream.rkt" "buffer.rkt" rackunit racket/match)
#lang restructure/test/racket
(require racket/match)
#|
approximates

@ -1,5 +1,5 @@
#lang restructure/racket
(require "buffer.rkt" "stream.rkt" "number.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -0,0 +1,54 @@
#lang restructure/test/racket
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/Enum.coffee
|#
;describe 'Enum', ->
; e = new Enum uint8, ['foo', 'bar', 'baz']
; it 'should have the right size', ->
; e.size().should.equal 1
(define e (+Enum uint8 '("foo" "bar" "baz")))
(check-equal? (send e size) 1)
;
; it 'should decode', ->
; stream = new DecodeStream new Buffer [1, 2, 0]
; e.decode(stream).should.equal 'bar'
; e.decode(stream).should.equal 'baz'
; e.decode(stream).should.equal 'foo'
(let ([stream (+DecodeStream (+Buffer '(1 2 0)))])
(check-equal? (send e decode stream) "bar")
(check-equal? (send e decode stream) "baz")
(check-equal? (send e decode stream) "foo"))
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [1, 2, 0]
; done()
;
; e.encode stream, 'bar'
; e.encode stream, 'baz'
; e.encode stream, 'foo'
; stream.end()
(let ([stream (+EncodeStream)])
(send e encode stream "bar")
(send e encode stream "baz")
(send e encode stream "foo")
(check-equal? (send stream dump) (+Buffer '(1 2 0))))
;
; it 'should throw on unknown option', ->
; stream = new EncodeStream
; should.throw ->
; e.encode stream, 'unknown'
(check-exn exn:fail:contract? (λ () (send e encode (+EncodeStream) "unknown")))

@ -1,5 +1,4 @@
#lang restructure/racket
(require "lazy-array.rkt" "array.rkt" "stream.rkt" "number.rkt" "buffer.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -0,0 +1,15 @@
#lang restructure/racket
(require "array-test.rkt"
"bitfield-test.rkt"
"buffer-test.rkt"
"enum-test.rkt"
"lazy-array-test.rkt"
"number-test.rkt"
"optional-test.rkt"
"pointer-test.rkt"
"reserved-test.rkt"
"stream-test.rkt"
"string-test.rkt"
"struct-test.rkt"
"versioned-struct-test.rkt")

@ -1,5 +1,5 @@
#lang restructure/racket
(require "number.rkt" "stream.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -0,0 +1,200 @@
#lang restructure/test/racket
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/Optional.coffee
|#
;describe 'Optional', ->
; describe 'decode', ->
; it 'should not decode when condition is falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, false
; should.not.exist optional.decode(stream)
; stream.pos.should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #f)])
(check-equal? (send optional decode stream) (void))
(check-equal? (· stream pos) 0))
; it 'should not decode when condition is a function and falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> false
; should.not.exist optional.decode(stream)
; stream.pos.should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #f))])
(check-equal? (send optional decode stream) (void))
(check-equal? (· stream pos) 0))
;
; it 'should decode when condition is omitted', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8)])
(check-not-equal? (send optional decode stream) (void))
(check-equal? (· stream pos) 1))
;
; it 'should decode when condition is truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, true
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #t)])
(check-not-equal? (send optional decode stream) (void))
(check-equal? (· stream pos) 1))
;
; it 'should decode when condition is a function and truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> true
; should.exist optional.decode(stream)
; stream.pos.should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #t))])
(check-not-equal? (send optional decode stream) (void))
(check-equal? (· stream pos) 1))
;
; describe 'size', ->
; it 'should return 0 when condition is falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, false
; optional.size().should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #f)])
(check-equal? (· optional size) 0))
;
; it 'should return 0 when condition is a function and falsy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> false
; optional.size().should.equal 0
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #f))])
(check-equal? (· optional size) 0))
;
; it 'should return given type size when condition is omitted', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8)])
(check-equal? (· optional size) 1))
;
; it 'should return given type size when condition is truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, true
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 #t)])
(check-equal? (· optional size) 1))
;
; it 'should return given type size when condition is a function and truthy', ->
; stream = new DecodeStream new Buffer [0]
; optional = new Optional uint8, -> true
; optional.size().should.equal 1
(let ([stream (+DecodeStream (+Buffer '(0)))]
[optional (+Optional uint8 (λ _ #t))])
(check-equal? (· optional size) 1))
;
; describe 'encode', ->
; it 'should not encode when condition is falsy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, false
; stream.pipe concat (buf) ->
; buf.should.deep.equal []
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 #f)])
(send optional encode stream 128)
(check-equal? (send stream dump) (+Buffer empty)))
;
; it 'should not encode when condition is a function and falsy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, -> false
; stream.pipe concat (buf) ->
; buf.should.deep.equal []
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 (λ _ #f))])
(send optional encode stream 128)
(check-equal? (send stream dump) (+Buffer empty)))
;
; it 'should encode when condition is omitted', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8)])
(send optional encode stream 128)
(check-equal? (send stream dump) (+Buffer '(128))))
;
; it 'should encode when condition is truthy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, true
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 #t)])
(send optional encode stream 128)
(check-equal? (send stream dump) (+Buffer '(128))))
;
; it 'should encode when condition is a function and truthy', (done) ->
; stream = new EncodeStream
; optional = new Optional uint8, -> true
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [128]
; done()
;
; optional.encode stream, 128
; stream.end()
(let ([stream (+EncodeStream)]
[optional (+Optional uint8 (λ _ #t))])
(send optional encode stream 128)
(check-equal? (send stream dump) (+Buffer '(128))))

@ -1,5 +1,4 @@
#lang restructure/racket
(require "pointer.rkt" "stream.rkt" "buffer.rkt" "base.rkt" "number.rkt" "struct.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -0,0 +1,6 @@
#lang racket/base
(require rackunit restructure restructure/racket)
(provide (all-from-out rackunit restructure restructure/racket))
(module reader syntax/module-reader
#:language 'restructure/test/racket)

@ -0,0 +1,50 @@
#lang restructure/test/racket
#|
approximates
https://github.com/mbutterick/restructure/blob/master/test/Reserved.coffee
|#
;describe 'Reserved', ->
; it 'should have a default count of 1', ->
; reserved = new Reserved uint8
; reserved.size().should.equal 1
(let ([reserved (+Reserved uint8)])
(check-equal? (send reserved size) 1))
;
; it 'should allow custom counts and types', ->
; reserved = new Reserved uint16, 10
; reserved.size().should.equal 20
(let ([reserved (+Reserved uint16be 10)])
(check-equal? (send reserved size) 20))
;
; it 'should decode', ->
; stream = new DecodeStream new Buffer [0, 0]
; reserved = new Reserved uint16
; should.not.exist reserved.decode(stream)
; stream.pos.should.equal 2
(let ([stream (+DecodeStream (+Buffer '(0 0)))]
[reserved (+Reserved uint16be)])
(check-equal? (send reserved decode stream) (void))
(check-equal? (send stream pos) 2))
;
; it 'should encode', (done) ->
; stream = new EncodeStream
; reserved = new Reserved uint16
; stream.pipe concat (buf) ->
; buf.should.deep.equal new Buffer [0, 0]
; done()
;
; reserved.encode stream
; stream.end()
(let ([stream (+EncodeStream)]
[reserved (+Reserved uint16be)])
(send reserved encode stream)
(check-equal? (send stream dump) (+Buffer '(0 0))))

@ -1,5 +1,5 @@
#lang restructure/racket
(require "number.rkt" "buffer.rkt" "stream.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -1,5 +1,5 @@
#lang restructure/racket
(require "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit)
#lang restructure/test/racket
#|
approximates

@ -1,5 +1,4 @@
#lang restructure/racket
(require "struct.rkt" "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit "pointer.rkt")
#lang restructure/test/racket
#|
approximates
@ -207,8 +206,8 @@ https://github.com/mbutterick/restructure/blob/master/test/Struct.coffee
(let ([stream (+EncodeStream)]
[struct (+Struct (dictify 'name (+StringT uint8)
'age uint8
'ptr (+Pointer uint8 (+StringT uint8))))])
(send struct encode stream (mhasheq 'name "devon" 'age 21 'ptr "hello"))
(check-equal? (send stream dump) (+Buffer "\x05devon\x15\x08\x05hello")))
[struct (+Struct (dictify 'name (+StringT uint8)
'age uint8
'ptr (+Pointer uint8 (+StringT uint8))))])
(send struct encode stream (mhasheq 'name "devon" 'age 21 'ptr "hello"))
(check-equal? (send stream dump) (+Buffer "\x05devon\x15\x08\x05hello")))

@ -0,0 +1,18 @@
#lang restructure/test/racket
(define Person
(make-object Struct
(list (cons 'name (make-object StringT uint8 'utf8))
(cons 'age uint8))))
;; decode a person from a buffer
(define stream-in (make-object DecodeStream #"\4MikeA"))
(define x (send Person decode stream-in))
(test-module
(check-equal? (dict-ref x 'name) "Mike")
(check-equal? (dict-ref x 'age) 65))
;; encode a person from a hash
(test-module
(check-equal? (send Person encode #f (hasheq 'name "Mike" 'age 65)) #"\4MikeA"))

@ -1,5 +1,4 @@
#lang restructure/racket
(require "versioned-struct.rkt" "string.rkt" "number.rkt" "buffer.rkt" "stream.rkt" rackunit "pointer.rkt")
#lang restructure/test/racket
#|
approximates
Loading…
Cancel
Save