| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 import unittest | 28 import unittest |
| 29 from automaton import Action | 29 from automaton import Action |
| 30 from regex_parser import RegexParser | 30 from regex_parser import RegexParser |
| 31 from transition_keys import TransitionKey | 31 from transition_keys import TransitionKey, KeyEncoding |
| 32 from nfa_builder import NfaBuilder | 32 from nfa_builder import NfaBuilder |
| 33 from dfa import Dfa | 33 from dfa import Dfa |
| 34 | 34 |
| 35 class AutomataTestCase(unittest.TestCase): | 35 class AutomataTestCase(unittest.TestCase): |
| 36 | 36 |
| 37 __encoding = KeyEncoding.get('latin1') |
| 38 |
| 37 @staticmethod | 39 @staticmethod |
| 38 def __build_automata(string): | 40 def __build_automata(string): |
| 39 nfa = NfaBuilder().nfa(RegexParser.parse(string)) | 41 encoding = AutomataTestCase.__encoding |
| 42 trees = {'main' : RegexParser.parse(string)} |
| 43 nfa = NfaBuilder.nfa(encoding, {}, trees, 'main') |
| 40 (start_name, dfa_nodes) = nfa.compute_dfa() | 44 (start_name, dfa_nodes) = nfa.compute_dfa() |
| 41 dfa = Dfa(start_name, dfa_nodes) | 45 dfa = Dfa(encoding, start_name, dfa_nodes) |
| 42 return (nfa, dfa, dfa.minimize()) | 46 return (nfa, dfa, dfa.minimize()) |
| 43 | 47 |
| 44 # (pattern, should match, should not match) | 48 # (pattern, should match, should not match) |
| 45 __test_data = [ | 49 __test_data = [ |
| 46 ("a", ["a"], ["b", ""]), | 50 ("a", ["a"], ["b", ""]), |
| 47 ("ab", ["ab"], ["bb", ""]), | 51 ("ab", ["ab"], ["bb", ""]), |
| 48 ("a+b", ["ab", "aab", "aaab"], ["a", "b", ""]), | 52 ("a+b", ["ab", "aab", "aaab"], ["a", "b", ""]), |
| 49 ("a?b", ["ab", "b"], ["a", "c", ""]), | 53 ("a?b", ["ab", "b"], ["a", "c", ""]), |
| 50 ("a*b", ["ab", "aaab", "b"], ["a", "c", ""]), | 54 ("a*b", ["ab", "aaab", "b"], ["a", "c", ""]), |
| 51 ("a|b", ["a", "b"], ["ab", "c", ""]), | 55 ("a|b", ["a", "b"], ["ab", "c", ""]), |
| 52 (".", ["a", "b"], ["", "aa"]), | 56 (".", ["a", "b"], ["", "aa"]), |
| 53 (".*", ["", "a", "abcaabbcc"], []), | 57 (".*", ["", "a", "abcaabbcc"], []), |
| 54 ("a.b", ["aab", "abb", "acb"], ["ab", ""]), | 58 ("a.b", ["aab", "abb", "acb"], ["ab", ""]), |
| 55 ("a.?b", ["aab", "abb", "acb", "ab"], ["aaab", ""]), | 59 ("a.?b", ["aab", "abb", "acb", "ab"], ["aaab", ""]), |
| 56 ("a.+b", ["aab", "abb", "acb"], ["aaac", "ab", ""]), | 60 ("a.+b", ["aab", "abb", "acb"], ["aaac", "ab", ""]), |
| 57 (".|.", ["a", "b"], ["aa", ""]), | 61 (".|.", ["a", "b"], ["aa", ""]), |
| 58 ("//.", ["//a"], ["aa", ""]), | 62 ("//.", ["//a"], ["aa", ""]), |
| 59 ("[ab]{2}", ["aa", "ab", "ba", "bb"], ["", "a", "b", "aaa", "bbb"]), | 63 ("[ab]{2}", ["aa", "ab", "ba", "bb"], ["", "a", "b", "aaa", "bbb"]), |
| 60 ("[ab]{2,3}", ["aa", "ab", "ba", "bb", "aab", "baa", "bbb"], | 64 ("[ab]{2,3}", ["aa", "ab", "ba", "bb", "aab", "baa", "bbb"], |
| 61 ["", "a", "b", "aaaa", "bbba"]), | 65 ["", "a", "b", "aaaa", "bbba"]), |
| 62 ("[ab]{2,4}", ["aa", "ab", "ba", "bb", "aab", "baa", "bbb", "abab"], | 66 ("[ab]{2,4}", ["aa", "ab", "ba", "bb", "aab", "baa", "bbb", "abab"], |
| 63 ["", "a", "b", "aaaba", "bbbaa"]), | 67 ["", "a", "b", "aaaba", "bbbaa"]), |
| 64 ("[\\101]", ["A"], ["B"]) | 68 ("[\\101]", ["A"], ["B"]) |
| 65 ] | 69 ] |
| 66 | 70 |
| 67 def test_matches(self): | 71 def test_matches(self): |
| 68 for (regex, matches, not_matches) in self.__test_data: | 72 for (regex, matches, not_matches) in self.__test_data: |
| 69 automata = self.__build_automata(regex) | 73 automata = self.__build_automata(regex) |
| 70 for string in matches: | 74 for string in matches: |
| 71 for automaton in automata: | 75 for automaton in automata: |
| 72 self.assertTrue(automaton.matches(string)) | 76 self.assertTrue(automaton.matches(string)) |
| 73 for string in not_matches: | 77 for string in not_matches: |
| 74 for automaton in automata: | 78 for automaton in automata: |
| 75 self.assertFalse(automaton.matches(string)) | 79 self.assertFalse(automaton.matches(string)) |
| 76 | 80 |
| 77 def test_can_construct_dot(self): | 81 def test_can_construct_dot(self): |
| 78 for (regex, matches, not_matches) in self.__test_data: | 82 for (regex, matches, not_matches) in self.__test_data: |
| 79 for automaton in self.__build_automata(regex): | 83 for automaton in self.__build_automata(regex): |
| 80 automaton.to_dot() | 84 automaton.to_dot() |
| 81 | 85 |
| 82 def test_minimization(self): | 86 def test_minimization(self): |
| 83 def empty_node(): | 87 encoding = self.__encoding |
| 84 return { 'transitions' : {}, 'terminal' : False, 'action' : None } | 88 def empty_node(): |
| 85 mapping = { k : empty_node() for k in ['S_0', 'S_1', 'S_2', 'S_3'] } | 89 return { |
| 86 key_a = TransitionKey.single_char('a') | 90 'transitions' : {}, |
| 87 key_b = TransitionKey.single_char('b') | 91 'terminal' : False, |
| 88 key_c = TransitionKey.single_char('c') | 92 'action' : Action.empty_action() } |
| 93 mapping = { k : empty_node() for k in ['S_0', 'S_1', 'S_2', 'S_3'] } |
| 94 key_a = TransitionKey.single_char(encoding, 'a') |
| 95 key_b = TransitionKey.single_char(encoding, 'b') |
| 96 key_c = TransitionKey.single_char(encoding, 'c') |
| 89 | 97 |
| 90 mapping['S_0']['transitions'][key_a] = 'S_1' | 98 mapping['S_0']['transitions'][key_a] = 'S_1' |
| 91 mapping['S_0']['transitions'][key_b] = 'S_2' | 99 mapping['S_0']['transitions'][key_b] = 'S_2' |
| 92 mapping['S_1']['transitions'][key_c] = 'S_3' | 100 mapping['S_1']['transitions'][key_c] = 'S_3' |
| 93 mapping['S_2']['transitions'][key_c] = 'S_3' | 101 mapping['S_2']['transitions'][key_c] = 'S_3' |
| 94 mapping['S_3']['terminal'] = True | 102 mapping['S_3']['terminal'] = True |
| 95 | 103 |
| 96 mdfa = Dfa('S_0', mapping).minimize() | 104 mdfa = Dfa(encoding, 'S_0', mapping).minimize() |
| 97 self.assertEqual(3, mdfa.node_count()) | 105 self.assertEqual(3, mdfa.node_count()) |
| OLD | NEW |