#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct CsfLayout {
pub nnz: u64,
pub mode_order: Vec<u32>,
}
impl CsfLayout {
pub fn new(nnz: u64, mode_order: Vec<u32>) -> Self {
Self { nnz, mode_order }
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::LayoutDescriptor;
use std::num::NonZeroU8;
#[test]
fn csf_tag_is_0x09() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
assert_eq!(layout.tag(), 0x09);
}
#[test]
fn csf_tag_is_0x09_for_varying_nnz_and_permutation() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(999, vec![2, 0, 1, 3]));
assert_eq!(layout.tag(), 0x09);
}
#[test]
fn csf_buffer_count_rank3_is_7() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(5, vec![0, 1, 2]));
assert_eq!(layout.buffer_count(), Some(NonZeroU8::new(7).unwrap()));
}
#[test]
fn csf_buffer_count_rank4_is_9() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
assert_eq!(layout.buffer_count(), Some(NonZeroU8::new(9).unwrap()));
}
#[test]
fn csf_buffer_count_rank5_is_11() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3, 4]));
assert_eq!(layout.buffer_count(), Some(NonZeroU8::new(11).unwrap()));
}
#[test]
fn csf_buffer_count_derived_from_mode_order_len() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(10, vec![2, 0, 1]));
assert_eq!(layout.buffer_count(), Some(NonZeroU8::new(7).unwrap()));
}
#[test]
fn csf_layout_new_stores_nnz() {
let c = CsfLayout::new(42, vec![0, 1, 2]);
assert_eq!(c.nnz, 42);
}
#[test]
fn csf_layout_new_stores_mode_order() {
let mo = vec![2u32, 0, 1];
let c = CsfLayout::new(0, mo.clone());
assert_eq!(c.mode_order, mo);
}
#[test]
fn csf_layout_new_nnz_zero_is_valid() {
let c = CsfLayout::new(0, vec![0, 1, 2]);
assert_eq!(c.nnz, 0);
}
#[test]
fn csf_validate_accepts_rank3() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn csf_validate_accepts_rank4() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
let shape = crate::Shape::new(vec![2u64, 3, 4, 5]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn csf_validate_accepts_rank5() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3, 4]));
let shape = crate::Shape::new(vec![2u64, 3, 4, 5, 6]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn csf_validate_rejects_rank0() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![]));
assert!(matches!(
layout.validate_against_shape(&crate::Shape::scalar()),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_rank1() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0]));
let shape = crate::Shape::new(vec![10u64]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_rank2() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1]));
let shape = crate::Shape::new(vec![4u64, 5]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_len_not_equal_to_rank() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 2, 3]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_out_of_range_value() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 3]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_rejects_mode_order_repeated_value() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![0, 1, 1]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(matches!(
layout.validate_against_shape(&shape),
Err(crate::Error::InvalidLayout(_))
));
}
#[test]
fn csf_validate_accepts_non_identity_permutation() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(4, vec![2, 0, 1]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn csf_validate_accepts_reverse_permutation() {
let layout = LayoutDescriptor::Csf(CsfLayout::new(0, vec![2, 1, 0]));
let shape = crate::Shape::new(vec![2u64, 3, 4]).unwrap();
assert!(layout.validate_against_shape(&shape).is_ok());
}
#[test]
fn csf_layout_partial_eq() {
let a = CsfLayout::new(4, vec![0, 1, 2]);
let b = CsfLayout::new(4, vec![0, 1, 2]);
assert_eq!(a, b);
}
#[test]
fn csf_layout_ne_different_nnz() {
let a = CsfLayout::new(4, vec![0, 1, 2]);
let b = CsfLayout::new(0, vec![0, 1, 2]);
assert_ne!(a, b);
}
#[test]
fn csf_layout_clone_is_equal() {
let orig = CsfLayout::new(10, vec![2, 0, 1]);
assert_eq!(orig.clone(), orig);
}
#[test]
fn csf_layout_debug_is_non_empty() {
let c = CsfLayout::new(0, vec![0, 1, 2]);
assert!(!format!("{c:?}").is_empty());
}
}