Skip to content

Instantly share code, notes, and snippets.

@sureshvv
Created June 10, 2017 05:03
Show Gist options
  • Save sureshvv/4f9e5b176c4613830ba43d729a6e89ef to your computer and use it in GitHub Desktop.
Save sureshvv/4f9e5b176c4613830ba43d729a6e89ef to your computer and use it in GitHub Desktop.
fox_hen_grain.py
from hypothesis import note, settings
from hypothesis.stateful import RuleBasedStateMachine, rule, invariant, precondition
from hypothesis import strategies as st
class Item():
def __init__(self, name):
self.crossed = False
self.name = name
class FoxHenGrainProblem(RuleBasedStateMachine):
solutions = {}
def __init__(self):
super().__init__()
self.start()
def start(self):
self.items = []
self.items.append(Item('Fox'))
self.items.append(Item('Grain'))
self.items.append(Item('Hen'))
self.farmer = Item('Farmer')
self.moves = []
@precondition(lambda self: self.farmer.crossed)
@rule(choice=st.choices())
def make_return_move(self, choice):
items = [x for x in self.items if x.crossed]
if len(items) > 1:
if choice([True, False]):
item = choice(items)
self.moves.append([item, 'back'])
item.crossed = False
self.farmer.crossed = False
@precondition(lambda self: not self.farmer.crossed)
@rule(choice=st.choices())
def make_forward_move(self, choice):
items = [x for x in self.items if not x.crossed]
item1 = choice(items)
self.moves.append([item1, 'fwd'])
item1.crossed = True
self.farmer.crossed = True
@invariant()
def items_safe(self):
if self.farmer.crossed:
items = [x for x in self.items if not x.crossed]
if len(items) == 0:
self.write_notes()
assert False
else:
items = [x for x in self.items if x.crossed]
if len(items) == 2:
items.sort(key=lambda x: x.name)
if items[0].name == 'Fox' and items[1].name == 'Hen':
self.start()
elif items[0].name == 'Grain' and items[1].name == 'Hen':
self.start()
def write_notes(self):
for x in self.moves:
note("%s" % [x[0].name, x[1]])
# The default of 200 is sometimes not enough for Hypothesis to find
# a falsifying example.
with settings(max_examples=700):
DieHardTest = FoxHenGrainProblem.TestCase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment