44use arbitrary:: Arbitrary ;
55use arbitrary:: Result ;
66use arbitrary:: Unstructured ;
7+ use arbitrary:: unstructured:: Int ;
78use vortex_array:: ArrayRef ;
89use vortex_array:: IntoArray ;
910use vortex_array:: arrays:: PrimitiveArray ;
@@ -13,6 +14,7 @@ use vortex_array::arrays::arbitrary::ArbitraryWith;
1314use vortex_array:: dtype:: DType ;
1415use vortex_array:: dtype:: Nullability ;
1516use vortex_array:: dtype:: PType ;
17+ use vortex_array:: dtype:: UnsignedPType ;
1618use vortex_array:: validity:: Validity ;
1719use vortex_buffer:: Buffer ;
1820use vortex_error:: VortexExpect ;
@@ -91,64 +93,72 @@ fn random_strictly_sorted_ends(
9193 target_len : usize ,
9294) -> Result < ArrayRef > {
9395 // Choose a random unsigned PType for ends
94- let ends_ptype = * u. choose ( & [ PType :: U8 , PType :: U16 , PType :: U32 , PType :: U64 ] ) ?;
96+ let mut ends_ptypes = vec ! [ PType :: U8 , PType :: U16 , PType :: U32 , PType :: U64 ] ;
97+ if target_len >= u8:: MAX as usize {
98+ ends_ptypes. remove ( 0 ) ;
99+ }
100+ if target_len >= u16:: MAX as usize {
101+ ends_ptypes. remove ( 0 ) ;
102+ }
103+ if target_len >= u32:: MAX as usize {
104+ ends_ptypes. remove ( 0 ) ;
105+ }
106+ let ends_ptype = * u. choose ( & ends_ptypes) ?;
107+
108+ match ends_ptype {
109+ PType :: U8 => random_strictly_sorted (
110+ u,
111+ num_runs,
112+ u8:: try_from ( target_len) . vortex_expect ( "must fit in u8" ) ,
113+ ) ,
114+ PType :: U16 => random_strictly_sorted (
115+ u,
116+ num_runs,
117+ u16:: try_from ( target_len) . vortex_expect ( "must fit in u16" ) ,
118+ ) ,
119+ PType :: U32 => random_strictly_sorted (
120+ u,
121+ num_runs,
122+ u32:: try_from ( target_len) . vortex_expect ( "must fit in u32" ) ,
123+ ) ,
124+ PType :: U64 => random_strictly_sorted (
125+ u,
126+ num_runs,
127+ u64:: try_from ( target_len) . vortex_expect ( "must fit in u64" ) ,
128+ ) ,
129+ _ => unreachable ! ( "Only unsigned integer types are valid for ends" ) ,
130+ }
131+ }
95132
133+ fn random_strictly_sorted < T : UnsignedPType + Int > (
134+ u : & mut Unstructured ,
135+ num_runs : usize ,
136+ target : T ,
137+ ) -> Result < ArrayRef > {
96138 // Generate strictly increasing values
97139 // Start from 0, increment by at least 1 each time
98- let mut ends: Vec < u64 > = Vec :: with_capacity ( num_runs) ;
99- let mut current: u64 = 0 ;
140+ let mut ends: Vec < T > = Vec :: with_capacity ( num_runs) ;
141+ let mut current = T :: zero ( ) ;
100142
101143 for i in 0 ..num_runs {
102144 // Each run must have at least length 1, so increment by at least 1
103145 let increment = match i == num_runs - 1 {
104146 true => {
105147 // Last element should reach target_len
106- let target = target_len as u64 ;
107148 if target > current {
108149 target - current
109150 } else {
110- 1
151+ T :: one ( )
111152 }
112153 }
113154 false => {
114155 // Random increment between 1 and 10
115- u. int_in_range ( 1 ..=10 ) ?
156+ u. int_in_range ( T :: one ( ) ..=T :: from ( 10 ) . vortex_expect ( "10 will fit in all T" ) ) ?
116157 }
117158 } ;
118159 current += increment;
119160 ends. push ( current) ;
120161 }
121162
122- // Convert to the chosen PType
123- // The values are bounded: max is num_runs (20) * max_increment (10) = 200
124- // This fits in all unsigned types
125- let ends_array = match ends_ptype {
126- PType :: U8 => {
127- let ends_typed: Vec < u8 > = ends
128- . iter ( )
129- . map ( |& e| u8:: try_from ( e) . vortex_expect ( "end value fits in u8" ) )
130- . collect ( ) ;
131- PrimitiveArray :: new ( Buffer :: copy_from ( ends_typed) , Validity :: NonNullable ) . into_array ( )
132- }
133- PType :: U16 => {
134- let ends_typed: Vec < u16 > = ends
135- . iter ( )
136- . map ( |& e| u16:: try_from ( e) . vortex_expect ( "end value fits in u16" ) )
137- . collect ( ) ;
138- PrimitiveArray :: new ( Buffer :: copy_from ( ends_typed) , Validity :: NonNullable ) . into_array ( )
139- }
140- PType :: U32 => {
141- let ends_typed: Vec < u32 > = ends
142- . iter ( )
143- . map ( |& e| u32:: try_from ( e) . vortex_expect ( "end value fits in u32" ) )
144- . collect ( ) ;
145- PrimitiveArray :: new ( Buffer :: copy_from ( ends_typed) , Validity :: NonNullable ) . into_array ( )
146- }
147- PType :: U64 => {
148- PrimitiveArray :: new ( Buffer :: copy_from ( ends) , Validity :: NonNullable ) . into_array ( )
149- }
150- _ => unreachable ! ( "Only unsigned integer types are valid for ends" ) ,
151- } ;
152-
153- Ok ( ends_array)
163+ Ok ( PrimitiveArray :: new ( Buffer :: copy_from ( ends) , Validity :: NonNullable ) . into_array ( ) )
154164}
0 commit comments