-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<test> raii-tuple-default-copy.t and fails/raii-tuple-custom-copy.t
- Loading branch information
Showing
3 changed files
with
100 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "terralibext" --load 'terralibext' to enable raii | ||
|
||
local std = {} | ||
std.io = terralib.includec("stdio.h") | ||
|
||
local function printtestheader(s) | ||
print() | ||
print("===========================") | ||
print(s) | ||
print("===========================") | ||
end | ||
|
||
struct A{ | ||
data : int | ||
} | ||
|
||
A.methods.__init = terra(self : &A) | ||
std.io.printf("__init: calling initializer.\n") | ||
self.data = 1 | ||
end | ||
|
||
A.methods.__dtor = terra(self : &A) | ||
std.io.printf("__dtor: calling destructor.\n") | ||
self.data = -1 | ||
end | ||
|
||
A.methods.__copy = terra(from : &A, to : &A) | ||
std.io.printf("__copy: calling custom copy.\n") | ||
to.data = from.data+1 | ||
end | ||
|
||
printtestheader("raii.t - testing custom copy for tuples") | ||
terra test0() | ||
var a = A{1} | ||
var b = A{2} | ||
a, b = b, a | ||
--tuple assignments are prohibited when __copy is implemented | ||
--because proper resource management cannot be guaranteed | ||
--(at least not yet) | ||
return a.data, b.data | ||
end | ||
test0() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "terralibext" --load 'terralibext' to enable raii | ||
local test = require "test" | ||
|
||
local std = {} | ||
std.io = terralib.includec("stdio.h") | ||
|
||
local function printtestheader(s) | ||
print() | ||
print("===========================") | ||
print(s) | ||
print("===========================") | ||
end | ||
|
||
struct A{ | ||
data : int | ||
} | ||
|
||
A.methods.__init = terra(self : &A) | ||
std.io.printf("__init: calling initializer.\n") | ||
self.data = 1 | ||
end | ||
|
||
A.methods.__dtor = terra(self : &A) | ||
std.io.printf("__dtor: calling destructor.\n") | ||
self.data = -1 | ||
end | ||
|
||
printtestheader("raii.t - testing default copy metamethod") | ||
terra test0() | ||
var a = A{1} | ||
var b = A{2} | ||
a, b = b, a --bitcopies should work in a swap | ||
--the following code is generated | ||
--var tmp_a = __move(b) --store evaluated rhs in a tmp variable | ||
--var tmp_b = __move(a) --store evaluated rhs in a tmp variable | ||
--a:__dtor() --delete old memory (nothing happens as 'a' has been moved from) | ||
--b:__dtor() --delete old memory (nothing happens as 'a' has been moved from) | ||
--a = __move(tmp_a) --move new data into 'a' | ||
--b = __move(tmp_b) --move new data into 'b' | ||
return a.data, b.data | ||
end | ||
test.meq({2, 1}, test0()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters