| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from hypothesis import strategies as st |
| 12 |
from hypothesis.internal.conjecture.junkdrawer import IntList |
| 13 |
from hypothesis.stateful import RuleBasedStateMachine, initialize, invariant, rule |
| 14 |
|
| 15 |
INTEGERS = st.integers(0, 2**68) |
| 16 |
|
| 17 |
|
| 18 |
@st.composite |
| 19 |
def valid_index(draw): |
| 20 |
machine = draw(st.runner()) |
| 21 |
if not machine.model: |
| 22 |
return draw(st.nothing()) |
| 23 |
return draw(st.integers(0, len(machine.model) - 1)) |
| 24 |
|
| 25 |
|
| 26 |
class IntListRules(RuleBasedStateMachine): |
| 27 |
@initialize(ls=st.lists(INTEGERS)) |
| 28 |
def starting_lists(self, ls): |
| 29 |
self.model = list(ls) |
| 30 |
self.target = IntList(ls) |
| 31 |
|
| 32 |
@invariant() |
| 33 |
def lists_are_equivalent(self): |
| 34 |
if hasattr(self, "model"): |
| 35 |
assert isinstance(self.model, list) |
| 36 |
assert isinstance(self.target, IntList) |
| 37 |
assert len(self.model) == len(self.target) |
| 38 |
assert list(self.target) == self.model |
| 39 |
|
| 40 |
@rule(n=INTEGERS) |
| 41 |
def append(self, n): |
| 42 |
self.model.append(n) |
| 43 |
self.target.append(n) |
| 44 |
|
| 45 |
@rule(i=valid_index()) |
| 46 |
def delete(self, i): |
| 47 |
del self.model[i] |
| 48 |
del self.target[i] |
| 49 |
|
| 50 |
@rule(i=valid_index()) |
| 51 |
def agree_on_values(self, i): |
| 52 |
assert self.model[i] == self.target[i] |
| 53 |
|
| 54 |
|
| 55 |
TestIntList = IntListRules.TestCase |