chibi-scheme/lib/chibi/generic-test.sld
Alex Shinn 00691b64f1 Making libraries portable where possible.
Many still import (chibi), and as (scheme base) is somewhat more
expensive to load at present these are changed to cond-expand.
Many libraries also rely on (srfi 33), and these have been changed
to a cond-expand first trying (srfi 60) where available.
Also fixing a few portability concerns (duplicate imports of the
same binding), and adding a few libraries missing from lib-tests.scm.
2015-04-26 16:17:38 +09:00

37 lines
1.1 KiB
Scheme

(define-library (chibi generic-test)
(export run-tests)
(import (chibi) (chibi generic) (chibi test))
(begin
(define (run-tests)
(test-begin "generics")
(let ()
(define-generic add)
(define-method (add (x number?) (y number?))
(+ x y))
(define-method (add (x string?) (y string?))
(string-append x y))
(define-method (add x (y list?))
(append x y))
(test 4 (add 2 2))
(test "22" (add "2" "2"))
(test '(2 2) (add '() '(2 2)))
(test '(2 2) (add '(2) '(2)))
(test '(2 2) (add '(2 2) '()))
(test '(2) (add #f '(2)))
(test-error (add #(2) #(2))))
(let ()
(define-generic mul)
(define-method (mul (x number?) (y number?))
(* x y))
(define-method (mul (x inexact?) (y inexact?))
(+ (* x y) 0.1))
(define-method (mul (x exact?) (y exact?))
(inexact->exact (call-next-method)))
(test 21 (mul 3 7))
(test 21.0 (mul 3.0 7))
(test 21.0 (mul 3 7.0))
(test 21.1 (mul 3.0 7.0)))
(test-end))))