xpath.py 1.2 KB

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