;; lib/apl/tests/runtime.sx — Tests for lib/apl/runtime.sx ;; --- Test framework --- (define apl-test-pass 0) (define apl-test-fail 0) (define apl-test-fails (list)) (define (apl-test name got expected) (if (= got expected) (set! apl-test-pass (+ apl-test-pass 1)) (begin (set! apl-test-fail (+ apl-test-fail 1)) (set! apl-test-fails (append apl-test-fails (list {:got got :expected expected :name name})))))) ;; --------------------------------------------------------------------------- ;; 1. Core vector constructors ;; --------------------------------------------------------------------------- (apl-test "iota 5" (apl-iota 5) (list 1 2 3 4 5)) (apl-test "iota 1" (apl-iota 1) (list 1)) (apl-test "iota 0" (apl-iota 0) (list)) (apl-test "rho list" (apl-rho (list 1 2 3)) 3) (apl-test "rho scalar" (apl-rho 42) 1) (apl-test "at 1" (apl-at (list 10 20 30) 1) 10) (apl-test "at 3" (apl-at (list 10 20 30) 3) 30) ;; --------------------------------------------------------------------------- ;; 2. Arithmetic — element-wise and rank-polymorphic ;; --------------------------------------------------------------------------- (apl-test "add v+v" (apl-add (list 1 2 3) (list 10 20 30)) (list 11 22 33)) (apl-test "add s+v" (apl-add 10 (list 1 2 3)) (list 11 12 13)) (apl-test "add v+s" (apl-add (list 1 2 3) 100) (list 101 102 103)) (apl-test "add s+s" (apl-add 3 4) 7) (apl-test "sub v-v" (apl-sub (list 5 4 3) (list 1 2 3)) (list 4 2 0)) (apl-test "mul v*s" (apl-mul (list 1 2 3) 3) (list 3 6 9)) (apl-test "neg -v" (apl-neg (list 1 -2 3)) (list -1 2 -3)) (apl-test "abs v" (apl-abs (list -1 2 -3)) (list 1 2 3)) (apl-test "floor v" (apl-floor (list 1.7 2.2 3.9)) (list 1 2 3)) (apl-test "ceil v" (apl-ceil (list 1.1 2.5 3)) (list 2 3 3)) (apl-test "max v v" (apl-max (list 1 5 3) (list 4 2 6)) (list 4 5 6)) (apl-test "min v v" (apl-min (list 1 5 3) (list 4 2 6)) (list 1 2 3)) ;; --------------------------------------------------------------------------- ;; 3. Comparison (returns 0/1) ;; --------------------------------------------------------------------------- (apl-test "eq 3 3" (apl-eq 3 3) 1) (apl-test "eq 3 4" (apl-eq 3 4) 0) (apl-test "gt v>s" (apl-gt (list 1 5 3 7) 4) (list 0 1 0 1)) (apl-test "lt v=s" (apl-ge (list 3 4 5) 4) (list 0 1 1)) (apl-test "neq v!=s" (apl-neq (list 1 2 3) 2) (list 1 0 1)) ;; --------------------------------------------------------------------------- ;; 4. Boolean logic (0/1 values) ;; --------------------------------------------------------------------------- (apl-test "and 1 1" (apl-and 1 1) 1) (apl-test "and 1 0" (apl-and 1 0) 0) (apl-test "or 0 1" (apl-or 0 1) 1) (apl-test "or 0 0" (apl-or 0 0) 0) (apl-test "not 0" (apl-not 0) 1) (apl-test "not 1" (apl-not 1) 0) (apl-test "not vec" (apl-not (list 1 0 1 0)) (list 0 1 0 1)) ;; --------------------------------------------------------------------------- ;; 5. Bitwise operations ;; --------------------------------------------------------------------------- (apl-test "bitand s" (apl-bitand 5 3) 1) (apl-test "bitor s" (apl-bitor 5 3) 7) (apl-test "bitxor s" (apl-bitxor 5 3) 6) (apl-test "bitnot 0" (apl-bitnot 0) -1) (apl-test "lshift 1 4" (apl-lshift 1 4) 16) (apl-test "rshift 16 2" (apl-rshift 16 2) 4) (apl-test "bitand vec" (apl-bitand (list 5 6) (list 3 7)) (list 1 6)) (apl-test "bitor vec" (apl-bitor (list 5 6) (list 3 7)) (list 7 7)) ;; --------------------------------------------------------------------------- ;; 6. Reduction and scan ;; --------------------------------------------------------------------------- (apl-test "reduce-add" (apl-reduce-add (list 1 2 3 4 5)) 15) (apl-test "reduce-mul" (apl-reduce-mul (list 1 2 3 4)) 24) (apl-test "reduce-max" (apl-reduce-max (list 3 1 4 1 5)) 5) (apl-test "reduce-min" (apl-reduce-min (list 3 1 4 1 5)) 1) (apl-test "reduce-and" (apl-reduce-and (list 1 1 1)) 1) (apl-test "reduce-and0" (apl-reduce-and (list 1 0 1)) 0) (apl-test "reduce-or" (apl-reduce-or (list 0 1 0)) 1) (apl-test "scan-add" (apl-scan-add (list 1 2 3 4)) (list 1 3 6 10)) (apl-test "scan-mul" (apl-scan-mul (list 1 2 3 4)) (list 1 2 6 24)) ;; --------------------------------------------------------------------------- ;; 7. Vector manipulation ;; --------------------------------------------------------------------------- (apl-test "reverse" (apl-reverse (list 1 2 3 4)) (list 4 3 2 1)) (apl-test "cat v v" (apl-cat (list 1 2) (list 3 4)) (list 1 2 3 4)) (apl-test "cat v s" (apl-cat (list 1 2) 3) (list 1 2 3)) (apl-test "cat s v" (apl-cat 1 (list 2 3)) (list 1 2 3)) (apl-test "cat s s" (apl-cat 1 2) (list 1 2)) (apl-test "take 3" (apl-take 3 (list 10 20 30 40 50)) (list 10 20 30)) (apl-test "take 0" (apl-take 0 (list 1 2 3)) (list)) (apl-test "take neg" (apl-take -2 (list 10 20 30)) (list 20 30)) (apl-test "drop 2" (apl-drop 2 (list 10 20 30 40)) (list 30 40)) (apl-test "drop neg" (apl-drop -1 (list 10 20 30)) (list 10 20)) (apl-test "rotate 2" (apl-rotate 2 (list 1 2 3 4 5)) (list 3 4 5 1 2)) (apl-test "compress" (apl-compress (list 1 0 1 0) (list 10 20 30 40)) (list 10 30)) (apl-test "index" (apl-index (list 10 20 30 40) (list 2 4)) (list 20 40)) ;; --------------------------------------------------------------------------- ;; 8. Set operations ;; --------------------------------------------------------------------------- (apl-test "member yes" (apl-member (list 1 2 5) (list 2 4 6)) (list 0 1 0)) (apl-test "member s" (apl-member 2 (list 1 2 3)) 1) (apl-test "member no" (apl-member 9 (list 1 2 3)) 0) (apl-test "nub" (apl-nub (list 1 2 1 3 2)) (list 1 2 3)) (apl-test "union" (apl-union (list 1 2 3) (list 2 3 4)) (list 1 2 3 4)) (apl-test "intersect" (apl-intersect (list 1 2 3 4) (list 2 4 6)) (list 2 4)) (apl-test "without" (apl-without (list 1 2 3 4) (list 2 4)) (list 1 3)) ;; --------------------------------------------------------------------------- ;; 9. Format ;; --------------------------------------------------------------------------- (apl-test "format vec" (apl-format (list 1 2 3)) "1 2 3") (apl-test "format scalar" (apl-format 42) "42") (apl-test "format empty" (apl-format (list)) "") ;; --------------------------------------------------------------------------- ;; Summary ;; --------------------------------------------------------------------------- (list apl-test-pass apl-test-fail)