mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-18 21:29:19 +02:00
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.
37 lines
1.1 KiB
Scheme
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))))
|