units: Extend test coverage for Sequence
What changed, and why it matters
This commit only adds new unit tests for the Sequence type. It does not change any production code, fix bugs, or alter behavior. There is no security relevance.
No action required; this is a test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff in units/src/sequence.rs expands the test module by adding coverage for SequenceEncoder, SequenceDecoder, from_height, and hex round-tripping. No implementation code is modified. The test module’s feature-gating is adjusted so tests can run under non-alloc configurations where appropriate.
Changed components
units/src/sequence.rs (tests only)Inspect captured patch +73 / −1
diff --git a/units/src/sequence.rs b/units/src/sequence.rs
index 3910195a..f52a4a80 100644
--- a/units/src/sequence.rs
+++ b/units/src/sequence.rs
@@ -390,12 +390,17 @@ impl<'a> Arbitrary<'a> for Sequence {
}
#[cfg(test)]
-#[cfg(feature = "alloc")]
mod tests {
+ #[cfg(feature = "alloc")]
use alloc::format;
use super::*;
+ #[cfg(feature = "encoding")]
+ use encoding::{Decodable as _, Decoder as _};
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ use encoding::UnexpectedEofError;
+
const MAXIMUM_ENCODABLE_SECONDS: u32 = u16::MAX as u32 * 512;
#[test]
@@ -470,4 +475,71 @@ mod tests {
let want: u32 = 0x7FFF_FFFF;
assert_eq!(format!("{}", sequence), want.to_string());
}
+
+ #[test]
+ #[cfg(feature = "alloc")]
+ fn sequence_unprefixed_hex_roundtrip() {
+ let sequence = Sequence(0x7FFF_FFFF);
+
+ let hex_str = format!("{:x}", sequence);
+ assert_eq!(hex_str, "7fffffff");
+
+ let roundtrip = Sequence::from_unprefixed_hex(&hex_str).unwrap();
+ assert_eq!(sequence, roundtrip);
+ }
+
+ #[test]
+ fn sequence_from_height() {
+ // Check near the boundaries
+ assert_eq!(Sequence::from_height(0), Sequence(0));
+ assert_eq!(Sequence::from_height(1), Sequence(1));
+ assert_eq!(Sequence::from_height(0x7FFF), Sequence(0x7FFF));
+ assert_eq!(Sequence::from_height(0xFFFF), Sequence(0xFFFF));
+
+ // Check steps throughout the whole range
+ let step = 512;
+ for v in (0..=u16::MAX).step_by(step) {
+ assert_eq!(Sequence::from_height(v), Sequence(v.into()));
+ }
+ }
+
+ #[test]
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ fn sequence_encoding_round_trip() {
+ let sequence = Sequence(0x7FFF_FFFF);
+ let expected_bytes = alloc::vec![0xff, 0xff, 0xff, 0x7f];
+
+ let encoded = encoding::encode_to_vec(&sequence);
+ assert_eq!(encoded, expected_bytes);
+
+ let decoded = encoding::decode_from_slice::<Sequence>(encoded.as_slice()).unwrap();
+ assert_eq!(decoded, sequence);
+ }
+
+ #[test]
+ #[cfg(feature = "encoding")]
+ fn sequence_decoding() {
+ let bytes = [0xff, 0xff, 0xff, 0xff];
+ let expected = Sequence::default();
+
+ let mut decoder = Sequence::decoder();
+ assert_eq!(decoder.read_limit(), 4);
+ assert!(!decoder.push_bytes(&mut bytes.as_slice()).unwrap());
+ assert_eq!(decoder.read_limit(), 0);
+
+ let decoded = decoder.end().unwrap();
+ assert_eq!(decoded, expected);
+ }
+
+ #[test]
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ fn sequence_decoding_error() {
+ let bytes = [0xff, 0xff, 0xff]; // 3 bytes is an EOF error
+
+ let mut decoder = SequenceDecoder::default();
+ assert!(decoder.push_bytes(&mut bytes.as_slice()).unwrap());
+
+ let error = decoder.end().unwrap_err();
+ assert!(matches!(error, SequenceDecoderError(UnexpectedEofError { .. })));
+ }
}
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.