blob: 47696261033b40ca3187bf4bd817ded1723d4ce2 [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 +00008def foo(ctx, x):
Daniel Veillard253aa2c2002-02-02 09:17:16 +00009 return x + 1
10
Daniel Veillardc575b992002-02-08 13:28:40 +000011def bar(ctx, x):
Daniel Veillard5d819032002-02-02 21:49:17 +000012 return "%d" % (x + 2)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000013
14doc = libxml2.parseFile("tst.xml")
15ctxt = doc.xpathNewContext()
16res = ctxt.xpathEval("//*")
Daniel Veillard5d819032002-02-02 21:49:17 +000017if len(res) != 2:
18 print "xpath query: wrong node set size"
19 sys.exit(1)
20if res[0].name != "doc" or res[1].name != "foo":
21 print "xpath query: wrong node set value"
22 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000023
24libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
25libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
26i = 10000
27while i > 0:
28 res = ctxt.xpathEval("foo(1)")
Daniel Veillard5d819032002-02-02 21:49:17 +000029 if res != 2:
30 print "xpath extension failure"
Daniel Veillard09b792b2004-02-23 10:53:52 +000031 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000032 i = i - 1
Daniel Veillard253aa2c2002-02-02 09:17:16 +000033i = 10000
34while i > 0:
35 res = ctxt.xpathEval("bar(1)")
Daniel Veillard5d819032002-02-02 21:49:17 +000036 if res != "3":
37 print "xpath extension failure got %s expecting '3'"
Daniel Veillard09b792b2004-02-23 10:53:52 +000038 sys.exit(1)
Daniel Veillard253aa2c2002-02-02 09:17:16 +000039 i = i - 1
Daniel Veillard253aa2c2002-02-02 09:17:16 +000040doc.freeDoc()
Daniel Veillardc575b992002-02-08 13:28:40 +000041ctxt.xpathFreeContext()
Daniel Veillard4e1b26c2002-02-03 20:13:06 +000042
43# Memory debug specific
44libxml2.cleanupParser()
45if libxml2.debugMemory(1) == 0:
46 print "OK"
47else:
48 print "Memory leak %d bytes" % (libxml2.debugMemory(1))
49 libxml2.dumpMemory()