Skip to content

Commit

Permalink
shape: Add tests for .coerce_shape()
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Apr 17, 2021
1 parent 1dfda50 commit 62a450c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions tests/reshape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use ndarray::Order;
fn reshape() {
let data = [1, 2, 3, 4, 5, 6, 7, 8];
let v = aview1(&data);
let u = v.into_shape((3, 3));
let u = v.coerce_shape((3, 3));
assert!(u.is_err());
let u = v.into_shape((2, 2, 2));
let u = v.coerce_shape((2, 2, 2));
assert!(u.is_ok());
let u = u.unwrap();
assert_eq!(u.shape(), &[2, 2, 2]);
let s = u.into_shape((4, 2)).unwrap();
let s = u.coerce_shape((4, 2)).unwrap();
assert_eq!(s.shape(), &[4, 2]);
assert_eq!(s, aview2(&[[1, 2], [3, 4], [5, 6], [7, 8]]));
}
Expand All @@ -24,17 +24,17 @@ fn reshape() {
fn reshape_error1() {
let data = [1, 2, 3, 4, 5, 6, 7, 8];
let v = aview1(&data);
let _u = v.into_shape((2, 5)).unwrap();
let _u = v.coerce_shape((2, 5)).unwrap();
}

#[test]
#[should_panic(expected = "IncompatibleLayout")]
fn reshape_error2() {
let data = [1, 2, 3, 4, 5, 6, 7, 8];
let v = aview1(&data);
let mut u = v.into_shape((2, 2, 2)).unwrap();
let mut u = v.coerce_shape((2, 2, 2)).unwrap();
u.swap_axes(0, 1);
let _s = u.into_shape((2, 4)).unwrap();
let _s = u.coerce_shape((2, 4)).unwrap();
}

#[test]
Expand All @@ -47,16 +47,16 @@ fn reshape_f() {
println!("{:?}", v);

// noop ok
let v2 = v.into_shape((3, 4));
let v2 = v.coerce_shape(((3, 4), Order::F));
assert!(v2.is_ok());
assert_eq!(v, v2.unwrap());

let u = v.into_shape((3, 2, 2));
let u = v.coerce_shape(((3, 2, 2), Order::F));
assert!(u.is_ok());
let u = u.unwrap();
println!("{:?}", u);
assert_eq!(u.shape(), &[3, 2, 2]);
let s = u.into_shape((4, 3)).unwrap();
let s = u.coerce_shape(((4, 3), Order::F)).unwrap();
println!("{:?}", s);
assert_eq!(s.shape(), &[4, 3]);
assert_eq!(s, aview2(&[[0, 4, 8], [1, 5, 9], [2, 6, 10], [3, 7, 11]]));
Expand Down

0 comments on commit 62a450c

Please sign in to comment.