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