use edgefirst_tensor::{CpuAccess, PixelFormat, Tensor, TensorMapTrait, TensorMemory, TensorTrait};
fn image() -> Tensor<u8> {
Tensor::<u8>::image(
16,
8,
PixelFormat::Rgb,
Some(TensorMemory::Mem),
CpuAccess::ReadWrite,
)
.expect("alloc")
}
#[test]
fn map_with_reports_the_direction_it_was_taken_with() {
let t = image();
assert!(
!t.map_with(CpuAccess::Read).expect("read map").is_writable(),
"a read map must not report itself writable"
);
assert!(t
.map_with(CpuAccess::Write)
.expect("write map")
.is_writable());
assert!(t
.map_with(CpuAccess::ReadWrite)
.expect("rw map")
.is_writable());
}
#[test]
fn plain_map_is_readwrite() {
assert!(image().map().expect("map").is_writable());
}
#[test]
fn map_read_matches_map_with_read() {
assert!(!image().map_read().expect("map_read").is_writable());
assert!(image().map_write().expect("map_write").is_writable());
}
#[test]
fn a_read_map_still_reads() {
let t = image();
{
let mut m = t.map_with(CpuAccess::ReadWrite).expect("rw");
m.as_mut_slice()[0] = 0x5A;
}
let m = t.map_with(CpuAccess::Read).expect("read");
assert_eq!(m.as_slice()[0], 0x5A);
}
#[test]
fn none_is_not_a_mappable_direction() {
assert!(
image().map_with(CpuAccess::None).is_err(),
"CpuAccess::None is not a direction a mapping can be taken in"
);
}
#[test]
#[should_panic(expected = "read-only")]
fn as_mut_slice_on_a_read_map_is_refused() {
let t = image();
let mut m = t.map_with(CpuAccess::Read).expect("read");
let _ = m.as_mut_slice();
}
#[test]
#[should_panic(expected = "read-only")]
fn deref_mut_on_a_read_map_is_refused() {
let t = image();
let mut m = t.map_with(CpuAccess::Read).expect("read");
m[0] = 0xFF;
}