2022-12-07 00:40:01 +03:00
#!/usr/bin/env python3
2002-02-03 00:49:17 +03:00
import sys
2023-08-15 13:49:27 +03:00
import setup_test
2002-02-02 12:17:16 +03:00
import libxml2
2002-02-03 23:13:06 +03:00
# Memory debug specific
libxml2 . debugMemory ( 1 )
2002-02-08 16:28:40 +03:00
def foo ( ctx , x ) :
2002-02-02 12:17:16 +03:00
return x + 1
2002-02-08 16:28:40 +03:00
def bar ( ctx , x ) :
2002-02-03 00:49:17 +03:00
return " %d " % ( x + 2 )
2002-02-02 12:17:16 +03:00
doc = libxml2 . parseFile ( " tst.xml " )
ctxt = doc . xpathNewContext ( )
res = ctxt . xpathEval ( " //* " )
2002-02-03 00:49:17 +03:00
if len ( res ) != 2 :
2013-03-30 17:38:20 +04:00
print ( " xpath query: wrong node set size " )
2002-02-03 00:49:17 +03:00
sys . exit ( 1 )
if res [ 0 ] . name != " doc " or res [ 1 ] . name != " foo " :
2013-03-30 17:38:20 +04:00
print ( " xpath query: wrong node set value " )
2002-02-03 00:49:17 +03:00
sys . exit ( 1 )
2002-02-02 12:17:16 +03:00
libxml2 . registerXPathFunction ( ctxt . _o , " foo " , None , foo )
libxml2 . registerXPathFunction ( ctxt . _o , " bar " , None , bar )
i = 10000
while i > 0 :
res = ctxt . xpathEval ( " foo(1) " )
2002-02-03 00:49:17 +03:00
if res != 2 :
2013-03-30 17:38:20 +04:00
print ( " xpath extension failure " )
2004-02-23 13:53:52 +03:00
sys . exit ( 1 )
2002-02-02 12:17:16 +03:00
i = i - 1
i = 10000
while i > 0 :
res = ctxt . xpathEval ( " bar(1) " )
2002-02-03 00:49:17 +03:00
if res != " 3 " :
2013-03-30 17:38:20 +04:00
print ( " xpath extension failure got %s expecting ' 3 ' " )
2004-02-23 13:53:52 +03:00
sys . exit ( 1 )
2002-02-02 12:17:16 +03:00
i = i - 1
doc . freeDoc ( )
2002-02-08 16:28:40 +03:00
ctxt . xpathFreeContext ( )
2002-02-03 23:13:06 +03:00
# Memory debug specific
libxml2 . cleanupParser ( )
if libxml2 . debugMemory ( 1 ) == 0 :
2013-03-30 17:38:20 +04:00
print ( " OK " )
2002-02-03 23:13:06 +03:00
else :
2013-03-30 17:38:20 +04:00
print ( " Memory leak %d bytes " % ( libxml2 . debugMemory ( 1 ) ) )