compareNodes.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. # Memory debug specific
  5. libxml2.debugMemory(1)
  6. #
  7. # Testing XML Node comparison and Node hash-value
  8. #
  9. doc = libxml2.parseDoc("""<root><foo/></root>""")
  10. root = doc.getRootElement()
  11. # Create two different objects which point to foo
  12. foonode1 = root.children
  13. foonode2 = root.children
  14. # Now check that [in]equality tests work ok
  15. if not ( foonode1 == foonode2 ):
  16. print "Error comparing nodes with ==, nodes should be equal but are unequal"
  17. sys.exit(1)
  18. if not ( foonode1 != root ):
  19. print "Error comparing nodes with ==, nodes should not be equal but are equal"
  20. sys.exit(1)
  21. if not ( foonode1 != root ):
  22. print "Error comparing nodes with !=, nodes should not be equal but are equal"
  23. if ( foonode1 != foonode2 ):
  24. print "Error comparing nodes with !=, nodes should be equal but are unequal"
  25. # Next check that the hash function for the objects also works ok
  26. if not (hash(foonode1) == hash(foonode2)):
  27. print "Error hash values for two equal nodes are different"
  28. sys.exit(1)
  29. if not (hash(foonode1) != hash(root)):
  30. print "Error hash values for two unequal nodes are not different"
  31. sys.exit(1)
  32. if hash(foonode1) == hash(root):
  33. print "Error hash values for two unequal nodes are equal"
  34. sys.exit(1)
  35. # Basic tests successful
  36. doc.freeDoc()
  37. # Memory debug specific
  38. libxml2.cleanupParser()
  39. if libxml2.debugMemory(1) == 0:
  40. print "OK"
  41. else:
  42. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  43. libxml2.dumpMemory()