aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-28 22:59:46 -0500
committerMustafa Quraish <[email protected]>2022-01-28 22:59:46 -0500
commitf66593a4c4f08f7a37d525421455166d51987999 (patch)
tree1aee56630eaec1fcbb000e861a003d96f23eb032
parentAdd some basic args parsing so we can test stuff from the CLI (diff)
downloadcup-f66593a4c4f08f7a37d525421455166d51987999.tar.xz
cup-f66593a4c4f08f7a37d525421455166d51987999.zip
Scripts: Reorganize a bit, add some rudimentary shell-testing
Just to make sure we don't break stuff. We'll probably want better unit tests specifically for each part once we're more stable.
-rwxr-xr-xrun.sh18
-rwxr-xr-xtest.sh21
-rw-r--r--tests/basics.sh39
-rw-r--r--tests/common.sh27
4 files changed, 89 insertions, 16 deletions
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..6038d91
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+if [ -z "$1" ]
+then
+ echo "Usage: $0 <path to .cup file>"
+ exit 1
+fi
+
+set -xe
+
+./compile.sh
+./cupcc "$@"
+./assemble.sh output.nasm
+
+set +e
+
+./a.out
+echo "Exit status: $?" \ No newline at end of file
diff --git a/test.sh b/test.sh
index 14f0145..ebaa622 100755
--- a/test.sh
+++ b/test.sh
@@ -1,18 +1,7 @@
#!/bin/bash
-if [ -z "$1" ]
-then
- echo "Usage: $0 <path to .cup file>"
- exit 1
-fi
-
-set -xe
-
-./compile.sh
-./cupcc $@
-./assemble.sh output.nasm
-
-set +e
-
-./a.out
-echo "Exit status: $?" \ No newline at end of file
+for i in `ls tests/*.sh | grep -v common.sh`
+do
+ echo "Running $i"
+ bash $i
+done \ No newline at end of file
diff --git a/tests/basics.sh b/tests/basics.sh
new file mode 100644
index 0000000..9ef3bf4
--- /dev/null
+++ b/tests/basics.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+. tests/common.sh
+
+set -e
+
+echo -n "- Testing basic return: "
+assert_exit_status 'fn main() { return 0; }' 0
+assert_exit_status 'fn main() { return 1; }' 1
+assert_exit_status 'fn main() { return 100; }' 100
+echo " OK"
+
+echo -n "- Testing unary ops: "
+assert_exit_status 'fn main() { return -1; }' 255
+assert_exit_status 'fn main() { return -100; }' 156
+assert_exit_status 'fn main() { return !0; }' 1
+assert_exit_status 'fn main() { return !1; }' 0
+assert_exit_status 'fn main() { return !34; }' 0
+assert_exit_status 'fn main() { return !-1; }' 0
+assert_exit_status 'fn main() { return ~34; }' 221
+echo " OK"
+
+echo -n "- Testing Binary ops: "
+assert_exit_status 'fn main() { return 1 + 1; }' 2
+assert_exit_status 'fn main() { return 1 + 100; }' 101
+assert_exit_status 'fn main() { return 100 + 1; }' 101
+assert_exit_status 'fn main() { return 1 - 1; }' 0
+assert_exit_status 'fn main() { return 1 - 100; }' 157
+assert_exit_status 'fn main() { return 100 - 1; }' 99
+assert_exit_status 'fn main() { return 1 * 1; }' 1
+assert_exit_status 'fn main() { return 1 * 100; }' 100
+assert_exit_status 'fn main() { return 100 * 1; }' 100
+assert_exit_status 'fn main() { return 7 * 3; }' 21
+assert_exit_status 'fn main() { return 1 / 1; }' 1
+assert_exit_status 'fn main() { return 100 / 1; }' 100
+assert_exit_status 'fn main() { return 100 / 7; }' 14
+assert_exit_status 'fn main() { return 100 / 100; }' 1
+assert_exit_status 'fn main() { return 100 / -1; }' 156
+echo " OK" \ No newline at end of file
diff --git a/tests/common.sh b/tests/common.sh
new file mode 100644
index 0000000..4469874
--- /dev/null
+++ b/tests/common.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+function assemble() {
+ # Note: macOS only for now, abstract this out later
+ nasm -f macho64 -o output.o output.nasm
+ ld -lSystem output.o
+}
+
+function assert_exit_status() {
+ ./cupcc -c "$1"
+ assemble
+
+ set +e
+ ./a.out
+ res=$?
+ if [ $res -ne $2 ]
+ then
+ echo ""
+ echo "----------------------------------"
+ echo "Test failed: expected $2, got $res"
+ echo "- Input was:"
+ echo " \`$1\`"
+ exit 1
+ fi
+ set -e
+ echo -n "."
+} \ No newline at end of file