main
Matthew Butterick 6 years ago
parent a242a4a059
commit 1cbbd7aa56

@ -58,7 +58,7 @@ We don't make port-arg the last arg (similar to other Racket port funcs) because
(unless port-arg (get-output-bytes port)))
(define (size xo [val #f] #:parent [parent #f] . args)
(send xo x:size (and val (send xo pre-encode val)) parent . args))
(send xo x:size val parent . args))
(define (xenomorphic-type? x) (is-a? x x:base%))
(define xenomorphic? xenomorphic-type?)

@ -308,16 +308,6 @@ If @racket[byte-dest] is an @racket[output-port?], the bytes are written there a
If @racket[val] does not match the @racket[xenomorphic-obj] type appropriately — for instance, you try to @racket[encode] a negative integer using an unsigned integer type like @racket[uint8] — then an error will arise.
}
@defproc[
(size
[xenomorphic-obj xenomorphic?]
[val any/c #false]
[#:parent parent (or/c xenomorphic? #false) #false]
[arg any/c] ...)
exact-nonnegative-integer?]{
The length of the @tech{byte string} that @racket[val] would produce if it were encoded using @racket[encode].
}
@section{Core xenomorphic objects}

@ -0,0 +1,43 @@
#lang br
(require xenomorph rackunit)
(define-simple-check (check-xenomorphic type val)
(let ([de (decode type (encode type val #f))])
(if (flonum? val)
(check-= de val 0.01)
(check-equal? de val))))
(define bigint (x:string #:pre-encode number->string
#:post-decode string->number))
(check-xenomorphic bigint 1234567890987654321)
(define exact (x:list #:type bigint
#:length 2
#:pre-encode (λ (x) (list (numerator x) (denominator x)))
#:post-decode (λ (nd) (apply / nd))))
(check-xenomorphic exact 12345678/8765)
(define real (x:versioned-dict
#:type uint8
#:version-key 'version
#:versions
(list
(cons 0 (list (cons 'val exact)))
(cons 1 (list (cons 'val float))))
#:pre-encode (λ (num) (list (cons 'val num)
(cons 'version (if (exact? num)
0
1))))
#:post-decode (λ (h) (hash-ref h 'val))))
(require math)
(check-xenomorphic real pi)
(define complex (x:list #:type real
#:length 2
#:pre-encode (λ (num) (list (real-part num) (imag-part num)))
#:post-decode (λ (ri) (+ (first ri) (* 0+1i (second ri))))))
(check-xenomorphic complex 3/4+5i)

@ -13,4 +13,5 @@
"string-test.rkt"
"symbol-test.rkt"
"vector-test.rkt"
"versioned-dict-test.rkt")
"versioned-dict-test.rkt"
"api-test.rkt")

Loading…
Cancel
Save