Merge rust-bitcoin/rust-bitcoin#3886: io: Add unit tests for `Take`

ac59b25f2c io: Add unit tests for Take (Tobin C. Harding)

Pull request description:

  Add two unit tests:

  - Check we can read into an empty buffer as validation of args as part of C-VALIDATE
  - Do basic read using `Take::read_to_end` since it is currently untested.

ACKs for top commit:
  jamillambert:
    ACK ac59b25f2c
  apoelstra:
    ACK ac59b25f2cfe8d65435ea9e6f4265e52ac06e92e; successfully ran local tests

Tree-SHA512: 19dcedaec05f0f8c028c59b5eb8771568a3708cb35793db9fadcc96147c00053ddc8239e0cad6eef5dd008bfb743bd0854e7b7674d6f77872a968bb00e928925
This commit is contained in:
merge-script 2025-01-13 17:23:51 +00:00
commit b4cb7f961f
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 31 additions and 0 deletions

View File

@ -445,4 +445,35 @@ mod tests {
assert_eq!(buf[0], 0x00); // Double check that buffer state is sane.
}
}
#[test]
fn read_into_zero_length_buffer() {
use crate::Read as _;
const BUF_LEN: usize = 64;
let data = [1_u8; BUF_LEN];
let mut buf = [0_u8; BUF_LEN];
let mut slice = data.as_ref();
let mut take = Read::take(&mut slice, 32);
let read = take.read(&mut buf[0..0]).unwrap();
assert_eq!(read, 0);
assert_eq!(buf[0], 0x00); // Check the buffer didn't get touched.
}
#[test]
#[cfg(feature = "alloc")]
fn take_and_read_to_end() {
const BUF_LEN: usize = 64;
let data = [1_u8; BUF_LEN];
let mut slice = data.as_ref();
let mut take = Read::take(&mut slice, 32);
let mut v = Vec::new();
let read = take.read_to_end(&mut v).unwrap();
assert_eq!(read, 32);
assert_eq!(data[0..32], v[0..32]);
}
}