| 1 | // Copyright (c) The buf-list Contributors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Property-based tests for Cursor. |
| 5 | |
| 6 | use crate::BufList; |
| 7 | use anyhow::{Context, Result, bail, ensure}; |
| 8 | use bytes::{Buf, Bytes}; |
| 9 | use proptest::prelude::*; |
| 10 | use std::{ |
| 11 | fmt, |
| 12 | io::{self, BufRead, IoSliceMut, Read, Seek, SeekFrom}, |
| 13 | }; |
| 14 | use test_strategy::{Arbitrary, proptest}; |
| 15 | |
| 16 | /// Assert that buf_list's cursor behaves identically to std::io::Cursor. |
| 17 | #[proptest] |
| 18 | fn proptest_cursor_ops( |
| 19 | #[strategy(buf_list_strategy())] buf_list: BufList, |
| 20 | #[strategy(cursor_ops_strategy())] ops: Vec<CursorOp>, |
| 21 | ) { |
| 22 | let bytes = buf_list.clone().copy_to_bytes(buf_list.remaining()); |
| 23 | let mut buf_list_cursor = crate::Cursor::new(&buf_list); |
| 24 | let mut oracle_cursor = io::Cursor::new(bytes.as_ref()); |
| 25 | |
| 26 | eprintln!("\n**** start!"); |
| 27 | |
| 28 | for (index, cursor_op) in ops.into_iter().enumerate() { |
| 29 | // apply_and_compare prints out the rest of the line. |
| 30 | eprint!("** index {}, operation {:?}: ", index, cursor_op); |
| 31 | cursor_op |
| 32 | .apply_and_compare(&mut buf_list_cursor, &mut oracle_cursor) |
| 33 | .with_context(|| format!("for index {}", index)) |
| 34 | .unwrap(); |
| 35 | } |
| 36 | eprintln!("**** success"); |
| 37 | } |
| 38 | |
| 39 | fn buf_list_strategy() -> impl Strategy<Value = BufList> { |
| 40 | prop::collection::vec(prop::collection::vec(any::<u8>(), 1..128), 0..32) |
| 41 | .prop_map(|chunks| chunks.into_iter().map(Bytes::from).collect()) |
| 42 | } |
| 43 | |
| 44 | #[derive(Arbitrary, Clone, Debug)] |
| 45 | enum CursorOp { |
| 46 | SetPosition(prop::sample::Index), |
| 47 | SeekStart(prop::sample::Index), |
| 48 | SeekEnd(prop::sample::Index), |
| 49 | SeekCurrent(prop::sample::Index), |
| 50 | Read(prop::sample::Index), |
| 51 | ReadVectored( |
| 52 | #[strategy(prop::collection::vec(any::<prop::sample::Index>(), 0..8))] |
| 53 | Vec<prop::sample::Index>, |
| 54 | ), |
| 55 | ReadExact(prop::sample::Index), |
| 56 | // fill_buf can't be tested here because oracle is a contiguous block. Instead, we check its |
| 57 | // return value separately. |
| 58 | Consume(prop::sample::Index), |
| 59 | // Buf trait operations |
| 60 | BufChunk, |
| 61 | BufAdvance(prop::sample::Index), |
| 62 | BufChunksVectored(prop::sample::Index), |
| 63 | BufCopyToBytes(prop::sample::Index), |
| 64 | BufGetU8, |
| 65 | BufGetU64, |
| 66 | BufGetU64Le, |
| 67 | // No need to test futures03 imps since they're simple wrappers around the main imps. |
| 68 | #[cfg(feature = "tokio1")] |
| 69 | PollRead { |
| 70 | capacity: prop::sample::Index, |
| 71 | filled: prop::sample::Index, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | impl CursorOp { |
| 76 | fn apply_and_compare( |
| 77 | self, |
| 78 | // The "mut" here is used in the branches corresponding to optional features. |
| 79 | #[allow(unused_mut)] mut buf_list: &mut crate::Cursor<&BufList>, |
| 80 | #[allow(unused_mut)] mut oracle: &mut io::Cursor<&[u8]>, |
| 81 | ) -> Result<()> { |
| 82 | let num_bytes = buf_list.get_ref().num_bytes(); |
| 83 | match self { |
| 84 | Self::SetPosition(index) => { |
| 85 | // Allow going past the end of the list a bit. |
| 86 | let index = index.index(1 + num_bytes * 5 / 4) as u64; |
| 87 | eprintln!("set position: {}", index); |
| 88 | |
| 89 | buf_list.set_position(index); |
| 90 | oracle.set_position(index); |
| 91 | } |
| 92 | Self::SeekStart(index) => { |
| 93 | // Allow going past the end of the list a bit. |
| 94 | let style = SeekFrom::Start(index.index(1 + num_bytes * 5 / 4) as u64); |
| 95 | eprintln!("style: {:?}", style); |
| 96 | |
| 97 | let buf_list_res = buf_list.seek(style); |
| 98 | let oracle_res = oracle.seek(style); |
| 99 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 100 | .context("operation result didn't match")?; |
| 101 | } |
| 102 | Self::SeekEnd(index) => { |
| 103 | // Allow going past the beginning and end of the list a bit. |
| 104 | let index = index.index(1 + num_bytes * 3 / 2) as i64; |
| 105 | let style = SeekFrom::End(index - (1 + num_bytes * 5 / 4) as i64); |
| 106 | eprintln!("style: {:?}", style); |
| 107 | |
| 108 | let buf_list_res = buf_list.seek(style); |
| 109 | let oracle_res = oracle.seek(style); |
| 110 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 111 | .context("operation result didn't match")?; |
| 112 | } |
| 113 | Self::SeekCurrent(index) => { |
| 114 | let index = index.index(1 + num_bytes * 3 / 2) as i64; |
| 115 | // Center the index at roughly 0. |
| 116 | let style = SeekFrom::Current(index - (num_bytes * 3 / 4) as i64); |
| 117 | eprintln!("style: {:?}", style); |
| 118 | |
| 119 | let buf_list_res = buf_list.seek(style); |
| 120 | let oracle_res = oracle.seek(style); |
| 121 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 122 | .context("operation result didn't match")?; |
| 123 | } |
| 124 | Self::Read(index) => { |
| 125 | let buf_size = index.index(1 + num_bytes * 5 / 4); |
| 126 | eprintln!("buf_size: {}", buf_size); |
| 127 | |
| 128 | // Must initialize the whole vec here so &mut returns the whole buffer -- can't use |
| 129 | // with_capacity! |
| 130 | let mut buf_list_buf = vec![0u8; buf_size]; |
| 131 | let mut oracle_buf = vec![0u8; buf_size]; |
| 132 | |
| 133 | let buf_list_res = buf_list.read(&mut buf_list_buf); |
| 134 | let oracle_res = oracle.read(&mut oracle_buf); |
| 135 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 136 | .context("operation result didn't match")?; |
| 137 | ensure!(buf_list_buf == oracle_buf, "read buffer matches"); |
| 138 | } |
| 139 | Self::ReadVectored(indexes) => { |
| 140 | // Build a bunch of IoSliceMuts. |
| 141 | let mut buf_list_vecs: Vec<_> = indexes |
| 142 | .into_iter() |
| 143 | .map(|index| { |
| 144 | // Must initialize the whole vec here so &mut returns the whole buffer -- can't |
| 145 | // use with_capacity! |
| 146 | let buf_size = index.index(1 + num_bytes); |
| 147 | vec![0u8; buf_size] |
| 148 | }) |
| 149 | .collect(); |
| 150 | let mut oracle_vecs = buf_list_vecs.clone(); |
| 151 | |
| 152 | let mut buf_list_slices: Vec<_> = buf_list_vecs |
| 153 | .iter_mut() |
| 154 | .map(|v| IoSliceMut::new(v)) |
| 155 | .collect(); |
| 156 | let mut oracle_slices: Vec<_> = |
| 157 | oracle_vecs.iter_mut().map(|v| IoSliceMut::new(v)).collect(); |
| 158 | |
| 159 | let buf_list_res = buf_list.read_vectored(&mut buf_list_slices); |
| 160 | let oracle_res = oracle.read_vectored(&mut oracle_slices); |
| 161 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 162 | .context("operation result didn't match")?; |
| 163 | |
| 164 | // Also check that the slices read match exactly. |
| 165 | ensure!( |
| 166 | buf_list_vecs == oracle_vecs, |
| 167 | "read vecs didn't match: buf_list: {:?} == oracle: {:?}", |
| 168 | buf_list_vecs, |
| 169 | oracle_vecs |
| 170 | ); |
| 171 | } |
| 172 | Self::ReadExact(index) => { |
| 173 | let buf_size = index.index(1 + num_bytes * 5 / 4); |
| 174 | eprintln!("buf_size: {}", buf_size); |
| 175 | |
| 176 | // Must initialize the whole vec here so &mut returns the whole buffer -- can't use |
| 177 | // with_capacity! |
| 178 | let mut buf_list_buf = vec![0u8; buf_size]; |
| 179 | let mut oracle_buf = vec![0u8; buf_size]; |
| 180 | |
| 181 | let buf_list_res = buf_list.read_exact(&mut buf_list_buf); |
| 182 | let oracle_res = oracle.read_exact(&mut oracle_buf); |
| 183 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 184 | .context("operation result didn't match")?; |
| 185 | ensure!(buf_list_buf == oracle_buf, "read buffer matches"); |
| 186 | } |
| 187 | Self::Consume(index) => { |
| 188 | let amt = index.index(1 + num_bytes * 5 / 4); |
| 189 | eprintln!("amt: {}", amt); |
| 190 | |
| 191 | buf_list.consume(amt); |
| 192 | oracle.consume(amt); |
| 193 | } |
| 194 | Self::BufChunk => { |
| 195 | eprintln!("buf_chunk"); |
| 196 | |
| 197 | let buf_list_chunk = buf_list.chunk(); |
| 198 | let oracle_chunk = oracle.chunk(); |
| 199 | |
| 200 | // We can't directly compare chunks because BufList returns one |
| 201 | // segment at a time while oracle returns the entire remaining |
| 202 | // buffer. Instead, verify that: |
| 203 | // |
| 204 | // 1. is_empty matches for both chunks. |
| 205 | // 2. Both start with the same data (buf_list's chunk is a prefix of oracle's) |
| 206 | ensure!( |
| 207 | buf_list_chunk.is_empty() == oracle_chunk.is_empty(), |
| 208 | "chunk emptiness didn't match: buf_list is_empty {} == oracle is_empty {}", |
| 209 | buf_list_chunk.is_empty(), |
| 210 | oracle_chunk.is_empty() |
| 211 | ); |
| 212 | |
| 213 | if !buf_list_chunk.is_empty() { |
| 214 | // Verify buf_list's chunk is a prefix of oracle's chunk |
| 215 | ensure!( |
| 216 | oracle_chunk.starts_with(buf_list_chunk), |
| 217 | "buf_list chunk is not a prefix of oracle chunk" |
| 218 | ); |
| 219 | } |
| 220 | } |
| 221 | Self::BufAdvance(index) => { |
| 222 | let amt = index.index(1 + num_bytes * 5 / 4); |
| 223 | eprintln!("buf_advance: {}", amt); |
| 224 | |
| 225 | // Skip if already past the end, as the oracle's Buf impl has a debug assertion |
| 226 | // that checks position even when advancing by 0 |
| 227 | if buf_list.remaining() > 0 || amt == 0 && oracle.remaining() > 0 { |
| 228 | // Cap the advance amount to the remaining bytes to avoid |
| 229 | // hitting the debug assertion in std::io::Cursor's Buf |
| 230 | // impl. While the Buf trait doesn't require this, the |
| 231 | // oracle has a debug_assert that panics if we try to |
| 232 | // advance past the end. |
| 233 | let amt = amt.min(buf_list.remaining()); |
| 234 | buf_list.advance(amt); |
| 235 | oracle.advance(amt); |
| 236 | } else { |
| 237 | eprintln!(" skipping: cursor past end"); |
| 238 | } |
| 239 | } |
| 240 | Self::BufChunksVectored(index) => { |
| 241 | let num_iovs = index.index(1 + num_bytes); |
| 242 | eprintln!("buf_chunks_vectored: {} iovs", num_iovs); |
| 243 | |
| 244 | // First verify remaining() matches |
| 245 | let buf_list_remaining = buf_list.remaining(); |
| 246 | let oracle_remaining = oracle.remaining(); |
| 247 | ensure!( |
| 248 | buf_list_remaining == oracle_remaining, |
| 249 | "chunks_vectored: remaining didn't match before \ |
| 250 | calling chunks_vectored: buf_list {} == oracle {}", |
| 251 | buf_list_remaining, |
| 252 | oracle_remaining |
| 253 | ); |
| 254 | |
| 255 | let mut buf_list_iovs = vec![io::IoSlice::new(&[]); num_iovs]; |
| 256 | let mut oracle_iovs = vec![io::IoSlice::new(&[]); num_iovs]; |
| 257 | |
| 258 | let buf_list_filled = buf_list.chunks_vectored(&mut buf_list_iovs); |
| 259 | let oracle_filled = oracle.chunks_vectored(&mut oracle_iovs); |
| 260 | |
| 261 | // We can't directly compare filled counts or total bytes |
| 262 | // because BufList may have multiple chunks while the oracle |
| 263 | // (std::io::Cursor) is contiguous. When there are fewer iovs |
| 264 | // than chunks, BufList will only fill what it can, while oracle |
| 265 | // fills everything into one iov. |
| 266 | // |
| 267 | // Instead, we verify that: |
| 268 | // 1. Both returned at least some data if there are bytes |
| 269 | // remaining |
| 270 | // 2. The data that was returned matches (buf_list's data is a |
| 271 | // prefix of oracle's data) |
| 272 | let buf_list_bytes: Vec<u8> = buf_list_iovs[..buf_list_filled] |
| 273 | .iter() |
| 274 | .flat_map(|iov| iov.as_ref().iter().copied()) |
| 275 | .collect(); |
| 276 | let oracle_bytes: Vec<u8> = oracle_iovs[..oracle_filled] |
| 277 | .iter() |
| 278 | .flat_map(|iov| iov.as_ref().iter().copied()) |
| 279 | .collect(); |
| 280 | |
| 281 | if buf_list_remaining > 0 && num_iovs > 0 { |
| 282 | // If there are bytes remaining and iovs available, should |
| 283 | // return some data. |
| 284 | ensure!( |
| 285 | !buf_list_bytes.is_empty(), |
| 286 | "chunks_vectored should return some data \ |
| 287 | when remaining = {buf_list_remaining} > 0 \ |
| 288 | and num_iovs = {num_iovs} > 0" |
| 289 | ); |
| 290 | ensure!( |
| 291 | !oracle_bytes.is_empty(), |
| 292 | "oracle chunks_vectored should return some data \ |
| 293 | when remaining > 0 and num_iovs > 0" |
| 294 | ); |
| 295 | |
| 296 | // Verify that buf_list's data matches the beginning of |
| 297 | // oracle's data. |
| 298 | ensure!( |
| 299 | oracle_bytes.starts_with(&buf_list_bytes), |
| 300 | "buf_list chunks_vectored data should match beginning \ |
| 301 | of oracle data" |
| 302 | ); |
| 303 | |
| 304 | // Verify that all iovs up to buf_list_filled are non-empty. |
| 305 | for (i, iov) in buf_list_iovs[..buf_list_filled].iter().enumerate() { |
| 306 | ensure!( |
| 307 | !iov.is_empty(), |
| 308 | "buf_list iov at index {i} should be non-empty", |
| 309 | ); |
| 310 | } |
| 311 | } else if buf_list_remaining == 0 { |
| 312 | // If no bytes remaining, should return no data |
| 313 | ensure!( |
| 314 | buf_list_bytes.is_empty() && oracle_bytes.is_empty(), |
| 315 | "chunks_vectored should return no data when \ |
| 316 | remaining == 0" |
| 317 | ); |
| 318 | } |
| 319 | // If num_iovs == 0, we can't check anything since no iovs were |
| 320 | // provided. All we're doing is ensuring that buf_list doesn't |
| 321 | // panic. |
| 322 | } |
| 323 | Self::BufCopyToBytes(index) => { |
| 324 | let len = index.index(1 + num_bytes * 5 / 4); |
| 325 | eprintln!("buf_copy_to_bytes: {}", len); |
| 326 | |
| 327 | // copy_to_bytes can panic if len > remaining, so check first |
| 328 | let buf_list_remaining = buf_list.remaining(); |
| 329 | let oracle_remaining = oracle.remaining(); |
| 330 | |
| 331 | if len <= buf_list_remaining && len <= oracle_remaining { |
| 332 | let buf_list_bytes = buf_list.copy_to_bytes(len); |
| 333 | let oracle_bytes = oracle.copy_to_bytes(len); |
| 334 | |
| 335 | ensure!(buf_list_bytes == oracle_bytes, "copy_to_bytes didn't match"); |
| 336 | } else { |
| 337 | // Both should panic, so just skip this operation |
| 338 | eprintln!(" skipping: len {} > remaining {}", len, buf_list_remaining); |
| 339 | } |
| 340 | } |
| 341 | Self::BufGetU8 => { |
| 342 | eprintln!("buf_get_u8"); |
| 343 | |
| 344 | if buf_list.remaining() >= 1 && oracle.remaining() >= 1 { |
| 345 | let buf_list_val = buf_list.get_u8(); |
| 346 | let oracle_val = oracle.get_u8(); |
| 347 | ensure!( |
| 348 | buf_list_val == oracle_val, |
| 349 | "get_u8 didn't match: buf_list {} == oracle {}", |
| 350 | buf_list_val, |
| 351 | oracle_val |
| 352 | ); |
| 353 | } else { |
| 354 | eprintln!(" skipping: not enough bytes remaining"); |
| 355 | } |
| 356 | } |
| 357 | Self::BufGetU64 => { |
| 358 | eprintln!("buf_get_u64"); |
| 359 | |
| 360 | if buf_list.remaining() >= 8 && oracle.remaining() >= 8 { |
| 361 | let buf_list_val = buf_list.get_u64(); |
| 362 | let oracle_val = oracle.get_u64(); |
| 363 | ensure!( |
| 364 | buf_list_val == oracle_val, |
| 365 | "get_u64 didn't match: buf_list {} == oracle {}", |
| 366 | buf_list_val, |
| 367 | oracle_val |
| 368 | ); |
| 369 | } else { |
| 370 | eprintln!(" skipping: not enough bytes remaining"); |
| 371 | } |
| 372 | } |
| 373 | Self::BufGetU64Le => { |
| 374 | eprintln!("buf_get_u64_le"); |
| 375 | |
| 376 | if buf_list.remaining() >= 8 && oracle.remaining() >= 8 { |
| 377 | let buf_list_val = buf_list.get_u64_le(); |
| 378 | let oracle_val = oracle.get_u64_le(); |
| 379 | ensure!( |
| 380 | buf_list_val == oracle_val, |
| 381 | "get_u64_le didn't match: buf_list {} == oracle {}", |
| 382 | buf_list_val, |
| 383 | oracle_val |
| 384 | ); |
| 385 | } else { |
| 386 | eprintln!(" skipping: not enough bytes remaining"); |
| 387 | } |
| 388 | } |
| 389 | #[cfg(feature = "tokio1")] |
| 390 | Self::PollRead { capacity, filled } => { |
| 391 | use std::{mem::MaybeUninit, pin::Pin, task::Poll}; |
| 392 | use tokio::io::{AsyncRead, ReadBuf}; |
| 393 | |
| 394 | let capacity = capacity.index(1 + num_bytes * 5 / 4); |
| 395 | let mut buf_list_vec = vec![MaybeUninit::uninit(); capacity]; |
| 396 | let mut oracle_vec = buf_list_vec.clone(); |
| 397 | |
| 398 | let mut buf_list_buf = ReadBuf::uninit(&mut buf_list_vec); |
| 399 | |
| 400 | // Fill up the first bytes of the vector. This uses capacity + 1 so that we can |
| 401 | // sometimes fill up the whole buffer. |
| 402 | let filled_index = filled.index(capacity + 1); |
| 403 | let fill_vec = vec![0u8; filled_index]; |
| 404 | buf_list_buf.put_slice(&fill_vec); |
| 405 | |
| 406 | let mut oracle_buf = ReadBuf::uninit(&mut oracle_vec); |
| 407 | oracle_buf.put_slice(&fill_vec); |
| 408 | |
| 409 | eprintln!("capacity: {}, filled_index: {}", capacity, filled_index); |
| 410 | |
| 411 | let waker = dummy_waker::dummy_waker(); |
| 412 | let mut context = std::task::Context::from_waker(&waker); |
| 413 | let mut buf_list_pinned = Pin::new(buf_list); |
| 414 | let buf_list_res = match buf_list_pinned |
| 415 | .as_mut() |
| 416 | .poll_read(&mut context, &mut buf_list_buf) |
| 417 | { |
| 418 | Poll::Ready(res) => res, |
| 419 | Poll::Pending => unreachable!("buf_list never returns pending"), |
| 420 | }; |
| 421 | |
| 422 | let mut oracle_pinned = Pin::new(oracle); |
| 423 | let oracle_res = match oracle_pinned |
| 424 | .as_mut() |
| 425 | .poll_read(&mut context, &mut oracle_buf) |
| 426 | { |
| 427 | Poll::Ready(res) => res, |
| 428 | Poll::Pending => unreachable!("oracle cursor never returns pending"), |
| 429 | }; |
| 430 | |
| 431 | Self::assert_io_result_eq(buf_list_res, oracle_res) |
| 432 | .context("result didn't match")?; |
| 433 | ensure!( |
| 434 | buf_list_buf.filled() == oracle_buf.filled(), |
| 435 | "filled section didn't match" |
| 436 | ); |
| 437 | ensure!( |
| 438 | buf_list_buf.remaining() == oracle_buf.remaining(), |
| 439 | "remaining count didn't match" |
| 440 | ); |
| 441 | |
| 442 | // Put buf_list and oracle back into their original places. |
| 443 | buf_list = buf_list_pinned.get_mut(); |
| 444 | oracle = oracle_pinned.get_mut(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Check general properties: remaining and has_remaining are the same. |
| 449 | let buf_list_remaining = buf_list.remaining(); |
| 450 | let oracle_remaining = oracle.remaining(); |
| 451 | ensure!( |
| 452 | buf_list_remaining == oracle_remaining, |
| 453 | "remaining didn't match: buf_list {} == oracle {}", |
| 454 | buf_list_remaining, |
| 455 | oracle_remaining |
| 456 | ); |
| 457 | |
| 458 | let buf_list_has_remaining = buf_list.has_remaining(); |
| 459 | let oracle_has_remaining = oracle.has_remaining(); |
| 460 | ensure!( |
| 461 | buf_list_has_remaining == oracle_has_remaining, |
| 462 | "has_remaining didn't match: buf_list {} == oracle {}", |
| 463 | buf_list_has_remaining, |
| 464 | oracle_has_remaining |
| 465 | ); |
| 466 | |
| 467 | // Also check that the position is the same. |
| 468 | let buf_list_position = buf_list.position(); |
| 469 | ensure!( |
| 470 | buf_list_position == oracle.position(), |
| 471 | "position didn't match: buf_list position {} == oracle position {}", |
| 472 | buf_list_position, |
| 473 | oracle.position(), |
| 474 | ); |
| 475 | Self::assert_io_result_eq(buf_list.stream_position(), oracle.stream_position()) |
| 476 | .context("stream position didn't match")?; |
| 477 | |
| 478 | // Check that fill_buf returns an empty slice iff it is actually empty. |
| 479 | let fill_buf = buf_list.fill_buf().expect("fill_buf never errors"); |
| 480 | if buf_list_position < num_bytes as u64 { |
| 481 | ensure!( |
| 482 | !fill_buf.is_empty(), |
| 483 | "fill_buf cannot be empty since buf_list.position {} < num_bytes {}", |
| 484 | buf_list_position, |
| 485 | num_bytes, |
| 486 | ); |
| 487 | } else { |
| 488 | ensure!( |
| 489 | fill_buf.is_empty(), |
| 490 | "fill_buf must be empty since buf_list.position {} >= num_bytes {}", |
| 491 | buf_list_position, |
| 492 | num_bytes, |
| 493 | ) |
| 494 | } |
| 495 | |
| 496 | // Finally, check that the internal invariants are upheld. |
| 497 | buf_list.assert_invariants()?; |
| 498 | |
| 499 | Ok(()) |
| 500 | } |
| 501 | |
| 502 | fn assert_io_result_eq<T: Eq + fmt::Debug>( |
| 503 | buf_list_res: io::Result<T>, |
| 504 | oracle_res: io::Result<T>, |
| 505 | ) -> Result<()> { |
| 506 | match (buf_list_res, oracle_res) { |
| 507 | (Ok(buf_list_value), Ok(oracle_value)) => { |
| 508 | ensure!( |
| 509 | buf_list_value == oracle_value, |
| 510 | "value didn't match: buf_list value {:?} == oracle value {:?}", |
| 511 | buf_list_value, |
| 512 | oracle_value |
| 513 | ); |
| 514 | } |
| 515 | (Ok(buf_list_value), Err(oracle_err)) => { |
| 516 | bail!( |
| 517 | "BufList value Ok({:?}) is not the same as oracle error Err({})", |
| 518 | buf_list_value, |
| 519 | oracle_err, |
| 520 | ); |
| 521 | } |
| 522 | (Err(buf_list_err), Ok(oracle_value)) => { |
| 523 | bail!( |
| 524 | "BufList error ({}) is not the same as oracle value ({:?})", |
| 525 | buf_list_err, |
| 526 | oracle_value |
| 527 | ) |
| 528 | } |
| 529 | (Err(buf_list_err), Err(oracle_err)) => { |
| 530 | // The kinds should match. |
| 531 | ensure!( |
| 532 | buf_list_err.kind() == oracle_err.kind(), |
| 533 | "error kind didn't match: buf_list {:?} == oracle {:?}", |
| 534 | buf_list_err.kind(), |
| 535 | oracle_err.kind() |
| 536 | ); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | Ok(()) |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | fn cursor_ops_strategy() -> impl Strategy<Value = Vec<CursorOp>> { |
| 545 | prop::collection::vec(any::<CursorOp>(), 0..256) |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn test_cursor_buf_trait() { |
| 550 | // Create a BufList with multiple chunks |
| 551 | let mut buf_list = BufList::new(); |
| 552 | buf_list.push_chunk(&b"hello "[..]); |
| 553 | buf_list.push_chunk(&b"world"[..]); |
| 554 | buf_list.push_chunk(&b"!"[..]); |
| 555 | |
| 556 | let mut cursor = crate::Cursor::new(buf_list.clone()); |
| 557 | |
| 558 | // Test remaining() |
| 559 | assert_eq!(cursor.remaining(), 12); |
| 560 | |
| 561 | // Test chunk() |
| 562 | assert_eq!(cursor.chunk(), b"hello "); |
| 563 | |
| 564 | // Test advance() |
| 565 | cursor.advance(6); |
| 566 | assert_eq!(cursor.remaining(), 6); |
| 567 | assert_eq!(cursor.chunk(), b"world"); |
| 568 | |
| 569 | // Advance within the same chunk |
| 570 | cursor.advance(3); |
| 571 | assert_eq!(cursor.remaining(), 3); |
| 572 | assert_eq!(cursor.chunk(), b"ld"); |
| 573 | |
| 574 | // Advance to the next chunk |
| 575 | cursor.advance(2); |
| 576 | assert_eq!(cursor.remaining(), 1); |
| 577 | assert_eq!(cursor.chunk(), b"!"); |
| 578 | |
| 579 | // Advance to the end |
| 580 | cursor.advance(1); |
| 581 | assert_eq!(cursor.remaining(), 0); |
| 582 | assert_eq!(cursor.chunk(), b""); |
| 583 | |
| 584 | // Test chunks_vectored |
| 585 | let mut cursor = crate::Cursor::new(buf_list.clone()); |
| 586 | let mut iovs = [io::IoSlice::new(&[]); 3]; |
| 587 | let filled = cursor.chunks_vectored(&mut iovs); |
| 588 | assert_eq!(filled, 3); |
| 589 | assert_eq!(iovs[0].as_ref(), b"hello "); |
| 590 | assert_eq!(iovs[1].as_ref(), b"world"); |
| 591 | assert_eq!(iovs[2].as_ref(), b"!"); |
| 592 | |
| 593 | // Test chunks_vectored after advancing |
| 594 | cursor.advance(6); |
| 595 | let mut iovs = [io::IoSlice::new(&[]); 3]; |
| 596 | let filled = cursor.chunks_vectored(&mut iovs); |
| 597 | assert_eq!(filled, 2); |
| 598 | assert_eq!(iovs[0].as_ref(), b"world"); |
| 599 | assert_eq!(iovs[1].as_ref(), b"!"); |
| 600 | |
| 601 | // Test chunks_vectored with more iovs than remaining chunks |
| 602 | let cursor2 = crate::Cursor::new(&buf_list); |
| 603 | let mut iovs2 = [io::IoSlice::new(&[]); 10]; |
| 604 | let filled2 = cursor2.chunks_vectored(&mut iovs2); |
| 605 | assert_eq!(filled2, 3, "Should only fill 3 iovs for 3 chunks"); |
| 606 | let total_bytes: usize = iovs2[..filled2].iter().map(|iov| iov.len()).sum(); |
| 607 | assert_eq!(total_bytes, 12, "Total bytes should be 12"); |
| 608 | } |