Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- import math
- import random
- import sys
- from collections.abc import Iterator
- from itertools import chain, tee
- from hypothesis import given
- import hypothesis.strategies as st
- import sut
- @given(st.text(), st.text(), st.lists(st.text()))
- def test_common_prefix(s1, s2, rest):
- lcp = sut.common_prefix(s1, s2, *rest)
- assert isinstance(lcp, str)
- data = chain([s1, s2], rest)
- len_min = len(s1)
- for s in data:
- assert s.startswith(lcp)
- len_min = min(len_min, len(s))
- len_lcp = len(lcp)
- if len_min > len_lcp:
- next_sym = s1[len_lcp]
- assert not all(next_sym == s[len_lcp] for s in chain(s2, rest))
- alltypes = st.one_of(st.none(),
- st.integers(),
- st.characters())
- @given(st.iterables(alltypes))
- def test_factor(seq):
- res = sut.factor(seq)
- assert "levels" in dir(res)
- assert "elements" in dir(res)
- assert isinstance(res.elements, list)
- assert isinstance(res.levels, dict)
- level_cursor = 0
- known = set()
- for i, elem in enumerate(seq):
- if elem not in known:
- assert elem in res.levels
- assert res.levels[elem] == level_cursor
- known.add(elem)
- level_cursor += 1
- assert res.elements[i] == res.levels[elem]
- @given(st.iterables(alltypes), st.integers(min_value=0))
- def test_chunked(iterable, n):
- it1, it2, it3 = tee(iterable, 3)
- res = sut.chunked(it1, n)
- if n == 0:
- assert list(res) == []
- return
- size = sum(1 for item in it2)
- n_chunks = math.ceil(size / n)
- i = 0
- for chunk in res:
- i += 1
- if i != n_chunks:
- assert len(chunk) == n
- else:
- assert len(chunk) == size % n
- for item in chunk:
- assert item == next(it3)
- class Strategy(Iterator):
- def __or__(self, other):
- return OneOfStrategy(self, other)
- def example(self):
- return next(self)
- class OneOfStrategy(Strategy):
- def __init__(self, *strats):
- self.strats = strats
- def __next__(self):
- return random.choice(self.strats).example()
- class ConstStrategy(Strategy):
- def __init__(self, value):
- self.value = value
- def __next__(self):
- return self.value
- class UniformFloatStrategy(Strategy):
- def __next__(self):
- return random.random()
- class GaussianFloatStrategy(Strategy):
- def __next__(self):
- return random.gauss(0, 1)
- class BoundedFloatStrategy(Strategy):
- def __init__(self):
- self.min = sys.float_info.min
- self.max = sys.float_info.max
- def __next__(self):
- x = random.uniform(self.min, self.max)
- while x in [self.min, self.max]:
- x = random.uniform(self.min, self.max)
- return x
- class NastyFloatStrategy(Strategy):
- def __init__(self):
- self.values = [
- float("inf"),
- -float("inf"),
- float("nan"),
- +0.0,
- -0.0,
- sys.float_info.min,
- sys.float_info.max
- ]
- def __next__(self):
- return random.choice(self.values)
- def floats():
- return (UniformFloatStrategy() | GaussianFloatStrategy() |
- BoundedFloatStrategy() | NastyFloatStrategy())
- def given(*strats, n_trials):
- def decorator(f):
- @functools.wraps(f)
- def inner():
- for _ in range(n_trials):
- testcase = [s.example() for s in strats]
- repl_test = ', '.join(str(t) for t in testcase)
- try:
- f(*testcase)
- except AssertionError:
- print(f"Counterexample: {f.__name__}({repl_test})",
- file=sys.stderr)
- raise AssertionError
- del inner.__dict__['__wrapped__']
- return inner
- return decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement