use std::num::NonZeroU8;
use hurray_core::descriptor::TensorDescriptor;
use hurray_core::{
layout::{
addressing::csf as csf_addressing, is_invalid_tag, is_private_tag, is_reserved_tag,
validate_layout_tag_strict, BlockPagedLayout, BlockTableIndexType, CooLayout, CscLayout,
CsfLayout, CsrLayout, HilbertLayout, InnerStrides, KvRole, LayoutDescriptor, MortonLayout,
OuterStrides, PrivateExtensionLayout, StridedLayout, TiledLayout, UnknownLayout,
MAX_TILED_DEPTH, PRIVATE_LAYOUT_TAG_MAX, PRIVATE_LAYOUT_TAG_MIN, TAG_BLOCK_PAGED,
TAG_COL_MAJOR, TAG_COO, TAG_CSC, TAG_CSF, TAG_CSR, TAG_HILBERT, TAG_MORTON, TAG_ROW_MAJOR,
TAG_STRIDED, TAG_TILED,
},
BufferHandle, DeviceTag, ElementType, Error, Shape, SyncMode, DYNAMIC, MIN_BUFFER_ALIGNMENT,
};
fn shape(dims: &[u64]) -> Shape {
Shape::new(dims.to_vec()).expect("valid shape in test helper")
}
#[test]
fn tag_values_match_spec_constants() {
assert_eq!(TAG_ROW_MAJOR, 0x01);
assert_eq!(TAG_COL_MAJOR, 0x02);
assert_eq!(TAG_STRIDED, 0x03);
assert_eq!(TAG_TILED, 0x04);
assert_eq!(TAG_MORTON, 0x05);
assert_eq!(TAG_COO, 0x06);
assert_eq!(TAG_CSR, 0x07);
assert_eq!(TAG_CSC, 0x08);
assert_eq!(TAG_CSF, 0x09);
assert_eq!(TAG_BLOCK_PAGED, 0x0A);
assert_eq!(TAG_HILBERT, 0x40);
}
#[test]
fn row_major_tag_is_0x01() {
assert_eq!(LayoutDescriptor::RowMajor.tag(), 0x01);
}
#[test]
fn col_major_tag_is_0x02() {
assert_eq!(LayoutDescriptor::ColMajor.tag(), 0x02);
}
#[test]
fn strided_tag_is_0x03() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![4, 1]));
assert_eq!(d.tag(), 0x03);
}
#[test]
fn tiled_tag_is_0x04() {
let t = TiledLayout::new(vec![4, 4], 0x01, 0x01, None, None, None).unwrap();
assert_eq!(LayoutDescriptor::Tiled(Box::new(t)).tag(), 0x04);
}
#[test]
fn morton_tag_is_0x05() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![2, 2]).unwrap());
assert_eq!(d.tag(), 0x05);
}
#[test]
fn coo_tag_is_0x06() {
assert_eq!(LayoutDescriptor::Coo(CooLayout::new(0, false)).tag(), 0x06);
}
#[test]
fn csr_tag_is_0x07() {
assert_eq!(LayoutDescriptor::Csr(CsrLayout::new(0)).tag(), 0x07);
}
#[test]
fn csc_tag_is_0x08() {
assert_eq!(LayoutDescriptor::Csc(CscLayout::new(0)).tag(), 0x08);
}
#[test]
fn hilbert_tag_is_0x40() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
assert_eq!(d.tag(), 0x40);
}
#[test]
fn private_extension_tag_passthrough_all_valid_values() {
for tag in PRIVATE_LAYOUT_TAG_MIN..=PRIVATE_LAYOUT_TAG_MAX {
let d = LayoutDescriptor::PrivateExtension(
PrivateExtensionLayout::new(tag, 0, vec![]).unwrap(),
);
assert_eq!(
d.tag(),
tag,
"expected tag() == 0x{tag:02X} for PrivateExtension"
);
}
}
#[test]
fn unknown_tag_passthrough() {
let d = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![0xDE, 0xAD]).unwrap());
assert_eq!(d.tag(), 0x0C);
}
#[test]
fn row_major_buffer_count_is_1() {
assert_eq!(LayoutDescriptor::RowMajor.buffer_count(), NonZeroU8::new(1));
}
#[test]
fn col_major_buffer_count_is_1() {
assert_eq!(LayoutDescriptor::ColMajor.buffer_count(), NonZeroU8::new(1));
}
#[test]
fn strided_buffer_count_is_1() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![1, 4]));
assert_eq!(d.buffer_count(), NonZeroU8::new(1));
}
#[test]
fn tiled_buffer_count_is_1() {
let t = TiledLayout::new(vec![4], 0x01, 0x01, None, None, None).unwrap();
assert_eq!(
LayoutDescriptor::Tiled(Box::new(t)).buffer_count(),
NonZeroU8::new(1)
);
}
#[test]
fn morton_buffer_count_is_1() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![2]).unwrap());
assert_eq!(d.buffer_count(), NonZeroU8::new(1));
}
#[test]
fn hilbert_buffer_count_is_1() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
assert_eq!(d.buffer_count(), NonZeroU8::new(1));
}
#[test]
fn coo_buffer_count_is_2() {
assert_eq!(
LayoutDescriptor::Coo(CooLayout::new(0, false)).buffer_count(),
NonZeroU8::new(2)
);
}
#[test]
fn coo_buffer_count_is_2_for_sorted_nonempty() {
assert_eq!(
LayoutDescriptor::Coo(CooLayout::new(1_000_000, true)).buffer_count(),
NonZeroU8::new(2)
);
}
#[test]
fn csr_buffer_count_is_3() {
assert_eq!(
LayoutDescriptor::Csr(CsrLayout::new(0)).buffer_count(),
NonZeroU8::new(3)
);
}
#[test]
fn csc_buffer_count_is_3() {
assert_eq!(
LayoutDescriptor::Csc(CscLayout::new(0)).buffer_count(),
NonZeroU8::new(3)
);
}
#[test]
fn private_extension_buffer_count_is_none() {
let d =
LayoutDescriptor::PrivateExtension(PrivateExtensionLayout::new(0xF5, 42, vec![]).unwrap());
assert!(d.buffer_count().is_none());
}
#[test]
fn unknown_buffer_count_is_none() {
let d = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![]).unwrap());
assert!(d.buffer_count().is_none());
}
#[test]
fn unknown_can_hold_reserved_tag() {
let d = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![]).unwrap());
assert_eq!(d.tag(), 0x0C);
assert!(d.buffer_count().is_none());
}
#[test]
fn unknown_preserves_opaque_raw_bytes() {
let raw = vec![0xDE, 0xAD, 0xBE, 0xEF];
let u = UnknownLayout::new(0x3F, raw.clone()).unwrap();
assert_eq!(u.raw_bytes, raw);
}
#[test]
fn validate_strict_rejects_tag_0x00_with_invalid_layout_tag() {
assert!(matches!(
validate_layout_tag_strict(0x00),
Err(Error::InvalidLayoutTag(0x00))
));
}
#[test]
fn validate_strict_rejects_tag_0xff_with_invalid_layout_tag() {
assert!(matches!(
validate_layout_tag_strict(0xFF),
Err(Error::InvalidLayoutTag(0xFF))
));
}
#[test]
fn is_invalid_tag_true_for_0x00_and_0xff() {
assert!(is_invalid_tag(0x00));
assert!(is_invalid_tag(0xFF));
}
#[test]
fn is_invalid_tag_false_for_all_other_values() {
for tag in 0x01u8..=0xFE {
assert!(!is_invalid_tag(tag), "0x{tag:02X} should not be invalid");
}
}
#[test]
fn validate_strict_rejects_reserved_range_boundaries() {
let reserved_boundary_pairs = [(0x0C_u8, 0x3F), (0x41, 0x7F), (0x80, 0xEF)];
for (lo, hi) in reserved_boundary_pairs {
assert!(
matches!(
validate_layout_tag_strict(lo),
Err(Error::ReservedLayoutTag(_))
),
"0x{lo:02X} should be ReservedLayoutTag"
);
assert!(
matches!(
validate_layout_tag_strict(hi),
Err(Error::ReservedLayoutTag(_))
),
"0x{hi:02X} should be ReservedLayoutTag"
);
}
}
#[test]
fn is_reserved_tag_true_for_all_reserved_ranges() {
for tag in [0x0C_u8, 0x20, 0x3F, 0x41, 0x60, 0x7F, 0x80, 0xA0, 0xEF] {
assert!(is_reserved_tag(tag), "0x{tag:02X} should be reserved");
}
}
#[test]
fn is_reserved_tag_false_for_named_and_private_tags() {
for tag in [
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x40, 0xF0, 0xFE,
] {
assert!(
!is_reserved_tag(tag as u8),
"0x{tag:02X} should not be reserved"
);
}
}
#[test]
fn tag_0x0a_not_reserved() {
assert!(!is_reserved_tag(0x0A));
}
#[test]
fn tag_0x40_not_reserved() {
assert!(!is_reserved_tag(0x40));
}
#[test]
fn private_extension_accepts_all_valid_tags() {
for tag in 0xF0u8..=0xFE {
let result = PrivateExtensionLayout::new(tag, 0, vec![]);
assert!(
result.is_ok(),
"0x{tag:02X} should be accepted by PrivateExtensionLayout"
);
}
}
#[test]
fn private_extension_rejects_tag_0xff() {
assert!(matches!(
PrivateExtensionLayout::new(0xFF, 0, vec![]),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn private_extension_rejects_core_layout_tags() {
for tag in [
0x01_u8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x40,
] {
assert!(
matches!(
PrivateExtensionLayout::new(tag, 0, vec![]),
Err(Error::InvalidLayout(_))
),
"core tag 0x{tag:02X} should be rejected by PrivateExtensionLayout"
);
}
}
#[test]
fn private_extension_rejects_reserved_tag() {
assert!(matches!(
PrivateExtensionLayout::new(0xEF, 0, vec![]),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn validate_strict_rejects_private_tags() {
for tag in [0xF0_u8, 0xF7, 0xFE] {
assert!(
matches!(
validate_layout_tag_strict(tag),
Err(Error::PrivateLayoutTag(_))
),
"strict mode should reject private tag 0x{tag:02X}"
);
}
}
#[test]
fn is_private_tag_true_for_0xf0_to_0xfe() {
for tag in 0xF0u8..=0xFE {
assert!(is_private_tag(tag), "0x{tag:02X} should be private");
}
}
#[test]
fn is_private_tag_false_for_0xff() {
assert!(!is_private_tag(0xFF));
}
#[test]
fn is_private_tag_false_for_core_tags() {
assert!(!is_private_tag(0x01));
assert!(!is_private_tag(0x09));
assert!(!is_private_tag(0x40));
}
#[test]
fn private_tag_range_constants_match_spec() {
assert_eq!(PRIVATE_LAYOUT_TAG_MIN, 0xF0);
assert_eq!(PRIVATE_LAYOUT_TAG_MAX, 0xFE);
}
#[test]
fn private_extension_stores_id_and_data() {
let id = 0xDEAD_BEEF_CAFE_0001_u64;
let data = vec![0xAB, 0xCD, 0xEF];
let p = PrivateExtensionLayout::new(0xF2, id, data.clone()).unwrap();
assert_eq!(p.tag, 0xF2);
assert_eq!(p.extension_layout_id, id);
assert_eq!(p.extension_data, data);
}
#[test]
fn strided_valid_when_rank_matches() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![4, 1]));
assert!(d.validate_against_shape(&shape(&[3, 4])).is_ok());
}
#[test]
fn strided_invalid_when_strides_too_short() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![1]));
assert!(matches!(
d.validate_against_shape(&shape(&[3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn strided_invalid_when_strides_too_long() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![4, 1, 1]));
assert!(matches!(
d.validate_against_shape(&shape(&[3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn strided_rejects_scalar_shape() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![]));
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn strided_negative_strides_are_valid() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![-4, 1]));
assert!(d.validate_against_shape(&shape(&[3, 4])).is_ok());
}
#[test]
fn strided_zero_stride_is_valid() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![0, 1]));
assert!(d.validate_against_shape(&shape(&[3, 4])).is_ok());
}
#[test]
fn strided_mixed_strides_accepted() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![-10, 0, 1]));
assert!(d.validate_against_shape(&shape(&[5, 3, 4])).is_ok());
}
#[test]
fn strided_valid_for_rank_1() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![1]));
assert!(d.validate_against_shape(&shape(&[100])).is_ok());
}
#[test]
fn tiled_valid_when_rank_matches() {
let t = TiledLayout::new(vec![4, 4], 0x01, 0x01, None, None, None).unwrap();
assert!(LayoutDescriptor::Tiled(Box::new(t))
.validate_against_shape(&shape(&[8, 8]))
.is_ok());
}
#[test]
fn tiled_invalid_when_tile_shape_too_short() {
let t = TiledLayout::new(vec![4], 0x01, 0x01, None, None, None).unwrap();
assert!(matches!(
LayoutDescriptor::Tiled(Box::new(t)).validate_against_shape(&shape(&[8, 8])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_invalid_when_tile_shape_too_long() {
let t = TiledLayout::new(vec![4, 4, 4], 0x01, 0x01, None, None, None).unwrap();
assert!(matches!(
LayoutDescriptor::Tiled(Box::new(t)).validate_against_shape(&shape(&[8, 8])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_scalar_shape() {
let t = TiledLayout::new(vec![4], 0x01, 0x01, None, None, None).unwrap();
assert!(matches!(
LayoutDescriptor::Tiled(Box::new(t)).validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_zero_tile_dimension() {
assert!(matches!(
TiledLayout::new(vec![0, 4], 0x01, 0x01, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_all_zero_tile_dimensions() {
assert!(matches!(
TiledLayout::new(vec![0, 0], 0x01, 0x01, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_invalid_outer_layout_0x00() {
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x00, 0x01, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_outer_layout_0x04() {
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x04, 0x01, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_accepts_valid_outer_layouts() {
for outer in [0x01_u8, 0x02, 0x03] {
let outer_strides = if outer == 0x03 {
Some(OuterStrides::new(vec![2, 1]))
} else {
None
};
let result = TiledLayout::new(vec![4, 4], outer, 0x01, outer_strides, None, None);
assert!(
result.is_ok(),
"outer_layout 0x{outer:02X} should be accepted"
);
}
}
#[test]
fn tiled_rejects_invalid_inner_layout_0x05() {
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x01, 0x05, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_accepts_valid_inner_layouts() {
let r1 = TiledLayout::new(vec![4, 4], 0x01, 0x01, None, None, None);
assert!(r1.is_ok());
let r2 = TiledLayout::new(vec![4, 4], 0x01, 0x02, None, None, None);
assert!(r2.is_ok());
let inner_strides = Some(InnerStrides::new(vec![1, 4]));
let r3 = TiledLayout::new(vec![4, 4], 0x01, 0x03, None, inner_strides, None);
assert!(r3.is_ok());
let inner = TiledLayout::new(vec![2, 2], 0x01, 0x01, None, None, None).unwrap();
let r4 = TiledLayout::new(vec![4, 4], 0x01, 0x04, None, None, Some(Box::new(inner)));
assert!(r4.is_ok());
}
#[test]
fn tiled_rejects_missing_outer_strides_when_outer_strided() {
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x03, 0x01, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_spurious_outer_strides_when_outer_not_strided() {
let outer_strides = Some(OuterStrides::new(vec![2, 1]));
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x01, 0x01, outer_strides, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_missing_inner_strides_when_inner_strided() {
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x01, 0x03, None, None, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn tiled_rejects_spurious_inner_strides_when_inner_not_strided() {
let inner_strides = Some(InnerStrides::new(vec![1, 4]));
assert!(matches!(
TiledLayout::new(vec![4, 4], 0x01, 0x01, None, inner_strides, None),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn coo_rejects_scalar_shape() {
let d = LayoutDescriptor::Coo(CooLayout::new(0, false));
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn coo_valid_for_rank_1_shape() {
let d = LayoutDescriptor::Coo(CooLayout::new(0, false));
assert!(d.validate_against_shape(&shape(&[100])).is_ok());
}
#[test]
fn coo_valid_for_rank_2_shape() {
let d = LayoutDescriptor::Coo(CooLayout::new(42, true));
assert!(d.validate_against_shape(&shape(&[10, 20])).is_ok());
}
#[test]
fn coo_nnz_zero_is_valid() {
let d = LayoutDescriptor::Coo(CooLayout::new(0, false));
assert!(d.validate_against_shape(&shape(&[5, 5])).is_ok());
}
#[test]
fn csr_rejects_rank_0() {
let d = LayoutDescriptor::Csr(CsrLayout::new(0));
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csr_rejects_rank_1() {
let d = LayoutDescriptor::Csr(CsrLayout::new(0));
assert!(matches!(
d.validate_against_shape(&shape(&[10])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csr_rejects_rank_3() {
let d = LayoutDescriptor::Csr(CsrLayout::new(0));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csr_valid_for_rank_2() {
let d = LayoutDescriptor::Csr(CsrLayout::new(5));
assert!(d.validate_against_shape(&shape(&[4, 5])).is_ok());
}
#[test]
fn csr_nnz_zero_is_valid() {
let d = LayoutDescriptor::Csr(CsrLayout::new(0));
assert!(d.validate_against_shape(&shape(&[100, 200])).is_ok());
}
#[test]
fn csc_rejects_rank_0() {
let d = LayoutDescriptor::Csc(CscLayout::new(0));
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csc_rejects_rank_1() {
let d = LayoutDescriptor::Csc(CscLayout::new(0));
assert!(matches!(
d.validate_against_shape(&shape(&[10])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csc_rejects_rank_3() {
let d = LayoutDescriptor::Csc(CscLayout::new(0));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csc_valid_for_rank_2() {
let d = LayoutDescriptor::Csc(CscLayout::new(7));
assert!(d.validate_against_shape(&shape(&[4, 5])).is_ok());
}
#[test]
fn csc_nnz_zero_is_valid() {
let d = LayoutDescriptor::Csc(CscLayout::new(0));
assert!(d.validate_against_shape(&shape(&[50, 50])).is_ok());
}
#[test]
fn tiled_max_depth_constant_is_8() {
assert_eq!(MAX_TILED_DEPTH, 8);
}
#[test]
fn tiled_depth_7_is_accepted() {
let mut current: Option<Box<TiledLayout>> = None;
for _ in 0..(MAX_TILED_DEPTH - 2) {
let inner = if current.is_none() {
TiledLayout::new(vec![2, 2], 0x01, 0x01, None, None, None).unwrap()
} else {
TiledLayout::new(vec![2, 2], 0x01, 0x04, None, None, current).unwrap()
};
current = Some(Box::new(inner));
}
let result = TiledLayout::new(vec![2, 2], 0x01, 0x04, None, None, current);
assert!(
result.is_ok(),
"depth {} should be accepted",
MAX_TILED_DEPTH - 1
);
}
#[test]
#[ignore = "BUG: depth enforcement only tracks re-validation depth, not total chain depth; \
a bottom-up chain of 8 levels bypasses the limit (see test comment for details)"]
fn tiled_depth_8_is_rejected() {
fn make_inner(depth: usize) -> Option<Box<TiledLayout>> {
if depth == 0 {
return None;
}
TiledLayout::new(
vec![2, 2],
0x01,
if depth == 1 { 0x01 } else { 0x04 },
None,
None,
make_inner(depth - 1),
)
.ok()
.map(Box::new)
}
let result = TiledLayout::new(
vec![2, 2],
0x01,
0x04,
None,
None,
make_inner(MAX_TILED_DEPTH - 1),
);
assert!(
result.is_err(),
"depth {} should be rejected",
MAX_TILED_DEPTH
);
}
#[test]
fn hilbert_rejects_order_zero() {
assert!(matches!(
HilbertLayout::new(0, 2),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn hilbert_rejects_rank_1() {
assert!(matches!(
HilbertLayout::new(1, 1),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn hilbert_rejects_rank_0() {
assert!(matches!(
HilbertLayout::new(1, 0),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn hilbert_accepts_minimum_valid_params() {
let h = HilbertLayout::new(1, 2);
assert!(h.is_ok());
}
#[test]
fn hilbert_valid_for_4x4_order_2() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
assert!(d.validate_against_shape(&shape(&[4, 4])).is_ok());
}
#[test]
fn hilbert_valid_for_8x8x8_order_3_rank_3() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(3, 3).unwrap());
assert!(d.validate_against_shape(&shape(&[8, 8, 8])).is_ok());
}
#[test]
fn hilbert_rejects_shape_with_wrong_dimension_value() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
assert!(matches!(
d.validate_against_shape(&shape(&[3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn hilbert_rejects_mismatched_rank() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
assert!(matches!(
d.validate_against_shape(&shape(&[4, 4, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn hilbert_skips_dynamic_dimensions() {
let d = LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap());
let s = Shape::new(vec![4, DYNAMIC]).unwrap();
assert!(d.validate_against_shape(&s).is_ok());
}
#[test]
fn morton_rejects_empty_bits() {
assert!(matches!(
MortonLayout::new(vec![]),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn morton_rejects_zero_bit_count() {
assert!(matches!(
MortonLayout::new(vec![2, 0]),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn morton_rejects_rank_mismatch() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![2, 2, 2]).unwrap());
assert!(matches!(
d.validate_against_shape(&shape(&[4, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn morton_rejects_shape_exceeding_bit_capacity() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![1, 2]).unwrap());
assert!(matches!(
d.validate_against_shape(&shape(&[3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn morton_valid_when_shape_exactly_at_bit_capacity() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![2, 3]).unwrap());
assert!(d.validate_against_shape(&shape(&[4, 8])).is_ok());
}
#[test]
fn morton_valid_when_shape_below_bit_capacity() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![3, 3]).unwrap());
assert!(d.validate_against_shape(&shape(&[3, 5])).is_ok());
}
#[test]
fn morton_skips_dynamic_dimensions() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![2, 2]).unwrap());
let s = Shape::new(vec![4, DYNAMIC]).unwrap();
assert!(d.validate_against_shape(&s).is_ok());
}
#[test]
fn morton_large_bit_count_saturates_at_u64_max() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![64, 64]).unwrap());
assert!(d
.validate_against_shape(&shape(&[u64::MAX - 1, u64::MAX - 1]))
.is_ok());
}
#[test]
fn row_major_valid_for_scalar() {
assert!(LayoutDescriptor::RowMajor
.validate_against_shape(&Shape::scalar())
.is_ok());
}
#[test]
fn row_major_valid_for_rank_1() {
assert!(LayoutDescriptor::RowMajor
.validate_against_shape(&shape(&[100]))
.is_ok());
}
#[test]
fn row_major_valid_for_max_rank() {
let s = Shape::new(vec![1u64; 64]).unwrap();
assert!(LayoutDescriptor::RowMajor
.validate_against_shape(&s)
.is_ok());
}
#[test]
fn col_major_valid_for_scalar() {
assert!(LayoutDescriptor::ColMajor
.validate_against_shape(&Shape::scalar())
.is_ok());
}
#[test]
fn col_major_valid_for_rank_64() {
let s = Shape::new(vec![2u64; 64]).unwrap();
assert!(LayoutDescriptor::ColMajor
.validate_against_shape(&s)
.is_ok());
}
#[test]
fn unknown_passes_validation_for_scalar() {
let d = LayoutDescriptor::Unknown(UnknownLayout::new(0x0C, vec![]).unwrap());
assert!(d.validate_against_shape(&Shape::scalar()).is_ok());
}
#[test]
fn unknown_passes_validation_for_large_rank() {
let d = LayoutDescriptor::Unknown(UnknownLayout::new(0x3F, vec![0xAB]).unwrap());
let s = Shape::new(vec![100u64; 64]).unwrap();
assert!(d.validate_against_shape(&s).is_ok());
}
#[test]
fn private_extension_passes_validation() {
let d = LayoutDescriptor::PrivateExtension(
PrivateExtensionLayout::new(0xF0, 1, vec![0x01, 0x02]).unwrap(),
);
assert!(d.validate_against_shape(&Shape::scalar()).is_ok());
assert!(d.validate_against_shape(&shape(&[64, 64])).is_ok());
}
#[test]
fn validate_strict_accepts_all_known_tags() {
for tag in [
0x01_u8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x40,
] {
assert!(
validate_layout_tag_strict(tag).is_ok(),
"known tag 0x{tag:02X} should pass strict validation"
);
}
}
#[test]
fn first_reserved_range_lower_boundary() {
assert!(is_reserved_tag(0x0C));
assert!(!is_reserved_tag(0x0A));
assert!(!is_reserved_tag(0x0B));
}
#[test]
fn tag_0x0a_before_first_reserved_range_not_reserved() {
assert!(!is_reserved_tag(0x0A));
}
#[test]
fn tag_0x40_after_first_reserved_range_not_reserved() {
assert!(!is_reserved_tag(0x40));
}
#[test]
fn layout_descriptor_eq_same_variant() {
let a = LayoutDescriptor::Coo(CooLayout::new(42, true));
let b = LayoutDescriptor::Coo(CooLayout::new(42, true));
assert_eq!(a, b);
}
#[test]
fn layout_descriptor_ne_different_nnz() {
let a = LayoutDescriptor::Coo(CooLayout::new(42, true));
let b = LayoutDescriptor::Coo(CooLayout::new(0, true));
assert_ne!(a, b);
}
#[test]
fn layout_descriptor_ne_different_variants() {
let a = LayoutDescriptor::RowMajor;
let b = LayoutDescriptor::ColMajor;
assert_ne!(a, b);
}
#[test]
fn layout_descriptor_clone_is_equal() {
let orig = LayoutDescriptor::Strided(StridedLayout::new(vec![4, 1]));
let cloned = orig.clone();
assert_eq!(orig, cloned);
}
#[test]
fn layout_descriptor_debug_is_non_empty() {
let d = LayoutDescriptor::RowMajor;
let s = format!("{d:?}");
assert!(!s.is_empty());
}
#[test]
fn all_dense_variants_have_buffer_count_1() {
let dense: Vec<LayoutDescriptor> = vec![
LayoutDescriptor::RowMajor,
LayoutDescriptor::ColMajor,
LayoutDescriptor::Strided(StridedLayout::new(vec![4, 1])),
LayoutDescriptor::Tiled(Box::new(
TiledLayout::new(vec![4, 4], 0x01, 0x01, None, None, None).unwrap(),
)),
LayoutDescriptor::Morton(MortonLayout::new(vec![2, 2]).unwrap()),
LayoutDescriptor::Hilbert(HilbertLayout::new(2, 2).unwrap()),
];
for d in &dense {
assert_eq!(
d.buffer_count(),
NonZeroU8::new(1),
"dense variant tag=0x{:02X} should have buffer_count=1",
d.tag()
);
}
}
#[test]
fn sparse_coo_csr_csc_buffer_counts() {
let cases: &[(LayoutDescriptor, u8)] = &[
(LayoutDescriptor::Coo(CooLayout::new(0, false)), 2),
(LayoutDescriptor::Csr(CsrLayout::new(0)), 3),
(LayoutDescriptor::Csc(CscLayout::new(0)), 3),
];
for (d, expected) in cases {
assert_eq!(
d.buffer_count(),
NonZeroU8::new(*expected),
"tag=0x{:02X} expected buffer_count={expected}",
d.tag()
);
}
}
#[test]
fn tiled_validate_rejects_scalar_shape_via_rank_mismatch() {
let t = TiledLayout::new(vec![4], 0x01, 0x01, None, None, None).unwrap();
let result = LayoutDescriptor::Tiled(Box::new(t)).validate_against_shape(&Shape::scalar());
assert!(matches!(result, Err(Error::InvalidLayout(_))));
}
#[test]
fn coo_layout_stores_is_sorted_true() {
let c = CooLayout::new(10, true);
assert!(c.is_sorted);
assert_eq!(c.nnz, 10);
}
#[test]
fn coo_layout_stores_is_sorted_false() {
let c = CooLayout::new(0, false);
assert!(!c.is_sorted);
}
#[test]
fn strided_extreme_stride_values_do_not_panic() {
let d = LayoutDescriptor::Strided(StridedLayout::new(vec![i64::MIN, i64::MAX]));
assert!(d.validate_against_shape(&shape(&[10, 10])).is_ok());
}
#[test]
fn morton_shape_equal_to_2_pow_32_bits_is_valid() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![32]).unwrap());
assert!(d.validate_against_shape(&shape(&[4_294_967_296])).is_ok());
}
#[test]
fn morton_shape_one_above_2_pow_32_bits_is_rejected() {
let d = LayoutDescriptor::Morton(MortonLayout::new(vec![32]).unwrap());
assert!(matches!(
d.validate_against_shape(&shape(&[4_294_967_297])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn coo_nnz_u64_max_is_structurally_valid() {
let d = LayoutDescriptor::Coo(CooLayout::new(u64::MAX, false));
assert!(d.validate_against_shape(&shape(&[100, 100])).is_ok());
}
#[test]
fn csr_nnz_u64_max_is_structurally_valid() {
let d = LayoutDescriptor::Csr(CsrLayout::new(u64::MAX));
assert!(d.validate_against_shape(&shape(&[50, 50])).is_ok());
}
#[test]
fn csc_nnz_u64_max_is_structurally_valid() {
let d = LayoutDescriptor::Csc(CscLayout::new(u64::MAX));
assert!(d.validate_against_shape(&shape(&[50, 50])).is_ok());
}
#[test]
fn hilbert_large_order_accepted_by_constructor() {
let h = HilbertLayout::new(30, 2);
assert!(h.is_ok());
assert_eq!(h.unwrap().hilbert_order, 30);
}
#[test]
fn hilbert_high_rank_accepted_by_constructor() {
let h = HilbertLayout::new(1, 10);
assert!(h.is_ok());
assert_eq!(h.unwrap().hilbert_rank, 10);
}
#[test]
fn validate_strict_0xf0_gives_private_layout_tag() {
assert!(matches!(
validate_layout_tag_strict(0xF0),
Err(Error::PrivateLayoutTag(0xF0))
));
}
#[test]
fn validate_strict_0xfe_gives_private_layout_tag() {
assert!(matches!(
validate_layout_tag_strict(0xFE),
Err(Error::PrivateLayoutTag(0xFE))
));
}
#[test]
fn validate_strict_all_reserved_ranges_produce_reserved_error() {
let reserved_tags: Vec<u8> = (0x0C_u8..=0x3F)
.chain(0x41_u8..=0x7F)
.chain(0x80_u8..=0xEF)
.collect();
for tag in reserved_tags {
assert!(
matches!(
validate_layout_tag_strict(tag),
Err(Error::ReservedLayoutTag(_))
),
"tag 0x{tag:02X} in reserved range should produce ReservedLayoutTag"
);
}
}
#[test]
fn block_paged_tag_constant_is_0x0a() {
assert_eq!(TAG_BLOCK_PAGED, 0x0A);
}
#[test]
fn block_paged_tag_0x0a_passes_strict_validation() {
assert!(
validate_layout_tag_strict(0x0A).is_ok(),
"0x0A (block-paged) must pass strict validation"
);
}
#[test]
fn tag_0x0b_composite_passes_strict_validation() {
assert!(
validate_layout_tag_strict(0x0B).is_ok(),
"0x0B (composite / virtual head) must pass strict validation"
);
}
#[test]
fn is_reserved_tag_returns_false_for_0x0b_true_for_0x0c() {
assert!(!is_reserved_tag(0x0B));
assert!(is_reserved_tag(0x0C));
}
fn make_block_paged(
page_size: u32,
num_pages: u64,
num_seqs: u32,
kv_role: KvRole,
layer_index: Option<u32>,
index_type: BlockTableIndexType,
) -> LayoutDescriptor {
LayoutDescriptor::BlockPaged(BlockPagedLayout::new(
page_size,
num_pages,
0, num_seqs,
kv_role,
layer_index,
index_type,
))
}
#[test]
fn block_paged_descriptor_tag_is_0x0a() {
let d = make_block_paged(16, 64, 2, KvRole::Key, Some(0), BlockTableIndexType::U32);
assert_eq!(d.tag(), 0x0A);
}
#[test]
fn block_paged_descriptor_buffer_count_is_3() {
let d = make_block_paged(16, 64, 2, KvRole::Value, None, BlockTableIndexType::U64);
assert_eq!(d.buffer_count(), NonZeroU8::new(3));
}
#[test]
fn block_paged_buffer_count_3_for_all_kv_roles() {
for role in [KvRole::Key, KvRole::Value, KvRole::Fused] {
let d = make_block_paged(4, 10, 1, role, None, BlockTableIndexType::U32);
assert_eq!(
d.buffer_count(),
NonZeroU8::new(3),
"buffer_count must be 3 for role {role:?}"
);
}
}
#[test]
fn block_paged_validate_accepts_rank_3() {
let d = make_block_paged(4, 10, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let s = Shape::new(vec![8u64, 2, 8]).unwrap();
assert!(d.validate_against_shape(&s).is_ok());
}
#[test]
fn block_paged_validate_rejects_rank_0() {
let d = make_block_paged(4, 10, 1, KvRole::Key, None, BlockTableIndexType::U32);
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn block_paged_validate_rejects_rank_1() {
let d = make_block_paged(4, 10, 1, KvRole::Key, None, BlockTableIndexType::U32);
assert!(matches!(
d.validate_against_shape(&Shape::new(vec![8u64]).unwrap()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn block_paged_validate_rejects_rank_2() {
let d = make_block_paged(4, 10, 1, KvRole::Key, None, BlockTableIndexType::U32);
assert!(matches!(
d.validate_against_shape(&Shape::new(vec![8u64, 4]).unwrap()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn block_paged_validate_rejects_rank_4() {
let d = make_block_paged(4, 10, 1, KvRole::Key, None, BlockTableIndexType::U32);
assert!(matches!(
d.validate_against_shape(&Shape::new(vec![8u64, 4, 2, 2]).unwrap()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn block_paged_validate_rejects_paged_axis_nonzero() {
let bp = BlockPagedLayout::new(
4,
10,
1,
2,
KvRole::Key,
None,
BlockTableIndexType::U32,
);
let d = LayoutDescriptor::BlockPaged(bp);
assert!(matches!(
d.validate_against_shape(&Shape::new(vec![8u64, 2, 8]).unwrap()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn block_paged_validate_rejects_page_size_zero() {
let bp = BlockPagedLayout::new(
0,
10,
0,
2,
KvRole::Key,
None,
BlockTableIndexType::U32,
);
let d = LayoutDescriptor::BlockPaged(bp);
assert!(matches!(
d.validate_against_shape(&Shape::new(vec![8u64, 2, 8]).unwrap()),
Err(Error::InvalidLayout(_))
));
}
fn block_paged_descriptor(layout: LayoutDescriptor) -> TensorDescriptor {
let s = Shape::new(vec![8u64, 2, 8]).unwrap();
let buf = |sz: u64| {
BufferHandle::new(
sz,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap()
};
TensorDescriptor::new(
1,
0,
ElementType::Float16,
s,
0, layout,
vec![buf(320 * 2), buf(12), buf(12)], None,
None,
None,
None,
)
.unwrap()
}
#[test]
fn block_paged_codec_round_trip_key_u32_layer3() {
let layout = make_block_paged(4, 5, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn block_paged_codec_round_trip_value_u64_layer_none() {
let layout = make_block_paged(16, 128, 4, KvRole::Value, None, BlockTableIndexType::U64);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn block_paged_codec_round_trip_fused_layer_index_zero() {
let layout = make_block_paged(32, 64, 0, KvRole::Fused, Some(0), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
if let LayoutDescriptor::BlockPaged(bp) = &decoded.layout {
assert_eq!(bp.layer_index, Some(0));
} else {
panic!("expected BlockPaged layout after decode");
}
}
#[test]
fn block_paged_codec_round_trip_layer_index_large() {
let layout = make_block_paged(
16,
10,
1,
KvRole::Key,
Some(0xFFFF_FFFE),
BlockTableIndexType::U32,
);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
if let LayoutDescriptor::BlockPaged(bp) = &decoded.layout {
assert_eq!(bp.layer_index, Some(0xFFFF_FFFE));
} else {
panic!("expected BlockPaged layout after decode");
}
}
#[test]
fn block_paged_codec_round_trip_empty_batch_num_seqs_zero() {
let layout = make_block_paged(16, 32, 0, KvRole::Key, Some(1), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn block_paged_codec_round_trip_large_pool() {
let layout = make_block_paged(
64,
0x0001_0000_0000_u64,
8,
KvRole::Value,
None,
BlockTableIndexType::U64,
);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn block_paged_encoded_payload_length_is_32_bytes() {
let layout = make_block_paged(16, 5, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
let re_encoded = decoded.encode().unwrap();
assert_eq!(
encoded, re_encoded,
"encode → decode → encode must produce identical bytes"
);
}
#[test]
fn block_paged_decoder_rejects_nonzero_reserved_byte() {
let layout = make_block_paged(16, 5, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let encoded = desc.encode().unwrap();
let tag_byte = 15_usize;
assert_eq!(encoded[tag_byte], 0x0A, "layout tag must be at byte 15");
let payload_start = tag_byte + 1 + 36;
let reserved_start = payload_start + 26;
for i in 0..6 {
let mut poisoned = encoded.clone();
poisoned[reserved_start + i] = 0xFF;
let result = TensorDescriptor::decode(&poisoned);
assert!(
matches!(result, Err(Error::ReservedBytesNonZero { .. })),
"non-zero reserved byte at payload[26+{i}] must be ReservedBytesNonZero"
);
}
assert!(TensorDescriptor::decode(&encoded).is_ok());
}
#[test]
fn block_paged_decoder_rejects_unknown_kv_role_byte() {
let layout = make_block_paged(16, 5, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let mut encoded = desc.encode().unwrap();
let tag_byte = 15_usize;
assert_eq!(encoded[tag_byte], 0x0A, "layout tag must be at byte 15");
let payload_start = tag_byte + 1 + 36; let kv_role_offset = payload_start + 20;
assert!(
encoded[kv_role_offset] <= 0x02,
"expected a valid kv_role byte at offset {kv_role_offset}"
);
encoded[kv_role_offset] = 0x03; let result = TensorDescriptor::decode(&encoded);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"unknown kv_role byte 0x03 must be rejected with InvalidLayout"
);
}
#[test]
fn block_paged_decoder_rejects_unknown_block_table_index_type_byte() {
let layout = make_block_paged(16, 5, 2, KvRole::Key, Some(3), BlockTableIndexType::U32);
let desc = block_paged_descriptor(layout);
let mut encoded = desc.encode().unwrap();
let tag_byte = 15_usize;
assert_eq!(encoded[tag_byte], 0x0A, "layout tag must be at byte 15");
let payload_start = tag_byte + 1 + 36; let index_type_offset = payload_start + 25;
encoded[index_type_offset] = 0x02; let result = TensorDescriptor::decode(&encoded);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"unknown block_table_index_type byte 0x02 must be rejected with InvalidLayout"
);
}
#[test]
fn csf_tag_constant_is_0x09() {
assert_eq!(TAG_CSF, 0x09);
}
#[test]
fn csf_tag_0x09_is_not_reserved() {
use hurray_core::layout::is_reserved_tag;
assert!(!is_reserved_tag(0x09));
}
#[test]
fn csf_tag_passes_strict_validation() {
assert!(
validate_layout_tag_strict(0x09).is_ok(),
"0x09 (CSF) must pass strict validation"
);
}
#[test]
fn tag_0x0c_is_reserved_lower_boundary_after_composite() {
assert!(
matches!(
validate_layout_tag_strict(0x0C),
Err(Error::ReservedLayoutTag(0x0C))
),
"0x0C must be ReservedLayoutTag (new lower boundary after CSF, block-paged, composite)"
);
}
#[test]
fn csf_descriptor_tag_is_0x09() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
assert_eq!(d.tag(), 0x09);
}
#[test]
fn csf_buffer_count_rank3_is_7() {
let d = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
assert_eq!(d.buffer_count(), NonZeroU8::new(7));
}
#[test]
fn csf_buffer_count_rank4_is_9() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
assert_eq!(d.buffer_count(), NonZeroU8::new(9));
}
#[test]
fn csf_buffer_count_rank5_is_11() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3, 4]));
assert_eq!(d.buffer_count(), NonZeroU8::new(11));
}
#[test]
fn csf_buffer_count_uses_mode_order_len_not_a_separate_rank_field() {
let d = LayoutDescriptor::Csf(CsfLayout::new(10, vec![2, 0, 1]));
assert_eq!(d.buffer_count(), NonZeroU8::new(7));
}
#[test]
fn csf_validate_accepts_rank3_identity() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
assert!(d.validate_against_shape(&shape(&[2, 3, 4])).is_ok());
}
#[test]
fn csf_validate_accepts_rank4_identity() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
assert!(d.validate_against_shape(&shape(&[2, 3, 4, 5])).is_ok());
}
#[test]
fn csf_validate_accepts_rank5_identity() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3, 4]));
assert!(d.validate_against_shape(&shape(&[2, 3, 4, 5, 6])).is_ok());
}
#[test]
fn csf_validate_rejects_rank0() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![]));
assert!(matches!(
d.validate_against_shape(&Shape::scalar()),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_rank1() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0]));
assert!(matches!(
d.validate_against_shape(&shape(&[10])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_rank2() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1]));
assert!(matches!(
d.validate_against_shape(&shape(&[4, 5])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_len_shorter_than_rank() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1]));
let d2 = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
assert!(matches!(
d2.validate_against_shape(&shape(&[2, 3, 4, 5])),
Err(Error::InvalidLayout(_))
));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_len_longer_than_rank() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_out_of_range_value() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 3]));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_repeated_value() {
let d = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 1]));
assert!(matches!(
d.validate_against_shape(&shape(&[2, 3, 4])),
Err(Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_accepts_permutation_2_0_1() {
let d = LayoutDescriptor::Csf(CsfLayout::new(4, vec![2, 0, 1]));
assert!(d.validate_against_shape(&shape(&[2, 3, 4])).is_ok());
}
#[test]
fn csf_validate_accepts_reverse_permutation_2_1_0() {
let d = LayoutDescriptor::Csf(CsfLayout::new(4, vec![2, 1, 0]));
assert!(d.validate_against_shape(&shape(&[2, 3, 4])).is_ok());
}
fn csf_descriptor(layout: LayoutDescriptor) -> TensorDescriptor {
let rank = match &layout {
LayoutDescriptor::Csf(c) => c.mode_order.len(),
_ => panic!("expected Csf layout"),
};
let buf_count = 2 * rank + 1;
let dims: Vec<u64> = (0..rank).map(|i| i as u64 + 2).collect(); let s = Shape::new(dims).unwrap();
let buf = || {
BufferHandle::new(
64,
MIN_BUFFER_ALIGNMENT,
DeviceTag::Cpu,
SyncMode::ProducerSynced,
)
.unwrap()
};
TensorDescriptor::new(
1,
0,
ElementType::Float32,
s,
0, layout,
(0..buf_count).map(|_| buf()).collect(),
None,
None,
None,
None,
)
.unwrap()
}
#[test]
fn csf_codec_round_trip_rank3_identity_nnz4() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn csf_codec_round_trip_rank3_identity_nnz0() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn csf_codec_round_trip_rank3_permutation_2_0_1() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(7, vec![2, 0, 1]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn csf_codec_round_trip_rank4_varied_permutation() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(100, vec![3, 0, 2, 1]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn csf_codec_round_trip_rank5_reverse_permutation_nnz0() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![4, 3, 2, 1, 0]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
let decoded = TensorDescriptor::decode(&encoded).unwrap();
assert_eq!(decoded, desc);
}
#[test]
fn csf_codec_idempotent_encode_decode_encode() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(42, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let enc1 = desc.encode().unwrap();
let dec = TensorDescriptor::decode(&enc1).unwrap();
let enc2 = dec.encode().unwrap();
assert_eq!(enc1, enc2, "encode → decode → encode must be idempotent");
}
#[test]
fn csf_decoder_rejects_nonzero_reserved_byte() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let encoded = desc.encode().unwrap();
assert_eq!(encoded[15], 0x09, "layout tag must be at byte 15");
let payload_start = 15 + 1 + 36; let reserved_offset = payload_start + 20;
for i in 0..8 {
let mut poisoned = encoded.clone();
poisoned[reserved_offset + i] = 0x01;
let result = TensorDescriptor::decode(&poisoned);
assert!(
matches!(result, Err(Error::ReservedBytesNonZero { .. })),
"non-zero _reserved byte at payload[20+{i}] must be ReservedBytesNonZero"
);
}
assert!(TensorDescriptor::decode(&encoded).is_ok());
}
#[test]
fn csf_decoder_rejects_mode_order_out_of_range_value() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let mut encoded = desc.encode().unwrap();
assert_eq!(encoded[15], 0x09, "layout tag must be at byte 15");
let payload_start = 15 + 1 + 36; let mo2_offset = payload_start + 8 + 2 * 4; encoded[mo2_offset] = 3;
encoded[mo2_offset + 1] = 0;
encoded[mo2_offset + 2] = 0;
encoded[mo2_offset + 3] = 0;
let result = TensorDescriptor::decode(&encoded);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"mode_order[2]=3 is out of range for rank=3, must be InvalidLayout"
);
}
#[test]
fn csf_decoder_rejects_mode_order_duplicate_value() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let desc = csf_descriptor(layout);
let mut encoded = desc.encode().unwrap();
assert_eq!(encoded[15], 0x09, "layout tag must be at byte 15");
let payload_start = 15 + 1 + 36;
let mo2_offset = payload_start + 8 + 2 * 4;
encoded[mo2_offset] = 1;
encoded[mo2_offset + 1] = 0;
encoded[mo2_offset + 2] = 0;
encoded[mo2_offset + 3] = 0;
let result = TensorDescriptor::decode(&encoded);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"mode_order [0,1,1] has a duplicate, must be InvalidLayout"
);
}
const SPEC_MODE_ORDER: &[u32] = &[0, 1, 2];
const SPEC_POS_0: &[u64] = &[0, 2];
const SPEC_CRD_0: &[u64] = &[0, 1];
const SPEC_POS_1: &[u64] = &[0, 2, 3];
const SPEC_CRD_1: &[u64] = &[0, 2, 1];
const SPEC_POS_2: &[u64] = &[0, 1, 2, 4];
const SPEC_CRD_2: &[u64] = &[1, 3, 0, 2];
fn spec_pos_levels() -> Vec<&'static [u64]> {
vec![SPEC_POS_0, SPEC_POS_1, SPEC_POS_2]
}
fn spec_crd_levels() -> Vec<&'static [u64]> {
vec![SPEC_CRD_0, SPEC_CRD_1, SPEC_CRD_2]
}
#[test]
fn csf_element_lookup_spec_example_1_1_2_yields_leaf_3() {
let result = csf_addressing::element_offset(
&[1, 1, 2],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, Some(3), "(1,1,2) must resolve to values[3]");
}
#[test]
fn csf_element_lookup_0_0_1_yields_leaf_0() {
let result = csf_addressing::element_offset(
&[0, 0, 1],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, Some(0));
}
#[test]
fn csf_element_lookup_0_2_3_yields_leaf_1() {
let result = csf_addressing::element_offset(
&[0, 2, 3],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, Some(1));
}
#[test]
fn csf_element_lookup_1_1_0_yields_leaf_2() {
let result = csf_addressing::element_offset(
&[1, 1, 0],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, Some(2));
}
#[test]
fn csf_element_lookup_absent_at_level1_returns_none() {
let result = csf_addressing::element_offset(
&[0, 1, 0],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, None, "(0,1,0) is not stored; must be implicit zero");
}
#[test]
fn csf_element_lookup_absent_at_level1_for_i1_returns_none() {
let result = csf_addressing::element_offset(
&[1, 0, 0],
SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap();
assert_eq!(result, None);
}
#[test]
fn csf_element_lookup_rank_mismatch_returns_error() {
let err = csf_addressing::element_offset(
&[0, 1], SPEC_MODE_ORDER,
&spec_pos_levels(),
&spec_crd_levels(),
)
.unwrap_err();
assert!(
matches!(err, Error::IndexRankMismatch { .. }),
"expected IndexRankMismatch, got {err:?}"
);
}
#[test]
fn csf_element_lookup_non_identity_mode_order_column_first() {
let mode_order: &[u32] = &[2, 1, 0];
let pos_levels: Vec<&[u64]> = vec![&[0, 1], &[0, 1], &[0, 1]];
let crd_levels: Vec<&[u64]> = vec![&[1], &[0], &[0]];
let found =
csf_addressing::element_offset(&[0, 0, 1], mode_order, &pos_levels, &crd_levels).unwrap();
assert_eq!(found, Some(0));
let absent =
csf_addressing::element_offset(&[0, 0, 0], mode_order, &pos_levels, &crd_levels).unwrap();
assert_eq!(absent, None);
}
#[test]
fn csf_validate_index_buffers_spec_example_is_valid() {
assert!(csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&spec_pos_levels(),
&spec_crd_levels(),
)
.is_ok());
}
#[test]
fn csf_validate_index_buffers_empty_tensor_nnz0_is_valid() {
let pos_levels: Vec<&[u64]> = vec![&[0, 0], &[0], &[0]];
let crd_levels: Vec<&[u64]> = vec![&[], &[], &[]];
assert!(csf_addressing::validate_index_buffers(
0,
SPEC_MODE_ORDER,
&[2, 3, 4],
&pos_levels,
&crd_levels,
)
.is_ok());
}
#[test]
fn csf_validate_index_buffers_rejects_pos_not_starting_at_zero() {
let pos_levels: Vec<&[u64]> = vec![&[1, 2], SPEC_POS_1, SPEC_POS_2];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&pos_levels,
&spec_crd_levels(),
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"pos_0[0]=1 must violate invariant 1"
);
}
#[test]
fn csf_validate_index_buffers_rejects_non_monotone_pos() {
let pos_levels: Vec<&[u64]> = vec![SPEC_POS_0, &[0, 3, 2], SPEC_POS_2];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&pos_levels,
&spec_crd_levels(),
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"pos_1=[0,3,2] violates non-decreasing invariant"
);
}
#[test]
fn csf_validate_index_buffers_rejects_terminal_pos_not_equal_to_nnz() {
let pos_levels: Vec<&[u64]> = vec![SPEC_POS_0, SPEC_POS_1, &[0, 1, 2, 3]];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&pos_levels,
&spec_crd_levels(),
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"pos_2 terminal=3 != nnz=4 must be rejected"
);
}
#[test]
fn csf_validate_index_buffers_rejects_duplicate_siblings() {
let crd_levels: Vec<&[u64]> = vec![&[0, 0], SPEC_CRD_1, SPEC_CRD_2];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&spec_pos_levels(),
&crd_levels,
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"duplicate siblings [0,0] in crd_0 must be rejected"
);
}
#[test]
fn csf_validate_index_buffers_rejects_decreasing_siblings() {
let crd_levels: Vec<&[u64]> = vec![&[1, 0], SPEC_CRD_1, SPEC_CRD_2];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&spec_pos_levels(),
&crd_levels,
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"decreasing siblings [1,0] in crd_0 must be rejected"
);
}
#[test]
fn csf_validate_index_buffers_rejects_coordinate_out_of_bounds() {
let crd_levels: Vec<&[u64]> = vec![SPEC_CRD_0, SPEC_CRD_1, &[1, 3, 0, 4]];
let result = csf_addressing::validate_index_buffers(
4,
SPEC_MODE_ORDER,
&[2, 3, 4],
&spec_pos_levels(),
&crd_levels,
);
assert!(
matches!(result, Err(Error::InvalidLayout(_))),
"crd_2[3]=4 >= shape[2]=4 must be rejected"
);
}
#[test]
fn csf_descriptor_element_offset_returns_multi_buffer_error() {
let d = LayoutDescriptor::Csf(CsfLayout::new(4, vec![0, 1, 2]));
let s = Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(matches!(
d.element_offset(&[0, 0, 0], &s),
Err(Error::LayoutRequiresMultiBuffer { layout_tag: 0x09 })
));
}