From b013ba0e550a3b22fa2bfc718eb921d26ba62a99 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Wed, 11 Dec 2024 16:26:57 -0500 Subject: [PATCH] Add test code --- test/arith.w12 | 43 ++++++++++++++++++++++++++++++++++++++++++ test/bad.w12 | 1 + test/basic-structs.w12 | 21 +++++++++++++++++++++ test/empty.w12 | 0 test/fib.w12 | 38 +++++++++++++++++++++++++++++++++++++ test/functions.w12 | 32 +++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 test/arith.w12 create mode 100644 test/bad.w12 create mode 100644 test/basic-structs.w12 create mode 100644 test/empty.w12 create mode 100644 test/fib.w12 create mode 100644 test/functions.w12 diff --git a/test/arith.w12 b/test/arith.w12 new file mode 100644 index 0000000..9417035 --- /dev/null +++ b/test/arith.w12 @@ -0,0 +1,43 @@ +fn factorial(n: int) -> int { + var result: int; + + if n == 0 { + result = 1; + } else { + result = n * factorial(n - 1); + } + + return result; +} + +fn is_positive(n: int) -> bool { + return n > 0; +} + +fn main() -> int { + var a: int = 5; + printf("a = %d\n", a); + + var b: int = 3; + printf("b = %d\n", b); + + var c: int = (a + b) * 2 - 1; + c += factorial(5); + + printf("c = %d\n", c); + + while c > 0 { + printf("c = %d\n", c); + + if c > 50 { + printf("c is greater than 50\n"); + } else { + printf("c is less than or equal to 50\n"); + } + + c -= 16; + } + + printf("End result: %d\n", c); + return 0; +} diff --git a/test/bad.w12 b/test/bad.w12 new file mode 100644 index 0000000..2ea721b --- /dev/null +++ b/test/bad.w12 @@ -0,0 +1 @@ +function test() {} diff --git a/test/basic-structs.w12 b/test/basic-structs.w12 new file mode 100644 index 0000000..7f59ef4 --- /dev/null +++ b/test/basic-structs.w12 @@ -0,0 +1,21 @@ +struct Test { + a: int, + b: char, +} + +fn main() -> int { + var t: Test; + t.a = 5; + t.b = 'a'; + + printf("t.a = %d\n", t.a); + printf("t.b = %c\n", t.b); + + t.a += 3; + t.b = 'b'; + + printf("t.a = %d\n", t.a); + printf("t.b = %c\n", t.b); + + return 0; +} diff --git a/test/empty.w12 b/test/empty.w12 new file mode 100644 index 0000000..e69de29 diff --git a/test/fib.w12 b/test/fib.w12 new file mode 100644 index 0000000..242321f --- /dev/null +++ b/test/fib.w12 @@ -0,0 +1,38 @@ +fn rec_fib(n: int) -> int { + if n <= 1 { + return n; + } + + return rec_fib(n - 1) + rec_fib(n - 2); +} + +fn loop_fib(n: int) -> int { + var a: int = 0; + var b: int = 1; + var i: int = 0; + + while i < n { + var c: int = a + b; + a = b; + b = c; + i += 1; + } + + return a; +} + +fn main() -> int { + var n: int = 20; + var rec_result: int = rec_fib(n); + var loop_result: int = loop_fib(n); + printf("Fibonacci of %d via recursion is %d\n", n, rec_result); + printf("Fibonacci of %d via loop is %d\n", n, loop_result); + + if rec_result == loop_result { + printf("Results match\n"); + } else { + printf("Results do not match\n"); + } + + return 0; +} diff --git a/test/functions.w12 b/test/functions.w12 new file mode 100644 index 0000000..b7f6232 --- /dev/null +++ b/test/functions.w12 @@ -0,0 +1,32 @@ +fn mult3(a: int, b: int, c: int) -> int { + return a * b * c; +} + +fn loop_test() -> int { + var i: int = 0; + var result: int = 0; + + while i < 10 { + printf("i = %d\n", i); + result += i; + i += 1; + } + + return result; +} + +fn main() -> int { + var a: int = 5; + var b: int = 3; + var c: int = 2; + + var result: int = mult3(a, b, c); + printf("Result: %d\n", result); + + result = loop_test(); + printf("Result: %d\n", result); + + return 0; +} + +