xref: /aosp_15_r20/external/libxml2/python/tests/xpathext.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2import sys
3import setup_test
4import libxml2
5
6# Memory debug specific
7libxml2.debugMemory(1)
8
9def foo(ctx, x):
10    return x + 1
11
12def bar(ctx, x):
13    return "%d" % (x + 2)
14
15doc = libxml2.parseFile("tst.xml")
16ctxt = doc.xpathNewContext()
17res = ctxt.xpathEval("//*")
18if len(res) != 2:
19    print("xpath query: wrong node set size")
20    sys.exit(1)
21if res[0].name != "doc" or res[1].name != "foo":
22    print("xpath query: wrong node set value")
23    sys.exit(1)
24
25libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
26libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
27i = 10000
28while i > 0:
29    res = ctxt.xpathEval("foo(1)")
30    if res != 2:
31        print("xpath extension failure")
32        sys.exit(1)
33    i = i - 1
34i = 10000
35while i > 0:
36    res = ctxt.xpathEval("bar(1)")
37    if res != "3":
38        print("xpath extension failure got %s expecting '3'")
39        sys.exit(1)
40    i = i - 1
41doc.freeDoc()
42ctxt.xpathFreeContext()
43
44# Memory debug specific
45libxml2.cleanupParser()
46if libxml2.debugMemory(1) == 0:
47    print("OK")
48else:
49    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
50