summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/features/test_auto_deref.zc34
-rw-r--r--tests/features/test_move_semantics.zc64
-rw-r--r--tests/memory/test_memory_safety.zc2
3 files changed, 99 insertions, 1 deletions
diff --git a/tests/features/test_auto_deref.zc b/tests/features/test_auto_deref.zc
new file mode 100644
index 0000000..b7b0c51
--- /dev/null
+++ b/tests/features/test_auto_deref.zc
@@ -0,0 +1,34 @@
+
+struct Point {
+ x: int;
+ y: int;
+}
+
+test "auto_deref" {
+ var p = Point { x: 10, y: 20 };
+ var ptr = &p;
+
+ // This uses the dot operator on a pointer
+ // In standard C, this requires ->. In Zen-C w/ Auto-Deref, . should work.
+ assert(ptr.x == 10, "read ptr.x failed");
+
+ ptr.y = 30;
+ assert(p.y == 30, "write ptr.y failed");
+
+ assert(ptr.y == 30, "read ptr.y after write failed");
+
+ var i = Inner { val: 42 };
+ var o = Outer { inner: i };
+ var p_out = &o;
+
+ // Nested access: p.inner.val -> p->inner.val
+ assert(p_out.inner.val == 42, "Nested access failed");
+}
+
+struct Inner {
+ val: int;
+}
+
+struct Outer {
+ inner: Inner;
+}
diff --git a/tests/features/test_move_semantics.zc b/tests/features/test_move_semantics.zc
new file mode 100644
index 0000000..70390c2
--- /dev/null
+++ b/tests/features/test_move_semantics.zc
@@ -0,0 +1,64 @@
+
+struct Point {
+ x: int;
+}
+
+struct Mover {
+ val: int;
+}
+
+fn test_basic_move() {
+ var p1 = Mover { val: 10 };
+ var p2 = p1; // p1 moved to p2
+
+ // Valid usage of p2
+ assert(p2.val == 10, "p2 should be valid");
+
+ // Invalid usage of p1 (Uncomment to test compiler error)
+ // var p3 = p1;
+}
+
+fn test_primitive_copy() {
+ var i = 10;
+ var j = i; // Copy
+ var k = i; // Copy again - should be valid
+ assert(k == 10, "Primitive copy failed");
+}
+
+fn test_reassignment() {
+ var m1 = Mover { val: 1 };
+ var m2 = m1; // m1 moved
+
+ m1 = Mover { val: 2 }; // Resurrect m1
+ var m3 = m1; // Valid now
+ assert(m3.val == 2, "Resurrection failed");
+}
+
+fn test_func_arg(m: Mover) {
+ assert(m.val == 10, "Func arg failed");
+}
+
+fn main() {
+ test_basic_move();
+ test_primitive_copy();
+ test_reassignment();
+
+ var m = Mover { val: 10 };
+ test_func_arg(m); // m moved
+
+ // ** Negative Tests (Uncomment to verify) **
+
+ // 1. Use after move (Assignment)
+ // test_basic_move(); // See line 18 inside function
+
+ // 2. Use after move (Call)
+ // test_func_arg(m); // Should fail: Use of moved value 'm'
+
+ // 3. Use after return
+ /*
+ fn fail_return(m: Mover) -> Mover {
+ var m2 = m;
+ return m; // Should fail: Use of moved value 'm'
+ }
+ */
+}
diff --git a/tests/memory/test_memory_safety.zc b/tests/memory/test_memory_safety.zc
index 652fe21..020a7ba 100644
--- a/tests/memory/test_memory_safety.zc
+++ b/tests/memory/test_memory_safety.zc
@@ -183,7 +183,7 @@ test "test_immutable" {
"Distance: {dist}";
var sum = accumulate(10, 5);
"Accumulate: {sum}";
- var p3 = process_point(p1);
+ var p3 = process_point(Point { x: 0, y: 0 });
"Processed: ({p3.x}, {p3.y})";
counter_immut();
}