Skip to content

Commit 51c4e11

Browse files
author
andrei-papou
committed
Added tests for 1- and 2-dimensional array with overflow
1 parent fbf8cac commit 51c4e11

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed

src/arrayformat.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::{
1515
};
1616
use crate::dimension::IntoDimension;
1717

18-
const PRINT_ELEMENTS_LIMIT: Ix = 2;
18+
pub const PRINT_ELEMENTS_LIMIT: Ix = 3;
1919

2020
fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
2121
f: &mut fmt::Formatter,
@@ -142,6 +142,7 @@ fn format_array_v2<A, S, D, F>(view: &ArrayBase<S, D>,
142142
Ok(())
143143
}
144144

145+
#[allow(dead_code)]
145146
fn format_array<A, S, D, F>(view: &ArrayBase<S, D>, f: &mut fmt::Formatter,
146147
mut format: F)
147148
-> fmt::Result
@@ -252,7 +253,7 @@ impl<'a, A: fmt::LowerExp, S, D: Dimension> fmt::LowerExp for ArrayBase<S, D>
252253
where S: Data<Elem=A>,
253254
{
254255
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
255-
format_array(self, f, <_>::fmt)
256+
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
256257
}
257258
}
258259

@@ -264,7 +265,7 @@ impl<'a, A: fmt::UpperExp, S, D: Dimension> fmt::UpperExp for ArrayBase<S, D>
264265
where S: Data<Elem=A>,
265266
{
266267
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
267-
format_array(self, f, <_>::fmt)
268+
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
268269
}
269270
}
270271
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -275,7 +276,7 @@ impl<'a, A: fmt::LowerHex, S, D: Dimension> fmt::LowerHex for ArrayBase<S, D>
275276
where S: Data<Elem=A>,
276277
{
277278
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
278-
format_array(self, f, <_>::fmt)
279+
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
279280
}
280281
}
281282

@@ -287,6 +288,6 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
287288
where S: Data<Elem=A>,
288289
{
289290
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
290-
format_array(self, f, <_>::fmt)
291+
format_array_v2(self, f, <_>::fmt, PRINT_ELEMENTS_LIMIT)
291292
}
292293
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ mod array_serde;
149149
mod arrayformat;
150150
mod data_traits;
151151

152+
pub use arrayformat::PRINT_ELEMENTS_LIMIT;
153+
152154
pub use crate::aliases::*;
153155

154156
#[allow(deprecated)]

tests/format.rs

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate ndarray;
22

33
use ndarray::prelude::*;
4-
use ndarray::rcarr1;
4+
use ndarray::{rcarr1, PRINT_ELEMENTS_LIMIT};
55

66
#[test]
77
fn formatting()
@@ -36,6 +36,132 @@ fn formatting()
3636
assert_eq!(s, "[01, ff, fe]");
3737
}
3838

39+
#[cfg(test)]
40+
mod formatting_with_omit {
41+
use super::*;
42+
43+
fn print_output_diff(expected: &str, actual: &str) {
44+
println!("Expected output:\n{}\nActual output:\n{}", expected, actual);
45+
}
46+
47+
#[test]
48+
fn dim_1() {
49+
let overflow: usize = 5;
50+
let a = Array1::from_elem((PRINT_ELEMENTS_LIMIT * 2 + overflow, ), 1);
51+
let mut expected_output = String::from("[");
52+
a.iter()
53+
.take(PRINT_ELEMENTS_LIMIT)
54+
.for_each(|elem| { expected_output.push_str(format!("{}, ", elem).as_str()) });
55+
expected_output.push_str("...");
56+
a.iter()
57+
.skip(PRINT_ELEMENTS_LIMIT + overflow)
58+
.for_each(|elem| { expected_output.push_str(format!(", {}", elem).as_str()) });
59+
expected_output.push(']');
60+
let actual_output = format!("{}", a);
61+
62+
print_output_diff(&expected_output, &actual_output);
63+
assert_eq!(actual_output, expected_output);
64+
}
65+
66+
#[test]
67+
fn dim_2_last_axis_overflow() {
68+
let overflow: usize = 3;
69+
let a = Array2::from_elem((PRINT_ELEMENTS_LIMIT, PRINT_ELEMENTS_LIMIT * 2 + overflow), 1);
70+
let mut expected_output = String::from("[");
71+
72+
for i in 0..PRINT_ELEMENTS_LIMIT {
73+
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
74+
for j in 1..PRINT_ELEMENTS_LIMIT {
75+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
76+
}
77+
expected_output.push_str(", ...");
78+
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
79+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
80+
}
81+
expected_output.push_str(if i < PRINT_ELEMENTS_LIMIT - 1 { "],\n " } else { "]" });
82+
}
83+
expected_output.push(']');
84+
let actual_output = format!("{}", a);
85+
86+
print_output_diff(&expected_output, &actual_output);
87+
assert_eq!(actual_output, expected_output);
88+
}
89+
90+
#[test]
91+
fn dim_2_non_last_axis_overflow() {
92+
let overflow: usize = 5;
93+
let a = Array2::from_elem((PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT), 1);
94+
let mut expected_output = String::from("[");
95+
96+
for i in 0..PRINT_ELEMENTS_LIMIT {
97+
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
98+
for j in 1..PRINT_ELEMENTS_LIMIT {
99+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
100+
}
101+
expected_output.push_str("],\n ");
102+
}
103+
expected_output.push_str("...,\n ");
104+
for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
105+
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
106+
for j in 1..PRINT_ELEMENTS_LIMIT {
107+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
108+
}
109+
expected_output.push_str(if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
110+
"]"
111+
} else {
112+
"],\n "
113+
});
114+
}
115+
expected_output.push(']');
116+
let actual_output = format!("{}", a);
117+
118+
print_output_diff(&expected_output, &actual_output);
119+
assert_eq!(actual_output, expected_output);
120+
}
121+
122+
#[test]
123+
fn dim_2_multi_directional_overflow() {
124+
let overflow: usize = 5;
125+
let a = Array2::from_elem(
126+
(PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT * 2 + overflow), 1
127+
);
128+
let mut expected_output = String::from("[");
129+
130+
for i in 0..PRINT_ELEMENTS_LIMIT {
131+
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
132+
for j in 1..PRINT_ELEMENTS_LIMIT {
133+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
134+
}
135+
expected_output.push_str(", ...");
136+
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
137+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
138+
}
139+
expected_output.push_str("],\n ");
140+
}
141+
expected_output.push_str("...,\n ");
142+
for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
143+
expected_output.push_str(format!("[{}", a[(i, 0)]).as_str());
144+
for j in 1..PRINT_ELEMENTS_LIMIT {
145+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
146+
}
147+
expected_output.push_str(", ...");
148+
for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
149+
expected_output.push_str(format!(", {}", a[(i, j)]).as_str());
150+
}
151+
expected_output.push_str(if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
152+
"]"
153+
} else {
154+
"],\n "
155+
});
156+
}
157+
expected_output.push(']');
158+
let actual_output = format!("{}", a);
159+
160+
print_output_diff(&expected_output, &actual_output);
161+
assert_eq!(actual_output, expected_output);
162+
}
163+
}
164+
39165
#[test]
40166
fn debug_format() {
41167
let a = Array2::<i32>::zeros((3, 4));

0 commit comments

Comments
 (0)