// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use bitmaps::Bits; use core::iter; use ::arbitrary::{size_hint, Arbitrary, Result, Unstructured}; use crate::{types::ChunkLength, Chunk, InlineArray, SparseChunk}; #[cfg(feature = "ringbuffer")] use crate::RingBuffer; fn empty() -> Box> { Box::new(iter::empty()) } fn shrink_collection( entries: impl Iterator, f: impl Fn(&T) -> Box>, ) -> Box>> { let entries: Vec<_> = entries.collect(); if entries.is_empty() { return empty(); } let mut shrinkers: Vec> = vec![]; let mut i = entries.len(); loop { shrinkers.push(entries.iter().take(i).map(&f).collect()); i /= 2; if i == 0 { break; } } Box::new(iter::once(Vec::new()).chain(iter::from_fn(move || loop { let mut shrinker = shrinkers.pop()?; let x: Option> = shrinker.iter_mut().map(|s| s.next()).collect(); if x.is_none() { continue; } shrinkers.push(shrinker); return x; }))) } impl Arbitrary for Chunk where A: Arbitrary, N: ChunkLength + 'static, { fn arbitrary(u: &mut Unstructured<'_>) -> Result { u.arbitrary_iter()?.take(Self::CAPACITY).collect() } fn arbitrary_take_rest(u: Unstructured<'_>) -> Result { u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect() } fn size_hint(depth: usize) -> (usize, Option) { size_hint::recursion_guard(depth, |depth| { let (_, upper) = A::size_hint(depth); (0, upper.map(|upper| upper * Self::CAPACITY)) }) } fn shrink(&self) -> Box> { let collections = shrink_collection(self.iter(), |x| x.shrink()); Box::new(collections.map(|entries| entries.into_iter().collect())) } } #[cfg(feature = "ringbuffer")] impl Arbitrary for RingBuffer where A: Arbitrary, N: ChunkLength + 'static, { fn arbitrary(u: &mut Unstructured<'_>) -> Result { u.arbitrary_iter()?.take(Self::CAPACITY).collect() } fn arbitrary_take_rest(u: Unstructured<'_>) -> Result { u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect() } fn size_hint(depth: usize) -> (usize, Option) { size_hint::recursion_guard(depth, |depth| { let (_, upper) = A::size_hint(depth); (0, upper.map(|upper| upper * Self::CAPACITY)) }) } fn shrink(&self) -> Box> { let collections = shrink_collection(self.iter(), |x| x.shrink()); Box::new(collections.map(|entries| entries.into_iter().collect())) } } impl Arbitrary for SparseChunk where A: Clone, Option: Arbitrary, N: ChunkLength + Bits + 'static, { fn arbitrary(u: &mut Unstructured<'_>) -> Result { u.arbitrary_iter()?.take(Self::CAPACITY).collect() } fn arbitrary_take_rest(u: Unstructured<'_>) -> Result { u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect() } fn size_hint(depth: usize) -> (usize, Option) { size_hint::recursion_guard(depth, |depth| { let (_, upper) = Option::::size_hint(depth); (0, upper.map(|upper| upper * Self::CAPACITY)) }) } fn shrink(&self) -> Box> { let collections = shrink_collection(self.clone().option_drain(), |x| x.shrink()); Box::new(collections.map(|entries| entries.into_iter().collect())) } } impl Arbitrary for InlineArray where A: Arbitrary, T: 'static, { fn arbitrary(u: &mut Unstructured<'_>) -> Result { u.arbitrary_iter()?.take(Self::CAPACITY).collect() } fn arbitrary_take_rest(u: Unstructured<'_>) -> Result { u.arbitrary_take_rest_iter()?.take(Self::CAPACITY).collect() } fn size_hint(depth: usize) -> (usize, Option) { size_hint::recursion_guard(depth, |depth| { let (_, upper) = A::size_hint(depth); (0, upper.map(|upper| upper * Self::CAPACITY)) }) } fn shrink(&self) -> Box> { let collections = shrink_collection(self.iter(), |x| x.shrink()); Box::new(collections.map(|entries| entries.into_iter().collect())) } }