diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-01 04:08:59 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-01 04:08:59 -0500 |
| commit | d471a41369690a9d2d9b8862ea5ff0ae9cbe40fc (patch) | |
| tree | 9399d12700c3d975a95b0946833dc419b3268419 /tests/core.sh | |
| parent | Testing: Add support for testing stdout results, check builtins (diff) | |
| download | cup-d471a41369690a9d2d9b8862ea5ff0ae9cbe40fc.tar.xz cup-d471a41369690a9d2d9b8862ea5ff0ae9cbe40fc.zip | |
Add basic `defer` implementation.
We don't have any closures yet, so it's essentially the same as just
moving the statement after the `defer` keyword to the end of the block/
right before returning from the function.
Diffstat (limited to 'tests/core.sh')
| -rwxr-xr-x | tests/core.sh | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/tests/core.sh b/tests/core.sh new file mode 100755 index 0000000..71c539d --- /dev/null +++ b/tests/core.sh @@ -0,0 +1,189 @@ +#!/bin/bash + +. tests/common.sh + +set -e + +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 "- 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 "- 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 +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" + +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 +assert_exit_status 'fn main() { return 1 != 2; }' 1 + +assert_exit_status 'fn main() { return 1 < 2; }' 1 +assert_exit_status 'fn main() { return 2 < 2; }' 0 + +assert_exit_status 'fn main() { return 1 <= 2; }' 1 +assert_exit_status 'fn main() { return 2 <= 2; }' 1 +assert_exit_status 'fn main() { return 3 <= 2; }' 0 + +assert_exit_status 'fn main() { return 2 > 2; }' 0 +assert_exit_status 'fn main() { return 3 > 2; }' 1 + +assert_exit_status 'fn main() { return 1 >= 2; }' 0 +assert_exit_status 'fn main() { return 2 >= 2; }' 1 +assert_exit_status 'fn main() { return 3 >= 2; }' 1 +echo " OK" + +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 +assert_exit_status 'fn main() { return 5 && 1; }' 1 + +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" + +echo -n "- Defer: " +assert_stdout_text \ +"fn main() { + defer print(5); + print(4); +}" \ +"4 +5" + +assert_stdout_text \ +"fn main() { + defer print(1); + { + defer print(2); + { + defer print(3); + print(4); + } + print(5); + return 0; + } + print(10); +}" \ +"4 +3 +5 +2 +1" + +assert_stdout_text \ +"fn test() { + defer { + defer print(1); + print(2); + } + print(3); +} +fn main() { + defer print(4); + defer test(); + print(10); +}" \ +"10 +3 +2 +1 +4" + +assert_stdout_text \ +"let g: int; +fn test(): int { + g = 5; + defer g = 10; + return g; +} +fn main() { + print(test()); + print(g); +}" \ +"5 +10" +echo " OK"
\ No newline at end of file |