(require-library cells simple-tests datatype) (import typed-lists simple-tests datatype) (define-test (number-lists?) (check ;; argument module (module nums (type? equ?) (import scheme cells) (define (type? x) (or (number? x) ((cell-of? number?) x))) (define (equ? x y) (or (and (number? x) (number? y) (= x y)) (and (cell? x) (cell? y) (= (cell-ref x) (cell-ref y))))) ) ;; apply functor (module lists = (typed-lists nums)) ;; import (import lists cells) (define nil (list-null)) (typed-list? nil) (list-null? nil) (not (null? nil)) (define nls (list-cons 1 nil)) (typed-list? nls) nls (define nlst (typed-list 0 1 (cell 2) 3 4)) (typed-list? nlst) (not (list? nlst)) nlst (= (list-apply + 1 2 (typed-list 3 4 5)) 15) (list-equal? (list-repeat 5 0) (typed-list 0 0 0 0 0)) (list-equal? (list-iterate 5 add1 0) (typed-list 0 1 2 3 4)) (list-equal? (list-iterate-while (lambda (x) (< x 5)) add1 0) (typed-list 0 1 2 3 4)) (list-equal? (list-iterate-until (lambda (x) (= x 5)) add1 0) (typed-list 0 1 2 3 4)) (list-equal? (list-zip (typed-list 1 2 3 4 5) (typed-list 10 20 30)) (typed-list 1 10 2 20 3 30 4 5)) (list-equal? (list-interpose 10 (typed-list 1 2 3 4 5)) (typed-list 1 10 2 10 3 10 4 10 5)) (list-equal? (list-drop 3 nlst) (typed-list 3 4)) (list-equal? (list-drop-while odd? (typed-list 1 3 2 4 5)) (typed-list 2 4 5)) (list-equal? (list-take-while odd? (typed-list 1 3 2 4 5)) (typed-list 1 3)) (receive (head tail) (list-split-with even? (typed-list 1 3 2 4 5)) (and (list-equal? head (typed-list 1 3)) (list-equal? tail (typed-list 2 4 5)))) (list-equal? (list-take 2 nlst) (typed-list 0 1)) (define nrest (list-rest nlst)) nrest (typed-list? (list-null)) (list-null? (list-null)) (not (list-null? nls)) (not (typed-list? '(1 2))) (list-null? (list-rest nls)) (= (list-first nlst) 0) (typed-list? (list-reverse nlst)) (list-reverse nlst) (equal? (typed-list->untyped-list nlst) (list 0 1 (cell 2) 3 4)) (equal? (list-item 2 nlst) (cell 2)) (cell-set! (list-item 2 nlst) 20) (equal? (list-item 2 nlst) (cell 20)) (= (cell-ref (list-item 2 nlst)) 20) (= (list-length nlst) 5) (list-equal? (list-from-upto 2 4 nlst) (typed-list (cell 20) 3)) (list-equal? (list-append (typed-list 0 1 2 3) (typed-list 4 5 6)) (typed-list 0 1 2 3 4 5 6)) (list-equal? (list-append (typed-list 0) (typed-list 1) (typed-list 2) (typed-list 3 4) (typed-list 5 6 7) (typed-list 8)) (typed-list 0 1 2 3 4 5 6 7 8)) (list-equal? (list-map add1 (typed-list 0 1 2 3)) (typed-list 1 2 3 4)) (list-equal? (list-map + (typed-list 1 2 3) (typed-list 10 20 30 40)) (typed-list 11 22 33)) (list-equal? (list-mappend typed-list (typed-list 10 20 30) (typed-list 1 2 3 4 5)) (typed-list 10 1 20 2 30 3)) (list-equal? (list-fold-right list-cons (list-null) (typed-list 0 1 2 3 4)) (typed-list 0 1 2 3 4)) (list-equal? (list-fold-right list-cons (typed-list 0 1 2) (typed-list 3 4)) (typed-list 3 4 0 1 2)) (= (list-fold-right * 1 (typed-list 1 2 3 4 5)) 120) (= (list-fold-left * 1 (typed-list 1 2 3 4 5)) 120) (= (list-fold-left + 0 (typed-list 1 2 3) (typed-list 10 20 30)) 66) (equal? (list-fold-left cons '(100) (typed-list 1 2 3 4)) '(((((100) . 1) . 2) . 3) . 4)) (equal? (call-with-values (lambda () (list-reverse (typed-list 1 2 3) (typed-list 10 20 30))) list) (list (typed-list 3 2 1) (typed-list 30 20 10))) (list-equal? (list-remove 0 (typed-list 1 0 2 0 3 0 4)) (typed-list 1 2 3 4)) (list-equal? (list-merge < (typed-list 2 4 5 7 8) (typed-list 1 3 6 9 10)) (typed-list 1 2 3 4 5 6 7 8 9 10)) (not (condition-case (list-merge < (list-null) (typed-list 1 3 2)) ((exn) #f))) (list-equal? (list-sort <= (typed-list 2 0 1 4 3)) (typed-list 0 1 2 3 4)) (not (list-sorted? <= (typed-list 2 0 1 4 3))) (list-sorted? <= (typed-list 0 1 2 3 4)) (list-every? odd? (typed-list 1 3 5)) (list-every? odd? (typed-list)) (= (list-some odd? (typed-list 2 3 5)) 3) (not (list-some odd? (typed-list 2 4 6))) (list-not-every? odd? (typed-list 1 2 3)) (list-not-any? odd? (typed-list 2 4 6)) (list-in? (typed-list 2 3) (typed-list 1 2 3)) (not (list-in? (typed-list 1 2 3) (typed-list 2 3))) (not (list-in? (typed-list 1 2 3) (typed-list 2 1 3))) (list-in? (typed-list) (typed-list 2 3)) )) (define-test (any-lists?) (check ;; argument module (module any (type? equ?) (import scheme) (define (type? x) #t) (define (equ? x y) (equal? x y)) ) ;; apply functor (module any-lists = (typed-lists any)) ;; import (import (prefix any-lists a) cells) (define als (alist-repeat 3 (cell #f))) (atyped-list? als) (not (list? als)) (= (alist-length als) 3) (equal? (atyped-list->untyped-list (alist-map cell-ref als)) (make-list 3)) (define alst (atyped-list (lambda (x) #f) 'a "x" (cell 3) #\z)) (procedure? (alist-first alst)) (alist-equal? (alist-memp cell? alst) (atyped-list (cell 3) #\z)) (alist-equal? (alist-member #\z alst) (alist-cons #\z (alist-null))) )) (define-test (sets?) (check ; ;; argument module ; (module any (type? equ?) ; (import scheme) ; (define (type? x) #t) ; (define (equ? x y) (equal? x y)) ; ) ; ;; apply functor ; (module any-lists = (typed-lists any)) ; ;; import ; (import any-lists) (aset= (atyped-list->set (atyped-list 1 2 1 3 2 3)) (aset 3 2 1)) (aset? (aset 1 2 3)) (aset? (aset 1 2 2 3)) (aset= (aset 2 1 3) (aset 1 2 2 3)) (aset-in? 2 (aset 1 1 2 3)) (aset<= (aset 2 1 2) (aset 4 1 2 3 4)) (aset= (aset-add 0 (aset 1 2 3)) (aset 0 1 2 3)) (aset= (aset-add 2 (aset 1 2 3)) (aset 1 2 3)) (= (aset-cardinality (aset 2 1 2 3 2)) 3) (aset= (aset-remove 2 (aset 2 1 2 3 2)) (aset 1 3)) (aset= (aset 0 1 1 0 2 3 2) (aset 2 3 0 1)) (aset= (aset-difference (aset 0 2 1 3) (aset 1 1)) (aset 0 2 3)) (aset= (aset-union (aset 1 2) (aset 2 3) (aset 3 4)) (aset 1 2 3 4)) (aset= (aset-intersection (aset 1 2 3 4) (aset 2 3 5) (aset 3 4)) (aset 3)) (aset= (aset-filter odd? (aset 2 1 3 3 1 1)) (aset 3 1)) )) (define-test (string-lists?) (check (module strings (equ? type?) (import scheme) (define equ? string=?) (define type? string?)) (module string-lists = (typed-lists strings)) (import (prefix string-lists str-)) (str-list-equal? (str-list-append (str-typed-list "a" "b") (str-typed-list "c")) (str-typed-list "a" "b" "c")) )) (define-test (symbol-lists?) (check (module symbols (equ? type?) (import scheme) (define equ? eq?) (define type? symbol?)) (module symbol-lists = (typed-lists symbols)) (import (prefix symbol-lists sym-)) (sym-list-equal? (sym-list-append (sym-typed-list 'a 'b) (sym-typed-list 'c)) (sym-typed-list 'a 'b 'c)) (equal? (sym-list-bind (x y z) (sym-typed-list 'a 'b 'c) (list x y z)) '(a b c)) (sym-list-equal? (sym-list-bind (x . y) (sym-typed-list 'a 'b 'c) y) (sym-typed-list 'b 'c)) (xpr:val (sym-list-bind (x . y) (sym-typed-list 'a 'b) (list x y))) (sym-list-null? (sym-list-bind x (sym-list-null) x)) (sym-list-bind () (sym-list-null) #t) )) (define-test (list-lists?) (check (module lists (equ? type?) (import scheme (only data-structures list-of?) (only chicken condition-case)) (define equ? equal?) (define type? (list-of? symbol?)));list?)) (module list-lists = (typed-lists lists)) (import (prefix list-lists l)) (not (condition-case (llist-cons '(1) (llist-null)) ((exn) #f))) (llist-equal? (llist-append (ltyped-list '(a) '(b)) (ltyped-list '(c))) (ltyped-list '(a) '(b) '(c))) )) (define-test (pair-lists?) (check (module pairs (type? equ?) (import scheme) (define (type? x) (and (pair? x) (number? (car x)) (string? (cdr x)))) (define equ? equal?)) (module pair-lists = (typed-lists pairs)) (import (prefix pair-lists nsp-)) (define nspl (nsp-typed-list (cons 1 "one") (cons 2 "two") (cons 3 "three"))) (equal? (nsp-list-assoc 2 nspl) '(2 . "two")) (not (nsp-list-assp zero? nspl)) )) (compound-test (TYPED-LISTS) (number-lists?) (any-lists?) (sets?) (string-lists?) (symbol-lists?) (list-lists?) (pair-lists?) )