def test_get_trans_num(self): """get_trans_num()""" # Simple regression test for small automaton. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.get_trans_num(), 3) # Test after removing fallback transitions aut.enable_fallback_state(1, warning=False) aut.remove_fallback_transitions() self.assertEqual(aut.get_trans_num(), 2)
def _test_compute2(self): delay_dfa = DELAY_DFA() parser = pcre_parser() parser.set_text("/^(a|b)+/") delay_dfa.create_by_parser(parser) delay_dfa.compute() self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a","a",0)) b.add_symbols(b_Sym_char("b","b",1)) b.add_symbols(DEF_SYMBOLS("default", 2)) b.add_states(b_State(0,set())) b.add_states(b_State(1,set([0]))) b.start = 0 b.final = set([1]) b.add_transitions( (0,0,1) ) b.add_transitions( (0,1,1) ) b.add_transitions( (1,2,0) ) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def test_report_memory_naive(self): """report_memory_naive()""" # Simple regression test for small automaton. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.report_memory_naive(), 12) # Test after removing fallback transitions. report_memory_naive depends # on number of states and symbols, not transitions, so nothing changes aut.enable_fallback_state(1, warning=False) aut.remove_fallback_transitions() self.assertEqual(aut.report_memory_naive(), 12) # Manually remove symbol and state from _automaton1 del aut._automaton1.states[2] del aut._automaton1.alphabet[2] self.assertEqual(aut.report_memory_naive(), 6)
def test_compute_join(self): """compute_join()""" # Check whther _rnum returned object contains the values of the two # _rnum b_State() class object. state_one = b_State(rnum = set([0])) state_two = b_State(rnum = set([1])) self.assertTrue((state_one.compute_join(state_two))._rnum == set([0,1]))
def test_get_alpha_num(self): """get_alpha_num()""" # Simple regression test for small automaton. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.get_alpha_num(), 3) # Manually remove symbol from _automaton1 del aut._automaton1.alphabet[2] self.assertEqual(aut.get_alpha_num(), 2)
def test_disable_fallback_state(self): """disable_fallback_state()""" # Test if the variables _compute, fallback and fallback_state were set # to the default values. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.enable_fallback_state(warning=False) aut.compute() aut.disable_fallback_state() self.assertFalse(aut.get_compute()) self.assertFalse(aut.fallback) self.assertEqual(aut.fallback_state, -1)
def _test_compute2(self): delay_dfa = DELAY_DFA() parser = pcre_parser() parser.set_text("/^(a|b)+/") delay_dfa.create_by_parser(parser) delay_dfa.compute() self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a", "a", 0)) b.add_symbols(b_Sym_char("b", "b", 1)) b.add_symbols(DEF_SYMBOLS("default", 2)) b.add_states(b_State(0, set())) b.add_states(b_State(1, set([0]))) b.start = 0 b.final = set([1]) b.add_transitions((0, 0, 1)) b.add_transitions((0, 1, 1)) b.add_transitions((1, 2, 0)) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def test_is_final(self): """is_final()""" # If len(self._rnum) == 0, then returns False, otherwise it returns # True. state = b_State() self.assertTrue(state.is_final() == False) final_state = b_State(15, set([0])) self.assertTrue(final_state.is_final() == True)
def test_enable_fallback_state(self): """enable_fallback_state()""" # Test if fallback and fallback_state is set accordingly, _compute is # set to False and warning is/is not printed on stdout depending on # value of parameter warning. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() # redirect stdout to file tmp = sys.stdout f = open("stdout.output", 'w') sys.stdout = f aut.enable_fallback_state(2, warning=False) f.close() e = open("stdout.output", 'r') line = e.readline() # warning was set to False, stdout should be empty self.assertFalse(line) # check if the fallback_state was set self.assertEqual(aut.fallback_state, 2) self.assertFalse(aut.get_compute()) self.assertTrue(aut.fallback) f = open("stdout.output", 'w') sys.stdout = f aut.enable_fallback_state() f.close() e = open("stdout.output", 'r') line = e.readline() # warning should be printed by default self.assertTrue(line) # check if the fallback_state was chosen correctly self.assertEqual(aut.fallback_state, 1) self.assertFalse(aut.get_compute()) self.assertTrue(aut.fallback) # restore sys.stdout sys.stdout = tmp os.remove("stdout.output")
def test___str__(self): """__str__()""" # determine whether the returned string corresponding str(self._id), # call the object function str(object) state = b_State() self.assertTrue(state.__str__() == "0") state = b_State(mid = 15) self.assertTrue(state.__str__() == "15")
def test_get_regexp_number(self): """get_regexp_number()""" # Check whether the returned value is self._rnum. state = b_State() self.assertTrue(state.get_regexp_number() == set()) self.assertTrue(state.get_regexp_number() == state._rnum) state = b_State(1, set([0])) self.assertTrue(state.get_regexp_number() == set([0])) self.assertTrue(state.get_regexp_number() == state._rnum)
def test___repr__(self): """__repr__()""" # determine whether the returned string corresponding to # "<" + str(self._id) + ", " + str(self._rnum) + ">", # call the object function repr(object) state = b_State() self.assertTrue(state.__repr__() == "<0, set([])>") state = b_State(mid = 15, rnum = set([0,1])) self.assertTrue(state.__repr__() == "<15, set([0, 1])>")
def test_validate_transition(self): """validate_transition()""" # Test correct transition validation for both faulty and non-faulty # transition table. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() for t in aut._automaton1.transitions: # all transitions must be valid self.assertTrue(aut.validate_transition(aut._transition_rep(t))) # some nonexistent transitions -> invalid t = (0,2,0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (1,0,2) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (len(aut._automaton1.states), len(aut._automaton1.alphabet), 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (0, len(aut._automaton1.alphabet), 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (len(aut._automaton1.states), 0, 0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) # faulty transitions aut.enable_faulty_transitions(32) aut.compute() for t in aut._automaton1.transitions: # all transitions must be valid self.assertTrue(aut.validate_transition(aut._transition_rep(t))) # some nonexistent transitions -> invalid, collisions are improbable t = (0,2,0) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (1,0,2) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (10,10,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (11,11,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t))) t = (12,12,1) self.assertFalse(aut.validate_transition(aut._transition_rep(t)))
def test_get_type(self): """get_type()""" # Check whether the returned value is self.ctype and if equal # b_state.types["b_State"]. state = b_State() self.assertTrue(state.get_type() == state.ctype and state.get_type() == types["b_State"]) state = b_State() state.ctype = types["ColouredState"] state.stypes = [types["ColouredState"]] self.assertTrue(state.get_type() == state.ctype and state.get_type() == types["ColouredState"])
def test_get_support_type(self): """get_support_type()""" # Check whether the returned value is self.stypes and if it is equal to # [b_state.types ["b_State"]]. state = b_State() self.assertTrue(state.get_support_type() == state.stypes and state.get_support_type() == [types["b_State"]]) state = b_State() state.ctype = types["ColouredState"] state.stypes = [types["ColouredState"]] self.assertTrue(state.get_support_type() == state.stypes and state.get_support_type() == [types["ColouredState"]])
def test___ne__(self): """__ne__()""" # Test using the operator != # Test all code branches. # - Check the situation when compared to able to solve the self. # - Check the situation when compared to able to solve other. # - Check the situation when the comparison is not able to solve # even the self or other - check thrown symbol_equality_exception. # - Check a situation where the self or the Other is of a # different type than the inherit from b_symbol -> returns True. symbol = b_Sym_char("symbol", 'a', 1) comp_symbol = b_Sym_char("comp_symbol", 'b', 2) self.assertTrue(symbol != comp_symbol) self.assertTrue(comp_symbol != symbol) symbol.ctype = '4' comp_symbol.ctype = '4' try: symbol != comp_symbol self.assertTrue(False) except symbol_equality_exception: self.assertTrue(True) symbol = b_Sym_char("symbol", 'a', 1) state = b_State(0, set([])) self.assertTrue(symbol != state)
def _test_compute3(self): # Get test directory tdir = aux_func.getPatternMatchDir() + "/algorithms/delay_dfa/" delay_dfa = DELAY_DFA() nfaData = nfa_data().load_from_file(tdir + "test_data/text_ddfa.nfa_data") delay_dfa.create_from_nfa_data(nfaData) delay_dfa.determinise() delay_dfa.compute(False) self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a", "a", 0)) b.add_symbols(b_Sym_char("b", "b", 1)) b.add_symbols(b_Sym_char("c", "c", 2)) b.add_symbols(b_Sym_char("d", "d", 3)) b.add_symbols(DEF_SYMBOLS("default", 4)) b.add_states(b_State(0, set())) b.add_states(b_State(1, set([0]))) b.add_states(b_State(2, set())) b.add_states(b_State(3, set([0]))) b.add_states(b_State(4, set([0]))) b.start = 0 b.final = set([1, 3, 4]) b.add_transitions((0, 2, 0)) b.add_transitions((0, 0, 1)) b.add_transitions((0, 1, 2)) b.add_transitions((0, 3, 3)) b.add_transitions((1, 4, 0)) b.add_transitions((2, 2, 4)) b.add_transitions((2, 4, 0)) b.add_transitions((3, 4, 0)) b.add_transitions((4, 4, 0)) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def _test_compute3(self): # Get test directory tdir = aux_func.getPatternMatchDir() + "/algorithms/delay_dfa/" delay_dfa = DELAY_DFA() nfaData = nfa_data().load_from_file(tdir + "test_data/text_ddfa.nfa_data") delay_dfa.create_from_nfa_data(nfaData) delay_dfa.determinise() delay_dfa.compute(False) self.assertTrue(delay_dfa.get_compute()) a = delay_dfa.get_automaton() b = nfa_data() b.add_symbols(b_Sym_char("a","a",0)) b.add_symbols(b_Sym_char("b","b",1)) b.add_symbols(b_Sym_char("c","c",2)) b.add_symbols(b_Sym_char("d","d",3)) b.add_symbols(DEF_SYMBOLS("default", 4)) b.add_states(b_State(0,set())) b.add_states(b_State(1,set([0]))) b.add_states(b_State(2,set())) b.add_states(b_State(3,set([0]))) b.add_states(b_State(4,set([0]))) b.start = 0 b.final = set([1,3,4]) b.add_transitions( (0,2,0) ) b.add_transitions( (0,0,1) ) b.add_transitions( (0,1,2) ) b.add_transitions( (0,3,3) ) b.add_transitions( (1,4,0) ) b.add_transitions( (2,2,4) ) b.add_transitions( (2,4,0) ) b.add_transitions( (3,4,0) ) b.add_transitions( (4,4,0) ) self.assertEqual(a.states.keys(), b.states.keys()) self.assertEqual(a.start, b.start) self.assertEqual(a.final, b.final) self.assertEqual(a.alphabet, b.alphabet) self.assertEqual(a.transitions, b.transitions) self.assertTrue(a.Flags["Delay DFA"])
def test__identify_fading_states(self): """_identify_fading_states(nfa_closure_states)""" history = HistoryFA() history._state_representation = [ set([0]), set([0,1]), set([0,2]), set([0,3]), set([0,4]), set([0,5]), set([0,6]), set([0,2,4]), set([0,2,5]), set([0,2,6]) ] self.assertTrue(history._identify_fading_states([2]) == [2, 7, 8, 9]) act = nfa_data() act.states[0] = b_State(0,set()) act.states[1] = b_State(1,set()) act.states[2] = b_State(2,set()) act.states[3] = b_State(3,set([0])) act.states[4] = b_State(4,set()) act.states[5] = b_State(5,set()) act.states[6] = b_State(6,set([1])) act.alphabet[0] = b_Sym_char("a", "a", 0) act.alphabet[1] = b_Sym_char("b", "b", 1) act.alphabet[2] = b_Sym_char("c", "c", 2) act.alphabet[3] = b_Sym_char("d", "d", 3) act.alphabet[4] = b_Sym_char("e", "e", 4) act.alphabet[5] = b_Sym_char("f", "f", 5) star = set() for ord_char in range(0, 256): star.add(chr(ord_char)) act.alphabet[6] = b_Sym_char_class("*", star, 6) mimo_a = set() for ord_char in range(0, 256): mimo_a.add(chr(ord_char)) mimo_a.remove('a') act.alphabet[7] = b_Sym_char_class("^a", mimo_a, 7) act.start = 0 act.final.add(3) act.final.add(6) act.transitions.add( (0, 6, 0) ) act.transitions.add( (0, 0, 1) ) act.transitions.add( (1, 1, 2) ) act.transitions.add( (2, 7, 2) ) act.transitions.add( (2, 2, 3) ) act.transitions.add( (0, 3, 4) ) act.transitions.add( (4, 4, 5) ) act.transitions.add( (5, 5, 6) ) history = HistoryFA() history._automaton = act history.remove_epsilons() NFA = history.get_automaton(True) history.determinise(create_table = True) nfa_closure_states = history._discover_closure_states(NFA) self.assertTrue(history._identify_fading_states(nfa_closure_states) == [5, 7, 8, 9])
def test_report_memory_real(self): """report_memory_real()""" # Few simple regression tests for different sizes of PHF table, state # and symbol representations and faulty transitions. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(8) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.report_memory_real(), 120) aut.set_table_parameters((4,6)) self.assertEqual(aut.report_memory_real(), 48) aut.set_table_parameters((4,7)) self.assertEqual(aut.report_memory_real(), 72) a.set_limit(5) aut.set_PHF_class(a) aut.compute() self.assertEqual(aut.report_memory_real(), 45) aut.enable_faulty_transitions(10) self.assertEqual(aut.report_memory_real(), 30) aut.enable_faulty_transitions(19) self.assertEqual(aut.report_memory_real(), 60)
def test__discover_closure_states(self): """_discover_closure_states(NFA)""" act = nfa_data() act.states[0] = b_State(0,set()) act.states[1] = b_State(1,set()) act.states[2] = b_State(2,set()) act.states[3] = b_State(3,set([0])) act.states[4] = b_State(4,set()) act.states[5] = b_State(5,set()) act.states[6] = b_State(6,set([1])) act.alphabet[0] = b_Sym_char("a", "a", 0) act.alphabet[1] = b_Sym_char("b", "b", 1) act.alphabet[2] = b_Sym_char("c", "c", 2) act.alphabet[3] = b_Sym_char("d", "d", 3) act.alphabet[4] = b_Sym_char("e", "e", 4) act.alphabet[5] = b_Sym_char("f", "f", 5) star = set() for ord_char in range(0, 256): star.add(chr(ord_char)) act.alphabet[6] = b_Sym_char_class("*", star, 6) mimo_a = set() for ord_char in range(0, 256): mimo_a.add(chr(ord_char)) mimo_a.remove('a') act.alphabet[7] = b_Sym_char_class("^a", mimo_a, 7) act.start = 0 act.final.add(3) act.final.add(6) act.transitions.add( (0, 6, 0) ) act.transitions.add( (0, 0, 1) ) act.transitions.add( (1, 1, 2) ) act.transitions.add( (2, 7, 2) ) act.transitions.add( (2, 2, 3) ) act.transitions.add( (0, 3, 4) ) act.transitions.add( (4, 4, 5) ) act.transitions.add( (5, 5, 6) ) history = HistoryFA() history._automaton = act history.remove_epsilons() NFA = history.get_automaton(True) self.assertTrue(history._discover_closure_states(NFA) == [2])
def test_remove_fallback_transitions(self): """remove_fallback_transitions()""" # 1. /abc/, state -1 (automatically chosen 0) - 4 transitions removed nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (0,1,0) ) nfaData.transitions.add( (0,2,0) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (1,0,1) ) nfaData.transitions.add( (1,2,0) ) nfaData.transitions.add( (2,2,3) ) nfaData.transitions.add( (2,0,1) ) nfaData.transitions.add( (2,1,0) ) nfaData.transitions.add( (3,0,3) ) nfaData.transitions.add( (3,1,3) ) nfaData.transitions.add( (3,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.enable_fallback_state(warning=False) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertEqual(len(cp.transitions), 8) # 4 removed transitions for i in cp.transitions: # no transitions to fallback_state self.assertNotEqual(i[2], aut.fallback_state) self.assertEqual(len(cp.final), len(result.final)) # 2. /abc/, state 1 - 3 transitions removed aut._automaton1 = aut._automaton aut.enable_fallback_state(1, False) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertEqual(len(cp.transitions), 9) # 3 removed transitions for i in cp.transitions: # no transitions to fallback_state self.assertNotEqual(i[2], aut.fallback_state) self.assertEqual(len(cp.final), len(result.final)) # 3. /^abc/, state 0 - automaton does not change nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.enable_fallback_state(0, warning=False) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertEqual(len(cp.transitions), len(result.transitions)) for i in cp.transitions: # no transitions to fallback_state self.assertNotEqual(i[2], aut.fallback_state) self.assertEqual(len(cp.final), len(result.final))
def test_get_text(self): """get_text()""" # Check whether the returned value is equal to str(self._id). state = b_State(1, set([1])) self.assertTrue(state.get_text() == "1")
def test_get_id(self): """get_id()""" # Check whether the returned value is self._id. state = b_State(1, set([1])) self.assertTrue(state.get_id() == 1)
def test_set_id(self): """set_id()""" # Check whether the adjustable self._id properly. state = b_State(1, set([1])) state.set_id(15) self.assertTrue(state._id == 15)
def test_set_regexp_number(self): """set_regexp_number()""" # Check whether the adjustable self._rnum properly. state = b_State() state.set_regexp_number(set([1])) self.assertTrue(state._rnum == set([1]))
def test_compute(self): """compute()""" # 1. /^abc/ - automaton does not change, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertEqual(len(cp.transitions), len(result.transitions)) self.assertEqual(len(cp.final), len(result.final)) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 2. determinization of /^ab|ac/, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set([0])) nfaData.states[3] = b_State(3,set()) nfaData.states[4] = b_State(4,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (0,0,3) ) nfaData.transitions.add( (3,2,4) ) nfaData.final.add(2) nfaData.final.add(4) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), 3) self.assertEqual(len(cp.alphabet), 3) self.assertEqual(len(cp.transitions), 3) self.assertEqual(len(cp.final), 1) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 3. resolve alphabet - /^[a-c][b-d]/, PHF table is created nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set([0])) nfaData.alphabet[0] = b_Sym_char_class("ch0", set(['a', 'b', 'c']), 0) nfaData.alphabet[1] = b_Sym_char_class("ch1", set(['b', 'c', 'd']), 1) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.final.add(2) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), 3) self.assertEqual(len(cp.alphabet), 3) self.assertEqual(len(cp.transitions), 4) self.assertEqual(len(cp.final), 1) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute()) # 4. /abc/ and enable_fallback_state - some transitions are removed nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (0,1,0) ) nfaData.transitions.add( (0,2,0) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (1,0,1) ) nfaData.transitions.add( (1,2,0) ) nfaData.transitions.add( (2,2,3) ) nfaData.transitions.add( (2,0,1) ) nfaData.transitions.add( (2,1,0) ) nfaData.transitions.add( (3,0,3) ) nfaData.transitions.add( (3,1,3) ) nfaData.transitions.add( (3,2,3) ) nfaData.final.add(3) result = copy.deepcopy(nfaData) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.enable_fallback_state(warning=False) aut.compute() cp = aut._automaton1 self.assertEqual(len(cp.states), len(result.states)) self.assertEqual(len(cp.alphabet), len(result.alphabet)) self.assertTrue(len(cp.transitions) < len(result.transitions)) self.assertEqual(len(cp.final), len(result.final)) self.assertNotEqual(aut.trans_table, None) self.assertTrue(aut.get_compute())
def compute(self): """ Computes Hybrid automaton. """ b_Automaton.compute(self) self._compute = False # Automaton doesn't have any state = automaton is empty if self._automaton.is_empty() or self._automaton.start < 0: return # Make epsilon free automaton self.remove_epsilons() # save input NFA for tails NFA parts self._nfaEpsFree = b_nfa() self._nfaEpsFree.create_from_nfa_data(self._automaton, True) counter = 0 stack = list() newStates = dict() newStatesRev = dict() tmp = set() tmp.add(self._automaton.start) newStates[counter] = tmp newStatesRev[frozenset(tmp)] = counter stack.append(counter) counter += 1 final = set() transitions = set() alphCounter = 0 alphabet = dict() alphabetRev = dict() states = dict() states[0] = b_State(0, self._automaton.states[self._automaton.start].get_regexp_number()) borders = dict() noSpecials = set() # transtions from each state for transition in self._automaton.transitions: self._stateTrans.setdefault(transition[0], set()).add((transition[1], transition[2])) # Set depth for each state self._setDepth() # copy alphabet, ID's 0,1,... mapId = dict() # maps old id -> new id for id, sym in self._automaton.alphabet.iteritems(): sym.set_id(alphCounter) alphabet[alphCounter] = sym alphabetRev[sym] = alphCounter mapId[id] = alphCounter alphCounter += 1 while stack: actState = stack.pop() if newStates[actState].intersection(self._automaton.final): final.add(actState) for fromState in newStates[actState]: if self._is_special(fromState): # marked current DFA state as special and set borders.setdefault(actState, set()).add(fromState) else: noSpecials.add(fromState) # transitions from actual state for each symbol outSymbols = dict() # (symbol id, set of states id) for state in newStates[actState]: if state not in self._stateTrans: continue if actState in borders and state in borders[actState]: continue for t in self._stateTrans[state]: outSymbols.setdefault(mapId[t[0]], set()).add(t[1]) # resolve symbol collisions symbolAdded = True while symbolAdded: symbolAdded = False for sym1 in list(outSymbols.keys()): toCompare = list(outSymbols.keys()) toCompare.remove(sym1) for sym2 in toCompare: if not (outSymbols[sym1] and outSymbols[sym2]): continue # no next state for one of the symbols if not alphabet[sym1].collision([alphabet[sym2]]): continue #print "COLLISION DETECTED" symStates = list([[]] * 3) symStates[0] = outSymbols[sym1] symStates[2] = outSymbols[sym2] symStates[1] = symStates[0] | symStates[2] outSymbols[sym1] = set() outSymbols[sym2] = set() ret = alphabet[sym1].resolve_collision(alphabet[sym2]) for i in range(3): if not ret[i]: # no symbol returned continue for new in ret[i]: symbolAdded = True if new not in alphabetRev.keys(): # add new symbol new.set_id(alphCounter) alphabet[alphCounter] = new alphabetRev[new] = alphCounter id = alphCounter alphCounter += 1 else: id = alphabetRev[new] # update next states for symbol tmp = outSymbols.setdefault(id, set()) outSymbols[id] = tmp | symStates[i] self._head_size += len(outSymbols) # create new transitions for symbol, nextState in outSymbols.iteritems(): if not nextState: continue # no next states -> ignore symbol if frozenset(nextState) not in newStatesRev: # create a new state newStatesRev[frozenset(nextState)] = counter newStates[counter] = nextState stack.append(counter) endVal = set() # set of regular expression numbres for state in nextState: if self._automaton.states[state].is_final() == True: endVal |= self._automaton.states[state].get_regexp_number() states[counter] = b_State(counter, endVal) counter = counter + 1 transitions.add((actState, symbol, newStatesRev[frozenset(nextState)])) # remove unused symbols toRemove = alphabet.keys() for trans in transitions: if trans[1] in toRemove: toRemove.remove(trans[1]) self._automaton.alphabet = alphabet self._automaton.remove_symbols(toRemove) # set new symbol ID's mapId = dict() # maps old id -> new id alphCounter = 0 alphabet = dict() for id, sym in self._automaton.alphabet.iteritems(): sym.set_id(alphCounter) alphabet[alphCounter] = sym mapId[id] = alphCounter alphCounter += 1 # correct symbol ID's in transitions newTrans = set() for trans in transitions: newTrans.add((trans[0], mapId[trans[1]], trans[2])) # create head DFA self.dfa.set_multilanguage(self.get_multilanguage()) self.dfa._automaton.start = 0 self.dfa._automaton.alphabet = alphabet self.dfa._automaton.states = states self.dfa._automaton.transitions = newTrans self.dfa._automaton.final = final self.dfa._automaton.Flags["Hybrid FA - DFA part"] = True self.dfa._automaton.Flags["Deterministic"] = True self.dfa._automaton.Flags["Epsilon free"] = True self.dfa._automaton.Flags["Alphabet collision free"] = True nfaPosition = 0 for dfaState, nfaStates in borders.iteritems(): for nfaState in nfaStates: self.nfas[nfaPosition] = b_nfa() self.nfas[nfaPosition].create_from_nfa_data(self._nfaEpsFree.get_automaton(False), True) self.nfas[nfaPosition]._automaton.start = nfaState self.nfas[nfaPosition]._automaton.Flags["Hybrid FA - one NFA part"] = True self.nfas[nfaPosition].remove_unreachable() self.tran_aut[nfaPosition] = dfaState nfaPosition += 1 self._automaton = b_nfa() a = self.dfa.get_automaton(False) # shortcut cur_state = a.start # sort transitions for s in range(0, len(a.states)): self._sort[s] = [] for t in a.transitions: self._sort[t[0]].append(t[1:]) self._compute = True
def test_join(self): """join()""" # Check whether _rnum of returned object contains values of # two _rnum b_State() class objects. # Create a class object ColouredState and try whether there would # be successful united in cases: # - the self is ColouredState # - other is ColouredState # SAME CODE AS IN "test_b_state.py" # check join two b_State - self join other self_state = b_State() other_state = b_State() self.assertTrue((self_state.join(other_state))._rnum == set()) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = b_State() other_state = b_State() other_state._rnum = set([0]) self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() other_state._rnum = set([1]) self.assertTrue((self_state.join(other_state))._rnum == set([0,1])) # check join two b_State - other join self self_state = b_State() other_state = b_State() self.assertTrue((other_state.join(self_state))._rnum == set()) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = b_State() other_state = b_State() other_state._rnum = set([0]) self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() other_state._rnum = set([1]) self.assertTrue((other_state.join(self_state))._rnum == set([0,1])) # self is ColouredState class and other is b_State class self_state = ColouredState(0, set([11]), set([1])) other_state = b_State(1, set([15])) self.assertTrue((self_state.join(other_state))._rnum == set([11, 15])) self_state = ColouredState(2, set([14]), set([1])) other_state = b_State(3, set([2, 3])) self.assertTrue((other_state.join(self_state))._rnum == set([2,3,14])) # try error self_state.stypes = [] try : self_state.join(other_state) self.assertTrue(False) except state_join_exception: self.assertTrue(True) # ColouredState - self join other self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set()) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set([0, 1])) # ColouredState - other join self self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set()) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([1])) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([0, 1]))
def test_generate_PHF_table(self): """generate_PHF_table()""" # Test of PHF table generation - the right size of tabel, every # transition is exactly once in the table and on the right index. nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (0,1,0) ) nfaData.transitions.add( (0,2,0) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (1,0,1) ) nfaData.transitions.add( (1,2,0) ) nfaData.transitions.add( (2,2,3) ) nfaData.transitions.add( (2,0,1) ) nfaData.transitions.add( (2,1,0) ) nfaData.transitions.add( (3,0,3) ) nfaData.transitions.add( (3,1,3) ) nfaData.transitions.add( (3,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut._automaton1 = nfaData aut.generate_PHF_table() # transition table size self.assertEqual(aut.ran, len(aut.trans_table)) self.assertEqual(aut.ran, 384) # count number of unique lines in transition table tranCount = dict() for l in aut.trans_table: tranCount.setdefault(l[1], 0) tranCount[l[1]] += 1 # test if every automaton transition is just once in the table for t in aut._automaton1.transitions: self.assertEqual(tranCount[aut._transition_rep(t)], 1) t = ([2 ** aut.state_bits - 1, 2 ** aut.symbol_bits - 1, 0]) # rest of trans are the nonexistent transitions self.assertEqual(tranCount[aut._transition_rep(t)], aut.ran - len(aut._automaton1.transitions)) # check if each transition is on its index returned by hash function for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(rep, aut.trans_table[aut.hash_function.hash(rep)][1]) # test the representation in faulty table aut.enable_faulty_transitions(8) aut.generate_PHF_table() for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(aut.compress_hash.hash(rep), aut.trans_table[aut.hash_function.hash(rep)][3]) # change the size of PHF table and repeat tests aut = PHF_DFA() a = bdz() a.set_ratio(6.0) a.set_iteration_limit(10) aut.set_PHF_class(a) aut._automaton1 = nfaData aut.generate_PHF_table() # transition table size self.assertEqual(aut.ran, len(aut.trans_table)) self.assertEqual(aut.ran, 72) # count number of unique lines in transition table tranCount = dict() for l in aut.trans_table: tranCount.setdefault(l[1], 0) tranCount[l[1]] += 1 # test if every automaton transition is just once in the table for t in aut._automaton1.transitions: self.assertEqual(tranCount[aut._transition_rep(t)], 1) t = ([2 ** aut.state_bits - 1, 2 ** aut.symbol_bits - 1, 0]) # rest of trans are the nonexistent transitions self.assertEqual(tranCount[aut._transition_rep(t)], aut.ran - len(aut._automaton1.transitions)) # check if each transition is on its index returned by hash function for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(rep, aut.trans_table[aut.hash_function.hash(rep)][1]) # test the representation in faulty table aut.enable_faulty_transitions(8) aut.generate_PHF_table() for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(aut.compress_hash.hash(rep), aut.trans_table[aut.hash_function.hash(rep)][3]) # RE /#include.*>/ and enable fallback_state par = pcre_parser() par.set_text("/#include.*>/s") aut = PHF_DFA() a = bdz() a.set_ratio(2.5) a.set_iteration_limit(10) aut.set_PHF_class(a) aut.create_by_parser(par) aut.enable_fallback_state(warning=False) aut.compute() # transition table size self.assertEqual(aut.ran, len(aut.trans_table)) self.assertEqual(aut.ran, 90) # count number of unique lines in transition table tranCount = dict() for l in aut.trans_table: tranCount.setdefault(l[1], 0) tranCount[l[1]] += 1 # test if every automaton transition is just once in the table for t in aut._automaton1.transitions: self.assertEqual(tranCount[aut._transition_rep(t)], 1) t = ([2 ** aut.state_bits - 1, 2 ** aut.symbol_bits - 1, 0]) # rest of trans are the nonexistent transitions self.assertEqual(tranCount[aut._transition_rep(t)], aut.ran - len(aut._automaton1.transitions)) # check if each transition is on its index returned by hash function for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(rep, aut.trans_table[aut.hash_function.hash(rep)][1]) # test the representation in faulty table aut.enable_faulty_transitions(8) aut.generate_PHF_table() for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(aut.compress_hash.hash(rep), aut.trans_table[aut.hash_function.hash(rep)][3]) # disable fallback_state aut.disable_fallback_state() aut.compute() self.assertEqual(aut.ran, len(aut.trans_table)) self.assertEqual(aut.ran, 252) # count number of unique lines in transition table tranCount = dict() for l in aut.trans_table: tranCount.setdefault(l[1], 0) tranCount[l[1]] += 1 # test if every automaton transition is just once in the table for t in aut._automaton1.transitions: self.assertEqual(tranCount[aut._transition_rep(t)], 1) t = ([2 ** aut.state_bits - 1, 2 ** aut.symbol_bits - 1, 0]) # rest of trans are the nonexistent transitions self.assertEqual(tranCount[aut._transition_rep(t)], aut.ran - len(aut._automaton1.transitions)) # check if each transition is on its index returned by hash function for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(rep, aut.trans_table[aut.hash_function.hash(rep)][1]) # test the representation in faulty table aut.enable_faulty_transitions(8) aut.generate_PHF_table() for t in aut._automaton1.transitions: rep = aut._transition_rep(t) self.assertEqual(aut.compress_hash.hash(rep), aut.trans_table[aut.hash_function.hash(rep)][3])
def test_search(self): """search()""" # 1. RE /^abc/ nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (2,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.search("abc"), [1]) self.assertEqual(aut.search("aaaaaaaaaaaaaabc"), [0]) self.assertEqual(aut.search("ccccbbbabc"), [0]) self.assertEqual(aut.search("ababc"), [0]) self.assertEqual(aut.search("d"), [0]) self.assertEqual(aut.search("cbabbacba"), [0]) # 2. RE /abc/ nfaData = nfa_data() nfaData.states[0] = b_State(0,set()) nfaData.states[1] = b_State(1,set()) nfaData.states[2] = b_State(2,set()) nfaData.states[3] = b_State(3,set([0])) nfaData.alphabet[0] = b_Sym_char("a", "a", 0) nfaData.alphabet[1] = b_Sym_char("b", "b", 1) nfaData.alphabet[2] = b_Sym_char("c", "c", 2) nfaData.start = 0 nfaData.transitions.add( (0,0,1) ) nfaData.transitions.add( (0,1,0) ) nfaData.transitions.add( (0,2,0) ) nfaData.transitions.add( (1,1,2) ) nfaData.transitions.add( (1,0,1) ) nfaData.transitions.add( (1,2,0) ) nfaData.transitions.add( (2,2,3) ) nfaData.transitions.add( (2,0,1) ) nfaData.transitions.add( (2,1,0) ) nfaData.transitions.add( (3,0,3) ) nfaData.transitions.add( (3,1,3) ) nfaData.transitions.add( (3,2,3) ) nfaData.final.add(3) aut = PHF_DFA() a = bdz() a.set_limit(128) aut.set_PHF_class(a) aut.create_from_nfa_data(nfaData) aut.compute() self.assertEqual(aut.search("abc"), [1]) self.assertEqual(aut.search("aaaaaaaaaaaaaabc"), [1]) self.assertEqual(aut.search("ccccbbbabc"), [1]) self.assertEqual(aut.search("ababc"), [1]) self.assertEqual(aut.search("d"), [0]) self.assertEqual(aut.search("cbabbacba"), [0]) # 2a. same test with faulty transitions aut.enable_faulty_transitions(32) aut.compute() self.assertEqual(aut.search("abc"), [1]) self.assertEqual(aut.search("aaaaaaaaaaaaaabc"), [1]) self.assertEqual(aut.search("ccccbbbabc"), [1]) self.assertEqual(aut.search("ababc"), [1]) self.assertEqual(aut.search("d"), [0]) self.assertEqual(aut.search("cbabbacba"), [0]) # 3. RE /#include.*>/ with enable_fallback_state par = pcre_parser() par.set_text("/#include.*>/") aut = PHF_DFA() a = bdz() a.set_ratio(2.5) a.set_iteration_limit(10) aut.set_PHF_class(a) aut.create_by_parser(par) aut.enable_fallback_state(warning=False) aut.compute() self.assertEqual(aut.search("#include <stdio.h>"), [1]) self.assertEqual(aut.search("#include <stdlib.h>"), [1]) self.assertEqual(aut.search("#include <stdio.h>bba"), [1]) self.assertEqual(aut.search('#include "pcre.h"'), [0]) self.assertEqual(aut.search('asdf#include <stdio.h>'), [1])
def test_join(self): """join()""" # Check whether _rnum of returned object contains values of # two _rnum b_State() class objects. # Create a class object ColouredState and try whether there would # be successful united in cases: # - the self is ColouredState # - other is ColouredState # SAME CODE AS IN "test_b_state.py" # check join two b_State - self join other self_state = b_State() other_state = b_State() self.assertTrue((self_state.join(other_state))._rnum == set()) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = b_State() other_state = b_State() other_state._rnum = set([0]) self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() other_state._rnum = set([1]) self.assertTrue((self_state.join(other_state))._rnum == set([0, 1])) # check join two b_State - other join self self_state = b_State() other_state = b_State() self.assertTrue((other_state.join(self_state))._rnum == set()) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = b_State() other_state = b_State() other_state._rnum = set([0]) self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = b_State() self_state._rnum = set([0]) other_state = b_State() other_state._rnum = set([1]) self.assertTrue((other_state.join(self_state))._rnum == set([0, 1])) # self is ColouredState class and other is b_State class self_state = ColouredState(0, set([11]), set([1])) other_state = b_State(1, set([15])) self.assertTrue((self_state.join(other_state))._rnum == set([11, 15])) self_state = ColouredState(2, set([14]), set([1])) other_state = b_State(3, set([2, 3])) self.assertTrue( (other_state.join(self_state))._rnum == set([2, 3, 14])) # try error self_state.stypes = [] try: self_state.join(other_state) self.assertTrue(False) except state_join_exception: self.assertTrue(True) # ColouredState - self join other self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set()) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set([0])) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((self_state.join(other_state))._rnum == set([0, 1])) # ColouredState - other join self self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set()) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([0])) self_state = ColouredState(0, set([]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([1])) self_state = ColouredState(0, set([0]), set([1])) other_state = ColouredState(1, set([1]), set([5])) self.assertTrue((other_state.join(self_state))._rnum == set([0, 1]))