aboutsummaryrefslogtreecommitdiff
path: root/libxml2-2.9.10/python/tests/indexes.py
blob: e41a0d92cf2e52a52a547b3d3936f7c4ed146e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python -u
# -*- coding: ISO-8859-1 -*-
import sys
import libxml2

# Memory debug specific
libxml2.debugMemory(1)

ctxt = None

class callback:
    def __init__(self, startd, starte, ende, delta, endd):
        self.startd = startd
        self.starte = starte
        self.ende = ende
        self.endd = endd
        self.delta = delta
        self.count = 0

    def startDocument(self):
        global ctxt
        if ctxt.byteConsumed() != self.startd:
            print("document start at wrong index: %d expecting %d\n" % (
                  ctxt.byteConsumed(), self.startd))
            sys.exit(1)

    def endDocument(self):
        global ctxt
        expect = self.ende + self.delta * (self.count - 1) + self.endd
        if ctxt.byteConsumed() != expect:
            print("document end at wrong index: %d expecting %d\n" % (
                  ctxt.byteConsumed(), expect))
            sys.exit(1)

    def startElement(self, tag, attrs):
        global ctxt
        if tag == "bar1":
            expect = self.starte + self.delta * self.count
            if ctxt.byteConsumed() != expect:
                print("element start at wrong index: %d expecting %d\n" % (
                   ctxt.byteConsumed(), expect))
                sys.exit(1)
            

    def endElement(self, tag):
        global ctxt
        if tag == "bar1":
            expect = self.ende + self.delta * self.count
            if ctxt.byteConsumed() != expect:
                print("element end at wrong index: %d expecting %d\n" % (
                      ctxt.byteConsumed(), expect))
                sys.exit(1)
            self.count = self.count + 1

    def characters(self, data):
        pass

#
# First run a pure UTF-8 test
#
handler = callback(0, 13, 27, 198, 183)
ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml")
chunk = """  <bar1>chars1</bar1>
  <bar2>chars2</bar2>
  <bar3>chars3</bar3>
  <bar4>chars4</bar4>
  <bar5>chars5</bar5>
  <bar6>&lt;s6</bar6>
  <bar7>chars7</bar7>
  <bar8>&#38;8</bar8>
  <bar9>chars9</bar9>
"""
i = 0
while i < 10000:
    ctxt.parseChunk(chunk, len(chunk), 0)
    i = i + 1
chunk = "</foo>"
ctxt.parseChunk(chunk, len(chunk), 1)
ctxt=None

#
# Then run a test relying on ISO-Latin-1
#
handler = callback(43, 57, 71, 198, 183)
chunk="""<?xml version="1.0" encoding="ISO-8859-1"?>
<foo>
"""
ctxt = libxml2.createPushParser(handler, chunk, len(chunk), "test.xml")
chunk = """  <bar1>chars1</bar1>
  <bar2>chars2</bar2>
  <bar3>chars3</bar3>
  <bar4>chàrs4</bar4>
  <bar5>chars5</bar5>
  <bar6>&lt;s6</bar6>
  <bar7>chars7</bar7>
  <bar8>&#38;8</bar8>
  <bar9>très 9</bar9>
"""
i = 0
while i < 10000:
    ctxt.parseChunk(chunk, len(chunk), 0)
    i = i + 1
chunk = "</foo>"
ctxt.parseChunk(chunk, len(chunk), 1)
ctxt=None

# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
    print("OK")
else:
    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
    libxml2.dumpMemory()