Improve witness unit tests for single empty element

The current unit test is incorrect, the indices field of a witness with
a single element starts at 1 because 0 is encode as a single
byte (compact encoded integer).

Fix the debug test and add a test that pushes an empty slice.
This commit is contained in:
Tobin C. Harding 2024-09-24 12:40:30 +10:00
parent 40ba08f369
commit fe967279e5
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 23 additions and 6 deletions

View File

@ -645,16 +645,33 @@ mod test {
v
}
#[test]
fn witness_debug_can_display_empty_instruction() {
let witness = Witness {
// A witness with a single element that is empty (zero length).
fn single_empty_element() -> Witness {
// The first is 0 serialized as a compact size integer.
// The last four bytes represent start at index 0.
let content = [0_u8; 5];
Witness {
witness_elements: 1,
content: append_u32_vec(vec![], &[0]),
indices_start: 2,
};
content: content.to_vec(),
indices_start: 1,
}
}
#[test]
fn witness_debug_can_display_empty_element() {
let witness = single_empty_element();
println!("{:?}", witness);
}
#[test]
fn witness_single_empty_element() {
let mut got = Witness::new();
got.push(&[]);
let want = single_empty_element();
assert_eq!(got, want)
}
#[test]
fn test_push() {
let mut witness = Witness::default();