Lua improvements - Mark Gossage patch #1295168
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7476 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
f6ee32a26a
commit
886ce073e1
5 changed files with 442 additions and 0 deletions
104
SWIG/Examples/test-suite/lua/li_std_string_runme.lua
Normal file
104
SWIG/Examples/test-suite/lua/li_std_string_runme.lua
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
require("import") -- the import fn
|
||||
import("li_std_string") -- import lib
|
||||
|
||||
for k,v in pairs(li_std_string) do _G[k]=v end -- move to global
|
||||
|
||||
-- catch "undefined" global variables
|
||||
setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
|
||||
|
||||
-- helper to check type
|
||||
function is_std_string(s)
|
||||
return type(s)=='userdata' and swig_type(s)=='_p_std__string'
|
||||
end
|
||||
|
||||
-- std::string by value is just a Lua string
|
||||
s=test_value("foo")
|
||||
assert(type(s)=="string" and s=="foo")
|
||||
|
||||
-- std::string by const ref is also just a Lua string
|
||||
s=test_const_reference("foo")
|
||||
assert(type(s)=="string" and s=="foo")
|
||||
|
||||
-- std:string* is an object
|
||||
obj=test_pointer_out()
|
||||
assert(is_std_string(obj) and obj:c_str()=="x") -- check type & value
|
||||
|
||||
test_pointer(obj) -- this wants an object
|
||||
|
||||
cobj=test_const_pointer_out()
|
||||
assert(is_std_string(cobj) and cobj:c_str()=="x") -- check type & value
|
||||
|
||||
test_const_pointer(cobj)
|
||||
|
||||
-- this shouldnt work, but it does
|
||||
-- swig doesnt appear to diff between const object ptrs & object ptrs very well
|
||||
test_pointer(cobj) -- this wants an non const object (give it a const one!)
|
||||
|
||||
-- refs are also wrappered as ptrs (unless the correct typemaps are applied)
|
||||
robj=test_reference_out()
|
||||
assert(is_std_string(robj) and robj:c_str()=="test_reference_out message") -- check type & value
|
||||
|
||||
test_reference(robj)
|
||||
test_reference(obj) -- object ptr is ok
|
||||
test_reference(cobj) -- obj const ptr is also ok
|
||||
|
||||
-- throwing string
|
||||
ok,ex=pcall(test_throw)
|
||||
assert(ok==false and type(ex)=="string") -- failed & threw string
|
||||
|
||||
ok,ex=pcall(test_const_reference_throw)
|
||||
assert(ok==false and type(ex)=="string") -- failed & threw string
|
||||
|
||||
-- const ptrs are thrown as str::string**
|
||||
-- not quite right
|
||||
ok,ex=pcall(test_const_pointer_throw)
|
||||
assert(ok==false and type(ex)=="userdata") -- failed & threw object
|
||||
|
||||
-- ditto non const ptrs
|
||||
ok,ex=pcall(test_pointer_throw)
|
||||
assert(ok==false and type(ex)=="userdata") -- failed & threw object
|
||||
|
||||
-- testing the structure:
|
||||
|
||||
s=Structure()
|
||||
|
||||
-- because of the SWIG's 'everything else is a ptr'
|
||||
-- member strings are a little unusal
|
||||
assert(is_std_string(s.MemberString)) -- std::string*
|
||||
assert(type(s.MemberString2)=="string") -- typemaps make this a string
|
||||
assert(is_std_string(s.ConstMemberString)) -- std::string*
|
||||
|
||||
-- set them
|
||||
s.MemberString:assign("a") -- as its std::string* must use assign
|
||||
s.MemberString2="b" -- typemaps ok
|
||||
s.ConstMemberString="c" -- silently ignored
|
||||
s.ConstMemberString:assign("c") -- works (oops!!!)
|
||||
--print(s.MemberString:data(),s.MemberString2,s.ConstMemberString:data())
|
||||
|
||||
--check type again
|
||||
assert(is_std_string(s.MemberString)) -- std::string*
|
||||
assert(type(s.MemberString2)=="string") -- typemaps make this a string
|
||||
assert(is_std_string(s.ConstMemberString)) -- std::string*
|
||||
|
||||
-- for static types: they are really variables,
|
||||
-- so we must still use the module name
|
||||
|
||||
-- check static type
|
||||
assert(is_std_string(li_std_string.Structure_StaticMemberString))
|
||||
assert(type(li_std_string.Structure_StaticMemberString2)=="string")
|
||||
assert(is_std_string(li_std_string.Structure_ConstStaticMemberString))
|
||||
|
||||
-- try setting
|
||||
li_std_string.Structure_StaticMemberString:assign('d')
|
||||
li_std_string.Structure_StaticMemberString2='e'
|
||||
li_std_string.Structure_ConstStaticMemberString='f' -- silently ignored
|
||||
li_std_string.Structure_ConstStaticMemberString:assign('f') -- works (oops!!!)
|
||||
--[[print(li_std_string.Structure_StaticMemberString:data(),
|
||||
li_std_string.Structure_StaticMemberString2,
|
||||
li_std_string.Structure_ConstStaticMemberString:data())]]
|
||||
|
||||
-- check static type again
|
||||
assert(is_std_string(li_std_string.Structure_StaticMemberString))
|
||||
assert(type(li_std_string.Structure_StaticMemberString2)=="string")
|
||||
assert(is_std_string(li_std_string.Structure_ConstStaticMemberString))
|
||||
|
||||
66
SWIG/Examples/test-suite/lua/li_std_vector_runme.lua
Normal file
66
SWIG/Examples/test-suite/lua/li_std_vector_runme.lua
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
require("import") -- the import fn
|
||||
import("li_std_vector") -- import code
|
||||
|
||||
for k,v in pairs(li_std_vector) do _G[k]=v end -- move to global
|
||||
|
||||
iv = IntVector(4)
|
||||
for i=0,3 do
|
||||
iv[i] = i
|
||||
end
|
||||
|
||||
for i=0,3 do assert(iv[i]==i) end
|
||||
|
||||
x = average(iv)
|
||||
|
||||
function near(x,y) return math.abs(x-y)<0.001 end
|
||||
|
||||
assert(near(x,1.5))
|
||||
|
||||
rv = RealVector()
|
||||
rv:push_back(10)
|
||||
rv:push_back(10.5)
|
||||
rv:push_back(11)
|
||||
rv:push_back(11.5)
|
||||
|
||||
a=half(rv)
|
||||
for i=0,rv:size()-1 do
|
||||
assert(near(a[i],rv[i]/2))
|
||||
end
|
||||
|
||||
dv = DoubleVector(10)
|
||||
for i=0,9 do dv[i] = i/2.0 end
|
||||
|
||||
halve_in_place(dv)
|
||||
|
||||
for i=0,9 do
|
||||
assert(near(dv[i],i/4))
|
||||
end
|
||||
|
||||
sv=StructVector(4)
|
||||
|
||||
for i=0,3 do
|
||||
sv[i]=Struct(i)
|
||||
end
|
||||
|
||||
for i=0,3 do
|
||||
assert( swig_type(sv[i]) =='_p_Struct' and sv[i].num==i)
|
||||
end
|
||||
|
||||
-- range checking
|
||||
idx=0
|
||||
function test_set() iv[idx]=0 end
|
||||
function test_get() iv[idx]=0 end
|
||||
|
||||
idx=0 --ok
|
||||
assert(pcall(test_get)==true)
|
||||
assert(pcall(test_set)==true)
|
||||
idx=-1 --should error
|
||||
assert(pcall(test_get)==false)
|
||||
assert(pcall(test_set)==false)
|
||||
idx=3 --ok
|
||||
assert(pcall(test_get)==true)
|
||||
assert(pcall(test_set)==true)
|
||||
idx=4 --should error
|
||||
assert(pcall(test_get)==false)
|
||||
assert(pcall(test_set)==false)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue