You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
535 B
Racket
21 lines
535 B
Racket
6 years ago
|
#lang racket/base
|
||
6 years ago
|
(require rackunit
|
||
|
xenomorph
|
||
|
racket/class)
|
||
6 years ago
|
|
||
|
(define Person
|
||
|
(make-object Struct
|
||
|
(list (cons 'name (make-object StringT uint8 'utf8))
|
||
|
(cons 'age uint8))))
|
||
|
|
||
6 years ago
|
;; decode a person from a port
|
||
|
(define ip (open-input-bytes #"\4MikeA"))
|
||
|
(define x (send Person decode ip))
|
||
6 years ago
|
|
||
6 years ago
|
(module+ test
|
||
6 years ago
|
(check-equal? (dict-ref x 'name) "Mike")
|
||
|
(check-equal? (dict-ref x 'age) 65))
|
||
|
|
||
|
;; encode a person from a hash
|
||
6 years ago
|
(module+ test
|
||
6 years ago
|
(check-equal? (send Person encode #f (hasheq 'name "Mike" 'age 65)) #"\4MikeA"))
|