1#!/usr/bin/python -u
2import sys
3import libxml2
4
5# Memory debug specific
6libxml2.debugMemory(1)
7
8def foo(ctx, x):
9    return x + 1
10
11def bar(ctx, x):
12    return "%d" % (x + 2)
13
14doc = libxml2.parseFile("tst.xml")
15ctxt = doc.xpathNewContext()
16res = ctxt.xpathEval("//*")
17if 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)
23
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)")
29    if res != 2:
30        print "xpath extension failure"
31        sys.exit(1)
32    i = i - 1
33i = 10000
34while i > 0:
35    res = ctxt.xpathEval("bar(1)")
36    if res != "3":
37        print "xpath extension failure got %s expecting '3'"
38        sys.exit(1)
39    i = i - 1
40doc.freeDoc()
41ctxt.xpathFreeContext()
42
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()
50