use o3::buffer::{BLOCK_CAPACITY, CapacityError, Owned, Shared, SnapshotBuf};
type FixedOwned = Owned<BLOCK_CAPACITY>;
#[cfg(target_pointer_width = "64")]
#[test]
fn buffer_handles_stay_thin() {
assert_eq!(size_of::<Owned>(), 16);
assert_eq!(size_of::<FixedOwned>(), 16);
assert_eq!(size_of::<Shared>(), 24);
assert_eq!(size_of::<SnapshotBuf<1_048_576>>(), 24);
}
#[test]
fn capacity_policies_preserve_public_identity() {
let owned = Owned::try_with_capacity(5).unwrap();
let block = FixedOwned::new();
assert_eq!(format!("{owned:?}"), "Owned { len: 0, capacity: 5 }");
assert_eq!(format!("{block:?}"), "Owned { len: 0, capacity: 65536 }");
}
#[test]
fn fixed_owner_is_one_reusable_allocation() {
assert_eq!(FixedOwned::CAPACITY, 64 * 1024);
let mut owned = FixedOwned::new();
assert!(owned.is_empty());
let ptr = owned.as_ptr();
owned
.try_extend_from_slice(b"hello")
.expect("small write must fit the fixed block");
owned
.try_push(b'!')
.expect("single byte must fit the fixed block");
assert_eq!(owned.as_slice(), b"hello!");
owned.truncate(5);
assert_eq!(owned.as_slice(), b"hello");
owned.clear();
assert!(owned.is_empty());
assert_eq!(owned.as_ptr(), ptr);
owned
.try_extend_from_slice(b"reused")
.expect("clearing must preserve reusable storage");
assert_eq!(owned.as_slice(), b"reused");
}
#[test]
fn clone_copies_and_freeze_transfers_the_fixed_allocation() {
let mut owned = FixedOwned::new();
owned
.try_extend_from_slice(b"fixed block")
.expect("payload must fit the fixed block");
let clone = owned.clone();
assert_eq!(clone.as_slice(), owned.as_slice());
assert_ne!(clone.as_ptr(), owned.as_ptr());
let ptr = owned.as_ptr();
let shared = owned.freeze();
assert_eq!(shared.as_ptr(), ptr);
assert_eq!(shared.as_slice(), b"fixed block");
assert_eq!(shared.clone().as_slice(), b"fixed block");
}
#[test]
fn large_vec_transfers_and_shares_its_allocation() {
let payload = vec![b'x'; 4096];
let ptr = payload.as_ptr();
let shared = Shared::from(payload);
assert_eq!(shared.as_ptr(), ptr);
let slice = shared.get(1024..3072).unwrap();
drop(shared);
assert_eq!(slice.as_slice(), &[b'x'; 2048]);
assert_eq!(slice.as_ptr(), ptr.wrapping_add(1024));
}
#[test]
fn spare_writer_commits_initialized_storage() {
let mut owned = FixedOwned::new();
let mut spare = owned.spare_writer();
assert_eq!(spare.capacity(), FixedOwned::CAPACITY);
spare
.try_extend_from_slice(b"xyz")
.expect("small write must fit the fixed block");
spare.finish();
assert_eq!(owned.as_slice(), b"xyz");
let mut writer = owned.spare_writer();
let ptr = writer.as_mut_ptr();
unsafe { std::ptr::copy_nonoverlapping(b"raw".as_ptr(), ptr, 3) };
let initialized = unsafe { std::slice::from_raw_parts(ptr, 3) };
unsafe {
writer
.try_commit_initialized(initialized)
.expect("initialized bytes came from this writer");
}
assert_eq!(writer.finish(), 3);
assert_eq!(owned.as_slice(), b"xyzraw");
}
#[test]
fn fixed_capacity_accepts_exactly_its_capacity() {
let bytes = vec![b'x'; FixedOwned::CAPACITY];
let mut owned = FixedOwned::new();
owned
.try_extend_from_slice(&bytes)
.expect("one complete block must fit");
assert_eq!(owned.len(), FixedOwned::CAPACITY);
let error = owned
.try_push(b'y')
.expect_err("a full block must reject another byte");
assert_eq!(error.attempted(), FixedOwned::CAPACITY + 1);
assert_eq!(error.capacity(), FixedOwned::CAPACITY);
assert_eq!(owned.as_slice(), bytes);
}
#[test]
fn oversized_write_leaves_the_fixed_owner_unchanged() {
let mut owned = FixedOwned::new();
owned
.try_extend_from_slice(b"prefix")
.expect("prefix must fit");
let oversized = vec![0; FixedOwned::CAPACITY];
let error = owned
.try_extend_from_slice(&oversized)
.expect_err("combined payload must exceed the block");
assert_eq!(error.attempted(), FixedOwned::CAPACITY + b"prefix".len());
assert_eq!(error.capacity(), FixedOwned::CAPACITY);
assert_eq!(owned.as_slice(), b"prefix");
}
#[test]
fn owned_has_an_exact_runtime_capacity_without_growth() {
let mut owned = Owned::try_with_capacity(5).unwrap();
assert_eq!(owned.capacity(), 5);
owned
.try_extend_from_slice(b"exact")
.expect("the exact payload must fit");
assert_eq!(owned.as_slice(), b"exact");
assert!(owned.try_push(b'!').is_err());
}
#[test]
fn owned_fills_its_exact_allocation() {
let owned = Owned::try_filled(4, b'x').unwrap();
assert_eq!(owned.capacity(), 4);
assert_eq!(owned.as_slice(), b"xxxx");
}
#[test]
fn exact_build_keeps_initialization_inside_the_safe_writer() {
let owned = Owned::try_build_exact(4, |out| {
for byte in b"safe" {
out.try_push(*byte)?;
}
Ok::<_, CapacityError>(())
})
.expect("checked byte writes fill the exact allocation");
assert_eq!(owned.as_slice(), b"safe");
}