(import defstruct test) (defstruct empty) (test-group "empty" (test #t (empty? (make-empty)))) (defstruct simple field) (test-group "simple" (test #t (simple? (make-simple field: 'foo))) (test 'foo (simple-field (make-simple field: 'foo)))) (defstruct init (field-1 'one) (field-2 (list 'two))) (test-group "with initializer" (test #t (init? (make-init 'foo))) (test 'foo (init-field-1 (make-init field-1: 'foo))) ;; (list) should get evaluated twice (test #t (not (eq? (init-field-2 (make-init)) (init-field-2 (make-init)))))) (defstruct complex field-1 (field-2 'two) field-3 (field-4 (list 'four))) (test-group "complex" (test #t (complex? (make-complex))) (test 'foo (complex-field-1 (make-complex field-1: 'foo))) (test #f (complex-field-3 (make-complex field-1: 'foo))) (test #f (complex-field-1 (make-complex field-2: 'foo))) (test 'two (complex-field-2 (make-complex field-1: 'foo))) (test 'hai (complex-field-2 (make-complex field-2: 'hai field-1: 'foo)))) (test-group "updaters" (let* ((c1 (make-complex field-1: 'foo)) (c2 (update-complex c1 field-1: '())) (c3 (update-complex c1 field-2: 'qux))) (test 'foo (complex-field-1 c1)) (test 'two (complex-field-2 c1)) (test '() (complex-field-1 c2)) (test 'two (complex-field-2 c2)) (test 'foo (complex-field-1 c3)) ;; (list) initializer should not get re-evaluated on copy (test #t (eq? (complex-field-4 c1) (complex-field-4 c2))) (test #t (eq? (complex-field-4 c1) (complex-field-4 c3))) (test #f (complex-field-3 c1)) (set-complex! c1 field-1: 'mutated) (test 'mutated (complex-field-1 c1)) (test '() (complex-field-1 c2)))) (test-group "hygiene" (defstruct ini-capture (uninitialized 1)) (define i1 (make-ini-capture)) (test 1 (ini-capture-uninitialized (update-ini-capture i1))) (test 2 (ini-capture-uninitialized (update-ini-capture i1 uninitialized: 2))) (set-ini-capture! i1 uninitialized: 'uninitialized) (test 'uninitialized (ini-capture-uninitialized i1))) (test-group "alist conversion" (define rec1 (make-complex field-1: 1 field-2: 2 field-3: 3 field-4: 4)) (test '((field-1 . 1) (field-2 . 2) (field-3 . 3) (field-4 . 4)) (complex->alist rec1)) (define rec2 (alist->complex '((field-1 . 1) (field-2 . 2) (field-3 . 3) (field-4 . 4)))) (test 1 (complex-field-1 rec2)) (test 2 (complex-field-2 rec2)) (test 3 (complex-field-3 rec2)) (test 4 (complex-field-4 rec2)) (test-assert (complex? (alist->complex '()))) ;; Found by David Krentzlin (test-assert (complex? (alist->complex '((nonexistant-field . 'hi)))))) (test-exit)