blob: e47c34d4d9db7964f61526f5d3eda7da34e73c7d [file] [log] [blame]
Daniel Veillard253aa2c2002-02-02 09:17:16 +00001#!/usr/bin/python -u
Daniel Veillard5d819032002-02-02 21:49:17 +00002import sys
Daniel Veillard253aa2c2002-02-02 09:17:16 +00003import libxml2
4
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00005#memory debug specific
6libxml2.debugMemory(1)
7
Daniel Veillardc575b992002-02-08 13:28:40 +00008called = ""
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00009
Daniel Veillardc575b992002-02-08 13:28:40 +000010def foo(ctx, x):
11 global called
12
13 #
14 # test that access to the XPath evaluation contexts
15 #
16 pctxt = libxml2.xpathParserContext(_obj=ctx)
17 ctxt = pctxt.context()
18 called = ctxt.function()
Daniel Veillard253aa2c2002-02-02 09:17:16 +000019 return x + 1
20
Daniel Veillardc575b992002-02-08 13:28:40 +000021def bar(ctxt, x):
Daniel Veillard5d819032002-02-02 21:49:17 +000022 return "%d" % (x + 2)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000023
24doc = libxml2.parseFile("tst.xml")
25ctxt = doc.xpathNewContext()
26res = ctxt.xpathEval("//*")
Daniel Veillard5d819032002-02-02 21:49:17 +000027if len(res) != 2:
28 print "xpath query: wrong node set size"
29 sys.exit(1)
30if res[0].name != "doc" or res[1].name != "foo":
31 print "xpath query: wrong node set value"
32 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000033libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
34libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
35i = 10000
36while i > 0:
37 res = ctxt.xpathEval("foo(1)")
Daniel Veillard5d819032002-02-02 21:49:17 +000038 if res != 2:
39 print "xpath extension failure"
Daniel Veillard09b792b2004-02-23 10:53:52 +000040 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000041 i = i - 1
Daniel Veillard253aa2c2002-02-02 09:17:16 +000042i = 10000
43while i > 0:
44 res = ctxt.xpathEval("bar(1)")
Daniel Veillard5d819032002-02-02 21:49:17 +000045 if res != "3":
46 print "xpath extension failure got %s expecting '3'"
Daniel Veillard09b792b2004-02-23 10:53:52 +000047 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000048 i = i - 1
Daniel Veillard253aa2c2002-02-02 09:17:16 +000049doc.freeDoc()
Daniel Veillardc575b992002-02-08 13:28:40 +000050ctxt.xpathFreeContext()
51
52if called != "foo":
53 print "xpath function: failed to access the context"
54 print "xpath function: %s" % (called)
55 sys.exit(1)
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000056
57#memory debug specific
58libxml2.cleanupParser()
59if libxml2.debugMemory(1) == 0:
60 print "OK"
61else:
62 print "Memory leak %d bytes" % (libxml2.debugMemory(1))
63 libxml2.dumpMemory()