Advertisement
Guest User

asda

a guest
Dec 13th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import functools
  2. import math
  3. import random
  4. import sys
  5. from collections.abc import Iterator
  6. from itertools import chain, tee
  7. from hypothesis import given
  8. import hypothesis.strategies as st
  9. import sut
  10.  
  11.  
  12. @given(st.text(), st.text(), st.lists(st.text()))
  13. def test_common_prefix(s1, s2, rest):
  14. lcp = sut.common_prefix(s1, s2, *rest)
  15. assert isinstance(lcp, str)
  16. data = chain([s1, s2], rest)
  17. len_min = len(s1)
  18. for s in data:
  19. assert s.startswith(lcp)
  20. len_min = min(len_min, len(s))
  21. len_lcp = len(lcp)
  22. if len_min > len_lcp:
  23. next_sym = s1[len_lcp]
  24. assert not all(next_sym == s[len_lcp] for s in chain(s2, rest))
  25.  
  26.  
  27. alltypes = st.one_of(st.none(),
  28. st.integers(),
  29. st.characters())
  30.  
  31.  
  32. @given(st.iterables(alltypes))
  33. def test_factor(seq):
  34. res = sut.factor(seq)
  35. assert "levels" in dir(res)
  36. assert "elements" in dir(res)
  37. assert isinstance(res.elements, list)
  38. assert isinstance(res.levels, dict)
  39. level_cursor = 0
  40. known = set()
  41. for i, elem in enumerate(seq):
  42. if elem not in known:
  43. assert elem in res.levels
  44. assert res.levels[elem] == level_cursor
  45. known.add(elem)
  46. level_cursor += 1
  47. assert res.elements[i] == res.levels[elem]
  48.  
  49.  
  50. @given(st.iterables(alltypes), st.integers(min_value=0))
  51. def test_chunked(iterable, n):
  52. it1, it2, it3 = tee(iterable, 3)
  53. res = sut.chunked(it1, n)
  54. if n == 0:
  55. assert list(res) == []
  56. return
  57. size = sum(1 for item in it2)
  58. n_chunks = math.ceil(size / n)
  59. i = 0
  60. for chunk in res:
  61. i += 1
  62. if i != n_chunks:
  63. assert len(chunk) == n
  64. else:
  65. assert len(chunk) == size % n
  66. for item in chunk:
  67. assert item == next(it3)
  68.  
  69.  
  70. class Strategy(Iterator):
  71.  
  72. def __or__(self, other):
  73. return OneOfStrategy(self, other)
  74.  
  75. def example(self):
  76. return next(self)
  77.  
  78.  
  79. class OneOfStrategy(Strategy):
  80.  
  81. def __init__(self, *strats):
  82. self.strats = strats
  83.  
  84. def __next__(self):
  85. return random.choice(self.strats).example()
  86.  
  87.  
  88. class ConstStrategy(Strategy):
  89.  
  90. def __init__(self, value):
  91. self.value = value
  92.  
  93. def __next__(self):
  94. return self.value
  95.  
  96.  
  97. class UniformFloatStrategy(Strategy):
  98.  
  99. def __next__(self):
  100. return random.random()
  101.  
  102.  
  103. class GaussianFloatStrategy(Strategy):
  104.  
  105. def __next__(self):
  106. return random.gauss(0, 1)
  107.  
  108.  
  109. class BoundedFloatStrategy(Strategy):
  110.  
  111. def __init__(self):
  112. self.min = sys.float_info.min
  113. self.max = sys.float_info.max
  114.  
  115. def __next__(self):
  116. x = random.uniform(self.min, self.max)
  117. while x in [self.min, self.max]:
  118. x = random.uniform(self.min, self.max)
  119. return x
  120.  
  121.  
  122. class NastyFloatStrategy(Strategy):
  123.  
  124. def __init__(self):
  125. self.values = [
  126. float("inf"),
  127. -float("inf"),
  128. float("nan"),
  129. +0.0,
  130. -0.0,
  131. sys.float_info.min,
  132. sys.float_info.max
  133. ]
  134.  
  135. def __next__(self):
  136. return random.choice(self.values)
  137.  
  138.  
  139. def floats():
  140. return (UniformFloatStrategy() | GaussianFloatStrategy() |
  141. BoundedFloatStrategy() | NastyFloatStrategy())
  142.  
  143.  
  144. def given(*strats, n_trials):
  145. def decorator(f):
  146. @functools.wraps(f)
  147. def inner():
  148. for _ in range(n_trials):
  149. testcase = [s.example() for s in strats]
  150. repl_test = ', '.join(str(t) for t in testcase)
  151. try:
  152. f(*testcase)
  153. except AssertionError:
  154. print(f"Counterexample: {f.__name__}({repl_test})",
  155. file=sys.stderr)
  156. raise AssertionError
  157. del inner.__dict__['__wrapped__']
  158. return inner
  159. return decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement