PageRenderTime 45ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/lua/li_std_vector_runme.lua

#
Lua | 66 lines | 48 code | 17 blank | 1 comment | 8 complexity | 9f1b922a36748ee89cded77e96663dd4 MD5 | raw file
 1require("import")	-- the import fn
 2import("li_std_vector")	-- import code
 3
 4for k,v in pairs(li_std_vector) do _G[k]=v end -- move to global
 5
 6iv = IntVector(4)
 7for i=0,3 do
 8    iv[i] = i
 9end
10
11for i=0,3 do assert(iv[i]==i) end
12
13x = average(iv)
14
15function near(x,y) return math.abs(x-y)<0.001 end
16
17assert(near(x,1.5))
18
19rv = RealVector()
20rv:push_back(10)
21rv:push_back(10.5)
22rv:push_back(11)
23rv:push_back(11.5)
24
25a=half(rv)
26for i=0,rv:size()-1 do
27	assert(near(a[i],rv[i]/2))
28end
29
30dv = DoubleVector(10)
31for i=0,9 do dv[i] = i/2.0 end
32
33halve_in_place(dv)
34
35for i=0,9 do 
36	assert(near(dv[i],i/4))
37end
38
39sv=StructVector(4)
40
41for i=0,3 do
42	sv[i]=Struct(i)
43end
44
45for i=0,3 do
46	assert( swig_type(sv[i]) =='Struct *' and sv[i].num==i)
47end
48
49-- range checking
50idx=0
51function test_set() iv[idx]=0 end
52function test_get() iv[idx]=0 end
53
54idx=0 --ok
55assert(pcall(test_get)==true)
56assert(pcall(test_set)==true)
57idx=-1 --should error
58assert(pcall(test_get)==false)
59assert(pcall(test_set)==false)
60idx=3 --ok
61assert(pcall(test_get)==true)
62assert(pcall(test_set)==true)
63idx=4 --should error
64assert(pcall(test_get)==false)
65assert(pcall(test_set)==false)
66