diff options
Diffstat (limited to 'tests/memory/test_resources.zc')
| -rw-r--r-- | tests/memory/test_resources.zc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/memory/test_resources.zc b/tests/memory/test_resources.zc new file mode 100644 index 0000000..e21653d --- /dev/null +++ b/tests/memory/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..."; + + let p1 = Point{x: 10, y: 20}; + let p2 = p1; // Copied + + let b1 = Box{val: 99}; + let b2 = b1.clone(); + // let b3 = b1; // This would move if uncommented. + + if b2.val != 99 { + !"Clone failed"; + exit(1); + } + + // Re-initialization + // struct Resource (Global) + + let r1 = Resource{ptr: NULL}; + let r2 = r1; // Moved + + r1 = Resource{ptr: NULL}; // Re-init + let r3 = r1; // Valid again + + // Function Param Move (Simulated) + take_point(p1); + take_point(p1); // Still valid because Copy + + "Resource Semantics Passed."; +} + |
