Skip to content

Commit

Permalink
shape: Replace reshape with into_shape_with_order in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Mar 10, 2024
1 parent e16dc45 commit 49f5870
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
26 changes: 13 additions & 13 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn arrayviewmut_shrink_lifetime<'a, 'b: 'a>(
fn test_mat_mul() {
// smoke test, a big matrix multiplication of uneven size
let (n, m) = (45, 33);
let a = ArcArray::linspace(0., ((n * m) - 1) as f32, n as usize * m as usize).reshape((n, m));
let a = ArcArray::linspace(0., ((n * m) - 1) as f32, n as usize * m as usize).into_shape_with_order((n, m)).unwrap();
let b = ArcArray::eye(m);
assert_eq!(a.dot(&b), a);
let c = ArcArray::eye(n);
Expand Down Expand Up @@ -542,7 +542,7 @@ fn test_add() {

#[test]
fn test_multidim() {
let mut mat = ArcArray::zeros(2 * 3 * 4 * 5 * 6).reshape((2, 3, 4, 5, 6));
let mut mat = ArcArray::zeros(2 * 3 * 4 * 5 * 6).into_shape_with_order((2, 3, 4, 5, 6)).unwrap();
mat[(0, 0, 0, 0, 0)] = 22u8;
{
for (i, elt) in mat.iter_mut().enumerate() {
Expand Down Expand Up @@ -604,7 +604,7 @@ fn test_cow() {
assert_eq!(n[[0, 0]], 1);
assert_eq!(n[[0, 1]], 0);
assert_eq!(n.get((0, 1)), Some(&0));
let mut rev = mat.reshape(4);
let mut rev = mat.into_shape_with_order(4).unwrap();
rev.slice_collapse(s![..;-1]);
assert_eq!(rev[0], 4);
assert_eq!(rev[1], 3);
Expand Down Expand Up @@ -643,7 +643,7 @@ fn test_cow_shrink() {
assert_eq!(n[[0, 1]], 0);
assert_eq!(n.get((0, 1)), Some(&0));
// small has non-C strides this way
let mut small = mat.reshape(6);
let mut small = mat.into_shape_with_order(6).unwrap();
small.slice_collapse(s![4..;-1]);
assert_eq!(small[0], 6);
assert_eq!(small[1], 5);
Expand All @@ -660,22 +660,22 @@ fn test_cow_shrink() {
#[test]
#[cfg(feature = "std")]
fn test_sub() {
let mat = ArcArray::linspace(0., 15., 16).reshape((2, 4, 2));
let mat = ArcArray::linspace(0., 15., 16).into_shape_with_order((2, 4, 2)).unwrap();
let s1 = mat.index_axis(Axis(0), 0);
let s2 = mat.index_axis(Axis(0), 1);
assert_eq!(s1.shape(), &[4, 2]);
assert_eq!(s2.shape(), &[4, 2]);
let n = ArcArray::linspace(8., 15., 8).reshape((4, 2));
let n = ArcArray::linspace(8., 15., 8).into_shape_with_order((4, 2)).unwrap();
assert_eq!(n, s2);
let m = ArcArray::from(vec![2., 3., 10., 11.]).reshape((2, 2));
let m = ArcArray::from(vec![2., 3., 10., 11.]).into_shape_with_order((2, 2)).unwrap();
assert_eq!(m, mat.index_axis(Axis(1), 1));
}

#[should_panic]
#[test]
#[cfg(feature = "std")]
fn test_sub_oob_1() {
let mat = ArcArray::linspace(0., 15., 16).reshape((2, 4, 2));
let mat = ArcArray::linspace(0., 15., 16).into_shape_with_order((2, 4, 2)).unwrap();
mat.index_axis(Axis(0), 2);
}

Expand Down Expand Up @@ -1337,7 +1337,7 @@ fn from_vec_dim_stride_2d_rejects() {

#[test]
fn views() {
let a = ArcArray::from(vec![1, 2, 3, 4]).reshape((2, 2));
let a = ArcArray::from(vec![1, 2, 3, 4]).into_shape_with_order((2, 2)).unwrap();
let b = a.view();
assert_eq!(a, b);
assert_eq!(a.shape(), b.shape());
Expand All @@ -1354,7 +1354,7 @@ fn views() {

#[test]
fn view_mut() {
let mut a = ArcArray::from(vec![1, 2, 3, 4]).reshape((2, 2));
let mut a = ArcArray::from(vec![1, 2, 3, 4]).into_shape_with_order((2, 2)).unwrap();
for elt in &mut a.view_mut() {
*elt = 0;
}
Expand All @@ -1373,7 +1373,7 @@ fn view_mut() {

#[test]
fn slice_mut() {
let mut a = ArcArray::from(vec![1, 2, 3, 4]).reshape((2, 2));
let mut a = ArcArray::from(vec![1, 2, 3, 4]).into_shape_with_order((2, 2)).unwrap();
for elt in a.slice_mut(s![.., ..]) {
*elt = 0;
}
Expand Down Expand Up @@ -1680,7 +1680,7 @@ fn arithmetic_broadcast() {
#[test]
fn char_array() {
// test compilation & basics of non-numerical array
let cc = ArcArray::from_iter("alphabet".chars()).reshape((4, 2));
let cc = ArcArray::from_iter("alphabet".chars()).into_shape_with_order((4, 2)).unwrap();
assert!(cc.index_axis(Axis(1), 0) == ArcArray::from_iter("apae".chars()));
}

Expand Down Expand Up @@ -1740,7 +1740,7 @@ fn split_at() {
}
assert_eq!(a, arr2(&[[1., 5.], [8., 4.]]));

let b = ArcArray::linspace(0., 59., 60).reshape((3, 4, 5));
let b = ArcArray::linspace(0., 59., 60).into_shape_with_order((3, 4, 5)).unwrap();

let (left, right) = b.view().split_at(Axis(2), 2);
assert_eq!(left.shape(), [3, 4, 2]);
Expand Down
38 changes: 19 additions & 19 deletions tests/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn double_ended() {
#[test]
fn iter_size_hint() {
// Check that the size hint is correctly computed
let a = ArcArray::from_iter(0..24).reshape((2, 3, 4));
let a = ArcArray::from_iter(0..24).into_shape_with_order((2, 3, 4)).unwrap();
let mut data = [0; 24];
for (i, elt) in enumerate(&mut data) {
*elt = i as i32;
Expand All @@ -64,7 +64,7 @@ fn indexed() {
for (i, elt) in a.indexed_iter() {
assert_eq!(i, *elt as usize);
}
let a = a.reshape((2, 4, 1));
let a = a.into_shape_with_order((2, 4, 1)).unwrap();
let (mut i, mut j, k) = (0, 0, 0);
for (idx, elt) in a.indexed_iter() {
assert_eq!(idx, (i, j, k));
Expand Down Expand Up @@ -96,11 +96,11 @@ fn as_slice() {
}

let a = ArcArray::linspace(0., 7., 8);
let a = a.reshape((2, 4, 1));
let a = a.into_shape_with_order((2, 4, 1)).unwrap();

assert_slice_correct(&a);

let a = a.reshape((2, 4));
let a = a.into_shape_with_order((2, 4)).unwrap();
assert_slice_correct(&a);

assert!(a.view().index_axis(Axis(1), 0).as_slice().is_none());
Expand All @@ -123,7 +123,7 @@ fn as_slice() {
assert!(u.as_slice().is_some());
assert_slice_correct(&u);

let a = a.reshape((8, 1));
let a = a.into_shape_with_order((8, 1)).unwrap();
assert_slice_correct(&a);
let u = a.slice(s![..;2, ..]);
println!(
Expand All @@ -138,7 +138,7 @@ fn as_slice() {
#[test]
fn inner_iter() {
let a = ArcArray::from_iter(0..12);
let a = a.reshape((2, 3, 2));
let a = a.into_shape_with_order((2, 3, 2)).unwrap();
// [[[0, 1],
// [2, 3],
// [4, 5]],
Expand Down Expand Up @@ -187,7 +187,7 @@ fn inner_iter_corner_cases() {
#[test]
fn inner_iter_size_hint() {
// Check that the size hint is correctly computed
let a = ArcArray::from_iter(0..24).reshape((2, 3, 4));
let a = ArcArray::from_iter(0..24).into_shape_with_order((2, 3, 4)).unwrap();
let mut len = 6;
let mut it = a.rows().into_iter();
assert_eq!(it.len(), len);
Expand All @@ -202,7 +202,7 @@ fn inner_iter_size_hint() {
#[test]
fn outer_iter() {
let a = ArcArray::from_iter(0..12);
let a = a.reshape((2, 3, 2));
let a = a.into_shape_with_order((2, 3, 2)).unwrap();
// [[[0, 1],
// [2, 3],
// [4, 5]],
Expand Down Expand Up @@ -261,7 +261,7 @@ fn outer_iter() {
#[test]
fn axis_iter() {
let a = ArcArray::from_iter(0..12);
let a = a.reshape((2, 3, 2));
let a = a.into_shape_with_order((2, 3, 2)).unwrap();
// [[[0, 1],
// [2, 3],
// [4, 5]],
Expand Down Expand Up @@ -353,7 +353,7 @@ fn outer_iter_corner_cases() {
#[test]
fn outer_iter_mut() {
let a = ArcArray::from_iter(0..12);
let a = a.reshape((2, 3, 2));
let a = a.into_shape_with_order((2, 3, 2)).unwrap();
// [[[0, 1],
// [2, 3],
// [4, 5]],
Expand All @@ -380,7 +380,7 @@ fn outer_iter_mut() {
#[test]
fn axis_iter_mut() {
let a = ArcArray::from_iter(0..12);
let a = a.reshape((2, 3, 2));
let a = a.into_shape_with_order((2, 3, 2)).unwrap();
// [[[0, 1],
// [2, 3],
// [4, 5]],
Expand All @@ -400,7 +400,7 @@ fn axis_iter_mut() {
#[test]
fn axis_chunks_iter() {
let a = ArcArray::from_iter(0..24);
let a = a.reshape((2, 6, 2));
let a = a.into_shape_with_order((2, 6, 2)).unwrap();

let it = a.axis_chunks_iter(Axis(1), 2);
assert_equal(
Expand All @@ -413,7 +413,7 @@ fn axis_chunks_iter() {
);

let a = ArcArray::from_iter(0..28);
let a = a.reshape((2, 7, 2));
let a = a.into_shape_with_order((2, 7, 2)).unwrap();

let it = a.axis_chunks_iter(Axis(1), 2);
assert_equal(
Expand Down Expand Up @@ -535,7 +535,7 @@ fn axis_chunks_iter_corner_cases() {
// and enable checking if no pointer offsetting is out of bounds. However
// checking the absence of of out of bounds offsetting cannot (?) be
// done automatically, so one has to launch this test in a debugger.
let a = ArcArray::<f32, _>::linspace(0., 7., 8).reshape((8, 1));
let a = ArcArray::<f32, _>::linspace(0., 7., 8).into_shape_with_order((8, 1)).unwrap();
let it = a.axis_chunks_iter(Axis(0), 4);
assert_equal(it, vec![a.slice(s![..4, ..]), a.slice(s![4.., ..])]);
let a = a.slice(s![..;-1,..]);
Expand Down Expand Up @@ -634,7 +634,7 @@ fn axis_chunks_iter_split_at() {
#[test]
fn axis_chunks_iter_mut() {
let a = ArcArray::from_iter(0..24);
let mut a = a.reshape((2, 6, 2));
let mut a = a.into_shape_with_order((2, 6, 2)).unwrap();

let mut it = a.axis_chunks_iter_mut(Axis(1), 2);
let mut col0 = it.next().unwrap();
Expand All @@ -658,7 +658,7 @@ fn axis_chunks_iter_mut_zero_axis_len() {
#[test]
fn outer_iter_size_hint() {
// Check that the size hint is correctly computed
let a = ArcArray::from_iter(0..24).reshape((4, 3, 2));
let a = ArcArray::from_iter(0..24).into_shape_with_order((4, 3, 2)).unwrap();
let mut len = 4;
let mut it = a.outer_iter();
assert_eq!(it.len(), len);
Expand Down Expand Up @@ -690,7 +690,7 @@ fn outer_iter_size_hint() {

#[test]
fn outer_iter_split_at() {
let a = ArcArray::from_iter(0..30).reshape((5, 3, 2));
let a = ArcArray::from_iter(0..30).into_shape_with_order((5, 3, 2)).unwrap();

let it = a.outer_iter();
let (mut itl, mut itr) = it.clone().split_at(2);
Expand All @@ -712,15 +712,15 @@ fn outer_iter_split_at() {
#[test]
#[should_panic]
fn outer_iter_split_at_panics() {
let a = ArcArray::from_iter(0..30).reshape((5, 3, 2));
let a = ArcArray::from_iter(0..30).into_shape_with_order((5, 3, 2)).unwrap();

let it = a.outer_iter();
it.split_at(6);
}

#[test]
fn outer_iter_mut_split_at() {
let mut a = ArcArray::from_iter(0..30).reshape((5, 3, 2));
let mut a = ArcArray::from_iter(0..30).into_shape_with_order((5, 3, 2)).unwrap();

{
let it = a.outer_iter_mut();
Expand Down
6 changes: 3 additions & 3 deletions xtest-serialization/tests/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn serial_many_dim_serde() {

{
// Test a sliced array.
let mut a = ArcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
let mut a = ArcArray::linspace(0., 31., 32).into_shape_with_order((2, 2, 2, 4)).unwrap();
a.slice_collapse(s![..;-1, .., .., ..2]);
let serial = serde_json::to_string(&a).unwrap();
println!("Encode {:?} => {:?}", a, serial);
Expand Down Expand Up @@ -155,7 +155,7 @@ fn serial_many_dim_serde_msgpack() {

{
// Test a sliced array.
let mut a = ArcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
let mut a = ArcArray::linspace(0., 31., 32).into_shape_with_order((2, 2, 2, 4)).unwrap();
a.slice_collapse(s![..;-1, .., .., ..2]);

let mut buf = Vec::new();
Expand Down Expand Up @@ -208,7 +208,7 @@ fn serial_many_dim_ron() {

{
// Test a sliced array.
let mut a = ArcArray::linspace(0., 31., 32).reshape((2, 2, 2, 4));
let mut a = ArcArray::linspace(0., 31., 32).into_shape_with_order((2, 2, 2, 4)).unwrap();
a.slice_collapse(s![..;-1, .., .., ..2]);

let a_s = ron_serialize(&a).unwrap();
Expand Down

0 comments on commit 49f5870

Please sign in to comment.