From 7d3fb0f38333996f93ac9af190eefe5ca663cb70 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Tue, 20 Jan 2026 21:14:18 +0000 Subject: Fix 'Clone' among other things --- tests/features/test_resources.zc | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/features/test_resources.zc (limited to 'tests') diff --git a/tests/features/test_resources.zc b/tests/features/test_resources.zc new file mode 100644 index 0000000..dc7b9f9 --- /dev/null +++ b/tests/features/test_resources.zc @@ -0,0 +1,59 @@ + +// Copy Trait +@derive(Copy) +struct Point { x: int; y: int; } + +// Clone Pattern +trait Clone { + fn clone(self) -> Self; +} + +struct Box { val: int; } + +impl Clone for Box { + fn clone(self) -> Box { + return Box{val: self.val}; + } +} + +// Re-initialization +struct Resource { ptr: void*; } + +// Function Param Helper +fn take_point(p: Point) { + if p.x != 10 { + // Error + } +} + +test "Resource Semantics" { + "Testing Resource Semantics..."; + + var p1 = Point{x: 10, y: 20}; + var p2 = p1; // Copied + + var b1 = Box{val: 99}; + var b2 = b1.clone(); + // var b3 = b1; // This would move if uncommented. + + if b2.val != 99 { + !"Clone failed"; + exit(1); + } + + // Re-initialization + // struct Resource (Global) + + var r1 = Resource{ptr: NULL}; + var r2 = r1; // Moved + + r1 = Resource{ptr: NULL}; // Re-init + var r3 = r1; // Valid again + + // Function Param Move (Simulated) + take_point(p1); + take_point(p1); // Still valid because Copy + + "Resource Semantics Passed."; +} + -- cgit v1.2.3