blob: 8d7a7fc87044504ea1eb559ce1f1fb2791eb62ec [file] [log] [blame]
Daniel Veillard36711902004-02-11 13:25:26 +00001#!/usr/bin/python -u
Daniel Veillarda9a1b102004-02-17 13:24:01 +00002# -*- coding: ISO-8859-1 -*-
Daniel Veillard36711902004-02-11 13:25:26 +00003import 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
Daniel Veillard09b792b2004-02-23 10:53:52 +000017 self.delta = delta
18 self.count = 0
Daniel Veillard36711902004-02-11 13:25:26 +000019
20 def startDocument(self):
21 global ctxt
Daniel Veillard09b792b2004-02-23 10:53:52 +000022 if ctxt.byteConsumed() != self.startd:
23 print "document start at wrong index: %d expecting %d\n" % (
24 ctxt.byteConsumed(), self.startd)
Daniel Veillard36711902004-02-11 13:25:26 +000025 sys.exit(1)
26
27 def endDocument(self):
28 global ctxt
Daniel Veillard09b792b2004-02-23 10:53:52 +000029 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)
Daniel Veillard36711902004-02-11 13:25:26 +000033 sys.exit(1)
34
35 def startElement(self, tag, attrs):
36 global ctxt
Daniel Veillard09b792b2004-02-23 10:53:52 +000037 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
Daniel Veillard36711902004-02-11 13:25:26 +000044
45 def endElement(self, tag):
46 global ctxt
Daniel Veillard09b792b2004-02-23 10:53:52 +000047 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
Daniel Veillard36711902004-02-11 13:25:26 +000054
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()