aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-31 03:10:36 -0500
committerMustafa Quraish <[email protected]>2022-01-31 03:21:09 -0500
commit23666c777b98402ff539816e291a979d1b62a724 (patch)
tree391aef0d42e18824f8f93773be64ee95d9f30d4f /tests
parentAdd `std/math.cup` with some common math functions (diff)
downloadcup-23666c777b98402ff539816e291a979d1b62a724.tar.xz
cup-23666c777b98402ff539816e291a979d1b62a724.zip
Move around tests in the categories; Add missing tests
We now also test for local variables in functions, and add a simple test to see if imports work properly.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/basics.sh64
-rwxr-xr-xtests/compound.sh161
-rwxr-xr-xtests/functions.sh5
-rwxr-xr-xtests/variables.sh146
4 files changed, 197 insertions, 179 deletions
diff --git a/tests/basics.sh b/tests/basics.sh
index cab2462..fc43847 100755
--- a/tests/basics.sh
+++ b/tests/basics.sh
@@ -4,13 +4,13 @@
set -e
-echo -n "- Testing basic return: "
+echo -n "- 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: "
+echo -n "- 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
@@ -20,7 +20,7 @@ assert_exit_status 'fn main() { return !-1; }' 0
assert_exit_status 'fn main() { return ~34; }' 221
echo " OK"
-echo -n "- Testing Arith Binary ops: "
+echo -n "- Arith 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
@@ -38,7 +38,7 @@ assert_exit_status 'fn main() { return 100 / 100; }' 1
assert_exit_status 'fn main() { return 100 / -1; }' 156
echo " OK"
-echo -n "- Testing Relational ops: "
+echo -n "- Relational ops: "
assert_exit_status 'fn main() { return 1 == 1; }' 1
assert_exit_status 'fn main() { return 1 == 2; }' 0
assert_exit_status 'fn main() { return 1 != 1; }' 0
@@ -59,7 +59,7 @@ assert_exit_status 'fn main() { return 2 >= 2; }' 1
assert_exit_status 'fn main() { return 3 >= 2; }' 1
echo " OK"
-echo -n "- Testing simple logical ops: "
+echo -n "- Simple logical ops: "
assert_exit_status 'fn main() { return 0 && 0; }' 0
assert_exit_status 'fn main() { return 0 && 5; }' 0
assert_exit_status 'fn main() { return 5 && 0; }' 0
@@ -69,4 +69,58 @@ assert_exit_status 'fn main() { return 0 || 0; }' 0
assert_exit_status 'fn main() { return 5 || 0; }' 1
assert_exit_status 'fn main() { return 0 || 3; }' 1
assert_exit_status 'fn main() { return 2 || 1; }' 1
+echo " OK"
+
+echo -n "- Short-circuiting: "
+assert_exit_status_stdin 5 <<EOF
+fn main() {
+ let x: int = 5;
+ let y: int = (1 || (x = 10));
+ return x;
+}
+EOF
+
+assert_exit_status_stdin 10 <<EOF
+fn main() {
+ let x: int = 5;
+ let y: int = (0 || (x = 10));
+ return x;
+}
+EOF
+
+assert_exit_status_stdin 5 <<EOF
+fn main() {
+ let x: int = 5;
+ let y: int = (0 && (x = 10));
+ return x;
+}
+EOF
+
+assert_exit_status_stdin 10 <<EOF
+fn main() {
+ let x: int = 5;
+ let y: int = (1 && (x = 10));
+ return x;
+}
+EOF
+echo " OK"
+
+echo -n "- Importing file: "
+assert_exit_status_stdin 10 <<EOF
+import "std/math.cup"
+
+fn main() {
+ let x: int = abs(-5);
+ let y: int = factorial(3);
+ return x + y - 1;
+}
+EOF
+
+assert_compile_failure_stdin <<EOF
+fn main() {
+ let x: int = abs(-5);
+ let y: int = factorial(3);
+ return x + y - 1;
+}
+EOF
echo " OK" \ No newline at end of file
diff --git a/tests/compound.sh b/tests/compound.sh
deleted file mode 100755
index cb49fa0..0000000
--- a/tests/compound.sh
+++ /dev/null
@@ -1,161 +0,0 @@
-#!/bin/bash
-
-# Test compound scopes
-
-. tests/common.sh
-
-set -e
-
-echo -n "- Nested Blocks: "
-assert_exit_status_stdin 3 <<EOF
-fn main() {
- let x: int = 1;
- {
- let y: int = 3;
- x = y;
- }
- return x;
-}
-EOF
-
-assert_exit_status_stdin 15 <<EOF
-fn main(): int {
- let x: int;
- {
- let y: int = 10;
- {
- let z: int = 5;
- x = y + z;
- }
- }
- return x;
-}
-EOF
-
-assert_exit_status_stdin 8 <<EOF
-fn main(): int {
- let x: int = 0;
- {
- let y: int = 3;
- x = x + y;
- }
- {
- let y: int = 5;
- x = x + y;
- }
- return x;
-}
-EOF
-
-assert_compile_failure_stdin <<EOF
-fn main(): int {
- let x: int;
- {
- let y: int = 10;
- {
- let z: int = 5;
- x = y + z;
- }
- y = z;
- }
- return x;
-}
-EOF
-
-assert_compile_failure_stdin <<EOF
-fn main(): int {
- let x: int;
- {
- let y: int = 10;
- {
- let z: int = 5;
- x = y + z;
- }
- }
- x = z;
- return x;
-}
-EOF
-
-assert_compile_failure_stdin <<EOF
-fn main(): int {
- let x: int;
- {
- let y: int = 10;
- {
- let z: int = 5;
- x = y + z;
- }
- }
- x = y;
- return x;
-}
-EOF
-
-# We do not allow shadowing variables on purpose
-assert_compile_failure_stdin <<EOF
-fn main(): int {
- let x: int;
- {
- let y: int = 10;
- {
- let z: int = 5;
- x = y + z;
- let x: int = 10;
- }
- }
- return x;
-}
-EOF
-echo " OK"
-
-echo -n "- Conditionals w/ blocks: "
-assert_exit_status_stdin 3 <<EOF
-fn main() {
- let x: int = 1;
- if (x == 1) {
- let y: int = 3;
- x = y;
- }
- return x;
-}
-EOF
-
-assert_exit_status_stdin 1 <<EOF
-fn main() {
- let x: int = 1;
- if (x != 1) {
- let y: int = 3;
- x = y;
- }
- return x;
-}
-EOF
-
-assert_exit_status_stdin 5 <<EOF
-fn main() {
- let x: int = 1;
- if (x != 1) {
- let y: int = 3;
- x = y;
- } else {
- let y: int = 5;
- x = y;
- }
- return x;
-}
-EOF
-
-assert_compile_failure_stdin <<EOF
-fn main() {
- let x: int = 1;
- if (x != 1) {
- let y: int = 3;
- x = y;
- }
- x = y; // Invalid
- return x;
-}
-EOF
-
-echo " OK" \ No newline at end of file
diff --git a/tests/functions.sh b/tests/functions.sh
index 3e87b37..e8c79f2 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -23,6 +23,11 @@ fn main() { return test(2, 3); }
EOF
assert_exit_status_stdin 5 <<EOF
+fn test(a: int, b: int) { let n: int = a + b; return n; }
+fn main() { return test(2, 3); }
+EOF
+
+assert_exit_status_stdin 5 <<EOF
fn test(a: int, b: int, c: int, d: int, e: int) { return a+b+c+d+e; }
fn main() { return test(1,1,1,1,1); }
EOF
diff --git a/tests/variables.sh b/tests/variables.sh
index 7b44fe7..2d08c76 100755
--- a/tests/variables.sh
+++ b/tests/variables.sh
@@ -68,36 +68,156 @@ fn main() {
EOF
echo " OK"
-echo -n "- Short-circuiting assignments: "
-assert_exit_status_stdin 5 <<EOF
+echo -n "- Nested Blocks: "
+assert_exit_status_stdin 3 <<EOF
fn main() {
- let x: int = 5;
- let y: int = (1 || (x = 10));
+ let x: int = 1;
+ {
+ let y: int = 3;
+ x = y;
+ }
return x;
}
EOF
-assert_exit_status_stdin 10 <<EOF
+assert_exit_status_stdin 15 <<EOF
+fn main(): int {
+ let x: int;
+ {
+ let y: int = 10;
+ {
+ let z: int = 5;
+ x = y + z;
+ }
+ }
+ return x;
+}
+EOF
+
+assert_exit_status_stdin 8 <<EOF
+fn main(): int {
+ let x: int = 0;
+ {
+ let y: int = 3;
+ x = x + y;
+ }
+ {
+ let y: int = 5;
+ x = x + y;
+ }
+ return x;
+}
+EOF
+
+assert_compile_failure_stdin <<EOF
+fn main(): int {
+ let x: int;
+ {
+ let y: int = 10;
+ {
+ let z: int = 5;
+ x = y + z;
+ }
+ y = z;
+ }
+ return x;
+}
+EOF
+
+assert_compile_failure_stdin <<EOF
+fn main(): int {
+ let x: int;
+ {
+ let y: int = 10;
+ {
+ let z: int = 5;
+ x = y + z;
+ }
+ }
+ x = z;
+ return x;
+}
+EOF
+
+assert_compile_failure_stdin <<EOF
+fn main(): int {
+ let x: int;
+ {
+ let y: int = 10;
+ {
+ let z: int = 5;
+ x = y + z;
+ }
+ }
+ x = y;
+ return x;
+}
+EOF
+
+# We do not allow shadowing variables on purpose
+assert_compile_failure_stdin <<EOF
+fn main(): int {
+ let x: int;
+ {
+ let y: int = 10;
+ {
+ let z: int = 5;
+ x = y + z;
+ let x: int = 10;
+ }
+ }
+ return x;
+}
+EOF
+echo " OK"
+
+echo -n "- Conditionals w/ blocks: "
+assert_exit_status_stdin 3 <<EOF
fn main() {
- let x: int = 5;
- let y: int = (0 || (x = 10));
+ let x: int = 1;
+ if (x == 1) {
+ let y: int = 3;
+ x = y;
+ }
+ return x;
+}
+EOF
+
+assert_exit_status_stdin 1 <<EOF
+fn main() {
+ let x: int = 1;
+ if (x != 1) {
+ let y: int = 3;
+ x = y;
+ }
return x;
}
EOF
assert_exit_status_stdin 5 <<EOF
fn main() {
- let x: int = 5;
- let y: int = (0 && (x = 10));
+ let x: int = 1;
+ if (x != 1) {
+ let y: int = 3;
+ x = y;
+ } else {
+ let y: int = 5;
+ x = y;
+ }
return x;
}
EOF
-assert_exit_status_stdin 10 <<EOF
+assert_compile_failure_stdin <<EOF
fn main() {
- let x: int = 5;
- let y: int = (1 && (x = 10));
+ let x: int = 1;
+ if (x != 1) {
+ let y: int = 3;
+ x = y;
+ }
+ x = y; // Invalid
return x;
}
EOF
-echo " OK"
+
+echo " OK" \ No newline at end of file