xref: /aosp_15_r20/external/libxml2/python/tests/xpath.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2#
3# this test exercise the XPath basic engine, parser, etc, and
4# allows to detect memory leaks
5#
6import sys
7import setup_test
8import libxml2
9
10# Memory debug specific
11libxml2.debugMemory(1)
12
13doc = libxml2.parseFile("tst.xml")
14if doc.name != "tst.xml":
15    print("doc.name error")
16    sys.exit(1);
17
18ctxt = doc.xpathNewContext()
19res = ctxt.xpathEval("//*")
20if len(res) != 2:
21    print("xpath query: wrong node set size")
22    sys.exit(1)
23if res[0].name != "doc" or res[1].name != "foo":
24    print("xpath query: wrong node set value")
25    sys.exit(1)
26ctxt.setContextNode(res[0])
27res = ctxt.xpathEval("foo")
28if len(res) != 1:
29    print("xpath query: wrong node set size")
30    sys.exit(1)
31if res[0].name != "foo":
32    print("xpath query: wrong node set value")
33    sys.exit(1)
34doc.freeDoc()
35ctxt.xpathFreeContext()
36i = 1000
37while i > 0:
38    doc = libxml2.parseFile("tst.xml")
39    ctxt = doc.xpathNewContext()
40    res = ctxt.xpathEval("//*")
41    doc.freeDoc()
42    ctxt.xpathFreeContext()
43    i = i -1
44del ctxt
45
46# Memory debug specific
47libxml2.cleanupParser()
48if libxml2.debugMemory(1) == 0:
49    print("OK")
50else:
51    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
52