1 #!/usr/local/bin/lua5.1 |
1 #!/usr/local/bin/lua5.1 |
2 -- See Copyright Notice in license.html |
2 -- See Copyright Notice in license.html |
3 -- $Id: test.lua,v 1.6 2006/06/08 20:34:52 tomas Exp $ |
3 -- $Id: test.lua,v 1.6 2006/06/08 20:34:52 tomas Exp $ |
4 |
4 |
5 require"lxp" |
5 if string.find(_VERSION, " 5.0") then |
|
6 lxp = assert(loadlib("./lxp.so", "luaopen_lxp"))() |
|
7 else |
|
8 lxp = require"lxp" |
|
9 gcinfo = function () return collectgarbage"count" end |
|
10 end |
6 print (lxp._VERSION) |
11 print (lxp._VERSION) |
|
12 assert(lxp.new, "Cannot find function lxp.new ("..tostring(lxp.new)..")") |
7 |
13 |
8 -- basic test with no preamble |
14 -- basic test with no preamble |
9 local p = lxp.new{} |
15 local p = lxp.new{} |
10 p:setencoding("ISO-8859-1") |
16 p:setencoding("ISO-8859-1") |
11 assert(p:parse[[<tag cap="5">hi</tag>]]) |
17 assert(p:parse[[<tag cap="5">hi</tag>]]) |
33 explanation ENTITY #REQUIRED> |
39 explanation ENTITY #REQUIRED> |
34 |
40 |
35 ]> |
41 ]> |
36 ]] |
42 ]] |
37 |
43 |
38 local X |
44 X = {} |
39 function getargs (...) X = arg end |
45 if string.find(_VERSION, " 5.0") then |
40 |
46 function getargs (...) X = arg end |
41 function xgetargs (c) |
47 function xgetargs (c) |
42 return function (...) |
48 return function (...) |
43 table.insert(arg, 1, c) |
49 table.insert(arg, 1, c) |
44 table.insert(X, arg) |
50 table.insert(X, arg) |
45 end |
51 end |
|
52 end |
|
53 else |
|
54 (loadstring or load)[[ |
|
55 function getargs (...) |
|
56 X = { ... } |
|
57 X.n = select('#', ...) |
|
58 end |
|
59 function xgetargs (c) |
|
60 return function (...) |
|
61 local arg = { ... } |
|
62 arg.n = select('#', ...) + 1 |
|
63 table.insert(arg, 1, c) |
|
64 table.insert(X, arg) |
|
65 end |
|
66 end |
|
67 table.getn = function (t) |
|
68 if t.n then |
|
69 return t.n |
|
70 else |
|
71 local n = 0 |
|
72 for i in pairs(t) do |
|
73 if type(i) == "number" then |
|
74 n = math.max(n, i) |
|
75 end |
|
76 end |
|
77 return n |
|
78 end |
|
79 end]]() |
46 end |
80 end |
|
81 |
47 |
82 |
48 |
83 |
49 ------------------------------- |
84 ------------------------------- |
50 print("testing start/end tags") |
85 print("testing start/end tags") |
51 callbacks = { |
86 callbacks = { |
59 <to priority="10" xu = "hi"> |
94 <to priority="10" xu = "hi"> |
60 ]])) |
95 ]])) |
61 assert(X.n == 3 and X[1] == p and X[2] == "to") |
96 assert(X.n == 3 and X[1] == p and X[2] == "to") |
62 x = X[3] |
97 x = X[3] |
63 assert(x.priority=="10" and x.xu=="hi" and x.method=="POST") |
98 assert(x.priority=="10" and x.xu=="hi" and x.method=="POST") |
64 assert(x[1] == "priority" and x[2] == "xu" and table.getn(x) == 2) |
99 assert(x[1] == "priority" and x[2] == "xu" and table.getn(x) == 2, "x[1] == "..tostring(x[1])..", x[2] == "..tostring(x[2])..", #x == "..tostring(table.getn(x))) |
65 assert(p:parse("</to>")) |
100 assert(p:parse("</to>")) |
66 assert(p:parse()) |
101 assert(p:parse()) |
67 p:close() |
102 p:close() |
68 |
103 |
69 |
104 |
93 p = lxp.new(callbacks) |
128 p = lxp.new(callbacks) |
94 assert(p:parse(preamble)) |
129 assert(p:parse(preamble)) |
95 assert(p:parse"<to>") |
130 assert(p:parse"<to>") |
96 assert(p:parse"<![CDATA[hi]]>") |
131 assert(p:parse"<![CDATA[hi]]>") |
97 assert(table.getn(X) == 3) |
132 assert(table.getn(X) == 3) |
98 assert(X[1][1] == "s" and X[1][2] == p) |
133 assert(X[1][1] == "s" and X[1][2] == p, "X[1][1] == "..tostring(X[1][1])..", X[1][2] == "..tostring(X[1][2])..", p == "..tostring(p)) |
99 assert(X[2][1] == "c" and X[2][2] == p and X[2][3] == "hi") |
134 assert(X[2][1] == "c" and X[2][2] == p and X[2][3] == "hi") |
100 assert(X[3][1] == "e" and X[3][2] == p) |
135 assert(X[3][1] == "e" and X[3][2] == p) |
101 assert(p:parse"</to>") |
136 assert(p:parse"</to>") |
102 p:close() |
137 p:close() |
103 |
138 |
314 print("\ntesting garbage collection") |
349 print("\ntesting garbage collection") |
315 collectgarbage(); collectgarbage() |
350 collectgarbage(); collectgarbage() |
316 local x = gcinfo() |
351 local x = gcinfo() |
317 for i=1,100000 do |
352 for i=1,100000 do |
318 -- due to a small bug in Lua... |
353 -- due to a small bug in Lua... |
319 if math.mod(i, 100) == 0 then collectgarbage() end |
354 if (math.mod or math.fmod)(i, 100) == 0 then collectgarbage() end |
320 lxp.new({}) |
355 lxp.new({}) |
321 end |
356 end |
322 collectgarbage(); collectgarbage() |
357 collectgarbage(); collectgarbage() |
323 assert(math.abs(gcinfo() - x) <= 2) |
358 assert(math.abs(gcinfo() - x) <= 2) |
324 |
359 |