Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 2 | import sys |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 3 | import libxml2 |
| 4 | |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 5 | #memory debug specific |
| 6 | libxml2.debugMemory(1) |
| 7 | |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 8 | called = "" |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 9 | |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 10 | def foo(ctx, x): |
| 11 | global called |
| 12 | |
| 13 | # |
| 14 | # test that access to the XPath evaluation contexts |
| 15 | # |
| 16 | pctxt = libxml2.xpathParserContext(_obj=ctx) |
| 17 | ctxt = pctxt.context() |
| 18 | called = ctxt.function() |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 19 | return x + 1 |
| 20 | |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 21 | def bar(ctxt, x): |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 22 | return "%d" % (x + 2) |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 23 | |
| 24 | doc = libxml2.parseFile("tst.xml") |
| 25 | ctxt = doc.xpathNewContext() |
| 26 | res = ctxt.xpathEval("//*") |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 27 | if len(res) != 2: |
| 28 | print "xpath query: wrong node set size" |
| 29 | sys.exit(1) |
| 30 | if res[0].name != "doc" or res[1].name != "foo": |
| 31 | print "xpath query: wrong node set value" |
| 32 | sys.exit(1) |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 33 | libxml2.registerXPathFunction(ctxt._o, "foo", None, foo) |
| 34 | libxml2.registerXPathFunction(ctxt._o, "bar", None, bar) |
| 35 | i = 10000 |
| 36 | while i > 0: |
| 37 | res = ctxt.xpathEval("foo(1)") |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 38 | if res != 2: |
| 39 | print "xpath extension failure" |
Daniel Veillard | 09b792b | 2004-02-23 10:53:52 +0000 | [diff] [blame] | 40 | sys.exit(1) |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 41 | i = i - 1 |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 42 | i = 10000 |
| 43 | while i > 0: |
| 44 | res = ctxt.xpathEval("bar(1)") |
Daniel Veillard | 5d81903 | 2002-02-02 21:49:17 +0000 | [diff] [blame] | 45 | if res != "3": |
| 46 | print "xpath extension failure got %s expecting '3'" |
Daniel Veillard | 09b792b | 2004-02-23 10:53:52 +0000 | [diff] [blame] | 47 | sys.exit(1) |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 48 | i = i - 1 |
Daniel Veillard | 253aa2c | 2002-02-02 09:17:16 +0000 | [diff] [blame] | 49 | doc.freeDoc() |
Daniel Veillard | c575b99 | 2002-02-08 13:28:40 +0000 | [diff] [blame] | 50 | ctxt.xpathFreeContext() |
| 51 | |
| 52 | if called != "foo": |
| 53 | print "xpath function: failed to access the context" |
| 54 | print "xpath function: %s" % (called) |
| 55 | sys.exit(1) |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 56 | |
| 57 | #memory debug specific |
| 58 | libxml2.cleanupParser() |
| 59 | if libxml2.debugMemory(1) == 0: |
| 60 | print "OK" |
| 61 | else: |
| 62 | print "Memory leak %d bytes" % (libxml2.debugMemory(1)) |
| 63 | libxml2.dumpMemory() |