Skip to content

Commit

Permalink
Merge rust-bitcoin#3886: io: Add unit tests for Take
Browse files Browse the repository at this point in the history
ac59b25 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 ac59b25
  apoelstra:
    ACK ac59b25; successfully ran local tests

Tree-SHA512: 19dcedaec05f0f8c028c59b5eb8771568a3708cb35793db9fadcc96147c00053ddc8239e0cad6eef5dd008bfb743bd0854e7b7674d6f77872a968bb00e928925
  • Loading branch information
apoelstra committed Jan 13, 2025
2 parents de62690 + ac59b25 commit b4cb7f9
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit b4cb7f9

Please sign in to comment.