Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/array/list/proptest.rs
6939 views
1
use proptest::prelude::{Strategy, any, any_with};
2
use proptest::sample::SizeRange;
3
4
use super::ListArray;
5
use crate::bitmap::Bitmap;
6
use crate::datatypes::{ArrowDataType, Field};
7
use crate::offset::OffsetsBuffer;
8
9
pub fn list_array_with_dtype(
10
size_range: impl Into<SizeRange>,
11
field: Box<Field>,
12
) -> impl Strategy<Value = ListArray<i64>> {
13
let size_range = size_range.into();
14
(
15
any::<bool>(),
16
any_with::<Vec<(bool, u32)>>(size_range.lift()),
17
)
18
.prop_flat_map(move |(do_validity, values)| {
19
let field = field.clone();
20
let validity = do_validity.then(|| Bitmap::from_iter(values.iter().map(|(v, _)| *v)));
21
22
let mut offsets = Vec::with_capacity(values.len() + 1);
23
offsets.push(0i64);
24
for (_, value) in &values {
25
let value = *value % 7;
26
offsets.push(*offsets.last().unwrap() + value as i64);
27
}
28
29
let child_length = *offsets.last().unwrap();
30
let offsets = unsafe { OffsetsBuffer::new_unchecked(offsets.into()) };
31
32
crate::array::proptest::array_with_dtype(field.dtype.clone(), child_length as usize)
33
.prop_map(move |child_array| {
34
ListArray::<i64>::new(
35
ArrowDataType::LargeList(field.clone()),
36
offsets.clone(),
37
child_array,
38
validity.clone(),
39
)
40
})
41
})
42
}
43
44