use crate::error::{Error, Result};
pub fn validate_scatter_extents(
dst_shape: &[usize],
src_shape: &[usize],
dim: usize,
) -> Result<()> {
let longer = dst_shape
.iter()
.zip(src_shape)
.enumerate()
.find(|&(axis, (dst, src))| axis != dim && src > dst);
match longer {
Some((axis, (dst, src))) => Err(Error::InvalidArgument {
arg: "src",
reason: format!(
"src shape {src_shape:?} is longer than dst shape {dst_shape:?} on axis {axis} \
({src} > {dst}); only the scatter axis {dim} may differ that way"
),
}),
None => Ok(()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_scatter_axis_may_be_longer() {
assert!(validate_scatter_extents(&[2, 3], &[2, 9], 1).is_ok());
}
#[test]
fn a_shorter_source_axis_is_accepted() {
assert!(validate_scatter_extents(&[4, 3], &[2, 3], 1).is_ok());
}
#[test]
fn a_longer_source_axis_is_rejected() {
let err = validate_scatter_extents(&[2, 3], &[5, 3], 1).unwrap_err();
assert!(err.to_string().contains("axis 0"), "{err}");
}
}