summaryrefslogtreecommitdiff
path: root/tests/functions
diff options
context:
space:
mode:
Diffstat (limited to 'tests/functions')
-rw-r--r--tests/functions/test_attributes.zc10
-rw-r--r--tests/functions/test_implicit_return.zc2
-rw-r--r--tests/functions/test_lambda_arrow.zc18
-rw-r--r--tests/functions/test_lambdas.zc55
-rw-r--r--tests/functions/test_must_use.zc6
-rw-r--r--tests/functions/test_raw_func_ptr.zc27
-rw-r--r--tests/functions/test_varargs.zc2
7 files changed, 93 insertions, 27 deletions
diff --git a/tests/functions/test_attributes.zc b/tests/functions/test_attributes.zc
index 6e52372..36ca3ad 100644
--- a/tests/functions/test_attributes.zc
+++ b/tests/functions/test_attributes.zc
@@ -50,8 +50,8 @@ struct NormalStruct {
b: U32;
}
-var init_called = 0;
-var cleanup_called = 0;
+let init_called = 0;
+let cleanup_called = 0;
@constructor
fn my_init() {
@@ -79,18 +79,18 @@ fn unused_func() {}
test "function attributes" {
// @must_use: result used - no warning
- var x = compute();
+ let x = compute();
assert(x == 42, "compute() failed");
// No must_use - can discard
optional_result();
// @inline works
- var sum = fast_add(1, 2);
+ let sum = fast_add(1, 2);
assert(sum == 3, "inline add failed");
// Combined attributes
- var c = combined();
+ let c = combined();
assert(c == 100, "combined failed");
// @must_use: result verified
diff --git a/tests/functions/test_implicit_return.zc b/tests/functions/test_implicit_return.zc
index bb35134..e82e286 100644
--- a/tests/functions/test_implicit_return.zc
+++ b/tests/functions/test_implicit_return.zc
@@ -12,7 +12,7 @@ test "test_implicit_return" {
println " Done!";
// With variables
- var x = 42;
+ let x = 42;
print "Value is: ";
println "{x}";
}
diff --git a/tests/functions/test_lambda_arrow.zc b/tests/functions/test_lambda_arrow.zc
index c976ecf..b4c8edc 100644
--- a/tests/functions/test_lambda_arrow.zc
+++ b/tests/functions/test_lambda_arrow.zc
@@ -4,17 +4,25 @@ fn compute(op: fn(I32, I32) -> I32, a: I32, b: I32) -> I32 {
}
test "test_lambda_arrow" {
- var doubler = x -> x * 2;
- var res1 = doubler(5);
+ let doubler = x -> x * 2;
+ let res1 = doubler(5);
"doubler(5) = {res1}";
if res1 != 10 { exit(1); }
- var add = (x, y) -> x + y;
- var res2 = add(10, 20);
+ let add = (x, y) -> x + y;
+ let res2 = add(10, 20);
"add(10, 20) = {res2}";
if res2 != 30 { exit(1); }
- var res3 = compute((a, b) -> a * b, 3, 4);
+ let res3 = compute((a, b) -> a * b, 3, 4);
"compute((a, b) -> a * b, 3, 4) = {res3}";
if res3 != 12 { exit(1); }
}
+
+test "lambda_inference_repro" {
+ let dble = x -> x * 2.0;
+ let res = dble(9.0);
+ if res != 18.0 {
+ exit(1);
+ }
+}
diff --git a/tests/functions/test_lambdas.zc b/tests/functions/test_lambdas.zc
index 5f2c086..24cb65b 100644
--- a/tests/functions/test_lambdas.zc
+++ b/tests/functions/test_lambdas.zc
@@ -12,8 +12,8 @@ test "test_complex_lambdas" {
println "Testing complex lambda scenarios...";
// Lambda with multiple parameters
- var add_three = (a, b, c) -> a + b + c;
- var res1 = add_three(1, 2, 3);
+ let add_three = (a, b, c) -> a + b + c;
+ let res1 = add_three(1, 2, 3);
if (res1 == 6) {
println " -> Multi-param lambda: Passed";
} else {
@@ -22,7 +22,7 @@ test "test_complex_lambdas" {
}
// Higher-order function
- var res2 = apply_twice(x -> x * 2, 5);
+ let res2 = apply_twice(x -> x * 2, 5);
if (res2 == 20) {
println " -> Higher-order function: Passed";
} else {
@@ -31,8 +31,8 @@ test "test_complex_lambdas" {
}
// Returning a lambda (closure)
- var add10 = make_adder(10);
- var res3 = add10(5);
+ let add10 = make_adder(10);
+ let res3 = add10(5);
if (res3 == 15) {
println " -> Returned lambda: Passed";
} else {
@@ -41,9 +41,9 @@ test "test_complex_lambdas" {
}
// Lambda composition
- var doubler = x -> x * 2;
- var add5 = x -> x + 5;
- var res4 = add5(doubler(10));
+ let doubler = x -> x * 2;
+ let add5 = x -> x + 5;
+ let res4 = add5(doubler(10));
if (res4 == 25) {
println " -> Lambda composition: Passed";
} else {
@@ -52,7 +52,7 @@ test "test_complex_lambdas" {
}
// Nested application
- var res5 = apply_twice(x -> x + 1, apply_twice(x -> x * 2, 3));
+ let res5 = apply_twice(x -> x + 1, apply_twice(x -> x * 2, 3));
// (3 * 2) * 2 = 12, then (12 + 1) + 1 = 14
if (res5 == 14) {
println " -> Nested application: Passed";
@@ -60,6 +60,37 @@ test "test_complex_lambdas" {
println " -> Nested application: Failed";
exit(1);
}
+
+ // Lambda with "fn" syntax (eg. block lambda)
+ let factor = 2;
+ let full = fn(x: int) -> int { return x * factor };
+
+ if (full(3) == 6) {
+ println " -> Lambda fn syntax: Passed";
+ } else {
+ println " -> Lambda fn syntax: Failed";
+ exit(1);
+ }
+
+ // Lambda with "fn" syntax (eg. block lambda) + multiple params
+ let addition = fn(sum1: int, sum2: int) -> int { return sum1 + sum2 };
+
+ if (addition(100, 505) == 605) {
+ println " -> Lambda fn syntax: Passed";
+ } else {
+ println " -> Lambda fn syntax: Failed";
+ exit(1);
+ }
+
+ // Lambda with "fn" syntax (eg. block lambda) + named args
+ let substract = fn(arg1: int, arg2: int) -> int { return arg1 - arg2 };
+
+ if (substract(arg1: 500, arg2: 100) == 400) {
+ println " -> Lambda fn syntax: Passed";
+ } else {
+ println " -> Lambda fn syntax: Failed";
+ exit(1);
+ }
println "All complex lambda tests passed!";
@@ -67,9 +98,9 @@ test "test_complex_lambdas" {
}
test "test_basic_closure" {
- var whatever = 10;
- var closure: fn(I32) -> I32 = x -> x + whatever;
- var res = closure(5);
+ let whatever = 10;
+ let closure: fn(I32) -> I32 = x -> x + whatever;
+ let res = closure(5);
// "Result: {res}";
if (res != 15) {
println "Closure failed: expected 15, got {res}";
diff --git a/tests/functions/test_must_use.zc b/tests/functions/test_must_use.zc
index a42b341..98936ea 100644
--- a/tests/functions/test_must_use.zc
+++ b/tests/functions/test_must_use.zc
@@ -26,7 +26,7 @@ fn optional_result() -> int {
test "attributes" {
// This should NOT warn (result used)
- var x = compute();
+ let x = compute();
assert(x == 42, "compute() should return 42");
// This should NOT warn (no must_use attribute)
@@ -36,11 +36,11 @@ test "attributes" {
compute();
// Test inline works
- var sum = fast_add(1, 2);
+ let sum = fast_add(1, 2);
assert(sum == 3, "inline add failed");
// Combined attributes
- var c = combined();
+ let c = combined();
assert(c == 100, "combined failed");
"Attributes test completed";
diff --git a/tests/functions/test_raw_func_ptr.zc b/tests/functions/test_raw_func_ptr.zc
new file mode 100644
index 0000000..115e421
--- /dev/null
+++ b/tests/functions/test_raw_func_ptr.zc
@@ -0,0 +1,27 @@
+
+fn print_msg(msg: char*) {
+ puts(msg);
+}
+
+fn run_callback(cb: fn*(char*), arg: char*) {
+ cb(arg);
+}
+
+fn get_printer() -> fn*(char*) {
+ return print_msg;
+}
+
+test "raw_func_ptr_basic" {
+ run_callback(print_msg, "Hello Raw Ptr");
+}
+
+test "raw_func_ptr_return_explicit" {
+ let p: fn*(char*) = get_printer();
+ p("Returned Ptr Explicit works");
+}
+
+test "fn_ptr_ptr" {
+ let p: fn*(char*) = print_msg;
+ let pp: fn**(char*) = &p;
+ (*pp)("Double Pointer works");
+}
diff --git a/tests/functions/test_varargs.zc b/tests/functions/test_varargs.zc
index b05e168..9b004db 100644
--- a/tests/functions/test_varargs.zc
+++ b/tests/functions/test_varargs.zc
@@ -4,7 +4,7 @@ test "test_varargs" {
println "Testing Varargs...";
printf(" Direct Printf: %d, %f, %s\n", 42, 3.14, "Hello");
- var buf = malloc(100);
+ let buf = malloc(100);
sprintf(buf, "Formatted: %d", 123);
println " {buf}";
}