Skip to content

Commit 9dd0f32

Browse files
committed
Make formatting tests easier to read
1 parent cf22e23 commit 9dd0f32

File tree

2 files changed

+85
-123
lines changed

2 files changed

+85
-123
lines changed

src/arrayformat.rs

+50-113
Original file line numberDiff line numberDiff line change
@@ -229,55 +229,46 @@ mod formatting_with_omit {
229229
use super::*;
230230
use crate::prelude::*;
231231

232-
fn print_output_diff(expected: &str, actual: &str) {
233-
println!("Expected output:\n{}\nActual output:\n{}", expected, actual);
232+
fn assert_str_eq(expected: &str, actual: &str) {
233+
assert_eq!(
234+
expected, actual,
235+
"\nexpected:\n{}\nactual:\n{}\n",
236+
expected, actual,
237+
);
234238
}
235239

236240
#[test]
237241
fn empty_arrays() {
238242
let a: Array2<u32> = arr2(&[[], []]);
239-
let actual_output = format!("{}", a);
240-
let expected_output = String::from("[[]]");
241-
print_output_diff(&expected_output, &actual_output);
242-
assert_eq!(expected_output, actual_output);
243+
let actual = format!("{}", a);
244+
let expected = "[[]]";
245+
assert_str_eq(expected, &actual);
243246
}
244247

245248
#[test]
246249
fn zero_length_axes() {
247250
let a = Array3::<f32>::zeros((3, 0, 4));
248-
let actual_output = format!("{}", a);
249-
let expected_output = String::from("[[[]]]");
250-
print_output_diff(&expected_output, &actual_output);
251-
assert_eq!(expected_output, actual_output);
251+
let actual = format!("{}", a);
252+
let expected = "[[[]]]";
253+
assert_str_eq(expected, &actual);
252254
}
253255

254256
#[test]
255257
fn dim_0() {
256258
let element = 12;
257259
let a = arr0(element);
258-
let actual_output = format!("{}", a);
259-
let expected_output = format!("{}", element);
260-
print_output_diff(&expected_output, &actual_output);
261-
assert_eq!(expected_output, actual_output);
260+
let actual = format!("{}", a);
261+
let expected = "12";
262+
assert_str_eq(expected, &actual);
262263
}
263264

264265
#[test]
265266
fn dim_1() {
266267
let overflow: usize = 5;
267268
let a = Array1::from_elem((PRINT_ELEMENTS_LIMIT * 2 + overflow,), 1);
268-
let mut expected_output = String::from("[");
269-
a.iter()
270-
.take(PRINT_ELEMENTS_LIMIT)
271-
.for_each(|elem| expected_output.push_str(format!("{}, ", elem).as_str()));
272-
expected_output.push_str("...");
273-
a.iter()
274-
.skip(PRINT_ELEMENTS_LIMIT + overflow)
275-
.for_each(|elem| expected_output.push_str(format!(", {}", elem).as_str()));
276-
expected_output.push(']');
277-
let actual_output = format!("{}", a);
278-
279-
print_output_diff(&expected_output, &actual_output);
280-
assert_eq!(actual_output, expected_output);
269+
let actual = format!("{}", a);
270+
let expected = "[1, 1, 1, ..., 1, 1, 1]";
271+
assert_str_eq(expected, &actual);
281272
}
282273

283274
#[test]
@@ -287,28 +278,12 @@ mod formatting_with_omit {
287278
(PRINT_ELEMENTS_LIMIT, PRINT_ELEMENTS_LIMIT * 2 + overflow),
288279
1,
289280
);
290-
let mut expected_output = String::from("[");
291-
292-
for i in 0..PRINT_ELEMENTS_LIMIT {
293-
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
294-
for j in 1..PRINT_ELEMENTS_LIMIT {
295-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
296-
}
297-
expected_output.push_str(", ...");
298-
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
299-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
300-
}
301-
expected_output.push_str(if i < PRINT_ELEMENTS_LIMIT - 1 {
302-
"],\n "
303-
} else {
304-
"]"
305-
});
306-
}
307-
expected_output.push(']');
308-
let actual_output = format!("{}", a);
309-
310-
print_output_diff(&expected_output, &actual_output);
311-
assert_eq!(actual_output, expected_output);
281+
let actual = format!("{}", a);
282+
let expected = "\
283+
[[1, 1, 1, ..., 1, 1, 1],
284+
[1, 1, 1, ..., 1, 1, 1],
285+
[1, 1, 1, ..., 1, 1, 1]]";
286+
assert_str_eq(expected, &actual);
312287
}
313288

314289
#[test]
@@ -318,32 +293,16 @@ mod formatting_with_omit {
318293
(PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT),
319294
1,
320295
);
321-
let mut expected_output = String::from("[");
322-
323-
for i in 0..PRINT_ELEMENTS_LIMIT {
324-
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
325-
for j in 1..PRINT_ELEMENTS_LIMIT {
326-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
327-
}
328-
expected_output.push_str("],\n ");
329-
}
330-
expected_output.push_str("...,\n ");
331-
for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
332-
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
333-
for j in 1..PRINT_ELEMENTS_LIMIT {
334-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
335-
}
336-
expected_output.push_str(if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
337-
"]"
338-
} else {
339-
"],\n "
340-
});
341-
}
342-
expected_output.push(']');
343-
let actual_output = format!("{}", a);
344-
345-
print_output_diff(&expected_output, &actual_output);
346-
assert_eq!(actual_output, expected_output);
296+
let actual = format!("{}", a);
297+
let expected = "\
298+
[[1, 1, 1],
299+
[1, 1, 1],
300+
[1, 1, 1],
301+
...,
302+
[1, 1, 1],
303+
[1, 1, 1],
304+
[1, 1, 1]]";
305+
assert_str_eq(expected, &actual);
347306
}
348307

349308
#[test]
@@ -356,49 +315,26 @@ mod formatting_with_omit {
356315
),
357316
1,
358317
);
359-
let mut expected_output = String::from("[");
360-
361-
for i in 0..PRINT_ELEMENTS_LIMIT {
362-
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
363-
for j in 1..PRINT_ELEMENTS_LIMIT {
364-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
365-
}
366-
expected_output.push_str(", ...");
367-
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
368-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
369-
}
370-
expected_output.push_str("],\n ");
371-
}
372-
expected_output.push_str("...,\n ");
373-
for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
374-
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
375-
for j in 1..PRINT_ELEMENTS_LIMIT {
376-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
377-
}
378-
expected_output.push_str(", ...");
379-
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
380-
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
381-
}
382-
expected_output.push_str(if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
383-
"]"
384-
} else {
385-
"],\n "
386-
});
387-
}
388-
expected_output.push(']');
389-
let actual_output = format!("{}", a);
390-
391-
print_output_diff(&expected_output, &actual_output);
392-
assert_eq!(actual_output, expected_output);
318+
let actual = format!("{}", a);
319+
let expected = "\
320+
[[1, 1, 1, ..., 1, 1, 1],
321+
[1, 1, 1, ..., 1, 1, 1],
322+
[1, 1, 1, ..., 1, 1, 1],
323+
...,
324+
[1, 1, 1, ..., 1, 1, 1],
325+
[1, 1, 1, ..., 1, 1, 1],
326+
[1, 1, 1, ..., 1, 1, 1]]";
327+
assert_str_eq(expected, &actual);
393328
}
394329

395330
#[test]
396331
fn dim_3_overflow_all() {
397332
let a = Array3::from_shape_fn((20, 10, 7), |(i, j, k)| {
398333
1000. + (100. * ((i as f64).sqrt() + (j as f64).sin() + k as f64)).round() / 100.
399334
});
335+
let actual = format!("{:.2}", a);
400336
// Generated using NumPy with `np.set_printoptions(suppress=True, floatmode='maxprec_equal')`.
401-
let correct = "\
337+
let expected = "\
402338
[[[1000.00, 1001.00, 1002.00, ..., 1004.00, 1005.00, 1006.00],
403339
[1000.84, 1001.84, 1002.84, ..., 1004.84, 1005.84, 1006.84],
404340
[1000.91, 1001.91, 1002.91, ..., 1004.91, 1005.91, 1006.91],
@@ -448,7 +384,7 @@ mod formatting_with_omit {
448384
[1005.02, 1006.02, 1007.02, ..., 1009.02, 1010.02, 1011.02],
449385
[1005.35, 1006.35, 1007.35, ..., 1009.35, 1010.35, 1011.35],
450386
[1004.77, 1005.77, 1006.77, ..., 1008.77, 1009.77, 1010.77]]]";
451-
assert_eq!(format!("{:.2}", a), correct);
387+
assert_str_eq(expected, &actual);
452388
}
453389

454390
#[test]
@@ -458,8 +394,9 @@ mod formatting_with_omit {
458394
/ 100.
459395
+ 1000.
460396
});
397+
let actual = format!("{:.2}", a);
461398
// Generated using NumPy with `np.set_printoptions(suppress=True, floatmode='maxprec_equal')`.
462-
let correct = "\
399+
let expected = "\
463400
[[[[1001.00, 1002.00, 1003.00, ..., 1006.00, 1007.00, 1008.00],
464401
[1001.84, 1002.84, 1003.84, ..., 1006.84, 1007.84, 1008.84],
465402
[1001.91, 1002.91, 1003.91, ..., 1006.91, 1007.91, 1008.91],
@@ -767,6 +704,6 @@ mod formatting_with_omit {
767704
[9106.69, 9107.69, 9108.69, ..., 9111.69, 9112.69, 9113.69],
768705
[9106.48, 9107.48, 9108.48, ..., 9111.48, 9112.48, 9113.48],
769706
[9107.16, 9108.16, 9109.16, ..., 9112.16, 9113.16, 9114.16]]]]";
770-
assert_eq!(format!("{:.2}", a), correct);
707+
assert_str_eq(expected, &actual);
771708
}
772709
}

tests/format.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,45 @@ use ndarray::rcarr1;
44
#[test]
55
fn formatting() {
66
let a = rcarr1::<f32>(&[1., 2., 3., 4.]);
7+
assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
8+
assert_eq!(format!("{:4}", a), "[ 1, 2, 3, 4]");
9+
let a = a.reshape((4, 1, 1));
710
assert_eq!(
811
format!("{}", a),
9-
//"[ 1, 2, 3, 4]");
10-
"[1, 2, 3, 4]"
12+
"\
13+
[[[1]],
14+
15+
[[2]],
16+
17+
[[3]],
18+
19+
[[4]]]"
1120
);
12-
assert_eq!(format!("{:4}", a), "[ 1, 2, 3, 4]");
13-
let a = a.reshape((4, 1, 1));
1421
assert_eq!(
1522
format!("{:4}", a),
16-
"[[[ 1]],\n\n [[ 2]],\n\n [[ 3]],\n\n [[ 4]]]",
23+
"\
24+
[[[ 1]],
25+
26+
[[ 2]],
27+
28+
[[ 3]],
29+
30+
[[ 4]]]",
1731
);
1832

1933
let a = a.reshape((2, 2));
20-
assert_eq!(format!("{}", a), "[[1, 2],\n [3, 4]]");
21-
assert_eq!(format!("{}", a), "[[1, 2],\n [3, 4]]");
22-
assert_eq!(format!("{:4}", a), "[[ 1, 2],\n [ 3, 4]]");
34+
assert_eq!(
35+
format!("{}", a),
36+
"\
37+
[[1, 2],
38+
[3, 4]]"
39+
);
40+
assert_eq!(
41+
format!("{:4}", a),
42+
"\
43+
[[ 1, 2],
44+
[ 3, 4]]"
45+
);
2346

2447
let b = arr0::<f32>(3.5);
2548
assert_eq!(format!("{}", b), "3.5");
@@ -36,13 +59,15 @@ fn debug_format() {
3659
let a = Array2::<i32>::zeros((3, 4));
3760
assert_eq!(
3861
format!("{:?}", a),
39-
"[[0, 0, 0, 0],
62+
"\
63+
[[0, 0, 0, 0],
4064
[0, 0, 0, 0],
4165
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), const ndim=2"
4266
);
4367
assert_eq!(
4468
format!("{:?}", a.into_dyn()),
45-
"[[0, 0, 0, 0],
69+
"\
70+
[[0, 0, 0, 0],
4671
[0, 0, 0, 0],
4772
[0, 0, 0, 0]] shape=[3, 4], strides=[4, 1], layout=C (0x1), dynamic ndim=2"
4873
);

0 commit comments

Comments
 (0)