Add iterator tests for EncodableByteIter
What changed, and why it matters
This commit only adds new test code and a development-only dependency. It does not change any production code, fix a bug, or alter behavior. There is no security issue here.
No action needed. This is a routine test-only commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces a new test file consensus_encoding/tests/iter.rs containing unit tests for EncodableByteIter, demonstrating integration with hex::BytesToHexIter. It also adds hex-conservative 0.3.0 as a dev-dependency in consensus_encoding/Cargo.toml and updates the lockfiles accordingly. No production source code is modified.
Changed components
Inspect captured patch +64 / −0
diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock
index 860f0dd3..5e6b6763 100644
--- a/Cargo-minimal.lock
+++ b/Cargo-minimal.lock
@@ -77,6 +77,7 @@ name = "bitcoin-consensus-encoding"
version = "1.0.0-rc.1"
dependencies = [
"bitcoin-internals",
+ "hex-conservative 0.3.0",
]
[[package]]
diff --git a/Cargo-recent.lock b/Cargo-recent.lock
index bc02789a..4bb5b847 100644
--- a/Cargo-recent.lock
+++ b/Cargo-recent.lock
@@ -76,6 +76,7 @@ name = "bitcoin-consensus-encoding"
version = "1.0.0-rc.1"
dependencies = [
"bitcoin-internals",
+ "hex-conservative 0.3.0",
]
[[package]]
diff --git a/consensus_encoding/Cargo.toml b/consensus_encoding/Cargo.toml
index 72ecdb75..f7233544 100644
--- a/consensus_encoding/Cargo.toml
+++ b/consensus_encoding/Cargo.toml
@@ -20,6 +20,9 @@ alloc = ["internals/alloc"]
[dependencies]
internals = { package = "bitcoin-internals", path = "../internals", version = "0.4.0" }
+[dev-dependencies]
+hex = { package = "hex-conservative", version = "0.3.0" }
+
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
diff --git a/consensus_encoding/tests/iter.rs b/consensus_encoding/tests/iter.rs
new file mode 100644
index 00000000..1e105aa2
--- /dev/null
+++ b/consensus_encoding/tests/iter.rs
@@ -0,0 +1,59 @@
+use hex::BytesToHexIter;
+
+use bitcoin_consensus_encoding::{Encodable, ArrayEncoder, Encoder2, EncodableByteIter};
+
+struct TestArray<const N: usize>([u8; N]);
+
+impl<const N: usize> Encodable for TestArray<N> {
+ type Encoder<'s>
+ = ArrayEncoder<N>
+ where
+ Self: 's;
+
+ fn encoder(&self) -> Self::Encoder<'_> { ArrayEncoder::without_length_prefix(self.0) }
+}
+
+struct TestCatArray<const N: usize, const M: usize>([u8; N], [u8; M]);
+
+impl<const N: usize, const M: usize> Encodable for TestCatArray<N, M> {
+ type Encoder<'s>
+ = Encoder2<ArrayEncoder<N>, ArrayEncoder<M>>
+ where
+ Self: 's;
+
+ fn encoder(&self) -> Self::Encoder<'_> { Encoder2::new(
+ ArrayEncoder::without_length_prefix(self.0),
+ ArrayEncoder::without_length_prefix(self.1),
+ )
+ }
+}
+
+#[test]
+fn hex_iter() {
+ let data = TestArray([255u8, 240, 9, 135]);
+ let byte_iter = EncodableByteIter::new(&data);
+ let mut iter = BytesToHexIter::new(byte_iter, hex::Case::Upper);
+
+ let expect_str = "FFF00987";
+ for byte in expect_str.chars() {
+ let iter_byte = iter.next().unwrap();
+ assert_eq!(iter_byte, byte);
+ }
+ let none = iter.next();
+ assert_eq!(none, None);
+}
+
+#[test]
+fn hex_iter_cat_encoder() {
+ let data = TestCatArray([222u8, 173], [190u8, 239]);
+ let byte_iter = EncodableByteIter::new(&data);
+ let mut iter = BytesToHexIter::new(byte_iter, hex::Case::Lower);
+
+ let expect_str = "deadbeef";
+ for byte in expect_str.chars() {
+ let iter_byte = iter.next().unwrap();
+ assert_eq!(iter_byte, byte);
+ }
+ let none = iter.next();
+ assert_eq!(none, None);
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.