1#!/usr/bin/python -u
2# -*- coding: ISO-8859-1 -*-
3import sys
4import libxml2
5
6# Memory debug specific
7libxml2.debugMemory(1)
8
9ctxt = None
10
11class callback:
12    def __init__(self, startd, starte, ende, delta, endd):
13        self.startd = startd
14        self.starte = starte
15        self.ende = ende
16        self.endd = endd
17        self.delta = delta
18        self.count = 0
19
20    def startDocument(self):
21        global ctxt
22        if ctxt.byteConsumed() != self.startd:
23            print "document start at wrong index: %d expecting %d\n" % (
24                  ctxt.byteConsumed(), self.startd)
25            sys.exit(1)
26
27    def endDocument(self):
28        global ctxt
29        expect = self.ende + self.delta * (self.count - 1) + self.endd
30        if ctxt.byteConsumed() != expect:
31            print "document end at wrong index: %d expecting %d\n" % (
32                  ctxt.byteConsumed(), expect)
33            sys.exit(1)
34
35    def startElement(self, tag, attrs):
36        global ctxt
37        if tag == "bar1":
38            expect = self.starte + self.delta * self.count
39            if ctxt.byteConsumed() != expect:
40                print "element start at wrong index: %d expecting %d\n" % (
41                   ctxt.byteConsumed(), expect)
42                sys.exit(1)
43
44
45    def endElement(self, tag):
46        global ctxt
47        if tag == "bar1":
48            expect = self.ende + self.delta * self.count
49            if ctxt.byteConsumed() != expect:
50                print "element end at wrong index: %d expecting %d\n" % (
51                      ctxt.byteConsumed(), expect)
52                sys.exit(1)
53            self.count = self.count + 1
54
55    def characters(self, data):
56        pass
57
58#
59# First run a pure UTF-8 test
60#
61handler = callback(0, 13, 27, 198, 183)
62ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml")
63chunk = """  <bar1>chars1</bar1>
64  <bar2>chars2</bar2>
65  <bar3>chars3</bar3>
66  <bar4>chars4</bar4>
67  <bar5>chars5</bar5>
68  <bar6>&lt;s6</bar6>
69  <bar7>chars7</bar7>
70  <bar8>&#38;8</bar8>
71  <bar9>chars9</bar9>
72"""
73i = 0
74while i < 10000:
75    ctxt.parseChunk(chunk, len(chunk), 0)
76    i = i + 1
77chunk = "</foo>"
78ctxt.parseChunk(chunk, len(chunk), 1)
79ctxt=None
80
81#
82# Then run a test relying on ISO-Latin-1
83#
84handler = callback(43, 57, 71, 198, 183)
85chunk="""<?xml version="1.0" encoding="ISO-8859-1"?>
86<foo>
87"""
88ctxt = libxml2.createPushParser(handler, chunk, len(chunk), "test.xml")
89chunk = """  <bar1>chars1</bar1>
90  <bar2>chars2</bar2>
91  <bar3>chars3</bar3>
92  <bar4>ch�rs4</bar4>
93  <bar5>chars5</bar5>
94  <bar6>&lt;s6</bar6>
95  <bar7>chars7</bar7>
96  <bar8>&#38;8</bar8>
97  <bar9>tr�s 9</bar9>
98"""
99i = 0
100while i < 10000:
101    ctxt.parseChunk(chunk, len(chunk), 0)
102    i = i + 1
103chunk = "</foo>"
104ctxt.parseChunk(chunk, len(chunk), 1)
105ctxt=None
106
107# Memory debug specific
108libxml2.cleanupParser()
109if libxml2.debugMemory(1) == 0:
110    print "OK"
111else:
112    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
113    libxml2.dumpMemory()
114