| 1 |
if not Test then |
| 2 |
local loadlib |
| 3 |
if not package then |
| 4 |
loadlib = _G['loadlib'] |
| 5 |
else |
| 6 |
loadlib = package.loadlib |
| 7 |
end |
| 8 |
f, e, eo = loadlib("./libtclass.so", "luaopen_tclass") |
| 9 |
if f then |
| 10 |
f() |
| 11 |
else |
| 12 |
print(eo, e) |
| 13 |
os.exit() |
| 14 |
end |
| 15 |
end |
| 16 |
|
| 17 |
a = {} |
| 18 |
rawset(a, ".c_instance", "something") |
| 19 |
|
| 20 |
function hello() |
| 21 |
|
| 22 |
print("hello world") |
| 23 |
end |
| 24 |
|
| 25 |
rawset(Test.B, "hello", hello) |
| 26 |
|
| 27 |
-- type convertion tests |
| 28 |
--print(Test.A) |
| 29 |
--print(tolua.type(Test.A.last)) |
| 30 |
--assert(tolua.type(Test.A.last) == 'Test::Tst_A') -- first time the object is mapped |
| 31 |
--assert(tolua.type(Test.B.last) == 'Test::Tst_B') -- type convertion to specialized type |
| 32 |
--assert(tolua.type(Test.A.last) == 'Test::Tst_B') -- no convertion: obj already mapped as B |
| 33 |
|
| 34 |
|
| 35 |
local a = Test.A:new() |
| 36 |
assert(tolua.type(Test.A.last) == 'Test::Tst_A') -- no type convertion: same type |
| 37 |
local b = Test.B:new() |
| 38 |
assert(tolua.type(Test.A.last) == 'Test::Tst_B') -- no convertion: obj already mapped as B |
| 39 |
local c = Test.luaC:new(0) |
| 40 |
assert(tolua.type(Test.A.last) == 'Test::Tst_C') -- no convertion: obj already mapped as C |
| 41 |
assert(tolua.type(Test.luaC.last) == 'Test::Tst_C') |
| 42 |
|
| 43 |
local aa = Test.A.AA:new() |
| 44 |
local bb = Test.A.BB:new() |
| 45 |
local xx = Test.create_aa() |
| 46 |
|
| 47 |
-- method calling tests |
| 48 |
assert(a:a() == 'A') |
| 49 |
assert(b:a() == 'A') |
| 50 |
assert(b:b() == 'B') |
| 51 |
assert(c:a() == 'A') |
| 52 |
assert(c:b() == 'B') |
| 53 |
assert(c:c() == 'C') |
| 54 |
assert(aa:aa() == 'AA') |
| 55 |
assert(bb:aa() == bb:Base():aa()) |
| 56 |
assert(xx:aa() == 'AA') |
| 57 |
assert(Test.is_aa(bb) == true) |
| 58 |
|
| 59 |
-- test ownershipping handling |
| 60 |
-- should delete objects: 6 7 8 9 10 (it may vary!) |
| 61 |
local set = {} |
| 62 |
for i=1,10 do |
| 63 |
local c = Test.luaC:new(i) |
| 64 |
if i>5 then |
| 65 |
tolua.takeownership(c) |
| 66 |
end |
| 67 |
--set[i] = c |
| 68 |
end |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
e = Test.B:new_local() |
| 73 |
|
| 74 |
print("e is type "..tolua.type(e)) |
| 75 |
print("ae is type "..tolua.type(ae)) |
| 76 |
|
| 77 |
--e:delete() |
| 78 |
|
| 79 |
b:hello() |
| 80 |
|
| 81 |
---------- |
| 82 |
local out = Test.Outside:new_local() |
| 83 |
out:outside() |
| 84 |
Test.Outside:outside_static(nil) |
| 85 |
|
| 86 |
|
| 87 |
print "***** cast" |
| 88 |
local acast = Test.B:create_void() |
| 89 |
print("type is "..tolua.type(acast)) |
| 90 |
local bcast = tolua.cast(acast, "Test::Tst_B") |
| 91 |
print("bcast is "..tostring(bcast)) |
| 92 |
print("type is "..tolua.type(bcast)) |
| 93 |
print(bcast:b()) |
| 94 |
|
| 95 |
-- test properies |
| 96 |
local n = 7 |
| 97 |
a.number = n |
| 98 |
assert(a.number == n*2) |
| 99 |
|
| 100 |
-- constructors |
| 101 |
print(getmetatable(Test.A)) |
| 102 |
print(getmetatable(Test.B)) |
| 103 |
print(getmetatable(Test.E)) |
| 104 |
|
| 105 |
local a = Test.A() |
| 106 |
local b = Test.B() |
| 107 |
local e = Test.E(5) |
| 108 |
--print(e+5) |
| 109 |
print(tostring(getmetatable(Test.B).__call)) |
| 110 |
print(tostring(Test.B.__call)) |
| 111 |
print(tostring(Test.B.__call(Test.B))) |
| 112 |
print(tolua.type(b)) |
| 113 |
|
| 114 |
e:set_ptr(e) |
| 115 |
local ve = tolua.cast(e:get_ptr(), "Test::Tst_E") |
| 116 |
ve:set_ptr(ve) |
| 117 |
|
| 118 |
print"1" |
| 119 |
Test.A.pete = {} |
| 120 |
print"2" |
| 121 |
table.insert(Test.A.pete, a) |
| 122 |
print"3" |
| 123 |
|
| 124 |
|
| 125 |
for i=1,100000 do |
| 126 |
la = {} |
| 127 |
tolua.inherit(la, a) |
| 128 |
end |
| 129 |
|
| 130 |
print("Class test OK") |
| 131 |
|