summaryrefslogtreecommitdiff
path: root/tests/misc/test_advanced.zc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/misc/test_advanced.zc')
-rw-r--r--tests/misc/test_advanced.zc211
1 files changed, 211 insertions, 0 deletions
diff --git a/tests/misc/test_advanced.zc b/tests/misc/test_advanced.zc
new file mode 100644
index 0000000..8b5b466
--- /dev/null
+++ b/tests/misc/test_advanced.zc
@@ -0,0 +1,211 @@
+import "std.zc"
+
+fn fibonacci(n: int) -> int {
+ if (n <= 1) {
+ return n;
+ }
+ return fibonacci(n - 1) + fibonacci(n - 2);
+}
+
+fn test_recursion() {
+ var fib10 = fibonacci(10);
+
+ if (fib10 != 55) {
+ println "FAIL: fibonacci(10) should be 55";
+ exit(1);
+ }
+
+ println " -> Recursive function works";
+}
+
+fn add_three(a: int, b: int, c: int) -> int {
+ return a + b + c;
+}
+
+fn test_multi_params() {
+ var result = add_three(1, 2, 3);
+
+ if (result != 6) {
+ println "FAIL: add_three(1,2,3) should be 6";
+ exit(1);
+ }
+
+ println " -> Multi-parameter function works";
+}
+
+fn double_val(x: int) -> int {
+ return x * 2;
+}
+
+fn test_nested_calls() {
+ var result = double_val(double_val(double_val(2)));
+
+ if (result != 16) {
+ println "FAIL: nested calls failed";
+ exit(1);
+ }
+
+ println " -> Nested function calls work";
+}
+
+struct Counter {
+ value: int;
+}
+
+impl Counter {
+ fn new() -> Counter {
+ return Counter { value: 0 };
+ }
+
+ fn get(self) -> int {
+ return self.value;
+ }
+
+ fn increment(self) {
+ self.value = self.value + 1;
+ }
+}
+
+fn test_struct_methods() {
+ var c = Counter::new();
+ c.increment();
+ c.increment();
+ c.increment();
+
+ if (c.get() != 3) {
+ println "FAIL: Counter should be 3";
+ exit(1);
+ }
+
+ println " -> Struct methods work";
+}
+
+fn test_casting() {
+ var f: float = 3.7;
+ var i: int = (int)f;
+
+ if (i != 3) {
+ println "FAIL: float to int cast failed";
+ exit(1);
+ }
+
+ var i2: int = 42;
+ var f2: float = (float)i2;
+
+ if (f2 < 41.9 || f2 > 42.1) {
+ println "FAIL: int to float cast failed";
+ exit(1);
+ }
+
+ println " -> Type casting works";
+}
+
+fn test_bitwise() {
+ var a = 0b1010; // 10
+ var b = 0b1100; // 12
+
+ var and_result = a & b; // 0b1000 = 8
+ var or_result = a | b; // 0b1110 = 14
+ var xor_result = a ^ b; // 0b0110 = 6
+
+ if (and_result != 8) {
+ println "FAIL: bitwise AND failed";
+ exit(1);
+ }
+
+ if (or_result != 14) {
+ println "FAIL: bitwise OR failed";
+ exit(1);
+ }
+
+ if (xor_result != 6) {
+ println "FAIL: bitwise XOR failed";
+ exit(1);
+ }
+
+ println " -> Bitwise operations work";
+}
+
+fn test_shift() {
+ var x = 1;
+ var left = x << 4; // 16
+
+ if (left != 16) {
+ println "FAIL: left shift failed";
+ exit(1);
+ }
+
+ var y = 32;
+ var right = y >> 2; // 8
+
+ if (right != 8) {
+ println "FAIL: right shift failed";
+ exit(1);
+ }
+
+ println " -> Shift operations work";
+}
+
+fn test_modulo() {
+ var a = 17;
+ var b = 5;
+ var result = a % b;
+
+ if (result != 2) {
+ println "FAIL: modulo operation failed";
+ exit(1);
+ }
+
+ println " -> Modulo operator works";
+}
+
+fn test_compound_expr() {
+ var x = 5;
+ var y = 10;
+ var z = 3;
+
+ var result = (x + y) * z - (y / 2);
+ // (5 + 10) * 3 - (10 / 2) = 15 * 3 - 5 = 45 - 5 = 40
+
+ if (result != 40) {
+ println "FAIL: compound expression failed";
+ exit(1);
+ }
+
+ println " -> Compound expressions work";
+}
+
+fn test_increment_style() {
+ var x = 5;
+ x = x + 1;
+
+ if (x != 6) {
+ println "FAIL: increment style failed";
+ exit(1);
+ }
+
+ x = x - 1;
+ if (x != 5) {
+ println "FAIL: decrement style failed";
+ exit(1);
+ }
+
+ print " -> Increment/decrement style works";
+}
+
+test "test_advanced" {
+ println "Running function and advanced tests...";
+
+ test_recursion();
+ test_multi_params();
+ test_nested_calls();
+ test_struct_methods();
+ test_casting();
+ test_bitwise();
+ test_shift();
+ test_modulo();
+ test_compound_expr();
+ test_increment_style();
+
+ println "All function and advanced tests passed!";
+}