1use core::fmt::Debug;
2
3use proptest::{prelude::*, sample::SizeRange};
4
5use crate::{Item, SumTree, Summary};
6
7impl<T> Arbitrary for SumTree<T>
8where
9 T: Debug + Arbitrary + Item + 'static,
10 T::Summary: Debug + Summary<Context<'static> = ()>,
11{
12 type Parameters = ();
13 type Strategy = BoxedStrategy<Self>;
14
15 fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
16 any::<Vec<T>>()
17 .prop_map(|vec| SumTree::from_iter(vec, ()))
18 .boxed()
19 }
20}
21
22/// A strategy for producing a [`SumTree`] with a given size.
23///
24/// Equivalent to [`proptest::collection::vec`].
25pub fn sum_tree<S, T>(values: S, size: impl Into<SizeRange>) -> impl Strategy<Value = SumTree<T>>
26where
27 T: Debug + Arbitrary + Item + 'static,
28 T::Summary: Debug + Summary<Context<'static> = ()>,
29 S: Strategy<Value = T>,
30{
31 proptest::collection::vec(values, size).prop_map(|vec| SumTree::from_iter(vec, ()))
32}