Skip to content

Commit 04fc7f6

Browse files
skip[fuzz]: fix random_list gen (#7875)
This fixes the random_list generator used in Arbitrary. We should likely rewrite all of this to be easier to use. Signed-off-by: Joe Isaacs <joe.isaacs@live.co.uk>
1 parent 1e7ad82 commit 04fc7f6

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

vortex-array/src/arrays/arbitrary.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,29 +235,43 @@ fn random_list(
235235
null: Nullability,
236236
chunk_len: Option<usize>,
237237
) -> Result<ArrayRef> {
238+
let array_length = chunk_len.unwrap_or(u.int_in_range(0..=20)?);
239+
// Worst-case total elements: each list can have up to 20 elements.
240+
let max_total_elements = array_length as u64 * 20;
241+
238242
match u.int_in_range(0..=5)? {
239-
0 => random_list_with_offset_type::<i16>(u, elem_dtype, null, chunk_len),
240-
1 => random_list_with_offset_type::<i32>(u, elem_dtype, null, chunk_len),
241-
2 => random_list_with_offset_type::<i64>(u, elem_dtype, null, chunk_len),
242-
3 => random_list_with_offset_type::<u16>(u, elem_dtype, null, chunk_len),
243-
4 => random_list_with_offset_type::<u32>(u, elem_dtype, null, chunk_len),
244-
5 => random_list_with_offset_type::<u64>(u, elem_dtype, null, chunk_len),
245-
_ => unreachable!("int_in_range returns a value in the above range"),
243+
0 if i16::max_value_as_u64() >= max_total_elements => {
244+
random_list_with_offset_type::<i16>(u, elem_dtype, null, array_length)
245+
}
246+
1 if i32::max_value_as_u64() >= max_total_elements => {
247+
random_list_with_offset_type::<i32>(u, elem_dtype, null, array_length)
248+
}
249+
3 if u16::max_value_as_u64() >= max_total_elements => {
250+
random_list_with_offset_type::<u16>(u, elem_dtype, null, array_length)
251+
}
252+
4 if u32::max_value_as_u64() >= max_total_elements => {
253+
random_list_with_offset_type::<u32>(u, elem_dtype, null, array_length)
254+
}
255+
// i64 and u64 always fit; also the fallback for when narrower types don't.
256+
_ => {
257+
if u.arbitrary::<bool>()? {
258+
random_list_with_offset_type::<i64>(u, elem_dtype, null, array_length)
259+
} else {
260+
random_list_with_offset_type::<u64>(u, elem_dtype, null, array_length)
261+
}
262+
}
246263
}
247264
}
248265

249266
/// Creates a random list array with the given [`IntegerPType`] for the internal offsets child.
250-
///
251-
/// If the `chunk_len` is specified, the length of the array will be equal to the chunk length.
252267
fn random_list_with_offset_type<O: IntegerPType>(
253268
u: &mut Unstructured,
254269
elem_dtype: &Arc<DType>,
255270
null: Nullability,
256-
chunk_len: Option<usize>,
271+
array_length: usize,
257272
) -> Result<ArrayRef> {
258-
let array_length = chunk_len.unwrap_or(u.int_in_range(0..=20)?);
259-
260-
let mut builder = ListViewBuilder::<O, O>::with_capacity(Arc::clone(elem_dtype), null, 20, 10);
273+
let mut builder =
274+
ListViewBuilder::<O, O>::with_capacity(Arc::clone(elem_dtype), null, array_length, 10);
261275

262276
for _ in 0..array_length {
263277
if null == Nullability::Nullable && u.arbitrary::<bool>()? {

0 commit comments

Comments
 (0)