Skip to content

Commit

Permalink
Fix FillValue::equals_all and add FillValue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Jan 21, 2024
1 parent b1ab248 commit d9bf896
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath`, `Node`, `AdditionalFields`, `Metadata`
- Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath`, `Node`, `AdditionalFields`, `Metadata`, `FillValue`
- `array_subset::IncompatibleStartEndIndicesError`

### Changed
Expand All @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Disallow an empty string for a `StoreKey`
- `ArrayBuilder` now validates additional fields
- `FillValue::equals_all` incorrect behaviour with a `FillValue` with size not equal to 1, 2, 4, 8, or 16 bytes.

## [0.10.0] - 2024-01-17

Expand Down
80 changes: 79 additions & 1 deletion src/array/fill_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,86 @@ impl FillValue {
&& aligned.iter().all(|x| x == &fill_value_128)
}
_ => bytes
.chunks_exact(bytes.len())
.chunks_exact(self.0.len())
.all(|element| element == self.0),
}
}
}

#[cfg(test)]
mod tests {
use crate::array::safe_transmute_to_bytes_vec;

use super::*;

#[test]
fn fill_value() {
assert_eq!(
FillValue::from([0u8, 1u8, 2u8].as_slice()).as_ne_bytes(),
&[0u8, 1u8, 2u8]
);
assert_eq!(
FillValue::from(vec![0u8, 1u8, 2u8]).as_ne_bytes(),
&[0u8, 1u8, 2u8]
);
assert_eq!(FillValue::from(false).as_ne_bytes(), &[0u8]);
assert_eq!(FillValue::from(true).as_ne_bytes(), &[1u8]);
assert_eq!(FillValue::from(1u8).as_ne_bytes(), 1u8.to_ne_bytes());
assert_eq!(FillValue::from(1u16).as_ne_bytes(), 1u16.to_ne_bytes());
assert_eq!(FillValue::from(1u32).as_ne_bytes(), 1u32.to_ne_bytes());
assert_eq!(FillValue::from(1u64).as_ne_bytes(), 1u64.to_ne_bytes());
assert_eq!(FillValue::from(1i8).as_ne_bytes(), 1i8.to_ne_bytes());
assert_eq!(FillValue::from(1i16).as_ne_bytes(), 1i16.to_ne_bytes());
assert_eq!(FillValue::from(1i32).as_ne_bytes(), 1i32.to_ne_bytes());
assert_eq!(FillValue::from(1i64).as_ne_bytes(), 1i64.to_ne_bytes());
assert_eq!(
FillValue::from(half::f16::from_f32_const(1.0)).as_ne_bytes(),
half::f16::from_f32_const(1.0).to_ne_bytes()
);
assert_eq!(
FillValue::from(half::bf16::from_f32_const(1.0)).as_ne_bytes(),
half::bf16::from_f32_const(1.0).to_ne_bytes()
);
assert_eq!(
FillValue::from(1.0_f32).as_ne_bytes(),
1.0_f32.to_ne_bytes()
);
assert_eq!(
FillValue::from(1.0_f64).as_ne_bytes(),
1.0_f64.to_ne_bytes()
);
assert_eq!(
FillValue::from(num::complex::Complex32::new(1.0, 2.0)).as_ne_bytes(),
[1.0_f32.to_ne_bytes(), 2.0_f32.to_ne_bytes()].concat()
);
assert_eq!(
FillValue::from(num::complex::Complex64::new(1.0, 2.0)).as_ne_bytes(),
[1.0_f64.to_ne_bytes(), 2.0_f64.to_ne_bytes()].concat()
);
}

#[test]
fn fill_value_equals() {
assert!(FillValue::from(1u64).equals_all(&safe_transmute_to_bytes_vec(vec![1u64; 5])));
assert!(!FillValue::from(1u64).equals_all(&safe_transmute_to_bytes_vec(vec![0u64; 5])));
assert!(
FillValue::from(num::complex::Complex32::new(1.0, 2.0)).equals_all(
&safe_transmute_to_bytes_vec(
FillValue::from(num::complex::Complex32::new(1.0, 2.0))
.as_ne_bytes()
.repeat(5)
)
)
);
assert!(
FillValue::from(num::complex::Complex64::new(1.0, 2.0)).equals_all(
&safe_transmute_to_bytes_vec(
FillValue::from(num::complex::Complex64::new(1.0, 2.0))
.as_ne_bytes()
.repeat(5)
)
)
);
assert!(FillValue::from(vec![1u8; 32]).equals_all(&vec![1u8; 32 * 5]));
}
}

0 comments on commit d9bf896

Please sign in to comment.