(import (chicken condition) (chicken format) (chicken sort) brev-separate pyffi test) (define-ir-syntax* ((test-signals-condition test-name kind body ...) `(test-assert ,test-name (handle-exceptions exn ((condition-predicate ,kind) exn) (begin ,@body #f))))) (py-start) (py-import "sys") (define-pyfun ("lambda x: (x.real, x.imag)" complex->tuple) x) (define-pyfun ("lambda x: sorted(list(x.items()))" dict-items) x) (define-pyfun "repr" obj) (define-pyfun ("lambda r, obj: eval(r) == obj and type(eval(r)) == type(obj)" repr-compare) r obj) (test-begin "basic tests") (test-assert "major version is either 3 or 2" (member (py-eval "sys.version_info[0]") '(3 2))) (test-assert "sys.version is a string" (string? (py-eval "sys.version"))) (print "Python version: " (py-eval "sys.version")) (test-end "basic tests") (test-begin "python->scheme type conversion") (test "python integer converted to scheme" 12345 (py-eval "12345")) ;; Pick a number that has an exact binary floating point representation. (test "python float converts to scheme" 0.25 (py-eval "0.25")) (test "python inf converts to scheme" +inf.0 (py-eval "float('inf')")) (test "python -inf converts to scheme" -inf.0 (py-eval "float('-inf')")) (test-assert "python nan converts to scheme" (nan? (py-eval "float('nan')"))) (test "python string converts to scheme" "hello world" (py-eval "'hello ' + 'world'")) (test "python complex converts to scheme" 1.0+2.0i (py-eval "complex(1.0, 2.0)")) (test "python tuple converts to scheme vector" #(10 20 30) (py-eval "(10, 20, 30)")) (test "python list converts to scheme" '(10 20 30) (py-eval "[10, 20, 30]")) (test "python dict converts to scheme alist" '(("a" 1) ("b" 2) ("c" 3)) (sort (py-eval "{'a': 1, 'b': 2, 'c': 3}") (lambda (x y) (stringscheme type conversion") (test-begin "scheme->python type conversion") (test-assert "scheme integer converted to python" (repr-compare "12345" 12345)) ;; Pick a number that has an exact binary floating point representation. (test-assert "Scheme float converts to python" (repr-compare "0.25" 0.25)) (test-assert "scheme inf converts to python" (repr-compare "float('inf')" +inf.0)) (test-assert "scheme inf converts to python" (repr-compare "float('-inf')" -inf.0)) (test "scheme nan converts to python" "nan" (repr +nan.0)) (test-assert "scheme string converts to python" (repr-compare "'hello'" "hello")) (test-assert "scheme complex converts to python" (repr-compare "2.0+4.0j" 2.0+4.0i)) (test-assert "scheme vector converts to python tuple" (repr-compare "(10, 20, 30)" #(10 20 30))) (test-assert "scheme list converts to python" (repr-compare "[10, 20, 30]" '(10 20 30))) (test-assert "scheme alist converts to python dict" (repr-compare "{'a': 1, 'b': 2, 'c': 3}" '(("a" 1) ("b" 2) ("c" 3)))) (test-end "scheme->python type conversion") (py-eval #< (test-failure-count) 1) "tests" "test")) (print "=====")) (test-exit)