Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/python -u
      2 # -*- coding: ISO-8859-1 -*-
      3 import sys
      4 import libxml2
      5 
      6 # Memory debug specific
      7 libxml2.debugMemory(1)
      8 
      9 ctxt = None
     10 
     11 class 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 #
     61 handler = callback(0, 13, 27, 198, 183)
     62 ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml")
     63 chunk = """  <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 """
     73 i = 0
     74 while i < 10000:
     75     ctxt.parseChunk(chunk, len(chunk), 0)
     76     i = i + 1
     77 chunk = "</foo>"
     78 ctxt.parseChunk(chunk, len(chunk), 1)
     79 ctxt=None
     80 
     81 #
     82 # Then run a test relying on ISO-Latin-1
     83 #
     84 handler = callback(43, 57, 71, 198, 183)
     85 chunk="""<?xml version="1.0" encoding="ISO-8859-1"?>
     86 <foo>
     87 """
     88 ctxt = libxml2.createPushParser(handler, chunk, len(chunk), "test.xml")
     89 chunk = """  <bar1>chars1</bar1>
     90   <bar2>chars2</bar2>
     91   <bar3>chars3</bar3>
     92   <bar4>chrs4</bar4>
     93   <bar5>chars5</bar5>
     94   <bar6>&lt;s6</bar6>
     95   <bar7>chars7</bar7>
     96   <bar8>&#38;8</bar8>
     97   <bar9>trs 9</bar9>
     98 """
     99 i = 0
    100 while i < 10000:
    101     ctxt.parseChunk(chunk, len(chunk), 0)
    102     i = i + 1
    103 chunk = "</foo>"
    104 ctxt.parseChunk(chunk, len(chunk), 1)
    105 ctxt=None
    106 
    107 # Memory debug specific
    108 libxml2.cleanupParser()
    109 if libxml2.debugMemory(1) == 0:
    110     print("OK")
    111 else:
    112     print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
    113     libxml2.dumpMemory()
    114