Skip to content

Commit

Permalink
Complete strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Apr 5, 2023
1 parent 5ebd5eb commit f11c7ce
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 66 deletions.
117 changes: 68 additions & 49 deletions tests/strategies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import partial
from operator import not_

from hypothesis import strategies
from hypothesis import strategies as _st

from prioq.base import PriorityQueue
from tests.utils import (PriorityQueuesPair,
Expand All @@ -17,54 +17,71 @@
to_values_tuples_with_keys,
to_values_with_keys)

booleans = strategies.booleans()
maybe_infinite_numbers_keys = strategies.sampled_from([identity, abs])
booleans = _st.booleans()
maybe_infinite_numbers_keys = _st.sampled_from([identity, abs])
finite_numbers_keys = (maybe_infinite_numbers_keys
| strategies.sampled_from([round, math.trunc, math.ceil,
math.floor]))
strings_keys = strategies.sampled_from([identity, str.lower, str.upper,
str.title, str.capitalize,
str.casefold, str.swapcase])
| _st.sampled_from([round, math.trunc, math.ceil,
math.floor]))
strings_keys = _st.sampled_from([identity, str.lower, str.upper,
str.title, str.capitalize,
str.casefold, str.swapcase])

base_values_with_keys_strategies = strategies.sampled_from(
[(strategies.integers(), finite_numbers_keys),
(strategies.floats(allow_nan=False), maybe_infinite_numbers_keys),
(strategies.floats(allow_nan=False,
allow_infinity=False),
finite_numbers_keys),
(strategies.booleans(), strategies.just(not_) | finite_numbers_keys),
(strategies.text(), strings_keys)])
values_with_keys_strategies = (strategies
.recursive(base_values_with_keys_strategies,
to_values_tuples_with_keys,
max_leaves=10))
values_with_keys = values_with_keys_strategies.flatmap(to_values_with_keys)
values_lists_with_keys = (values_with_keys_strategies
.flatmap(to_values_lists_with_keys))
empty_values_lists_with_keys = (values_with_keys_strategies
.flatmap(partial(to_values_lists_with_keys,
sizes=[(0, 0)])))
non_empty_values_lists_with_keys = (values_with_keys_strategies
.flatmap(partial(to_values_lists_with_keys,
sizes=[(1, None)])))
single_values_with_keys = (values_with_keys_strategies
.flatmap(partial(to_values_lists_with_keys,
sizes=[(1, 1)])))
priority_queues = strategies.builds(to_priority_queue,
values_lists_with_keys, booleans)
empty_priority_queues = strategies.builds(to_priority_queue,
empty_values_lists_with_keys,
booleans)
non_empty_priority_queues = strategies.builds(to_priority_queue,
non_empty_values_lists_with_keys,
booleans)
priority_queues_with_values = strategies.builds(
sortables_with_keys = {
_st.integers(): finite_numbers_keys,
_st.floats(allow_nan=False): maybe_infinite_numbers_keys,
_st.floats(allow_nan=False,
allow_infinity=False): finite_numbers_keys,
_st.booleans(): _st.just(not_) | finite_numbers_keys,
_st.text(): strings_keys
}
values_keys = {**sortables_with_keys,
_st.from_type(object): _st.sampled_from([id, lambda _: 0])}

base_values_with_keys_strategies = _st.sampled_from(list(values_keys.items()))
values_with_keys_strategies = (
_st.sampled_from([(sortables, _st.none())
for sortables in sortables_with_keys.keys()])
| _st.recursive(base_values_with_keys_strategies,
to_values_tuples_with_keys,
max_leaves=10)
)
values_with_keys = (
(_st.sampled_from(list(sortables_with_keys.keys()))
.flatmap(lambda values: _st.tuples(values, _st.none())))
| values_with_keys_strategies.flatmap(to_values_with_keys)
)
values_lists_with_keys = (
values_with_keys_strategies.flatmap(to_values_lists_with_keys)
)
empty_values_lists_with_keys = (
values_with_keys_strategies.flatmap(partial(to_values_lists_with_keys,
sizes=[(0, 0)]))
)
non_empty_values_lists_with_keys = (
values_with_keys_strategies.flatmap(partial(to_values_lists_with_keys,
sizes=[(1, None)]))
)
single_values_with_keys = (
values_with_keys_strategies.flatmap(partial(to_values_lists_with_keys,
sizes=[(1, 1)]))
)
priority_queues = _st.builds(to_priority_queue, values_lists_with_keys,
booleans)
empty_priority_queues = _st.builds(to_priority_queue,
empty_values_lists_with_keys, booleans)
non_empty_priority_queues = _st.builds(to_priority_queue,
non_empty_values_lists_with_keys,
booleans)
priority_queues_with_values = _st.builds(
to_priority_queue_with_value, non_empty_values_lists_with_keys,
booleans)
empty_priority_queues_with_values = strategies.builds(
to_priority_queue_with_value, single_values_with_keys, booleans)
booleans
)
empty_priority_queues_with_values = _st.builds(
to_priority_queue_with_value, single_values_with_keys, booleans
)
non_empty_priority_queues_with_their_values = (
non_empty_priority_queues.flatmap(to_priority_queues_with_their_values))
non_empty_priority_queues.flatmap(to_priority_queues_with_their_values)
)


def to_priority_queues_pair(values_lists_pair_with_key: ValuesListsPairWithKey,
Expand All @@ -80,11 +97,12 @@ def to_priority_queues_pair(values_lists_pair_with_key: ValuesListsPairWithKey,
return first_priority_queue, second_priority_queue


priority_queues_pairs = strategies.builds(
priority_queues_pairs = _st.builds(
to_priority_queues_pair,
values_with_keys_strategies.flatmap(partial(to_values_lists_with_keys,
sizes=[(0, None)] * 2)),
booleans, booleans)
booleans, booleans
)


def to_priority_queues_triplet(
Expand All @@ -106,8 +124,9 @@ def to_priority_queues_triplet(
return first_priority_queue, second_priority_queue, third_priority_queue


priority_queues_triplets = strategies.builds(
priority_queues_triplets = _st.builds(
to_priority_queues_triplet,
values_with_keys_strategies.flatmap(partial(to_values_lists_with_keys,
sizes=[(0, None)] * 3)),
booleans, booleans, booleans)
booleans, booleans, booleans
)
36 changes: 19 additions & 17 deletions tests/strategies/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@


def to_values_tuples_with_keys(
values_with_keys: Strategy[Tuple[Strategy[Value],
Strategy[SortingKey]]]
values_with_keys: Strategy[
Tuple[Strategy[Value], Strategy[SortingKey]]
]
) -> Strategy[Tuple[Strategy[Tuple[Value, ...]], Strategy[SortingKey]]]:
def to_values_tuples_with_key(
values_with_keys_list: List[Strategy[Tuple[Value, SortingKey]]]
Expand All @@ -37,11 +38,11 @@ def combined(keys: Sequence[SortingKey], values: Sequence[Value]) -> Key:
return tuple(key(arg) for key, arg in zip(keys, values))


def to_values_with_keys(values_with_keys: Tuple[Strategy[Value],
Strategy[SortingKey]]
) -> Strategy[Tuple[Value, Optional[SortingKey]]]:
def to_values_with_keys(
values_with_keys: Tuple[Strategy[Value], Strategy[SortingKey]]
) -> Strategy[Tuple[Value, Optional[SortingKey]]]:
values, keys = values_with_keys
return strategies.tuples(values, strategies.none() | keys)
return strategies.tuples(values, keys)


def to_values_lists_with_keys(
Expand All @@ -54,22 +55,23 @@ def to_values_lists_with_keys(
min_size=min_size,
max_size=max_size)
for min_size, max_size in sizes]
return strategies.tuples(*lists_strategies, strategies.none() | keys)
return strategies.tuples(*lists_strategies, keys)


def to_priority_queue(values_with_key: Tuple[List[Value],
Optional[SortingKey]],
reverse: bool) -> PriorityQueue:
def to_priority_queue(
values_with_key: Tuple[List[Value], Optional[SortingKey]],
reverse: bool
) -> PriorityQueue:
values, key = values_with_key
return PriorityQueue(*values,
key=key,
reverse=reverse)


def to_priority_queue_with_value(values_with_key: Tuple[List[Value],
Optional[SortingKey]],
reverse: bool
) -> Tuple[PriorityQueue, Value]:
def to_priority_queue_with_value(
values_with_key: Tuple[List[Value], Optional[SortingKey]],
reverse: bool
) -> Tuple[PriorityQueue, Value]:
values, key = values_with_key
value, *rest_values = values
return (PriorityQueue(*rest_values,
Expand All @@ -78,8 +80,8 @@ def to_priority_queue_with_value(values_with_key: Tuple[List[Value],
value)


def to_priority_queues_with_their_values(queue: PriorityQueue
) -> Strategy[Tuple[PriorityQueue,
Value]]:
def to_priority_queues_with_their_values(
queue: PriorityQueue
) -> Strategy[Tuple[PriorityQueue, Value]]:
return strategies.tuples(strategies.just(queue),
strategies.sampled_from(queue.values()))

0 comments on commit f11c7ce

Please sign in to comment.