-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<tests> added raii-copyctr.t that tests direct initialization and cop…
…y construction of managed structs.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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,57 @@ | ||
require "terralibext" --load 'terralibext' to enable raii | ||
--[[ | ||
We need that direct initialization | ||
var a : A = b | ||
yields the same result as | ||
var a : A | ||
a = b | ||
If 'b' is a variable or a literal (something with a value) and the user has | ||
implemented the right copy-assignment 'A.methods.__copy' then the copy | ||
should be performed using this method. | ||
--]] | ||
|
||
local test = require("test") | ||
io = terralib.includec("stdio.h") | ||
|
||
struct A{ | ||
data : int | ||
} | ||
|
||
A.methods.__init = terra(self : &A) | ||
io.printf("__init: calling initializer.\n") | ||
self.data = 1 | ||
end | ||
|
||
A.methods.__dtor = terra(self : &A) | ||
io.printf("__dtor: calling destructor.\n") | ||
self.data = -1 | ||
end | ||
|
||
A.methods.__copy = terra(from : &A, to : &A) | ||
io.printf("__copy: calling copy assignment {&A, &A} -> {}.\n") | ||
to.data = to.data + from.data + 1 | ||
end | ||
|
||
terra test1() | ||
var a : A --__init -> a.data = 1 | ||
var aa = a --__init + __copy -> a.data = 3 | ||
return aa.data | ||
end | ||
|
||
-- aa.data = aa.data + a.data + 1 = 3 | ||
test.eq(test1(), 3) | ||
|
||
--since A is managed, an __init, __dtor, and __copy will | ||
--be generated | ||
struct B{ | ||
data : A | ||
} | ||
|
||
terra test2() | ||
var a : A --__init -> a.data = 1 | ||
var b = B{a} --__init + __copy -> b.data.data = 3 | ||
return b.data.data | ||
end | ||
|
||
-- b.data.data = b.data.data + a.data + 1 = 3 | ||
test.eq(test2(), 3) |