move a test

main
Matthew Butterick 6 years ago
parent bd5c93a8bf
commit 7252ab67ef

@ -42,13 +42,16 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define h (make-hasheq))
(for ([(k v) (in-dict x)]
#:unless (memq k private-keys))
(hash-set! h k v))
(hash-set! h k v))
h)
(define xstruct%
(class xenobase%
(super-new)
(init-field [(@fields fields)])
(when @fields (unless (dict? @fields)
(raise-argument-error '+xstruct "dict" @fields)))
(define/augride (xxdecode port parent [len 0])
;; xstruct-setup and xstruct-parse-fields are separate to cooperate with VersionedStruct
@ -76,9 +79,9 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
'pointerSize 0))
(dict-set! parent 'pointerOffset (+ (pos port) (xxsize val parent #f)))
(for ([(key type) (in-dict @fields)])
(send type xxencode (dict-ref val key) port parent))
(send type xxencode (dict-ref val key) port parent))
(for ([ptr (in-list (dict-ref parent 'pointers))])
(send (dict-ref ptr 'type) xxencode (dict-ref ptr 'val) port (dict-ref ptr 'parent))))
(send (dict-ref ptr 'type) xxencode (dict-ref ptr 'val) port (dict-ref ptr 'parent))))
(define/augride (xxsize [val #f] [parent-arg #f] [include-pointers #t])
(define parent (mhasheq 'parent parent-arg
@ -86,7 +89,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
'pointerSize 0))
(define fields-size (for/sum ([(key type) (in-dict @fields)]
#:when (xenomorphic-type? type))
(send type xxsize (and val (dict-ref val key)) parent)))
(send type xxsize (and val (dict-ref val key)) parent)))
(define pointers-size (if include-pointers (dict-ref parent 'pointerSize) 0))
(+ fields-size pointers-size))))
@ -97,11 +100,9 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(unless (even? (length args))
(raise-argument-error '+xstruct "equal number of keys and values" dicts))
(define fields (for/list ([kv (in-slice 2 args)])
(unless (symbol? (car kv))
(raise-argument-error '+xstruct "symbol" (car kv)))
(apply cons kv)))
(unless (dict? fields)
(raise-argument-error '+xstruct "dict" fields))
(unless (symbol? (car kv))
(raise-argument-error '+xstruct "symbol" (car kv)))
(apply cons kv)))
(new class [fields fields]))
(module+ test
@ -109,15 +110,15 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define (random-pick xs) (list-ref xs (random (length xs))))
(check-exn exn:fail:contract? (λ () (+xstruct 42)))
(for ([i (in-range 20)])
;; make random structs and make sure we can round trip
(define field-types
(for/list ([i (in-range 40)])
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
(define size-num-types
(for/sum ([num-type (in-list field-types)])
(size num-type)))
(define xs (+xstruct (for/list ([num-type (in-list field-types)])
(cons (gensym) num-type))))
(define bs (apply bytes (for/list ([i (in-range size-num-types)])
(random 256))))
(check-equal? (encode xs (decode xs bs) #f) bs)))
;; make random structs and make sure we can round trip
(define field-types
(for/list ([i (in-range 40)])
(random-pick (list uint8 uint16be uint16le uint32be uint32le double))))
(define size-num-types
(for/sum ([num-type (in-list field-types)])
(size num-type)))
(define xs (+xstruct (for/list ([num-type (in-list field-types)])
(cons (gensym) num-type))))
(define bs (apply bytes (for/list ([i (in-range size-num-types)])
(random 256))))
(check-equal? (encode xs (decode xs bs) #f) bs)))
Loading…
Cancel
Save