Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply formatting to tests
Browse files Browse the repository at this point in the history
bluss committed Sep 16, 2019
1 parent f3859c5 commit 21b77e2
Showing 1 changed file with 53 additions and 16 deletions.
69 changes: 53 additions & 16 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
@@ -140,9 +140,14 @@ where
&[len] => {
let view = view.view().into_dimensionality::<Ix1>().unwrap();
f.write_str("[")?;
format_with_overflow(f, len, fmt_opt.collapse_limit(0), ", ", ELLIPSIS, |f, index| {
format(&view[index], f)
})?;
format_with_overflow(
f,
len,
fmt_opt.collapse_limit(0),
", ",
ELLIPSIS,
|f, index| format(&view[index], f),
)?;
f.write_str("]")?;
}
// For n-dimensional arrays, we proceed recursively
@@ -289,16 +294,24 @@ mod formatting_with_omit {
);
}

fn ellipsize(limit: usize, sep: &str, elements: impl IntoIterator<Item=impl fmt::Display>)
-> String
{
fn ellipsize(
limit: usize,
sep: &str,
elements: impl IntoIterator<Item = impl fmt::Display>,
) -> String {
let elements = elements.into_iter().collect::<Vec<_>>();
let edge = limit / 2;
if elements.len() <= limit {
format!("{}", elements.iter().format(sep))
} else {
format!("{}{}{}{}{}", elements[..edge].iter().format(sep), sep, ELLIPSIS, sep,
elements[elements.len() - edge..].iter().format(sep))
format!(
"{}{}{}{}{}",
elements[..edge].iter().format(sep),
sep,
ELLIPSIS,
sep,
elements[elements.len() - edge..].iter().format(sep)
)
}
}

@@ -348,7 +361,10 @@ mod formatting_with_omit {
#[test]
fn dim_2_last_axis_overflow() {
let overflow: usize = 2;
let a = Array2::from_elem((AXIS_2D_OVERFLOW_LIMIT, AXIS_2D_OVERFLOW_LIMIT + overflow), 1);
let a = Array2::from_elem(
(AXIS_2D_OVERFLOW_LIMIT, AXIS_2D_OVERFLOW_LIMIT + overflow),
1,
);
let actual = format!("{}", a);
let expected = "\
[[1, 1, 1, 1, 1, ..., 1, 1, 1, 1, 1],
@@ -370,7 +386,10 @@ mod formatting_with_omit {
let a = Array2::from_elem((ARRAY_MANY_ELEMENT_LIMIT / 10, 10), 1);
let actual = format!("{}", a);
let row = format!("{}", a.row(0));
let expected = format!("[{}]", ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row)));
let expected = format!(
"[{}]",
ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row))
);
assert_str_eq(&expected, &actual);
}

@@ -386,17 +405,32 @@ mod formatting_with_omit {
#[test]
fn dim_2_multi_directional_overflow() {
let overflow: usize = 2;
let a = Array2::from_elem((AXIS_2D_OVERFLOW_LIMIT + overflow, AXIS_2D_OVERFLOW_LIMIT + overflow), 1);
let a = Array2::from_elem(
(
AXIS_2D_OVERFLOW_LIMIT + overflow,
AXIS_2D_OVERFLOW_LIMIT + overflow,
),
1,
);
let actual = format!("{}", a);
let row = format!("[{}]", ellipsize(AXIS_LIMIT_ROW, ", ", a.row(0)));
let expected = format!("[{}]", ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row)));
let expected = format!(
"[{}]",
ellipsize(AXIS_LIMIT_COL, ",\n ", (0..a.nrows()).map(|_| &row))
);
assert_str_eq(&expected, &actual);
}

#[test]
fn dim_2_multi_directional_overflow_alternate() {
let overflow: usize = 2;
let a = Array2::from_elem((AXIS_2D_OVERFLOW_LIMIT + overflow, AXIS_2D_OVERFLOW_LIMIT + overflow), 1);
let a = Array2::from_elem(
(
AXIS_2D_OVERFLOW_LIMIT + overflow,
AXIS_2D_OVERFLOW_LIMIT + overflow,
),
1,
);
let actual = format!("{:#}", a);
let row = format!("{}", a.row(0));
let expected = format!("[{}]", (0..a.nrows()).map(|_| &row).format(",\n "));
@@ -405,9 +439,12 @@ mod formatting_with_omit {

#[test]
fn dim_3_overflow_most() {
let a = Array3::from_shape_fn((AXIS_LIMIT_STACKED + 1, AXIS_LIMIT_COL, AXIS_LIMIT_ROW + 1), |(i, j, k)| {
1000. + (100. * ((i as f64).sqrt() + (j as f64).sin() + k as f64)).round() / 100.
});
let a = Array3::from_shape_fn(
(AXIS_LIMIT_STACKED + 1, AXIS_LIMIT_COL, AXIS_LIMIT_ROW + 1),
|(i, j, k)| {
1000. + (100. * ((i as f64).sqrt() + (j as f64).sin() + k as f64)).round() / 100.
},
);
let actual = format!("{:6.1}", a);
let expected = "\
[[[1000.0, 1001.0, 1002.0, 1003.0, 1004.0, ..., 1007.0, 1008.0, 1009.0, 1010.0, 1011.0],

0 comments on commit 21b77e2

Please sign in to comment.