|
1 | 1 | extern crate ndarray;
|
2 | 2 |
|
3 | 3 | use ndarray::prelude::*;
|
4 |
| -use ndarray::rcarr1; |
| 4 | +use ndarray::{rcarr1, PRINT_ELEMENTS_LIMIT}; |
5 | 5 |
|
6 | 6 | #[test]
|
7 | 7 | fn formatting()
|
@@ -36,6 +36,132 @@ fn formatting()
|
36 | 36 | assert_eq!(s, "[01, ff, fe]");
|
37 | 37 | }
|
38 | 38 |
|
| 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 | + |
39 | 165 | #[test]
|
40 | 166 | fn debug_format() {
|
41 | 167 | let a = Array2::<i32>::zeros((3, 4));
|
|
0 commit comments