diff --git a/xenomorph/xenomorph/base.rkt b/xenomorph/xenomorph/base.rkt index d04088fc..c790784f 100644 --- a/xenomorph/xenomorph/base.rkt +++ b/xenomorph/xenomorph/base.rkt @@ -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?) diff --git a/xenomorph/xenomorph/scribblings/xenomorph.scrbl b/xenomorph/xenomorph/scribblings/xenomorph.scrbl index 6e8e7024..6f50b122 100644 --- a/xenomorph/xenomorph/scribblings/xenomorph.scrbl +++ b/xenomorph/xenomorph/scribblings/xenomorph.scrbl @@ -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} diff --git a/xenomorph/xenomorph/test/api-test.rkt b/xenomorph/xenomorph/test/api-test.rkt new file mode 100644 index 00000000..052929b9 --- /dev/null +++ b/xenomorph/xenomorph/test/api-test.rkt @@ -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) diff --git a/xenomorph/xenomorph/test/main.rkt b/xenomorph/xenomorph/test/main.rkt index 058f2987..57861dd2 100644 --- a/xenomorph/xenomorph/test/main.rkt +++ b/xenomorph/xenomorph/test/main.rkt @@ -13,4 +13,5 @@ "string-test.rkt" "symbol-test.rkt" "vector-test.rkt" - "versioned-dict-test.rkt") + "versioned-dict-test.rkt" + "api-test.rkt")