def test_remove_unnecessary_parens(): # mult contains a pattern containing a single conc containing a single mult? # that can be reduced greatly assert conc.parse("(a){2}b").reduce() == conc.parse("a{2}b") assert conc.parse("(a?)+b").reduce() == conc.parse("a*b") assert mult.parse("([ab])*").reduce() == mult.parse("[ab]*") assert mult.parse("(c{1,2}){3,4}").reduce() == mult.parse("c{3,8}")
def test_mult_gen(): # One term gen = mult.parse("[ab]").strings() assert next(gen) == "a" assert next(gen) == "b" try: next(gen) assert False except StopIteration: assert True # No terms gen = mult.parse("[ab]{0}").strings() assert next(gen) == "" try: next(gen) assert False except StopIteration: assert True # Many terms gen = mult.parse("[ab]*").strings() assert next(gen) == "" assert next(gen) == "a" assert next(gen) == "b" assert next(gen) == "aa" assert next(gen) == "ab" assert next(gen) == "ba" assert next(gen) == "bb" assert next(gen) == "aaa"
def test_charclass_multiplication(): # a * 1 = a assert charclass("a") * one == charclass("a") # a * {1,3} = a{1,3} assert charclass("a") * multiplier.parse("{1,3}") == mult.parse("a{1,3}") # a * {4,} = a{4,} assert charclass("a") * multiplier.parse("{4,}") == mult.parse("a{4,}")
def test_remove_unnecessary_parens(): # mult contains a pattern containing a single conc containing a single mult? # that can be reduced greatly # e.g. "([ab])*" -> "[ab]*" assert mult.parse("([ab])*").reduce() == mult.parse("[ab]*") # e.g. "(c{1,2}){3,4}" -> "c{3,8}" assert mult.parse("(c{1,2}){3,4}").reduce() == mult.parse("c{3,8}")
def test_mult_factor_out_qm(): # mult contains a pattern containing an empty conc? Pull the empty # part out where it's external assert mult.parse("(a|b*|)").reduce() == mult.parse("(a|b*)?") assert conc.parse("(a|b*|)c").reduce() == conc.parse("(a|b*)?c") # This happens even if emptystring is the only thing left inside the mult assert mult.parse("()").reduce() == conc.parse("") assert mult.parse("([$%\\^]|){1}").reduce() == mult.parse("[$%\\^]?")
def test_main_bug(): assert parse("a*").reduce() == mult.parse("a*") assert parse("a|a*").reduce() == mult.parse("a*") assert parse("a{1,2}|a{3,4}|bc").reduce() == pattern.parse("a{1,4}|bc") assert parse("a{1,2}|bc|a{3,4}").reduce() == pattern.parse("a{1,4}|bc") assert parse("a{1,2}|a{3,4}|a{5,6}|bc").reduce() == pattern.parse("a{1,6}|bc") assert parse("a{1,2}|a{3}|a{5,6}").reduce() == conc.parse("a{1,2}(a?|a{4})") assert parse("a{1,2}|a{3}|a{5,6}|bc").reduce() == pattern.parse("a{1,3}|a{5,6}|bc") assert parse("a{1,2}|a{4}|a{5,6}").reduce() == conc.parse("a{1,2}(a{3,4})?") assert parse("a{1,2}|a{4}|a{5,6}|bc").reduce() == pattern.parse("a{1,2}|a{4,6}|bc") assert (parse("a") | parse("a*")).reduce() == mult.parse("a*")
def test_cardinality(): assert charclass.parse("[]").cardinality() == 0 assert mult.parse("[]?").cardinality() == 1 assert mult.parse("[]{0,6}").cardinality() == 1 assert mult.parse("[ab]{3}").cardinality() == 8 assert mult.parse("[ab]{2,3}").cardinality() == 12 assert len(pattern.parse("abc|def(ghi|jkl)")) == 3 try: len(pattern.parse(".*")) assert False except OverflowError: assert True
def test_empty(): assert nothing.empty() assert charclass().empty() assert not dot.empty() assert not mult.parse("a{0}").empty() assert mult.parse("[]").empty() assert not mult.parse("[]?").empty() assert conc.parse("a[]").empty() assert not conc.parse("a[]?").empty() assert pattern().empty() assert not pattern.parse("a{0}").empty() assert not pattern.parse("[]?").empty()
def test_conc_reduction_basic(): assert conc.parse("a[]b").reduce() == charclass.parse("[]") # conc -> conc assert conc.parse("ab").reduce() == conc.parse("ab") # conc -> mult assert conc.parse("a{3,4}").reduce() == mult.parse("a{3,4}") # conc -> charclass assert conc.parse("a").reduce() == charclass.parse("a")
def test_pattern_reduce_basic(): # pattern -> pattern # (ab|cd) -> (ab|cd) assert pattern.parse("ab|cd").reduce() == pattern.parse("ab|cd") # pattern -> conc assert pattern.parse("a{2}b{2}").reduce() == conc.parse("a{2}b{2}") # pattern -> mult assert pattern.parse("a{2}").reduce() == mult.parse("a{2}") # pattern -> charclass assert pattern.parse("a").reduce() == charclass.parse("a")
def test_mult_parsing(): assert mult.parse("[a-g]+") == mult(charclass("abcdefg"), plus) assert mult.parse("[a-g0-8$%]+") == mult(charclass("abcdefg012345678$%"), plus) assert mult.parse("[a-g0-8$%\\^]+") == mult(charclass("abcdefg012345678$%^"), plus) assert mult.match("abcde[^fg]*", 5) == ( mult(~charclass("fg"), star), 11 ) assert mult.match("abcde[^fg]*h{5}[a-z]+", 11) == ( mult(charclass("h"), multiplier(bound(5), bound(5))), 15 ) assert mult.match("abcde[^fg]*h{5}[a-z]+T{1,}", 15) == ( mult(charclass("abcdefghijklmnopqrstuvwxyz"), plus), 21 ) assert mult.match("abcde[^fg]*h{5}[a-z]+T{2,}", 21) == ( mult(charclass("T"), multiplier(bound(2), inf)), 26 )
def test_mult_reduction_easy(): assert mult.parse("a").reduce() == charclass.parse("a") assert mult.parse("a").reduce() == charclass("a") assert mult.parse("a?").reduce() == mult(charclass("a"), qm) assert mult.parse("a{0}").reduce() == emptystring assert mult.parse("[]").reduce() == nothing assert mult.parse("[]?").reduce() == emptystring assert mult.parse("[]{0}").reduce() == emptystring assert mult.parse("[]{0,5}").reduce() == emptystring assert mult(pattern(), one).reduce() == nothing assert mult(pattern(), qm).reduce() == emptystring assert mult(pattern(), zero).reduce() == emptystring assert mult(pattern(), multiplier.parse("{0,5}")).reduce() == emptystring
def test_mult_multiplication(): # a{2,3} * 1 = a{2,3} assert mult.parse("a{2,3}") * one == mult.parse("a{2,3}") # a{2,3} * {4,5} = a{8,15} assert mult.parse("a{2,3}") * multiplier.parse("{4,5}") == mult.parse("a{8,15}") # a{2,} * {2,} = a{4,} assert mult.parse("a{2,}") * multiplier.parse("{2,}") == mult.parse("a{4,}")
def test_concatenation(): assert charclass.parse("a") + charclass.parse("b") == conc.parse("ab") assert charclass.parse("a") + mult.parse("b{0,8}") == conc.parse("ab{0,8}") assert charclass.parse("a") + conc.parse("bc") == conc.parse("abc") assert charclass.parse("a") + pattern.parse("b|cd") == conc.parse("a(b|cd)") assert mult.parse("b{0,8}") + charclass.parse("c") == conc.parse("b{0,8}c") assert mult.parse("a{3,4}") + mult.parse("b?") == conc.parse("a{3,4}b?") assert mult.parse("a{2}") + conc.parse("bc") == conc.parse("a{2}bc") assert mult.parse("a{2,3}") + pattern.parse("b|cd") == conc.parse("a{2,3}(b|cd)") assert conc.parse("ab") + charclass.parse("c") == conc.parse("abc") assert conc.parse("ab") + mult.parse("c*") == conc.parse("abc*") assert conc.parse("") + conc.parse("") == conc.parse("") assert conc.parse("ab") + conc.parse("cd") == conc.parse("abcd") assert conc.parse("za{2,3}") + pattern.parse("b|cd") == conc.parse("za{2,3}(b|cd)") assert pattern.parse("a|bd") + charclass.parse("c") == conc.parse("(a|bd)c") assert pattern.parse("b|cd") + mult.parse("a{2,3}") == conc.parse("(b|cd)a{2,3}") assert pattern.parse("b|cd") + conc.parse("za{2,3}") == conc.parse("(b|cd)za{2,3}") assert pattern.parse("a|bc") + pattern.parse("c|de") == conc.parse("(a|bc)(c|de)")
def test_reduce_concatenations(): assert parse("aa").reduce() == mult.parse("a{2}") assert parse("bb").reduce() == mult.parse("b{2}") assert parse("b*b").reduce() == mult.parse("b+") assert parse("aa{2,}").reduce() == mult.parse("a{3,}") assert parse("a*a{2}").reduce() == mult.parse("a{2,}") assert parse("aa{0,8}").reduce() == mult.parse("a{1,9}") assert parse("b{0,8}b").reduce() == mult.parse("b{1,9}") assert parse("aab").reduce() == conc.parse("a{2}b") assert parse("abb").reduce() == conc.parse("ab{2}") assert parse("abb*").reduce() == conc.parse("ab+") assert parse("abbc").reduce() == conc.parse("ab{2}c") assert parse("a?ab").reduce() == conc.parse("a{1,2}b") assert parse("(ac{2}|bc+)c").reduce() == conc.parse("(ac|bc*)c{2}") assert parse("a(a{2}b|a+c)").reduce() == conc.parse("a{2}(ab|a*c)") assert parse("a{2,3}(a{2}b|a+c)").reduce() == conc.parse("a{3,4}(ab|a*c)") assert parse("(ba{2}|ca+)a{2,3}").reduce() == conc.parse("(ba|ca*)a{3,4}") assert parse("za{2,3}(a{2}b|a+c)").reduce() == conc.parse("za{3,4}(ab|a*c)") assert parse("(ba{2}|ca+)a{2,3}z").reduce() == conc.parse("(ba|ca*)a{3,4}z") assert parse("(a|bc)(a|bc)").reduce() == mult.parse("(a|bc){2}")
def test_pattern_generator(): gen = pattern.parse("[ab]|[cde]").strings() assert next(gen) == "a" assert next(gen) == "b" assert next(gen) == "c" assert next(gen) == "d" assert next(gen) == "e" try: next(gen) assert False except StopIteration: assert True # more complex gen = pattern.parse("abc|def(ghi|jkl)").strings() assert next(gen) == "abc" assert next(gen) == "defghi" assert next(gen) == "defjkl" gen = mult.parse("[0-9a-fA-F]{3,10}").strings() assert next(gen) == "000" assert next(gen) == "001" assert next(gen) == "002"
def test_conc_reduction_basic(): assert conc.parse("a").reduce() == charclass.parse("a") assert conc.parse("a{3,4}").reduce() == mult.parse("a{3,4}") assert conc.parse("ab").reduce() == conc.parse("ab") assert conc.parse("a[]b").reduce() == charclass.parse("[]")
def test_even_star_bug(): # Defect: (a{2})* should NOT reduce to a* a2 = mult.parse("a{2}") a2star = a2 * star assert a2star == mult.parse("(a{2})*")
def test_mult_common(): assert mult.parse("a{3,4}").common(mult.parse("a{2,5}")) == mult.parse("a{2,3}") assert mult.parse("a{2,}").common(mult.parse("a{1,5}")) == mult.parse("a{1,5}") assert mult.parse("a{3,}").common(mult.parse("a{2,}")) == mult.parse("a{2,}")
def test_charclass_multiplication(): assert charclass("a") * one == charclass("a") assert charclass("a") * multiplier.parse("{1,3}") == mult.parse("a{1,3}") assert charclass("a") * multiplier.parse("{4,}") == mult.parse("a{4,}")
def test_mult_multiplication(): assert mult.parse("a{2,3}") * one == mult.parse("a{2,3}") assert mult.parse("a{2,3}") * multiplier.parse("{4,5}") == mult.parse("a{8,15}") assert mult.parse("a{2,}") * multiplier.parse("{2,}") == mult.parse("a{4,}")
def test_mult_squoosh(): # sequence squooshing of mults within a conc assert conc.parse("[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]").reduce() == mult.parse("[0-9A-Fa-f]{3}") assert conc.parse("[$%\\^]?[$%\\^]").reduce() == mult.parse("[$%\\^]{1,2}") assert conc.parse("(|(|(|(|(|(|[$%\\^])[$%\\^])[$%\\^])[$%\\^])[$%\\^])[$%\\^])[$%\\^][$%\\^]").reduce() == mult.parse("[$%\\^]{2,8}")
def test_conc_multiplication(): assert conc.parse("ab?") * qm == mult.parse("(ab?)?")
def test_obvious_reduction(): assert mult.parse("(a|b)*").reduce() == mult.parse("[ab]*")
def test_mult_dock(): assert mult.parse("a{4,5}").dock(mult.parse("a{3}")) == mult.parse("a{1,2}")
def test_mult_intersection(): assert mult.parse("a") & mult.parse("b?") == charclass() assert mult.parse("a") & mult.parse("b?") == nothing assert mult.parse("a") & mult.parse("a?") == charclass.parse("a") assert mult.parse("a{2}") & mult.parse("a{2,}") == mult.parse("a{2}") assert mult.parse("a") & mult.parse("b") == charclass.parse("[]") assert mult.parse("a") & mult.parse("a") == charclass.parse("a") assert mult.parse("a*") & mult.parse("a") == charclass.parse("a") assert mult.parse("a*") & mult.parse("b*") == conc.parse("") assert mult.parse("a*") & mult.parse("a+") == mult.parse("a+") assert mult.parse("a{2}") & mult.parse("a{4}") == charclass.parse("[]") assert mult.parse("a{3,}") & mult.parse("a{3,}") == mult.parse("a{3,}")
def test_pattern_reduce_basic(): assert pattern.parse("ab|cd").reduce() == pattern.parse("ab|cd") assert pattern.parse("a{2}b{2}").reduce() == conc.parse("a{2}b{2}") assert pattern.parse("a{2}").reduce() == mult.parse("a{2}") assert pattern.parse("a").reduce() == charclass.parse("a")
def test_pattern_multiplication(): assert pattern.parse("ab?|ba?") * multiplier.parse("{2,3}") == mult.parse("(ab?|ba?){2,3}")
def test_epsilon_reduction(): assert parse("|(ab)*|def").reduce() == pattern.parse("(ab)*|def") assert parse("|(ab)+|def").reduce() == pattern.parse("(ab)*|def") assert parse("|.+").reduce() == mult.parse(".*") assert parse("|a+|b+").reduce() in {pattern.parse("a+|b*"), pattern.parse("a*|b+")}
def test_mult_squoosh(): # sequence squooshing of mults within a conc assert conc.parse("[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]").reduce() == mult.parse("[0-9A-Fa-f]{3}") assert conc.parse("[$%\\^]?[$%\\^]").reduce() == mult.parse("[$%\\^]{1,2}") assert conc.parse("(|(|(|(|(|(|[$%\^])[$%\^])[$%\^])[$%\^])[$%\^])[$%\^])[$%\^][$%\^]").reduce() == mult.parse("[$%\^]{2,8}")
def test_derive(): assert parse("a+").derive("a") == mult.parse("a*") assert parse("a+|b+").derive("a") == mult.parse("a*") assert parse("abc|ade").derive("a") == pattern.parse("bc|de") assert parse("abc|ade").derive("ab") == charclass.parse("c")
def test_concatenation(): # empty conc + empty conc assert conc.parse("") + conc.parse("") == conc.parse("") # charclass + charclass # a + b = ab assert charclass.parse("a") + charclass.parse("b") == conc.parse("ab") # a + a = a{2} assert (charclass.parse("a") + charclass.parse("a")).reduce() == mult.parse("a{2}") # charclass + mult # a + a = a{2} assert (charclass.parse("a") + mult.parse("a")).reduce() == mult.parse("a{2}") # a + a{2,} = a{3,} assert (charclass.parse("a") + mult.parse("a{2,}")).reduce() == mult.parse("a{3,}") # a + a{,8} = a{1,9} assert (charclass.parse("a") + mult.parse("a{0,8}")).reduce() == mult.parse("a{1,9}") # a + b{,8} = ab{,8} assert charclass.parse("a") + mult.parse("b{0,8}") == conc.parse("ab{0,8}") # mult + charclass # b + b = b{2} assert (mult.parse("b") + charclass.parse("b")).reduce() == mult.parse("b{2}") # b* + b = b+ assert (mult.parse("b*") + charclass.parse("b")).reduce() == mult.parse("b+") # b{,8} + b = b{1,9} assert (mult.parse("b{0,8}") + charclass.parse("b")).reduce() == mult.parse("b{1,9}") # b{,8} + c = b{,8}c assert mult.parse("b{0,8}") + charclass.parse("c") == conc.parse("b{0,8}c") # charclass + conc # a + nothing = a assert (charclass.parse("a") + conc.parse("")).reduce() == charclass.parse("a") # a + bc = abc assert charclass.parse("a") + conc.parse("bc") == conc.parse("abc") # a + ab = a{2}b assert (charclass.parse("a") + conc.parse("ab")).reduce() == conc.parse("a{2}b") # conc + charclass # nothing + a = a assert (conc.parse("") + charclass.parse("a")).reduce() == charclass.parse("a") # ab + c = abc assert conc.parse("ab") + charclass.parse("c") == conc.parse("abc") # ab + b = ab{2} assert (conc.parse("ab") + charclass.parse("b")).reduce() == conc.parse("ab{2}") # pattern + charclass # (a|bd) + c = (a|bd)c assert pattern.parse("a|bd") + charclass.parse("c") == conc.parse("(a|bd)c") # (ac{2}|bc+) + c = (ac|bc*)c{2} assert (pattern.parse("ac{2}|bc+") + charclass.parse("c")).reduce() == conc.parse("(ac|bc*)c{2}") # charclass + pattern # a + (b|cd) = a(b|cd) assert charclass.parse("a") + pattern.parse("b|cd") == conc.parse("a(b|cd)") # a + (a{2}b|a+c) = a{2}(ab|a*c) assert (charclass.parse("a") + pattern.parse("(a{2}b|a+c)")).reduce() == conc.parse("a{2}(ab|a*c)") # mult + mult # a{3,4} + b? = a{3,4}b? assert mult.parse("a{3,4}") + mult.parse("b?") == conc.parse("a{3,4}b?") # a* + a{2} = a{2,} assert (mult.parse("a*") + mult.parse("a{2}")).reduce() == mult.parse("a{2,}") # mult + conc # a{2} + bc = a{2}bc assert mult.parse("a{2}") + conc.parse("bc") == conc.parse("a{2}bc") # a? + ab = a{1,2}b assert (mult.parse("a?") + conc.parse("ab")).reduce() == conc.parse("a{1,2}b") # conc + mult # ab + c* = abc* assert conc.parse("ab") + mult.parse("c*") == conc.parse("abc*") # ab + b* = ab+ assert (conc.parse("ab") + mult.parse("b*")).reduce() == conc.parse("ab+") # mult + pattern # a{2,3} + (b|cd) = a{2,3}(b|cd) assert mult.parse("a{2,3}") + pattern.parse("b|cd") == conc.parse("a{2,3}(b|cd)") # a{2,3} + (a{2}b|a+c) = a{3,4}(ab|a*c) assert (mult.parse("a{2,3}") + pattern.parse("a{2}b|a+c")).reduce() == conc.parse("a{3,4}(ab|a*c)") # pattern + mult # (b|cd) + a{2,3} = (b|cd)a{2,3} assert pattern.parse("b|cd") + mult.parse("a{2,3}") == conc.parse("(b|cd)a{2,3}") # (ba{2}|ca+) + a{2,3} = (ba|ca*)a{3,4} assert (pattern.parse("ba{2}|ca+") + mult.parse("a{2,3}")).reduce() == conc.parse("(ba|ca*)a{3,4}") # conc + conc # ab + cd = abcd assert conc.parse("ab") + conc.parse("cd") == conc.parse("abcd") # ab + bc = ab{2}c assert (conc.parse("ab") + conc.parse("bc")).reduce() == conc.parse("ab{2}c") # conc + pattern # za{2,3} + (b|cd) = za{2,3}(b|cd) assert conc.parse("za{2,3}") + pattern.parse("b|cd") == conc.parse("za{2,3}(b|cd)") # za{2,3} + (a{2}b|a+c) = za{3,4}(ab|a*c) assert (conc.parse("za{2,3}") + pattern.parse("a{2}b|a+c")).reduce() == conc.parse("za{3,4}(ab|a*c)") # pattern + conc # (b|cd) + za{2,3} = (b|cd)za{2,3} assert pattern.parse("b|cd") + conc.parse("za{2,3}") == conc.parse("(b|cd)za{2,3}") # (ba{2}|ca+) + a{2,3}z = (ba|ca*)a{3,4}z assert (pattern.parse("ba{2}|ca+") + conc.parse("a{2,3}z")).reduce() == conc.parse("(ba|ca*)a{3,4}z") # pattern + pattern # (a|bc) + (c|de) = (a|bc)(c|de) assert pattern.parse("a|bc") + pattern.parse("c|de") == conc.parse("(a|bc)(c|de)") # (a|bc) + (a|bc) = (a|bc){2} assert (pattern.parse("a|bc") + pattern.parse("a|bc")).reduce() == mult.parse("(a|bc){2}")
def test_two_or_more_qm_bug(): assert mult.parse("a{2,}") * qm == mult.parse("(a{2,})?")
def test_empty_pattern_suppression(): assert mult.parse("[]?").reduce() == conc.parse("") assert mult(pattern(), qm).reduce() == conc.parse("")
def test_two_two_bug(): assert mult.parse("a{2}") * multiplier.parse("{2}") == mult.parse("a{4}")