Skip to content

Commit

Permalink
Further extend the test
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Jul 19, 2024
1 parent e6722eb commit 3b87ba8
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions crates/numcodecs-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,23 @@ mod tests {
let config = PyDict::new_bound(py);
config.set_item("id", "crc32")?;

// create a codec using registry lookup
let codec = Registry::get_codec(config.as_borrowed())?;
assert_eq!(codec.class().codec_id()?, "crc32");

let data = &[1_u8, 2, 3, 4];

let encoded = codec.encode(
numpy::PyArray1::from_slice_bound(py, data)
.as_any()
.as_borrowed(),
)?;
let decoded = codec.decode(encoded.as_borrowed(), None)?;
// re-register the codec class under a custom name
let class = codec.class();
Registry::register_codec(class, Some("my-crc32"))?;
config.set_item("id", "my-crc32")?;

let encoded: Vec<u8> = encoded.extract()?;
let decoded: Vec<u8> = decoded.extract()?;
// create a codec using registry lookup of the custom name
let codec = Registry::get_codec(config.as_borrowed())?;
assert_eq!(codec.class().codec_id()?, "crc32");

assert_eq!(encoded, [205, 251, 60, 182, 1, 2, 3, 4]);
assert_eq!(decoded, data);
// create a codec using the class
let codec = class.codec_from_config(config.as_borrowed())?;

// check the codec's config
let config = codec.get_config()?;
assert_eq!(config.len(), 1);
assert_eq!(
Expand All @@ -340,6 +339,21 @@ mod tests {
Some("crc32")
);

// encode and decode data with the codec
let data = &[1_u8, 2, 3, 4];
let encoded = codec.encode(
numpy::PyArray1::from_slice_bound(py, data)
.as_any()
.as_borrowed(),
)?;
let decoded = codec.decode(encoded.as_borrowed(), None)?;

// check the encoded and decoded data
let encoded: Vec<u8> = encoded.extract()?;
let decoded: Vec<u8> = decoded.extract()?;
assert_eq!(encoded, [205, 251, 60, 182, 1, 2, 3, 4]);
assert_eq!(decoded, data);

Ok(())
})
}
Expand Down

0 comments on commit 3b87ba8

Please sign in to comment.