#!/bin/sh set -e failcnt=0 wasm_test(){ fname=$1 # wasm output cmp rm ${fname}.wat ${fname}.wasm || true make ${fname}.wasm if cmp ${fname}.wasm good/${fname}.wasm; then echo "GOOD ${fname}.wasm no regression" else echo "FAIL ${fname}.wasm regression" failcnt=`expr ${failcnt} + 1` fi } sugar_test(){ sed "s|TESTCASE|$1|g" good/testing.sugar.template >good/testee.sugar make good/testee.wasm output=`wasm-interp --host-print --run-all-exports testee.wasm` if [ "$output" = "$2" ]; then echo "GOOD - $1 passed" else echo "FAIL - $1 FAILED '$output' /= '$2'" failcnt=`expr ${failcnt} + 1` fi } dec(){ printf "%d\n" $1 } ## TODO test which captures sugar syntax errors # wasm comparison tests wasm_test zip-sugarV2 # code result tests ## MACROS sugar_test '(when (/= a b) (print -7) (print 124)) 0' 'called host host.print(i32:4294967289) => called host host.print(i32:124) => t1() => i32:0' /= 't1() => i32:0' sugar_test '(when (= a b) (print -7) (print 124)) 0' 't1() => i32:0' sugar_test '(unless (= a b) (print -7) (print 124)) 0' 'called host host.print(i32:4294967289) => called host host.print(i32:124) => t1() => i32:0' /= 't1() => i32:0' ## MATH sugar_test '(+ 3 1)' "t1() => i32:4" sugar_test '(+ 3 -15)' "t1() => i32:`dec 0xfffffff4`" sugar_test '(+ #xffffffff 1)' "t1() => i32:0" sugar_test '(^ #xcafebabe (aref some-array 0))' "t1() => i32:0" sugar_test '(^ #xdeadbeef (aref some-array 0))' "t1() => i32:340984913" sugar_test '(\& #xff (aref some-array 0))' "t1() => i32:`dec 0xbe`" sugar_test '(\& #xff00 (aref some-array 0))' "t1() => i32:`dec 0xba00`" sugar_test '(\& #xff0000 (aref some-array 0))' "t1() => i32:`dec 0xfe0000`" sugar_test '(\& #xff000000 (aref some-array 0))' "t1() => i32:`dec 0xca000000`" sugar_test '(bor #xff (aref some-array 0))' "t1() => i32:`dec 0xcafebaff`" sugar_test '(bor #xff00 (aref some-array 0))' "t1() => i32:`dec 0xcafeffbe`" sugar_test '(bor #xff0000 (aref some-array 0))' "t1() => i32:`dec 0xcaffbabe`" sugar_test '(bor #xff000000 (aref some-array 0))' "t1() => i32:`dec 0xfffebabe`" ## DEFCONSTANT sugar_test '(defconstant magic-one i32 #xcafebabe) magic-one' "t1() => i32:`dec 0xcafebabe`" sugar_test '(defconstant magic-two i64 #xcafebabeacedface) (coerce magic-two i32 i64)' "t1() => i32:`dec 0xacedface`" ## AREF sugar_test '(aset some-array 0 #xdeadbeef) (aref some-array 0)' 't1() => i32:3735928559' sugar_test '(aset some-array 0 #xdeadbeef 4) (aref some-array 0 4)' 't1() => i32:3735928559' sugar_test '(aset some-array 0 #xdead 2) (aref some-array 0 2)' 't1() => i32:57005' sugar_test '(aset some-array 0 #xde 1) (aref some-array 0 1)' 't1() => i32:222' sugar_test '(aset some-array 0 (i64 #xdeadbeefacedface) 8) (coerce (aref some-array 0 8) i32 i64)' 't1() => i32:2901277390' sugar_test '(aref some-array 0)' 't1() => i32:3405691582' sugar_test '(aref some-array 0 4)' 't1() => i32:3405691582' sugar_test '(aref some-array 0 2)' 't1() => i32:47806' sugar_test '(aref some-array 0 1)' 't1() => i32:190' sugar_test '(coerce (aref some-array 0 8) i32 i64)' 't1() => i32:3405691582' # sugar_test '(aref some-array 0 3)' 't1() => i32:3405691582' ## shortcut AND/OR sugar_test '(and a b c)' 't1() => i32:3' sugar_test '(and a b)' 't1() => i32:2' sugar_test '(and a)' 't1() => i32:1' sugar_test '(and)' 't1() => i32:1' sugar_test '(or a b c)' 't1() => i32:1' sugar_test '(or b c)' 't1() => i32:2' sugar_test '(or c)' 't1() => i32:3' sugar_test '(or)' 't1() => i32:0' sugar_test '(and (check-one 15) (do-thing 6 4))' 'called host host.print(i32:6) => called host host.print(i32:4) => t1() => i32:10' sugar_test '(and (check-one 115) (do-thing 6 4))' 't1() => i32:0' sugar_test '(or (check-one 15) (check-other 1) (do-thing 6 4))' 't1() => i32:1' sugar_test '(or (check-one 115) (check-other 0) (do-thing 6 4))' 't1() => i32:1' sugar_test '(or (check-one 115) (check-other 1) (do-thing 6 4))' 'called host host.print(i32:6) => called host host.print(i32:4) => t1() => i32:10' ## LOOPS sugar_test '(while (< a 3) (print a) (inc a)) a' 'called host host.print(i32:1) => called host host.print(i32:2) => t1() => i32:3' sugar_test '(forever (if (= a 3) (break)) (print a) (inc a)) a' 'called host host.print(i32:1) => called host host.print(i32:2) => t1() => i32:3' sugar_test '(forever (inc a) (if (< a 5) (continue)) (if (= a 8) (break)) (print a)) a' 'called host host.print(i32:5) => called host host.print(i32:6) => called host host.print(i32:7) => t1() => i32:8' if [ 0 -eq $failcnt ]; then printf "\nGOOD all tests passed\n" else printf "\nFAIL $failcnt tests failed\n" exit 1 fi