diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-20 10:33:17 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-20 10:33:27 +0000 |
| commit | 856198b2ea473a4fd2cd020e58db821265b21ca5 (patch) | |
| tree | a7e836404a51f58ae2f89943f6b3d465e80957f0 /tests/features/test_move_semantics.zc | |
| parent | bdf22227d8b822b6ef8cdc4c0a62c37018bcc415 (diff) | |
Move semantics...
Diffstat (limited to 'tests/features/test_move_semantics.zc')
| -rw-r--r-- | tests/features/test_move_semantics.zc | 64 |
1 files changed, 64 insertions, 0 deletions
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' + } + */ +} |
