Пример #1
0
 def test_simple(self):
     r = NFSM("ABC", 3, "ABC")
     self.assert_no_references(r)
     self.assertTrue(r.match("ABC"))
     self.assertFalse(r.match("CBA"))
     self.assertFalse(r.match("ABCD"))
     self.assertFalse(r.match("AABC"))
Пример #2
0
    def test_or_peek_and_constraint(self):
        r = NFSM("AB|BC", 2, "ABC")
        self.assert_no_references(r)

        self.assertEqual(set("AB"), r.peek_slot(0))
        self.assertEqual(set("BC"), r.peek_slot(1))

        r.constrain_slot(0, set("AC"))

        self.assertEqual(set("A"), r.peek_slot(0))
        self.assertEqual(set("B"), r.peek_slot(1))
Пример #3
0
    def test_simple_constraints(self):
        r = NFSM("...", 3, "ABC")
        self.assert_no_references(r)

        self.assertTrue(r.match("AAA"))
        self.assertTrue(r.match("ABC"))

        r.constrain_slot(1, set("AB"))
        self.assertTrue(r.match("AAA"))
        self.assertTrue(r.match("CBC"))
        self.assertFalse(r.match("ACA"))
        self.assertFalse(r.match("BCA"))

        r.constrain_slot(0, set("C"))
        self.assertTrue(r.match("CAB"))
        self.assertFalse(r.match("BAB"))
Пример #4
0
    def test_multi_or(self):
        r = NFSM("(DI|NS|TH|OM)*", 8, string.ascii_uppercase)
        self.assert_no_references(r)

        self.assertTrue(r.match("DIDIDIDI"))
        self.assertTrue(r.match("DINSTHOM"))
        self.assertTrue(r.match("OMTHNSDI"))

        self.assertFalse(r.match("ADINSTHOZ"))
        self.assertFalse(r.match("ZZZZZZZZ"))

        r.constrain_slot(0, set("DZ"))

        self.assertTrue(r.match("DIDIDIDI"))
        self.assertTrue(r.match("DINSTHOM"))
        self.assertFalse(r.match("OMTHNSDI"))
        self.assertFalse(r.match("ZINSTHOM"))
Пример #5
0
    def _compare(self, regex_str, length, alphabet):
        myr = NFSM(regex_str, length, alphabet)
        realr = re.compile(regex_str+"$")

        for s in ("".join(x) for x in product(alphabet, repeat=length)):
            self.assertEqual(bool(realr.match(s)), myr.match(s), msg=s)
Пример #6
0
    def test_simple_peek(self):
        r = NFSM("[ABC][AB]", 2, "ABC")
        self.assert_no_references(r)

        self.assertEqual(set("ABC"), r.peek_slot(0))
        self.assertEqual(set("AB"), r.peek_slot(1))
Пример #7
0
    def test_backref_simple(self):
        r = NFSM("(.)\\1", 2, "ABC")
        self.assertTrue(r.match("AA"))
        self.assertTrue(r.match("CC"))
        self.assertFalse(r.match("AB"))

        r.constrain_slot(0, set("AB"))

        self.assertTrue(r.match("AA"))
        self.assertTrue(r.match("BB"))
        self.assertFalse(r.match("CC"))

        r.constrain_slot(1, set("BC"))

        self.assertTrue(r.match("BB"))
        self.assertFalse(r.match("AA"))
        self.assertFalse(r.match("CC"))
Пример #8
0
    def test_or_star(self):
        r = NFSM("(RR|HHH)*.?", 10, string.ascii_uppercase)
        self.assert_no_references(r)

        self.assertTrue(r.match("RRRRRRRRRR"))
        self.assertTrue(r.match("RRRRRRHHHA"))
        self.assertTrue(r.match("RRRRRRHHHR"))
        self.assertTrue(r.match("RRRRRRHHHH"))
        self.assertTrue(r.match("HHHHHHRRRR"))
        self.assertTrue(r.match("RRHHHRRHHH"))
        self.assertTrue(r.match("HHHRRRRRRZ"))

        self.assertFalse(r.match("RHHHHHHHHH"))
        self.assertFalse(r.match("HHHRRRRRR"))
        self.assertFalse(r.match("HHHHHHHHHRR"))
        self.assertFalse(r.match("HHRRRRRRRR"))
        self.assertFalse(r.match("RRRRRRRRRRZ"))
        self.assertFalse(r.match("RRRRRRRRRRH"))

        r.constrain_slot(2, set("H"))

        self.assertTrue(r.match("RRHHHHHHRR"))
        self.assertTrue(r.match("HHHRRRRHHH"))
        self.assertTrue(r.match("HHHHHHHHHZ"))
        self.assertFalse(r.match("RRRRHHHHHH"))
        self.assertFalse(r.match("RRRRRRHHHZ"))

        r.constrain_slot(2, set("R"))

        self.assertFalse(r.match("HHHHHHHHHZ"))
        self.assertFalse(r.match("RRRRHHHHHH"))
        self.assertFalse(r.match("RRRRRRHHHH"))
        self.assertFalse(r.match("HHHHHHRRRR"))
Пример #9
0
    def test_complex(self):
        r = NFSM("F.*[AO].*[AO].*", 9, string.ascii_uppercase)
        self.assert_no_references(r)
        # A few positive matches
        self.assertTrue(r.match("FBCODEAFG"))
        self.assertTrue(r.match("FBOCDEAFG"))
        self.assertTrue(r.match("FBCADEAFG"))
        self.assertTrue(r.match("FBCADEFOG"))
        self.assertTrue(r.match("FODEFOGHI"))
        self.assertTrue(r.match("FBCAAEFOG"))
        self.assertTrue(r.match("FBCAOEFHG"))

        # A few negative matches
        self.assertFalse(r.match("ABCODEAFG"))
        self.assertFalse(r.match("FBZCDEAFG"))
        self.assertFalse(r.match("FBCABEZFG"))
        self.assertFalse(r.match("FZZZZZZZZ"))

        r.constrain_slot(1, set("AO"))
        
        self.assertFalse(r.match("FBCODEAFG"))
        self.assertFalse(r.match("FBOCDEAFG"))
        self.assertFalse(r.match("FBCADEAFG"))
        self.assertFalse(r.match("FBCADEFOG"))
        self.assertTrue(r.match("FODEFOGHI"))
        self.assertFalse(r.match("FBCAAEFOG"))
        self.assertFalse(r.match("FBCAOEFHG"))

        self.assertFalse(r.match("ABCODEAFG"))
        self.assertFalse(r.match("FBZCDEAFG"))
        self.assertFalse(r.match("FBCAOEZFG"))
        self.assertFalse(r.match("FZZZZZZZZ"))