Debian Codegraph @

python/tests/regexp.py

raw
1 #!/usr/bin/env python3
2 import sys
3 import setup_test
4 import libxml2
5
6 # Memory debug specific
7 libxml2.debugMemory(1)
8
9
10 def check_regex(regex, inside, outside):
11 re = libxml2.regexpCompile(regex)
12 for c in inside:
13 if re.regexpExec(c) != 1:
14 print("error checking inside", repr(regex), repr(c))
15 sys.exit(1)
16 for c in outside:
17 if re.regexpExec(c) != 0:
18 print("error checking outside", repr(regex), repr(c))
19 sys.exit(1)
20 if re.regexpIsDeterminist() != 1:
21 print("error checking determinism")
22 sys.exit(1)
23 del re
24
25
26 check_regex("a|b", "ab", ["ab", ""])
27 # https://gitlab.gnome.org/GNOME/libxml2/-/issues/1086
28 check_regex(r"[\t-\r]", "\t\n\r", ["a", "(", ""])
29 check_regex("[\\t-\\r]", "\t\n\r", ["a", "(", ""])
30 check_regex(r"[\[-\]]", "[]\\", "Z^")
31 check_regex(r"[\*-/]", "*+,-./", "()0")
32 # Non-standard escape sequences
33 check_regex(r"[\u0041-\u005A]", "ABCMYZ", ["a", "0", "[", ""])
34 check_regex(r"[\u0009\u0061-\u007A]", "\tabcmyz", ["A", "0", "{", "", "\n"])
35 check_regex(r"[\!-\%]", '!"#$%', ["&", " ", "a", ""])
36 check_regex(r"[\:-\>]", ':;<=>', ["?", "9", "@", ""])
37
38 # Memory debug specific
39 libxml2.cleanupParser()
40 if libxml2.debugMemory(1) == 0:
41 print("OK")
42 else:
43 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))