shithub: sl

ref: 49564ad96a2acfef1ffc6cb8ee32bfb51a29f450
dir: /test/defstruct.sl/

View raw version
(defstruct sa a b (c 3))
(assert (bound? 'make-sa)) ; default constructor is defined
(assert (bound? 'sa-a)) ; slot accessors are defined
(assert (bound? 'sa-b))
(assert (bound? 'sa-c))
(def ax (make-sa))
(assert (sa? ax))
(assert (not (vec? ax)))
(assert (not (cons? ax)))
(assert (equal? (type-of ax) '(struct sa)))
(assert (not (equal? #('sa :a NIL :b NIL :c 3) ax)))
(assert (not (eqv? #('sa :a NIL :b NIL :c 3) ax)))
(assert (not (sa-a ax))) ; a defaults to NIL
(assert (not (sa-b ax))) ; so is b
(assert (= (sa-c ax)))
(def ax (make-sa :a 1 :b 2))
(assert (sa? ax))
(assert (= (sa-a ax) 1))
(assert (= (sa-b ax) 2))
(assert (= (sa-c ax) 3))
(def ax (make-sa :c 0))
(assert (sa? ax))
(assert (not (sa-a ax)))
(assert (not (sa-b ax)))
(assert (= (sa-c ax) 0))

; same struct, different name
(defstruct sb a b (c 3))
(def bx (make-sb))
(assert (sb? bx))
(assert (not (sa? bx)))
(assert (not (vec? bx)))
(assert (not (cons? bx)))
(assert (equal? (type-of bx) '(struct sb)))

; struct as a list, NOT named
(defstruct sl :type list a b (c 3))
(def lx (make-sl))
(assert (not (bound? 'sl?))) ; not :named, should not have a predicate
(assert (cons? lx))
(assert (length= lx 3)) ; 3 slots, not named by default
(assert (not (sl-a lx)))
(assert (not (sl-b lx)))
(assert (= (sl-c lx) 3))

; struct as a list, named
(defstruct sln :type list :named T a b (c 3))
(def lx (make-sln))
(assert (bound? 'sln?)) ; :named, should have a predicate defined
(assert (cons? lx))
(assert (length= lx 4)) ; 4 slots (with the name)
(assert (not (sln-a lx)))
(assert (not (sln-b lx)))
(assert (= (sln-c lx) 3))