How to use is_accepting method in hypothesis

Best Python code snippet using hypothesis

tests.py

Source:tests.py Github

copy

Full Screen

...11 self.assertEqual(nfa.transition_function, {(0, 'x'): {1}})12 self.assertEqual(nfa.accept_states, {1})13 self.assertEqual(nfa.in_states, {0})14 # test NFA behaviour15 self.assertFalse(nfa.is_accepting())16 nfa.feed_symbol("x")17 self.assertEqual(nfa.in_states, {1})18 self.assertTrue(nfa.is_accepting())19 nfa.feed_symbol("x")20 self.assertFalse(nfa.is_accepting())21 self.assertEqual(nfa.in_states, set())22 self.assertTrue(nfa.is_dead())23 def test_nfa_concat(self):24 print("Testing concatenated NFA a.b")25 # recognizes "a.b"26 nfa = nfa_utils.get_concat(nfa_utils.get_single_symbol_regex("a"),27 nfa_utils.get_single_symbol_regex("b"))28 print(nfa)29 self.assertFalse(nfa.is_accepting())30 nfa.feed_symbol("a")31 self.assertFalse(nfa.is_accepting())32 nfa.feed_symbol("b")33 self.assertTrue(nfa.is_accepting())34 nfa.feed_symbol("a")35 self.assertFalse(nfa.is_accepting())36 def test_big_nfa_concat(self):37 print("Testing concatenated NFA a.b.c.c.b.a.G.G.G")38 concat_strings = ["abc", "cba", "GGG"]39 # construct a large NFA by creating 3 sub NFAs, and40 # concatenating them together41 nfa = None42 for str in concat_strings:43 # construct sub nfa44 sub_nfa = None45 for c in str:46 if sub_nfa is None:47 sub_nfa = nfa_utils.get_single_symbol_regex(c)48 else:49 sub_nfa = nfa_utils.get_concat(sub_nfa, nfa_utils.get_single_symbol_regex(c))50 # combine this sub NFA with the overall NFA51 if nfa is None:52 nfa = sub_nfa53 else:54 nfa = nfa_utils.get_concat(nfa, sub_nfa)55 print(nfa)56 # ensure the NFA does not accept until the entire input string has been fed in57 input_str = "".join(concat_strings)58 for symbol in input_str:59 self.assertFalse(nfa.is_accepting())60 nfa.feed_symbol(symbol)61 self.assertTrue(nfa.is_accepting())62 def test_empty_string(self):63 print("Testing empty string transitions")64 # construct an NFA equivalent to a|<empty string>65 nfa = nfa_utils.get_single_symbol_regex("a")66 nfa.add_state(2, True)67 nfa.add_transition(0, "", {2})68 nfa.reset()69 print(nfa)70 # NFA should accept straight away due to the empty string transition71 # from the initial state to an accept state72 self.assertTrue(nfa.is_accepting())73 # after feeding an 'a', the NFA should accept; state 2 is dead74 # but a transition from state 0 to 1 (accepts) should still work75 nfa.feed_symbol("a")76 self.assertTrue(nfa.is_accepting())77 # after feeding a final a, the NFA should be dead (no transitions available78 # for the current "active" states)79 nfa.feed_symbol("a")80 self.assertFalse(nfa.is_accepting())81 self.assertTrue(nfa.is_dead())82 def test_nfa_union(self):83 print("Testing NFA union a.b|c.d")84 # build NFA85 nfa = nfa_utils.get_union(86 nfa_utils.get_regex_nfa("a.b"),87 nfa_utils.get_regex_nfa("c.d")88 )89 nfa.reset()90 print(nfa)91 # test accepts a.b92 self.assertFalse(nfa.is_accepting())93 nfa.feed_symbol("a")94 self.assertFalse(nfa.is_accepting())95 nfa.feed_symbol("b")96 self.assertTrue(nfa.is_accepting())97 nfa.feed_symbol("c")98 self.assertFalse(nfa.is_accepting())99 # test accepts c.d100 nfa.reset()101 self.assertFalse(nfa.is_accepting())102 nfa.feed_symbol("c")103 self.assertFalse(nfa.is_accepting())104 nfa.feed_symbol("d")105 self.assertTrue(nfa.is_accepting())106 nfa.feed_symbol("e")107 self.assertFalse(nfa.is_accepting())108 def test_kleene_star(self):109 print("Testing kleene star NFA a*")110 # build NFA111 nfa = nfa_utils.get_regex_nfa("a*")112 nfa.reset()113 print(nfa)114 # test if accepts empty string115 self.assertTrue(nfa.is_accepting())116 # test if accepts 1 or many a's117 for i in range(20):118 nfa.feed_symbol("a")119 self.assertTrue(nfa.is_accepting())120 # test if rejects other symbols121 nfa.feed_symbol("b")122 self.assertFalse(nfa.is_accepting())123 self.assertTrue(nfa.is_dead())124 def test_big_kleene_star(self):125 print("Testing big kleene star NFA a*b*c*")126 # build NFA127 nfa = nfa_utils.get_regex_nfa("a*b*c*")128 nfa.reset()129 print(nfa)130 # test if accepts empty string (none of a, b, or c)131 self.assertTrue(nfa.is_accepting())132 # test if rejects other symbol133 nfa.feed_symbol("d")134 self.assertFalse(nfa.is_accepting())135 # test if accepts many a's, b's, and c's in order136 symbols = ["a", "b", "c"]137 # test different numbers of a's, b's, and c's138 for i in range(10):139 nfa.reset()140 # feed each symbol a number of times141 for symbol in symbols:142 for j in range(i + 1):143 nfa.feed_symbol(symbol)144 self.assertTrue(nfa.is_accepting())145 # test if rejects other symbol146 nfa.feed_symbol("d")147 self.assertFalse(nfa.is_accepting())148 def test_one_or_more_of(self):149 print("Testing \"one or more of\" operator a+")150 # build NFA151 nfa = nfa_utils.get_regex_nfa("a+")152 nfa.reset()153 print(nfa)154 # test if rejects empty string155 self.assertFalse(nfa.is_accepting())156 # test if accepts 1 or many a's157 for i in range(20):158 nfa.feed_symbol("a")159 self.assertTrue(nfa.is_accepting())160 # test if rejects other symbols161 nfa.feed_symbol("b")162 self.assertFalse(nfa.is_accepting())163 self.assertTrue(nfa.is_dead())164 def zero_or_one_of(self):165 print("Testing \"zero or one of\" operator a?bcd")166 # build NFA167 nfa = nfa_utils.get_regex_nfa("a?bcd")168 nfa.reset()169 print(nfa)170 # test rejects empty string171 self.assertFalse(nfa.is_accepting())172 # test accepts "bcd"173 nfa.feed_symbols("bcd")174 self.assertTrue(nfa.is_accepting())175 nfa.reset()176 # test also accepts "abcd"177 nfa.feed_symbol("abcd")178 self.assertTrue(nfa.is_accepting())179 nfa.reset()180 # test rejects "aabcd"181 nfa.feed_symbols("aabcd")182 self.assertFalse(nfa.is_accepting())183 nfa.reset()184 # test rejects "aaaaaaaaabcd"185 nfa.feed_symbols("aaaaaaaaabcd")186 self.assertFalse(nfa.is_accepting())187 nfa.reset()188 def test_nfa_equals(self):189 print("Testing NFA equals function")190 # test equal191 nfa_a = nfa_utils.get_regex_nfa("a.b|c")192 nfa_b = nfa_utils.get_regex_nfa("a.b|c")193 self.assertEqual(nfa_a, nfa_b)194 # test NOT equal195 nfa_a = nfa_utils.get_regex_nfa("a.b|c")196 nfa_b = nfa_utils.get_regex_nfa("a.b.c.d")197 self.assertNotEqual(nfa_a, nfa_b)198 def test_implicit_concatenation(self):199 print("Testing implicit NFA concatenation")200 nfa_a = nfa_utils.get_regex_nfa("a.b.c.d|c")201 nfa_b = nfa_utils.get_regex_nfa("abcd|c")202 self.assertEqual(nfa_a, nfa_b)203 def test_readme_example_1(self):204 print("Testing README example \"p.y.t.h.o.n or python\"")205 # test with and without implicit concatenation206 variations = ["python", "p.y.t.h.o.n"]207 for variation in variations:208 # build NFA209 nfa = nfa_utils.get_regex_nfa(variation)210 nfa.reset()211 print(nfa)212 # test rejects empty string213 self.assertFalse(nfa.is_accepting())214 # test accepts "python"215 nfa.feed_symbols("python")216 self.assertTrue(nfa.is_accepting())217 nfa.reset()218 # test rejects "java"219 nfa.feed_symbol("java")220 self.assertFalse(nfa.is_accepting())221 nfa.reset()222 def test_readme_example_2(self):223 print("Testing README example \"python|java|C#\"")224 # build NFA225 nfa = nfa_utils.get_regex_nfa("python|java|C#")226 nfa.reset()227 print(nfa)228 # test rejects empty string229 self.assertFalse(nfa.is_accepting())230 accept_list = ["python", "java", "C#"]231 reject_list = ["perl", "C++", "Go"]232 # test accepts all in accept list233 for symbol_input in accept_list:234 nfa.feed_symbols(symbol_input)235 self.assertTrue(nfa.is_accepting())236 nfa.reset()237 # test reject all in reject list238 for symbol_input in reject_list:239 nfa.feed_symbols(symbol_input)240 self.assertFalse(nfa.is_accepting())241 nfa.reset()242 def test_readme_example_3(self):243 print("Testing README example \"o+k then or o*ok then\"")244 # test both + symbol and * symbol variations245 variations = ["o+k then", "o*ok then"]246 for variation in variations:247 # build NFA248 nfa = nfa_utils.get_regex_nfa(variation)249 nfa.reset()250 print(nfa)251 # test rejects empty string252 self.assertFalse(nfa.is_accepting())253 accept_list = ["ok then", "ooook then", "ooooooook then"]254 reject_list = ["k then", "okay", "oki-doki"]255 # test accepts all in accept list256 for symbol_input in accept_list:257 nfa.feed_symbols(symbol_input)258 self.assertTrue(nfa.is_accepting())259 nfa.reset()260 # test reject all in reject list261 for symbol_input in reject_list:262 nfa.feed_symbols(symbol_input)263 self.assertFalse(nfa.is_accepting())264 nfa.reset()265 def test_readme_example_4(self):266 print("Testing README example \"c?loud\"")267 # build NFA268 nfa = nfa_utils.get_regex_nfa("c?loud")269 nfa.reset()270 print(nfa)271 # test rejects empty string272 self.assertFalse(nfa.is_accepting())273 accept_list = ["cloud", "loud"]274 reject_list = ["oud", "ccloud"]275 # test accepts all in accept list276 for symbol_input in accept_list:277 nfa.feed_symbols(symbol_input)278 self.assertTrue(nfa.is_accepting())279 nfa.reset()280 # test reject all in reject list281 for symbol_input in reject_list:282 nfa.feed_symbols(symbol_input)283 self.assertFalse(nfa.is_accepting())284 nfa.reset()285 def test_readme_example_5(self):286 print("Testing README example \"H?A?h?a?*!*|H?E?h?e?*!*\"")287 # build NFA288 nfa = nfa_utils.get_regex_nfa("H?A?h?a?*!*|H?E?h?e?*!*")289 nfa.reset()290 print(nfa)291 accept_list = ["Hah", "heh", "Haha", "AAAAAAAAAAHAHAHAHAHA!!", "eeeehehehehehe", "hhhaaaaaaaaaaaa", "HEHEEE!"]292 reject_list = ["Heaha", "Haha!h!", "!haha", "I don't get it"]293 # test accepts all in accept list294 for symbol_input in accept_list:295 nfa.feed_symbols(symbol_input)296 self.assertTrue(nfa.is_accepting())297 nfa.reset()298 # test reject all in reject list299 for symbol_input in reject_list:300 nfa.feed_symbols(symbol_input)301 self.assertFalse(nfa.is_accepting())...

Full Screen

Full Screen

fsa.py

Source:fsa.py Github

copy

Full Screen

1from typing import Dict, List, Optional, Set, Tuple2__all__ = ['EPSILON', 'DFAState', 'NFAState', 'nfa_to_dfa']3EPSILON: str = 'ε'4class DFAState:5 name: str6 edges: Dict[str, 'DFAState']7 is_accepting: bool8 def __init__(self, name: str):9 self.name = name10 self.edges = {}11 self.is_accepting = False12 def accepts(self, string: str) -> bool:13 if len(string) == 0:14 return self.is_accepting15 else:16 state = self.edges.get(string[0])17 if state is None:18 return False19 else:20 return state.accepts(string[1:])21 def add_edge(self, label: str, state: 'DFAState'):22 if len(label) != 1:23 raise ValueError(f'Label must be exactly one character long; got {label!r}')24 if label in self.edges:25 raise ValueError(f'Label {label} already exists as an edge!')26 self.edges[label] = state27 def follow_edge(self, label: str) -> Optional['DFAState']:28 return self.edges.get(label)29 def set_accepting(self):30 self.is_accepting = True31 def __str__(self):32 return f'DFAState({self.name!r}, {self.edges}, {self.is_accepting})'33 def __repr__(self):34 return str(self)35class NFAState:36 name: str37 edges: Dict[str, Set['NFAState']]38 is_accepting: bool39 def __init__(self, name: str):40 self.name = name41 self.edges = {}42 self.is_accepting = False43 def accepts(self, string: str) -> bool:44 if len(string) == 0:45 return self.is_accepting46 else:47 states = self.edges.get(string[0], set())48 for state in states:49 if state.accepts(string[1:]):50 return True51 return False52 def add_edge(self, label: str, state: 'NFAState'):53 if len(label) != 1:54 raise ValueError(f'Label must be exactly one character long; got {label!r}')55 if label not in self.edges:56 self.edges[label] = set()57 self.edges[label].add(state)58 def get_edge_labels(self, *, include_epsilon: bool = True) -> Set[str]:59 labels = set(self.edges)60 if not include_epsilon:61 labels -= {EPSILON}62 return labels63 def _get_epsilon_closure(self, seen: Set['NFAState']) -> Set['NFAState']:64 result = {self}65 targets = self.edges.get(EPSILON, set())66 for target in targets:67 if target not in seen:68 seen.add(target)69 result.update(target._get_epsilon_closure(seen))70 return result71 def get_epsilon_closure(self) -> Set['NFAState']:72 return self._get_epsilon_closure(set())73 def follow_edge(self, label: str) -> Set['NFAState']:74 return self.edges.get(label, set())75 def set_accepting(self):76 self.is_accepting = True77 def __str__(self):78 return f'NFAState({self.name!r}, {self.edges}, {self.is_accepting})'79 def __repr__(self):80 return str(self)81def nfa_to_dfa(nfa_start: NFAState) -> DFAState:82 def move(states: Set[NFAState], label: str) -> Set[NFAState]:83 resulting_states: Set[NFAState] = set()84 for state in states:85 resulting_states.update(state.follow_edge(label))86 return resulting_states87 def epsilon_closure(states: Set[NFAState]) -> Set[NFAState]:88 resulting_states: Set[NFAState] = set()89 for state in states:90 resulting_states.update(state.get_epsilon_closure())91 return resulting_states92 def make_dfa_name(states: Set[NFAState]) -> str:93 names = sorted(state.name for state in states)94 return '{' + ','.join(names) + '}'95 dfa_states_by_name: Dict[str, DFAState] = {}96 def get_or_create_dfa_state_for_nfa_states(states: Set[NFAState]) -> DFAState:97 name = make_dfa_name(states)98 dfa_state = dfa_states_by_name.get(name)99 if dfa_state is None:100 dfa_state = DFAState(name)101 if any(state.is_accepting for state in states):102 dfa_state.set_accepting()103 dfa_states_by_name[name] = dfa_state104 return dfa_state105 nfa_start_states = epsilon_closure({nfa_start})106 dfa_start = get_or_create_dfa_state_for_nfa_states(nfa_start_states)107 todo: List[Tuple[DFAState, Set[NFAState]]] = [(dfa_start, nfa_start_states)]108 seen: Set[DFAState] = {dfa_start}109 while len(todo) != 0:110 # Grab the next item off the todo list.111 dfa_state, nfa_states = todo.pop(0)112 # Work out the set of edge labels.113 labels: Set[str] = set()114 for state in nfa_states:115 labels.update(state.get_edge_labels(include_epsilon=False))116 for label in labels:117 # Follow this edge label.118 new_nfa_states = epsilon_closure(move(nfa_states, label))119 if len(new_nfa_states) != 0:120 new_dfa_state = get_or_create_dfa_state_for_nfa_states(new_nfa_states)121 dfa_state.add_edge(label, new_dfa_state)122 if new_dfa_state not in seen:123 todo.append((new_dfa_state, new_nfa_states))124 seen.add(new_dfa_state)...

Full Screen

Full Screen

regex_matcher.py

Source:regex_matcher.py Github

copy

Full Screen

1from string import ascii_lowercase2class _Node:3 def __init__(self, is_accepting: bool):4 self.is_accepting = is_accepting5 self.edges = []6class regex_matcher:7 def isMatch(self, s: str, p: str) -> bool:8 possible_states = []9 possible_states.append((self.construct_regex(p), 0))10 11 # loop through all possible states (is is a NFA, so multiple states are possible)12 while len(possible_states) > 0:13 state, index = possible_states.pop()14 # test if we reached the input end and are in an accepting state15 if index == len(s) and state.is_accepting:16 return True17 18 # progress through the states19 for edge in state.edges:20 if index < len(s) and s[index] in edge['pattern']:21 # this edge accepts the pattern, so add it to the next possible states22 possible_states.append((edge['node'], index+1))23 if '_' in edge['pattern']:24 possible_states.append((edge['node'], index))25 26 return False27 def construct_regex(self, pattern: str) -> _Node:28 start = _Node(is_accepting=False)29 prev = start30 31 for i in range(len(pattern)):32 n = _Node(is_accepting=False)33 edge = {"node": n, "pattern": []}34 if pattern[i] == "*":35 edge['pattern'].append('_') # create an epsilon edge to the new node36 elif pattern[i] == ".":37 edge['pattern'].extend(list(ascii_lowercase))38 else:39 edge['pattern'].append(pattern[i])40 41 if (i+1) < len(pattern) and pattern[i+1] == '*':42 edge['node'] = prev43 n = prev44 45 prev.edges.append(edge)46 prev = n47 48 prev.is_accepting = True...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful