user14942713
user14942713

Reputation: 31

How to combine Hypothesis in my code programaticly and not as a test? (Use Hypothesis to distinguish between automata and Python function)

I have a Python function which describes Language L which gets a word and returns True in case that the word is in the language and return False otherwise. In addition, I have a Deterministic Finite Automata (DFA) which describes another language L2 and I want to check if L2=L. I thought maybe I can use Hypothesis library to get counterexample and distinguish between the function and the DFA but I don't know how to combine Hypothesis in my code programmatically and not as a test. Thanks

Upvotes: 1

Views: 188

Answers (1)

Zac Hatfield-Dodds
Zac Hatfield-Dodds

Reputation: 3003

It looks like you asked this question as two parts on the Hypothesis issue tracker - thanks for moving it over here as suggested :-) Porting my answer over too for posterity:

The key insight here is that you can call a Hypothesis-wrapped function like any other, and have the inner function save its inputs. For example:

counterexample = None

@given(x=st.integers())
def check(f, g, x):
    if f(x) != g(x):
        global counterexample
        counterexample = x
        raise AssertionError

with contextlib.suppress(AssertionError):
    check(f=math.sin, g=math.cos)

assert counterexample is not None

Upvotes: 1

Related Questions