Exemple #1
0
    def _hypothesis_do_random(self, method, kwargs):
        if method == "choices":
            key = (method, len(kwargs["population"]), kwargs.get("k"))
        elif method == "choice":
            key = (method, len(kwargs["seq"]))
        elif method == "shuffle":
            key = (method, len(kwargs["x"]))
        else:
            key = (method, ) + tuple(sorted(kwargs))

        try:
            result, self.__state = self.__state.next_states[key]
        except KeyError:
            pass
        else:
            return self.__convert_result(method, kwargs, result)

        if method == "_randbelow":
            result = cu.integer_range(self.__data, 0, kwargs["n"] - 1)
        elif method in ("betavariate", "random"):
            result = self.__data.draw(UNIFORM)
        elif method == "uniform":
            a = normalize_zero(kwargs["a"])
            b = normalize_zero(kwargs["b"])
            result = self.__data.draw(st.floats(a, b))
        elif method in ("weibullvariate", "gammavariate"):
            result = self.__data.draw(
                st.floats(min_value=0.0, allow_infinity=False))
        elif method in ("gauss", "normalvariate"):
            mu = kwargs["mu"]
            result = mu + self.__data.draw(
                st.floats(allow_nan=False, allow_infinity=False))
        elif method == "vonmisesvariate":
            result = self.__data.draw(st.floats(0, 2 * math.pi))
        elif method == "randrange":
            if kwargs["stop"] is None:
                stop = kwargs["start"]
                start = 0
            else:
                start = kwargs["start"]
                stop = kwargs["stop"]

            step = kwargs["step"]
            if start == stop:
                raise ValueError("empty range for randrange(%d, %d, %d)" %
                                 (start, stop, step))

            if step != 1:
                endpoint = (stop - start) // step
                if (start - stop) % step == 0:
                    endpoint -= 1

                i = cu.integer_range(self.__data, 0, endpoint)
                result = start + i * step
            else:
                result = cu.integer_range(self.__data, start, stop - 1)
        elif method == "randint":
            result = cu.integer_range(self.__data, kwargs["a"], kwargs["b"])
        elif method == "choice":
            seq = kwargs["seq"]
            result = cu.integer_range(self.__data, 0, len(seq) - 1)
        elif method == "choices":
            k = kwargs["k"]
            result = self.__data.draw(
                st.lists(
                    st.integers(0,
                                len(kwargs["population"]) - 1),
                    min_size=k,
                    max_size=k,
                ))
        elif method == "sample":
            k = kwargs["k"]
            seq = kwargs["population"]

            if k > len(seq) or k < 0:
                raise ValueError(
                    "Sample size %d not in expected range 0 <= k <= %d" %
                    (k, len(seq)))

            result = self.__data.draw(
                st.lists(
                    st.sampled_from(range(len(seq))),
                    min_size=k,
                    max_size=k,
                    unique=True,
                ))

        elif method == "getrandbits":
            result = self.__data.draw_bits(kwargs["n"])
        elif method == "triangular":
            low = normalize_zero(kwargs["low"])
            high = normalize_zero(kwargs["high"])
            mode = normalize_zero(kwargs["mode"])
            if mode is None:
                result = self.__data.draw(st.floats(low, high))
            elif self.__data.draw_bits(1):
                result = self.__data.draw(st.floats(mode, high))
            else:
                result = self.__data.draw(st.floats(low, mode))
        elif method in ("paretovariate", "expovariate", "lognormvariate"):
            result = self.__data.draw(st.floats(min_value=0.0))
        elif method == "shuffle":
            result = self.__data.draw(st.permutations(range(len(kwargs["x"]))))
        # This is tested for but only appears in 3.9 so doesn't appear in coverage.
        elif method == "randbytes":  # pragma: no cover
            n = kwargs["n"]
            result = self.__data.draw(st.binary(min_size=n, max_size=n))
        else:
            raise NotImplementedError(method)

        new_state = RandomState()
        self.__state.next_states[key] = (result, new_state)
        self.__state = new_state

        return self.__convert_result(method, kwargs, result)
Exemple #2
0
    try:
        seeds_to_states = data.seeds_to_states
    except AttributeError:
        seeds_to_states = {}
        data.seeds_to_states = seeds_to_states

    try:
        state = seeds_to_states[seed]
    except KeyError:
        state = RandomState()
        seeds_to_states[seed] = state

    return state


UNIFORM = st.floats(0, 1)


def normalize_zero(f):
    if f == 0.0:
        return 0.0
    else:
        return f


class ArtificialRandom(HypothesisRandom):
    VERSION = 10**6

    def __init__(self, note_method_calls, data):
        HypothesisRandom.__init__(self, note_method_calls=note_method_calls)
        self.__data = data