diff --git a/src/lib.rs b/src/lib.rs index b4bc06da..c5a0a9a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,22 @@ impl FillValue { use IcechunkFormatError::FillValueDecodeError; match dt { + DataType::Bool => { + let value = value.to_owned(); + if value.len() != 1 { + Err(FillValueDecodeError{found_size: value.len(), target_size: 1, target_type: DataType::Bool}) + } else { + Ok(FillValue::Bool(value[0] != 0)) + } + }, + DataType::Int8 => value + .try_into() + .map(|x| FillValue::Int8(i8::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 1, target_type: DataType::Int8}), + DataType::Int16 => value + .try_into() + .map(|x| FillValue::Int16(i16::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 2, target_type: DataType::Int16}), DataType::Int32 => value .try_into() .map(|x| FillValue::Int32(i32::from_be_bytes(x))) @@ -193,6 +209,26 @@ impl FillValue { .try_into() .map(|x| FillValue::Int64(i64::from_be_bytes(x))) .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 8, target_type: DataType::Int64}), + DataType::UInt8 => value + .try_into() + .map(|x| FillValue::UInt8(u8::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 1, target_type: DataType::UInt8}), + DataType::UInt16 => value + .try_into() + .map(|x| FillValue::UInt16(u16::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 2, target_type: DataType::UInt16}), + DataType::UInt32 => value + .try_into() + .map(|x| FillValue::UInt32(u32::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::UInt32}), + DataType::UInt64 => value + .try_into() + .map(|x| FillValue::UInt64(u64::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 8, target_type: DataType::UInt64}), + DataType::Float16 => value + .try_into() + .map(|x| FillValue::Float16(f32::from_be_bytes(x))) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::Float16}), DataType::Float32 => value .try_into() .map(|x| FillValue::Float32(f32::from_be_bytes(x))) @@ -208,37 +244,66 @@ impl FillValue { let r = value[..4] .try_into() .map(f32::from_be_bytes) - .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::Float32}); + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::Complex64}); let i = value[4..] .try_into() .map(f32::from_be_bytes) - .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::Float32}); + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 4, target_type: DataType::Complex64}); Ok(FillValue::Complex64(r?, i?)) } }, + DataType::Complex128 => { + if value.len() != 16 { + Err(FillValueDecodeError{found_size: value.len(), target_size: 16, target_type: DataType::Complex64}) } + else { + let r = value[..8] + .try_into() + .map(f64::from_be_bytes) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 8, target_type: DataType::Complex128}); + let i = value[8..] + .try_into() + .map(f64::from_be_bytes) + .map_err(|_| FillValueDecodeError{found_size: value.len(), target_size: 8, target_type: DataType::Complex128}); + Ok(FillValue::Complex128(r?, i?)) + } + }, DataType::RawBits(_) => Ok(FillValue::RawBits(value.to_owned())), - _ => todo!(), - // DataType::Complex64 => value - // .try_into() } } fn get_data_type(&self) -> DataType { match self { + FillValue::Bool(_) => DataType::Bool, + FillValue::Int8(_) => DataType::Int8, + FillValue::Int16(_) => DataType::Int16, FillValue::Int32(_) => DataType::Int32, FillValue::Int64(_) => DataType::Int64, + FillValue::UInt8(_) => DataType::UInt8, + FillValue::UInt16(_) => DataType::UInt16, + FillValue::UInt32(_) => DataType::UInt32, + FillValue::UInt64(_) => DataType::UInt64, + FillValue::Float16(_) => DataType::Float16, FillValue::Float32(_) => DataType::Float32, FillValue::Float64(_) => DataType::Float64, FillValue::Complex64(_, _) => DataType::Complex64, + FillValue::Complex128(_, _) => DataType::Complex128, FillValue::RawBits(v) => DataType::RawBits(v.len() as usize), - _ => todo!(), } } fn to_be_bytes(&self) -> Vec { match self { + + FillValue::Bool(v) => vec![u8::from(v.to_owned())], + FillValue::Int8(v) => v.to_be_bytes().into(), + FillValue::Int16(v) => v.to_be_bytes().into(), FillValue::Int32(v) => v.to_be_bytes().into(), FillValue::Int64(v) => v.to_be_bytes().into(), + FillValue::UInt8(v) => v.to_be_bytes().into(), + FillValue::UInt16(v) => v.to_be_bytes().into(), + FillValue::UInt32(v) => v.to_be_bytes().into(), + FillValue::UInt64(v) => v.to_be_bytes().into(), + FillValue::Float16(v) => v.to_be_bytes().into(), FillValue::Float32(v) => v.to_be_bytes().into(), FillValue::Float64(v) => v.to_be_bytes().into(), FillValue::Complex64(r, i) => r @@ -246,8 +311,12 @@ impl FillValue { .into_iter() .chain(i.to_be_bytes().into_iter()) .collect(), + FillValue::Complex128(r, i) => r + .to_be_bytes() + .into_iter() + .chain(i.to_be_bytes().into_iter()) + .collect(), FillValue::RawBits(v) => v.to_owned(), - _ => todo!(), } } } diff --git a/src/strategies.rs b/src/strategies.rs index bb05d336..cec2acc5 100644 --- a/src/strategies.rs +++ b/src/strategies.rs @@ -17,14 +17,20 @@ pub fn fill_value_strategy() -> impl Strategy { } prop_oneof![ + any::().prop_map(FillValue::Bool), + any::().prop_map(FillValue::Int8), + any::().prop_map(FillValue::Int16), any::().prop_map(FillValue::Int32), any::().prop_map(FillValue::Int64), - // any::().prop_map(FillValue::UInt32), + any::().prop_map(FillValue::UInt8), + any::().prop_map(FillValue::UInt16), + any::().prop_map(FillValue::UInt32), + any::().prop_map(FillValue::UInt64), + any::().prop_map(FillValue::Float16), any::().prop_map(FillValue::Float32), any::().prop_map(FillValue::Float64), - Just(FillValue::Complex64(0f32, 1f32)), vec(any::(), 2).prop_map(gen_complex_64), - // vec(any::(), 2).prop_map(gen_complex_128), + vec(any::(), 2).prop_map(gen_complex_128), vec(any::(), 0..64).prop_map(FillValue::RawBits), ] } @@ -38,13 +44,3 @@ pub fn fill_values_vec_strategy() -> impl Strategy>> 0..10, ) } - -proptest! { - #[test] - fn test_fill_value_strategy(value in fill_value_strategy()) { - value; - } - fn test_fill_value_vec_strategy(value in fill_values_vec_strategy()) { - value; - } -} diff --git a/src/structure.rs b/src/structure.rs index 41cb1d42..0fdd0b4d 100644 --- a/src/structure.rs +++ b/src/structure.rs @@ -776,12 +776,25 @@ mod tests { #[test] fn test_fill_values_vec_roundtrip() { let fill_values = vec![ + // Some(FillValue::Bool), None, // for groups + Some(FillValue::Int8(0i8)), + Some(FillValue::Int16(0i16)), Some(FillValue::Int32(0i32)), Some(FillValue::Int64(0i64)), + None, + Some(FillValue::UInt8(0u8)), + Some(FillValue::UInt16(0u16)), + Some(FillValue::UInt32(0u32)), + Some(FillValue::UInt64(0u64)), + None, // for groups + Some(FillValue::Float16(0f32)), Some(FillValue::Float32(0f32)), Some(FillValue::Float64(0f64)), + None, // for groups Some(FillValue::Complex64(0f32, 1f32)), + Some(FillValue::Complex128(0f64, 1f64)), + None, // for groups Some(FillValue::RawBits(vec![b'1'])), ];