misc.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # coding=utf-8
  2. #
  3. # This file is part of Hypothesis, which may be found at
  4. # https://github.com/HypothesisWorks/hypothesis-python
  5. #
  6. # Most of this work is copyright (C) 2013-2018 David R. MacIver
  7. # (david@drmaciver.com), but it contains contributions by others. See
  8. # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9. # consult the git log if you need to determine who owns an individual
  10. # contribution.
  11. #
  12. # This Source Code Form is subject to the terms of the Mozilla Public License,
  13. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14. # obtain one at http://mozilla.org/MPL/2.0/.
  15. #
  16. # END HEADER
  17. from __future__ import division, print_function, absolute_import
  18. import hypothesis.internal.conjecture.utils as d
  19. from hypothesis.types import RandomWithSeed
  20. from hypothesis.searchstrategy.strategies import SearchStrategy, \
  21. MappedSearchStrategy
  22. class BoolStrategy(SearchStrategy):
  23. """A strategy that produces Booleans with a Bernoulli conditional
  24. distribution."""
  25. def __repr__(self):
  26. return u'BoolStrategy()'
  27. def calc_has_reusable_values(self, recur):
  28. return True
  29. def do_draw(self, data):
  30. return d.boolean(data)
  31. def is_simple_data(value):
  32. try:
  33. hash(value)
  34. return True
  35. except TypeError:
  36. return False
  37. class JustStrategy(SearchStrategy):
  38. """A strategy which simply returns a single fixed value with probability
  39. 1."""
  40. def __init__(self, value):
  41. SearchStrategy.__init__(self)
  42. self.value = value
  43. def __repr__(self):
  44. return 'just(%r)' % (self.value,)
  45. def calc_has_reusable_values(self, recur):
  46. return True
  47. def calc_is_cacheable(self, recur):
  48. return is_simple_data(self.value)
  49. def do_draw(self, data):
  50. return self.value
  51. class RandomStrategy(MappedSearchStrategy):
  52. """A strategy which produces Random objects.
  53. The conditional distribution is simply a RandomWithSeed seeded with
  54. a 128 bits of data chosen uniformly at random.
  55. """
  56. def pack(self, i):
  57. return RandomWithSeed(i)
  58. class SampledFromStrategy(SearchStrategy):
  59. """A strategy which samples from a set of elements. This is essentially
  60. equivalent to using a OneOfStrategy over Just strategies but may be more
  61. efficient and convenient.
  62. The conditional distribution chooses uniformly at random from some
  63. non-empty subset of the elements.
  64. """
  65. def __init__(self, elements):
  66. SearchStrategy.__init__(self)
  67. self.elements = d.check_sample(elements)
  68. assert self.elements
  69. def calc_has_reusable_values(self, recur):
  70. return True
  71. def calc_is_cacheable(self, recur):
  72. return is_simple_data(self.elements)
  73. def do_draw(self, data):
  74. return d.choice(data, self.elements)