use std::panic::{self, AssertUnwindSafe};
use cutile::api;
use cutile::prelude::*;
#[test]
fn shared_storage_blocks_borrowed_mutable_partition() {
let base = Arc::new(api::arange::<f32>(8).sync().expect("Failed."));
let _view = base.reshape(&[2, 4]).unwrap();
let mut owned = Arc::try_unwrap(base).expect("Expected unique outer Arc.");
let result = panic::catch_unwind(AssertUnwindSafe(|| {
let _ = (&mut owned).partition([8]);
}));
assert!(
result.is_err(),
"Expected the borrowed mutable partition to be rejected"
);
}
#[test]
fn try_partition_rejects_shared_storage_with_err() {
let base = Arc::new(api::arange::<f32>(8).sync().expect("Failed."));
let _view = base.reshape(&[2, 4]).unwrap();
let err = base
.try_partition([8])
.err()
.expect("shared storage must be an Err");
assert!(format!("{err}").contains("shared"), "{err}");
}
#[test]
fn eye_rect_reports_invalid_shapes_as_errors() {
assert!(api::eye_rect(usize::MAX, 2).sync().is_err());
assert!(api::eye_rect(0, 4).sync().is_err());
let eye = api::eye_rect(3, 5).sync().expect("valid eye_rect");
assert_eq!(eye.shape(), &[3, 5]);
let host: Vec<f32> = eye.to_host_vec().sync().expect("copy");
for r in 0..3 {
for c in 0..5 {
let expected = if r == c { 1.0 } else { 0.0 };
assert_eq!(host[r * 5 + c], expected, "eye_rect[{r}][{c}]");
}
}
}
#[test]
fn reshape_op_rejects_shape_that_exceeds_storage() {
let result = api::ones::<f32>(&[16]).reshape(&[32]).sync();
let err = result.expect_err("reshape to a larger element count must fail");
let msg = format!("{err}");
assert!(
msg.contains("preserve tensor size"),
"error must name the size mismatch, got: {msg}"
);
let t = api::ones::<f32>(&[16])
.reshape(&[4, 4])
.sync()
.expect("same-size reshape");
assert_eq!(t.shape(), &[4, 4]);
}
#[test]
fn oversized_allocation_is_an_error_not_a_panic() {
let err = Tensor::<f32>::uninitialized(1usize << 44)
.sync()
.expect_err("oversized allocation must fail");
let msg = format!("{err:?}");
assert!(
msg.contains("Driver"),
"expected the driver's allocation error, got: {msg}"
);
let t = api::ones::<f32>(&[16])
.sync()
.expect("allocation after OOM");
assert_eq!(t.shape(), &[16]);
}