Skip to content

Commit

Permalink
shape: Add test for into_shape_clone
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Mar 10, 2024
1 parent a5a4b98 commit ee7f435
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/reshape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,51 @@ fn into_shape_with_order() {
assert_eq!(s.shape(), &[4, 2]);
assert_eq!(s, array![[1, 5], [2, 6], [3, 7], [4, 8]]);
}

#[test]
fn into_shape_clone() {
// 1D -> C -> C
{
let data = [1, 2, 3, 4, 5, 6, 7, 8];
let v = Array::from(data.to_vec());
let u = v.clone().into_shape_clone(((3, 3), Order::RowMajor));
assert!(u.is_err());

let u = v.clone().into_shape_clone(((2, 2, 2), Order::C));
assert!(u.is_ok());

let u = u.unwrap();
assert_eq!(u.shape(), &[2, 2, 2]);
assert_eq!(u, array![[[1, 2], [3, 4]], [[5, 6], [7, 8]]]);

let s = u.into_shape_clone((4, 2)).unwrap();
assert_eq!(s.shape(), &[4, 2]);
assert_eq!(s, aview2(&[[1, 2], [3, 4], [5, 6], [7, 8]]));

let u = v.clone().into_shape_clone(((2, 2, 2), Order::F));
assert!(u.is_ok());

let u = u.unwrap();
assert_eq!(u.shape(), &[2, 2, 2]);
assert_eq!(u, array![[[1, 5], [3, 7]], [[2, 6], [4, 8]]]);
}

// 1D -> F -> F
{
let data = [1, 2, 3, 4, 5, 6, 7, 8];
let v = Array::from(data.to_vec());
let u = v.clone().into_shape_clone(((3, 3), Order::ColumnMajor));
assert!(u.is_err());

let u = v.into_shape_clone(((2, 2, 2), Order::ColumnMajor));
assert!(u.is_ok());

let u = u.unwrap();
assert_eq!(u.shape(), &[2, 2, 2]);
assert_eq!(u, array![[[1, 5], [3, 7]], [[2, 6], [4, 8]]]);

let s = u.into_shape_clone(((4, 2), Order::ColumnMajor)).unwrap();
assert_eq!(s.shape(), &[4, 2]);
assert_eq!(s, array![[1, 5], [2, 6], [3, 7], [4, 8]]);
}
}

0 comments on commit ee7f435

Please sign in to comment.