From 3b87ba8eff8fa476a813ce110da812fbf7fb4a31 Mon Sep 17 00:00:00 2001 From: Juniper Tyree <50025784+juntyr@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:14:45 +0000 Subject: [PATCH] Further extend the test --- crates/numcodecs-python/src/lib.rs | 38 ++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/numcodecs-python/src/lib.rs b/crates/numcodecs-python/src/lib.rs index 4ef28a9e..e64ab16e 100644 --- a/crates/numcodecs-python/src/lib.rs +++ b/crates/numcodecs-python/src/lib.rs @@ -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 = encoded.extract()?; - let decoded: Vec = 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!( @@ -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 = encoded.extract()?; + let decoded: Vec = decoded.extract()?; + assert_eq!(encoded, [205, 251, 60, 182, 1, 2, 3, 4]); + assert_eq!(decoded, data); + Ok(()) }) }