1 // Regression test for #53789.
6 use std::collections::BTreeMap;
9 macro_rules! valuetree {
11 type ValueTree = <Self::Strategy as $crate::Strategy>::Value;
15 macro_rules! product_unpack {
19 ($($factor: pat),*) => {
22 ($($factor: pat),*,) => {
27 macro_rules! product_type {
31 ($($factor: ty),*) => {
34 ($($factor: ty),*,) => {
39 macro_rules! default {
40 ($type: ty, $val: expr) => {
41 impl Default for $type {
42 fn default() -> Self {
49 // Pervasive internal sugar
51 ($(#[$meta:meta])* [$($vis:tt)*]
52 fn $name:ident[$($gen:tt)*]($parm:ident: $input:ty) -> $output:ty {
56 #[derive(Clone, Copy)]
57 $($vis)* struct $name;
58 impl $($gen)* statics::MapFn<$input> for $name {
59 type Output = $output;
64 macro_rules! opaque_strategy_wrapper {
65 ($(#[$smeta:meta])* pub struct $stratname:ident
66 [$($sgen:tt)*][$($swhere:tt)*]
67 ($innerstrat:ty) -> $stratvtty:ty;
69 $(#[$vmeta:meta])* pub struct $vtname:ident
70 [$($vgen:tt)*][$($vwhere:tt)*]
71 ($innervt:ty) -> $actualty:ty;
73 $(#[$smeta])* struct $stratname $($sgen)* (std::marker::PhantomData<(K, V)>)
76 $(#[$vmeta])* struct $vtname $($vgen)* ($innervt) $($vwhere)*;
78 impl $($sgen)* Strategy for $stratname $($sgen)* $($swhere)* {
79 type Value = $stratvtty;
82 impl $($vgen)* ValueTree for $vtname $($vgen)* $($vwhere)* {
83 type Value = $actualty;
93 type Value: ValueTree;
97 struct VecStrategy<T: Strategy> {
102 fn vec<T: Strategy>(element: T, size: Range<usize>) -> VecStrategy<T> {
103 VecStrategy { element: element, size: size }
106 type ValueFor<S> = <<S as Strategy>::Value as ValueTree>::Value;
108 trait Arbitrary<'a>: Sized {
109 fn arbitrary_with(args: Self::Parameters) -> Self::Strategy;
111 type Parameters: Default;
112 type Strategy: Strategy<Value = Self::ValueTree>;
113 type ValueTree: ValueTree<Value = Self>;
116 type StrategyFor<A> = StrategyType<'static, A>;
117 type StrategyType<'a, A> = <A as Arbitrary<'a>>::Strategy;
119 //#[derive(Clone, PartialEq, Eq, Hash, Debug, From, Into)]
120 struct SizeBounds(Range<usize>);
121 default!(SizeBounds, 0..100);
123 impl From<Range<usize>> for SizeBounds {
124 fn from(high: Range<usize>) -> Self {
129 impl From<SizeBounds> for Range<usize> {
130 fn from(high: SizeBounds) -> Self {
135 fn any_with<'a, A: Arbitrary<'a>>(args: A::Parameters) -> StrategyType<'a, A> {
139 impl<K: ValueTree, V: ValueTree> Strategy for (K, V)
141 <K as ValueTree>::Value: Ord,
143 type Value = TupleValueTree<(K, V)>;
146 impl<K: ValueTree, V: ValueTree> ValueTree for TupleValueTree<(K, V)>
148 <K as ValueTree>::Value: Ord,
150 type Value = BTreeMapValueTree<K, V>;
154 struct VecValueTree<T: ValueTree> {
158 #[derive(Clone, Copy)]
159 struct TupleValueTree<T> {
163 opaque_strategy_wrapper! {
165 pub struct BTreeMapStrategy[<K, V>]
166 [where K : Strategy, V : Strategy, ValueFor<K> : Ord](
167 statics::Filter<statics::Map<VecStrategy<(K,V)>,
168 VecToBTreeMap>, MinSize>)
169 -> BTreeMapValueTree<K::Value, V::Value>;
172 pub struct BTreeMapValueTree[<K, V>]
173 [where K : ValueTree, V : ValueTree, K::Value : Ord](
174 statics::Filter<statics::Map<VecValueTree<TupleValueTree<(K, V)>>,
175 VecToBTreeMap>, MinSize>)
176 -> BTreeMap<K::Value, V::Value>;
179 type RangedParams2<A, B> = product_type![SizeBounds, A, B];
181 impl<'a, A, B> Arbitrary<'a> for BTreeMap<A, B>
183 A: Arbitrary<'static> + Ord,
184 B: Arbitrary<'static>,
185 StrategyFor<A>: 'static,
186 StrategyFor<B>: 'static,
189 type Parameters = RangedParams2<A::Parameters, B::Parameters>;
190 type Strategy = BTreeMapStrategy<A::Strategy, B::Strategy>;
191 fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
192 let product_unpack![range, a, b] = args;
193 btree_map(any_with::<A>(a), any_with::<B>(b), range.into())
197 #[derive(Clone, Copy)]
198 struct MinSize(usize);
201 [] fn VecToBTreeMap[<K : Ord, V>]
202 (vec: Vec<(K, V)>) -> BTreeMap<K, V>
204 vec.into_iter().collect()
208 fn btree_map<K: Strategy + 'static, V: Strategy + 'static>(
212 ) -> BTreeMapStrategy<K, V>
220 pub(super) trait MapFn<T> {
225 pub struct Filter<S, F> {
230 impl<S, F> Filter<S, F> {
231 pub fn new(source: S, whence: String, filter: F) -> Self {
237 pub struct Map<S, F> {
242 impl<S, F> Map<S, F> {
243 pub fn new(source: S, fun: F) -> Self {