Skip to content

Commit

Permalink
exapnd data types
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Aug 12, 2024
1 parent c29dad2 commit 284d6c5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 20 deletions.
83 changes: 76 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand All @@ -208,46 +244,79 @@ 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<u8> {
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
.to_be_bytes()
.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!(),
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions src/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ pub fn fill_value_strategy() -> impl Strategy<Value=FillValue> {
}

prop_oneof![
any::<bool>().prop_map(FillValue::Bool),
any::<i8>().prop_map(FillValue::Int8),
any::<i16>().prop_map(FillValue::Int16),
any::<i32>().prop_map(FillValue::Int32),
any::<i64>().prop_map(FillValue::Int64),
// any::<u32>().prop_map(FillValue::UInt32),
any::<u8>().prop_map(FillValue::UInt8),
any::<u16>().prop_map(FillValue::UInt16),
any::<u32>().prop_map(FillValue::UInt32),
any::<u64>().prop_map(FillValue::UInt64),
any::<f32>().prop_map(FillValue::Float16),
any::<f32>().prop_map(FillValue::Float32),
any::<f64>().prop_map(FillValue::Float64),
Just(FillValue::Complex64(0f32, 1f32)),
vec(any::<f32>(), 2).prop_map(gen_complex_64),
// vec(any::<f64>(), 2).prop_map(gen_complex_128),
vec(any::<f64>(), 2).prop_map(gen_complex_128),
vec(any::<u8>(), 0..64).prop_map(FillValue::RawBits),
]
}
Expand All @@ -38,13 +44,3 @@ pub fn fill_values_vec_strategy() -> impl Strategy<Value=Vec<Option<FillValue>>>
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;
}
}
13 changes: 13 additions & 0 deletions src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'])),
];

Expand Down

0 comments on commit 284d6c5

Please sign in to comment.