#![allow(clippy::expect_used)]
use polyplug_abi::StringView;
use polyplug_abi::ffi::{polyplug_host_alloc, polyplug_host_free};
#[test]
fn test_stringview_embedded_null_length() {
let data: &[u8] = b"hello\x00world";
let sv: StringView = StringView {
ptr: data.as_ptr(),
len: data.len(),
};
assert_eq!(
sv.len, 11_usize,
"StringView len must not be truncated at null byte"
);
}
#[test]
fn test_stringview_roundtrip_through_host_alloc() {
let data: &[u8] = b"hello\x00world";
let size: usize = data.len();
let ptr: *mut u8 = polyplug_host_alloc(size, 1_usize);
assert!(!ptr.is_null(), "host_alloc must succeed for size=11");
unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), ptr, size) };
let read_back: &[u8] = unsafe { core::slice::from_raw_parts(ptr, size) };
assert_eq!(
read_back, data,
"data with embedded null must round-trip unchanged"
);
unsafe { polyplug_host_free(ptr, size, 1_usize) };
}
#[test]
fn test_stringview_from_static_with_embedded_null() {
let data: &'static [u8] = b"poly\x00plug";
let sv: StringView = StringView {
ptr: data.as_ptr(),
len: data.len(), };
assert_eq!(
sv.len, 9_usize,
"static StringView with embedded null must have full len"
);
let slice: &[u8] = unsafe { core::slice::from_raw_parts(sv.ptr, sv.len) };
assert_eq!(slice[4], 0_u8, "byte at index 4 must be null");
assert_eq!(slice[5], b'p', "byte at index 5 must be 'p'");
}