Skip to content

Commit

Permalink
Extend the test
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Jul 19, 2024
1 parent e058257 commit 8d2d76f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 238 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
rust: ["1.76", stable, nightly]
rust: ["1.65", stable, nightly]

steps:
- name: Checkout the Repository
Expand All @@ -48,7 +48,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
rust: ["1.76", stable, nightly]
rust: ["1.65", stable, nightly]
python: ["3.10", "3.11", "3.12"]

steps:
Expand Down Expand Up @@ -103,7 +103,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
rust: ["1.76", stable]
rust: ["1.65", stable]

steps:
- name: Checkout the Repository
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/Cargo.lock
218 changes: 0 additions & 218 deletions Cargo.lock

This file was deleted.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ edition = "2021"
authors = ["Juniper Tyree <[email protected]>"]
repository = "https://github.com/juntyr/numcodecs-rs"
license = "MPL-2.0"
rust-version = "1.57"
rust-version = "1.65"

[workspace.dependencies]
# workspace-internal crates
numcodecs = { path = "crates/numcodecs-python", default-features = false }

# crates.io third-party dependencies
numpy = { version = "0.21", default-features = false }
pyo3 = { version = "0.21", default-features = false }

[workspace.lints.rust]
Expand Down
1 change: 1 addition & 0 deletions crates/numcodecs-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ keywords = ["numcodecs", "compression", "encoding", "python", "pyo3"]
pyo3 = { workspace = true }

[dev-dependencies]
numpy = { workspace = true }
pyo3 = { workspace = true, features = ["auto-initialize"] }

[lints]
Expand Down
62 changes: 46 additions & 16 deletions crates/numcodecs-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ impl Registry {
///
/// Errors if no codec with a matching `id` has been registered, or if
/// constructing the codec fails.
///
/// # Examples
///
/// ```
/// # use pyo3::{prelude::*, types::PyDict};
/// # use numcodecs_python::Registry;
///
/// Python::with_gil(|py| {
/// let config = PyDict::new_bound(py);
/// config.set_item("id", "crc32");
///
/// let codec = Registry::get_codec(config.as_borrowed()).unwrap();
/// });
/// ```
pub fn get_codec<'py>(config: Borrowed<'_, 'py, PyDict>) -> Result<Bound<'py, Codec>, PyErr> {
static GET_CODEC: GILOnceCell<Py<PyAny>> = GILOnceCell::new();

Expand Down Expand Up @@ -96,7 +82,7 @@ impl Registry {
/// [`numcodecs.abc.Codec`]: https://numcodecs.readthedocs.io/en/stable/abc.html#module-numcodecs.abc
#[repr(transparent)]
pub struct Codec {
codec: PyAny,
_codec: PyAny,
}

/// Methods implemented for [`Codec`]s.
Expand Down Expand Up @@ -230,7 +216,7 @@ unsafe impl PyTypeInfo for Codec {
/// [`numcodecs.abc.Codec`]: https://numcodecs.readthedocs.io/en/stable/abc.html#module-numcodecs.abc
#[repr(transparent)]
pub struct CodecClass {
class: PyType,
_class: PyType,
}

/// Methods implemented for [`CodecClass`]es.
Expand Down Expand Up @@ -314,3 +300,47 @@ unsafe impl PyTypeInfo for CodecClass {
mod sealed {
pub trait Sealed {}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn crc32() -> Result<(), PyErr> {
Python::with_gil(|py| {
let config = PyDict::new_bound(py);
config.set_item("id", "crc32")?;

let codec = Registry::get_codec(config.as_borrowed())?;
assert_eq!(codec.class().codec_id()?, "crc32");

let data = &[1, 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)?;

let encoded: Vec<i32> = encoded.extract()?;
let decoded: Vec<i32> = decoded.extract()?;

assert_eq!(encoded, [123]);
assert_eq!(decoded, data);

let config = codec.get_config()?;
assert_eq!(config.len(), 1);
assert_eq!(
config
.get_item("id")?
.map(|i| i.extract::<String>())
.transpose()?
.as_deref(),
Some("crc32")
);

Ok(())
})
}
}

0 comments on commit 8d2d76f

Please sign in to comment.