|
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
4 | 4 | use std::iter; |
| 5 | +use std::ops::RangeInclusive; |
5 | 6 | use std::sync::Arc; |
6 | 7 |
|
7 | 8 | use arbitrary::Arbitrary; |
| 9 | +use arbitrary::Error::IncorrectFormat; |
8 | 10 | use arbitrary::Result; |
9 | 11 | use arbitrary::Unstructured; |
10 | 12 | use vortex_buffer::BitBuffer; |
@@ -41,16 +43,37 @@ use crate::validity::Validity; |
41 | 43 | #[derive(Clone, Debug)] |
42 | 44 | pub struct ArbitraryArray(pub ArrayRef); |
43 | 45 |
|
44 | | -impl<'a> Arbitrary<'a> for ArbitraryArray { |
45 | | - fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
46 | | - let dtype = u.arbitrary()?; |
47 | | - Self::arbitrary_with(u, None, &dtype) |
48 | | - } |
| 46 | +/// Trait for generating arbitrary values with a caller-provided configuration. |
| 47 | +pub trait ArbitraryWith<'a, C>: Sized { |
| 48 | + /// Generate an arbitrary value using the provided configuration. |
| 49 | + fn arbitrary_with_config(u: &mut Unstructured<'a>, config: &C) -> Result<Self>; |
| 50 | +} |
| 51 | + |
| 52 | +/// Configuration for arbitrary array generation. |
| 53 | +#[derive(Clone, Debug)] |
| 54 | +pub struct ArbitraryArrayConfig { |
| 55 | + /// Fixed dtype, or `None` to generate one from [`Unstructured`]. |
| 56 | + pub dtype: Option<DType>, |
| 57 | + /// Inclusive range for the total array length. |
| 58 | + pub len: RangeInclusive<usize>, |
49 | 59 | } |
50 | 60 |
|
51 | | -impl ArbitraryArray { |
52 | | - pub fn arbitrary_with(u: &mut Unstructured, len: Option<usize>, dtype: &DType) -> Result<Self> { |
53 | | - random_array(u, dtype, len).map(ArbitraryArray) |
| 61 | +impl<'a> ArbitraryWith<'a, ArbitraryArrayConfig> for ArbitraryArray { |
| 62 | + fn arbitrary_with_config( |
| 63 | + u: &mut Unstructured<'a>, |
| 64 | + config: &ArbitraryArrayConfig, |
| 65 | + ) -> Result<Self> { |
| 66 | + if config.len.is_empty() { |
| 67 | + return Err(IncorrectFormat); |
| 68 | + } |
| 69 | + |
| 70 | + let dtype = match &config.dtype { |
| 71 | + Some(dtype) => dtype.clone(), |
| 72 | + None => u.arbitrary()?, |
| 73 | + }; |
| 74 | + let len = u.int_in_range(config.len.clone())?; |
| 75 | + |
| 76 | + random_array(u, &dtype, Some(len)).map(ArbitraryArray) |
54 | 77 | } |
55 | 78 | } |
56 | 79 |
|
|
0 commit comments