use raylib::prelude::*;
#[test]
fn databuf_alloc_write_read_roundtrip() {
let buf = DataBuf::<i64>::alloc().expect("alloc").write(-99);
assert_eq!(*buf, -99);
}
#[test]
fn databuf_slice_alloc_cycle() {
let buf = DataBuf::<[u32]>::alloc_from_copy(&[1, 2, 3, 4]).expect("alloc");
let mut grown = buf.realloc(8).map_err(|(e, _)| e).expect("realloc");
for i in 4..8 {
grown[i].write(i as u32 + 1);
}
let grown = unsafe { grown.assume_init() };
assert_eq!(&*grown, &[1, 2, 3, 4, 5, 6, 7, 8]);
}
#[cfg(any(feature = "default", feature = "SUPPORT_COMPRESSION_API"))]
mod compression {
use raylib::core::error::CompressionError;
use raylib::prelude::*;
#[test]
fn compress_roundtrip_sizes() {
for size in [1usize, 4096, 1 << 20] {
let data: Vec<u8> = (0..size).map(|i| (i % 251) as u8).collect();
let compressed = compress_data(&data).expect("compress");
let decompressed = decompress_data(compressed.as_ref()).expect("decompress");
assert_eq!(
decompressed.as_ref(),
data.as_slice(),
"roundtrip at {size}B"
);
}
}
#[test]
fn decompress_garbage_is_an_error_not_a_panic() {
let garbage = [0xDEu8, 0xAD, 0xBE, 0xEF, 0x42, 0x13, 0x37];
let r = decompress_data(&garbage);
assert!(
matches!(r, Err(CompressionError::CompressionFailed)),
"garbage decompress must be an Err, got {r:?}"
);
}
#[test]
fn compress_empty_input_pinned() {
let r = compress_data(b"");
assert!(
matches!(r, Err(CompressionError::CompressionFailed)),
"compress of empty must be Err, got {r:?}"
);
}
#[test]
fn decompress_empty_input_pinned() {
let r = decompress_data(b"");
assert!(
matches!(r, Err(CompressionError::CompressionFailed)),
"decompress of empty must be Err, got {r:?}"
);
}
}
#[cfg(any(feature = "default", feature = "SUPPORT_COMPRESSION_API"))]
mod base64 {
use raylib::prelude::*;
#[test]
fn base64_roundtrip() {
let data = b"hello raylib base64 \x00\x01\xFF";
let encoded = encode_data_base64(data).expect("encode");
let decoded = decode_data_base64(encoded.as_ref()).expect("decode");
assert_eq!(decoded.as_ref(), data.as_slice());
}
#[test]
fn base64_decode_invalid_pinned() {
let r = decode_data_base64(b"!!!!not base64!!!!");
assert!(r.is_ok(), "decode of invalid base64 returned Err: {r:?}");
}
#[test]
fn base64_encode_empty_input_pinned() {
let r = encode_data_base64(b"");
let buf = r.expect("encode of empty should succeed");
assert_eq!(
buf.len(),
1,
"expected 1-byte NUL terminator for empty input"
);
assert_eq!(buf[0], 0, "expected NUL byte");
}
#[test]
fn base64_decode_empty_input_pinned() {
use raylib::core::error::Base64Error;
let r = decode_data_base64(b"");
assert!(
matches!(r, Err(Base64Error::DecodeFailed)),
"decode of empty must be Err(DecodeFailed), got {r:?}"
);
}
#[test]
fn base64_decode_all_padding_pinned() {
use raylib::core::error::Base64Error;
for input in [&b"="[..], b"==", b"====", b"========"] {
let r = decode_data_base64(input);
assert!(
matches!(r, Err(Base64Error::DecodeFailed)),
"decode of all-padding input {input:?} must be Err(DecodeFailed), got {r:?}"
);
}
}
}