Skip to content

Commit 4fa8a62

Browse files
author
LukeMathWalker
committed
Tests are passing
1 parent 5336b10 commit 4fa8a62

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/arrayformat.rs

+29-14
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ fn format_1d_array<A, S, F>(
2929
{
3030
let n = view.len();
3131
let indexes_to_be_printed = indexes_to_be_printed(n, limit);
32+
let last_index = indexes_to_be_printed.len();
3233
write!(f, "[")?;
33-
for index in indexes_to_be_printed {
34+
for (j, index) in indexes_to_be_printed.into_iter().enumerate() {
3435
match index {
35-
Some(i) => format(&view[i], f)?,
36-
None => write!(f, ", ..., ")?,
36+
Some(i) => {
37+
format(&view[i], f)?;
38+
if j != (last_index-1) {
39+
write!(f, ", ")?;
40+
}
41+
},
42+
None => write!(f, "..., ")?,
3743
}
3844
}
3945
write!(f, "]")?;
@@ -61,29 +67,35 @@ where
6167
D: Dimension,
6268
S: Data<Elem=A>,
6369
{
64-
let view = view.view().into_dyn();
70+
if view.shape().iter().any(|&x| x == 0) {
71+
write!(f, "{}{}", "[".repeat(view.ndim()), "]".repeat(view.ndim()))?;
72+
return Ok(())
73+
}
6574
match view.shape() {
6675
[] => format(view.iter().next().unwrap(), f)?,
67-
[_] => format_1d_array(&view.into_dimensionality::<Ix1>().unwrap(), f, format, limit)?,
76+
[_] => format_1d_array(&view.view().into_dimensionality::<Ix1>().unwrap(), f, format, limit)?,
6877
shape => {
78+
let view = view.view().into_dyn();
6979
let first_axis_length = shape[0];
7080
let indexes_to_be_printed = indexes_to_be_printed(first_axis_length, limit);
71-
writeln!(f, "[")?;
72-
for index in indexes_to_be_printed {
81+
let n_to_be_printed = indexes_to_be_printed.len();
82+
write!(f, "[")?;
83+
for (j, index) in indexes_to_be_printed.into_iter().enumerate() {
7384
match index {
7485
Some(i) => {
75-
write!(f, " ")?;
7686
format_array(
7787
&view.index_axis(Axis(0), i), f, format.clone(), limit
7888
)?;
79-
writeln!(f, ",")?
89+
if j != (n_to_be_printed -1) {
90+
write!(f, ",\n ")?
91+
}
8092
},
8193
None => {
82-
writeln!(f, " ...,")?
94+
write!(f, "...,\n ")?
8395
}
8496
}
8597
}
86-
writeln!(f, "]")?;
98+
write!(f, "]")?;
8799
}
88100
}
89101
Ok(())
@@ -183,15 +195,17 @@ mod formatting_with_omit {
183195
let a: Array2<u32> = arr2(&[[], []]);
184196
let actual_output = format!("{}", a);
185197
let expected_output = String::from("[[]]");
186-
assert_eq!(actual_output, expected_output);
198+
print_output_diff(&expected_output, &actual_output);
199+
assert_eq!(expected_output, actual_output);
187200
}
188201

189202
#[test]
190203
fn zero_length_axes() {
191204
let a = Array3::<f32>::zeros((3, 0, 4));
192205
let actual_output = format!("{}", a);
193206
let expected_output = String::from("[[[]]]");
194-
assert_eq!(actual_output, expected_output);
207+
print_output_diff(&expected_output, &actual_output);
208+
assert_eq!(expected_output, actual_output);
195209
}
196210

197211
#[test]
@@ -200,7 +214,8 @@ mod formatting_with_omit {
200214
let a = arr0(element);
201215
let actual_output = format!("{}", a);
202216
let expected_output = format!("{}", element);
203-
assert_eq!(actual_output, expected_output);
217+
print_output_diff(&expected_output, &actual_output);
218+
assert_eq!(expected_output, actual_output);
204219
}
205220

206221
#[test]

0 commit comments

Comments
 (0)