Skip to content

Commit

Permalink
<test> raii-tuple-default-copy.t and fails/raii-tuple-custom-copy.t
Browse files Browse the repository at this point in the history
  • Loading branch information
hiemstar committed May 27, 2024
1 parent 4e6bfc3 commit 95a7e84
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 29 deletions.
42 changes: 42 additions & 0 deletions tests/fails/raii-tuple-custom-copy.t
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()
42 changes: 42 additions & 0 deletions tests/raii-tuple-default-copy.t
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())
45 changes: 16 additions & 29 deletions tests/raii.t
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
--load 'terralibext' to enable raii
require "terralibext"
require "terralibext" --load 'terralibext' to enable raii
local test = require "test"

local std = {}
std.io = terralib.includec("stdio.h")
local std = {
io = terralib.includec("stdio.h")
}

local function printtestheader(s)
print()
Expand Down Expand Up @@ -40,11 +41,14 @@ A.methods.__copy:adddefinition(terra(from : &A, to : &int)
end)


printtestheader("raii.t - testing __init metamethod")
terra testinit()
var a : A
return a.data
end
test.eq(testinit(), 1)

printtestheader("raii.t - testing __dtor metamethod")
terra testdtor()
var x : &int
do
Expand All @@ -53,57 +57,40 @@ terra testdtor()
end
return @x
end
test.eq(testdtor(), -1)

printtestheader("raii.t - testing __copy metamethod in copy-construction")
terra testcopyconstruction()
var a : A
var b = a
return b.data
end
test.eq(testcopyconstruction(), 11)

printtestheader("raii.t - testing __copy metamethod in copy-assignment")
terra testcopyassignment()
var a : A
a.data = 2
var b : A
b = a
return b.data
end
test.eq(testcopyassignment(), 12)

printtestheader("raii.t - testing __copy metamethod in copy-assignment from integer to struct.")
terra testcopyassignment1()
var a : A
a = 3
return a.data
end
test.eq(testcopyassignment1(), 3)

printtestheader("raii.t - testing __copy metamethod in copy-assignment from struct to integer.")
terra testcopyassignment2()
var a : A
var x : int
a.data = 5
x = a
return x
end

local test = require "test"

--test if __init is called on object initialization to set 'a.data = 1'
printtestheader("raii.t - testing __init metamethod")
test.eq(testinit(), 1)

--test if __dtor is called at the end of the scope to set 'a.data = -1'
printtestheader("raii.t - testing __dtor metamethod")
test.eq(testdtor(), -1)

--test if __copy is called in construction 'var b = a'
printtestheader("raii.t - testing __copy metamethod in copy-construction")
test.eq(testcopyconstruction(), 11)

--test if __copy is called in an assignment 'b = a'
printtestheader("raii.t - testing __copy metamethod in copy-assignment")
test.eq(testcopyassignment(), 12)

--test if __copy is called in an assignment 'a = 3'
printtestheader("raii.t - testing __copy metamethod in copy-assignment from integer to struct.")
test.eq(testcopyassignment1(), 3)

--test if __copy is called in an assignment 'x = a' for integer x
printtestheader("raii.t - testing __copy metamethod in copy-assignment from struct to integer.")
test.eq(testcopyassignment2(), 5)

0 comments on commit 95a7e84

Please sign in to comment.