summaryrefslogtreecommitdiff
path: root/tests/misc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/misc')
-rw-r--r--tests/misc/test_advanced.zc211
-rw-r--r--tests/misc/test_chained.zc14
-rw-r--r--tests/misc/test_edge_cases.zc151
-rw-r--r--tests/misc/test_mix.zc61
4 files changed, 437 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!";
+}
diff --git a/tests/misc/test_chained.zc b/tests/misc/test_chained.zc
new file mode 100644
index 0000000..e758db7
--- /dev/null
+++ b/tests/misc/test_chained.zc
@@ -0,0 +1,14 @@
+
+import "std/string.zc"
+
+test "test_chained" {
+ var s = String::from("hello");
+ var opt = s.find('e');
+ if opt.is_some() {
+ "Found at index: {opt.unwrap()}";
+ }
+
+ if s.find('l').is_some() {
+ "Chained call works!";
+ }
+}
diff --git a/tests/misc/test_edge_cases.zc b/tests/misc/test_edge_cases.zc
new file mode 100644
index 0000000..f1a4cf5
--- /dev/null
+++ b/tests/misc/test_edge_cases.zc
@@ -0,0 +1,151 @@
+
+import "std.zc"
+
+// Empty struct
+@derive(Eq, Clone)
+struct Empty {}
+
+fn test_empty_struct() {
+ var e1 = Empty {};
+ var e2 = Empty {};
+
+ if (!e1.eq(e2)) {
+ println "FAIL: Empty struct eq failed";
+ exit(1);
+ }
+
+ println " -> Empty struct with @derive(Eq) works";
+}
+
+// Struct with many fields (stress @derive(Eq))
+@derive(Eq)
+struct ManyFields {
+ a: int;
+ b: int;
+ c: int;
+ d: int;
+ e: int;
+ f: int;
+ g: int;
+ h: int;
+}
+
+fn test_many_fields() {
+ var m1 = ManyFields { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 };
+ var m2 = ManyFields { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8 };
+ var m3 = ManyFields { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 9 }; // h differs
+
+ if (!m1.eq(m2)) {
+ println "FAIL: equal structs not detected as equal";
+ exit(1);
+ }
+
+ if (m1.eq(m3)) {
+ println "FAIL: different structs detected as equal";
+ exit(1);
+ }
+
+ println " -> ManyFields @derive(Eq) works correctly";
+}
+
+// Pointer to struct equality (should use ==, not _eq)
+struct Point {
+ x: int;
+ y: int;
+}
+
+fn test_pointer_equality() {
+ var p1 = Point { x: 1, y: 2 };
+ var ptr1: Point* = &p1;
+ var ptr2: Point* = &p1;
+ var p2 = Point { x: 1, y: 2 };
+ var ptr3: Point* = &p2;
+
+ // Same pointer should be equal
+ if (ptr1 != ptr2) {
+ println "FAIL: same pointer not equal";
+ exit(1);
+ }
+
+ // Different pointers (even to equal values) should not be pointer-equal
+ if (ptr1 == ptr3) {
+ println "FAIL: different pointers compare equal";
+ exit(1);
+ }
+
+ print " -> Pointer equality uses == not _eq";
+}
+
+// Very long string
+fn test_long_string() {
+ var s = String::from("This is a very long string that should test the string handling capabilities of the compiler and make sure that buffer allocations are correct and that there are no overflow issues with long strings.");
+
+ if (s.length() < 100) {
+ println "FAIL: long string length incorrect";
+ exit(1);
+ }
+
+ print " -> Long string handling works";
+}
+
+// Null pointer handling
+fn test_null_pointer() {
+ var ptr: int* = NULL;
+
+ if (ptr != NULL) {
+ println "FAIL: NULL pointer check failed";
+ exit(1);
+ }
+
+ println " -> NULL pointer handling works";
+}
+
+// Numeric edge cases
+fn test_numeric_edges() {
+ var max_int: int = 2147483647;
+ var min_int: int = -1000000000;
+
+ if (max_int <= 0) {
+ println "FAIL: max_int is wrong";
+ exit(1);
+ }
+
+ if (min_int >= 0) {
+ println "FAIL: min_int is wrong";
+ exit(1);
+ }
+
+ println " -> Numeric edge cases work";
+}
+
+// Boolean operations
+fn test_boolean_ops() {
+ var a = true;
+ var b = false;
+
+ if (!(a && !b)) {
+ println "FAIL: boolean AND failed";
+ exit(1);
+ }
+
+ if (!(a || b)) {
+ println "FAIL: boolean OR failed";
+ exit(1);
+ }
+
+ println " -> Boolean operations work\n";
+}
+
+test "test_edge_cases" {
+ println "Running edge case tests...";
+
+ test_empty_struct();
+ test_many_fields();
+ test_pointer_equality();
+ test_long_string();
+ test_null_pointer();
+ test_numeric_edges();
+ test_boolean_ops();
+
+ println "All edge case tests passed!";
+}
diff --git a/tests/misc/test_mix.zc b/tests/misc/test_mix.zc
new file mode 100644
index 0000000..b4edca4
--- /dev/null
+++ b/tests/misc/test_mix.zc
@@ -0,0 +1,61 @@
+
+enum Result {
+ Ok(int),
+ Err(int)
+}
+
+fn add(a: int, b: int) -> int {
+ return a + b;
+}
+
+fn square(x: int) -> int {
+ return x * x;
+}
+
+test "test_result_constructors" {
+ var res = Result_Ok(42);
+
+ assert(res.tag == Result_Ok_Tag, "Expected Ok tag");
+ assert(res.data.Ok == 42, "Data mismatch");
+
+ var err = Result_Err(500);
+ assert(err.tag == Result_Err_Tag, "Expected Err tag");
+}
+
+test "test_pipelines" {
+ // Logic: ((5 + 5) * (5 + 5))
+ // 5 |> add(5) -> 10
+ // 10 |> square() -> 100
+ var val = 5 |> add(5) |> square();
+
+ println "Pipeline result: {val}";
+ assert(val == 100, "Pipeline calculation failed");
+}
+
+test "test_fstrings" {
+ var x = 10;
+ var name = "ZPrep";
+
+ var s = f"Hello {name}, x = {x}, x * 2 = {x * 2}";
+
+ println "F-String: {s}";
+ assert(x == 10, "Sanity check");
+}
+
+test "test_defer" {
+ println " [Defer] 1. Start";
+
+ defer { println " [Defer] 4. Cleanup B"; }
+ defer { println " [Defer] 3. Cleanup A"; }
+
+ println " [Defer] 2. End";
+}
+
+test "test_hex_binary" {
+ var h = 0xFF; // 255
+ var b = 0b1010; // 10
+ assert(h == 255, "Hex parsing failed");
+ assert(b == 10, "Binary parsing failed");
+}
+
+// Removed empty test_result_enum