From 3ef1ffc33722c68646afd5261715347ced089a00 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 13 May 2024 14:56:47 +0200 Subject: [PATCH 1/4] test: add tests for the join module #79 --- members/libpt-bintols/tests/join.rs | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 members/libpt-bintols/tests/join.rs diff --git a/members/libpt-bintols/tests/join.rs b/members/libpt-bintols/tests/join.rs new file mode 100644 index 0000000..35a76c7 --- /dev/null +++ b/members/libpt-bintols/tests/join.rs @@ -0,0 +1,55 @@ +use libpt_bintols::join::*; + +#[test] +fn join_u128() { + let correct = [ + 16, + 255, + 256, + 0, + u128::MAX, + u64::MAX as u128, + u64::MAX as u128 + 1, + ]; + let source = [ + vec![16], + vec![255], + vec![1, 0], + vec![0], + vec![ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + ], + vec![255, 255, 255, 255, 255, 255, 255, 255], + vec![1, 0, 0, 0, 0, 0, 0, 0, 0], + ]; + for (i, n) in source.iter().enumerate() { + assert_eq!(array_to_unsigned::(n).unwrap(), correct[i]); + } +} + +#[test] +fn join_u64() { + let correct = [ + 16, + 255, + 256, + 0, + u64::MAX, + u32::MAX as u64, + 0b1_00000001, + 0b10011011_10110101_11110000_00110011, + ]; + let source = [ + vec![16], + vec![255], + vec![1, 0], + vec![0], + vec![255, 255, 255, 255, 255, 255, 255, 255], + vec![255, 255, 255, 255], + vec![1, 1], + vec![0b10011011, 0b10110101, 0b11110000, 0b00110011], + ]; + for (i, n) in source.iter().enumerate() { + assert_eq!(array_to_unsigned::(n).unwrap(), correct[i]); + } +} From 53e545832c3ab1c2219a1a135aa816e59475816a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 13 May 2024 12:59:44 +0000 Subject: [PATCH 2/4] automatic cargo CI changes --- members/libpt-bintols/src/lib.rs | 2 +- members/libpt-bintols/src/split.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/members/libpt-bintols/src/lib.rs b/members/libpt-bintols/src/lib.rs index 1680664..e69706c 100644 --- a/members/libpt-bintols/src/lib.rs +++ b/members/libpt-bintols/src/lib.rs @@ -25,5 +25,5 @@ pub const YOBI: u128 = 2u128.pow(80); // use libpt_core; pub mod datalayout; pub mod display; -pub mod split; pub mod join; +pub mod split; diff --git a/members/libpt-bintols/src/split.rs b/members/libpt-bintols/src/split.rs index c996ef7..3fb3a7d 100644 --- a/members/libpt-bintols/src/split.rs +++ b/members/libpt-bintols/src/split.rs @@ -3,7 +3,6 @@ //! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Rust provides - /// Split unsigned integers into a [Vec] of [u8]s /// /// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would @@ -23,7 +22,7 @@ /// ``` pub fn unsigned_to_vec(num: T) -> Vec where - u128: std::convert::From + u128: std::convert::From, { let mut num: u128 = num.into(); if num == 0 { From 7928423d6e2c4c6bbc5ac2320d80d0dc87a05c3d Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 13 May 2024 15:13:35 +0200 Subject: [PATCH 3/4] refactor(bintols-join): return err if the result cannot be converted to T #80 #79 --- members/libpt-bintols/src/join.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/members/libpt-bintols/src/join.rs b/members/libpt-bintols/src/join.rs index 35efcd7..96c8e6e 100644 --- a/members/libpt-bintols/src/join.rs +++ b/members/libpt-bintols/src/join.rs @@ -27,7 +27,13 @@ pub fn array_to_unsigned(parts: &[u8]) -> anyhow::Result where u128: std::convert::From, T: std::str::FromStr, + T: std::convert::TryFrom, ::Err: std::fmt::Debug, + ::Err: std::error::Error, + >::Error: std::error::Error, + >::Error: std::marker::Send, + >::Error: std::marker::Sync, + >::Error: 'static, { trace!("amount of parts: {}", parts.len()); if parts.len() > (u128::BITS / 8) as usize { @@ -40,5 +46,5 @@ where for (i, e) in parts.iter().rev().enumerate() { ri += (*e as u128) * 256u128.pow(i as u32); } - Ok(ri.to_string().parse().unwrap()) + T::try_from(ri).map_err(anyhow::Error::from) } From 5dba8395d38a0f7145c8b1824accfe73b1647558 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 13 May 2024 15:23:18 +0200 Subject: [PATCH 4/4] chore: bump version to v0.5.1 --- Cargo.toml | 4 ++-- members/libpt-bintols/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a1423a9..354ca92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ default-members = [".", "members/libpt-core"] [workspace.package] publish = true -version = "0.5.0" +version = "0.5.1" edition = "2021" authors = ["Christoph J. Scherr "] license = "MIT" @@ -29,7 +29,7 @@ categories = [ anyhow = "1.0.79" thiserror = "1.0.56" libpt-core = { version = "0.4.0", path = "members/libpt-core" } -libpt-bintols = { version = "0.5.0", path = "members/libpt-bintols" } +libpt-bintols = { version = "0.5.1", path = "members/libpt-bintols" } libpt-log = { version = "0.4.2", path = "members/libpt-log" } [package] diff --git a/members/libpt-bintols/Cargo.toml b/members/libpt-bintols/Cargo.toml index b349052..fe9e5af 100644 --- a/members/libpt-bintols/Cargo.toml +++ b/members/libpt-bintols/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libpt-bintols" publish.workspace = true -version = "0.5.0" +version = "0.5.1" edition.workspace = true authors.workspace = true license.workspace = true