@@ -15,7 +15,7 @@ use super::{
15
15
} ;
16
16
use crate :: dimension:: IntoDimension ;
17
17
18
- pub const PRINT_ELEMENTS_LIMIT : Ix = 3 ;
18
+ const PRINT_ELEMENTS_LIMIT : Ix = 3 ;
19
19
20
20
fn format_array_v2 < A , S , D , F > ( view : & ArrayBase < S , D > ,
21
21
f : & mut fmt:: Formatter ,
@@ -291,3 +291,130 @@ impl<'a, A: fmt::Binary, S, D: Dimension> fmt::Binary for ArrayBase<S, D>
291
291
format_array_v2 ( self , f, <_ >:: fmt, PRINT_ELEMENTS_LIMIT )
292
292
}
293
293
}
294
+
295
+ #[ cfg( test) ]
296
+ mod formatting_with_omit {
297
+ use crate :: prelude:: * ;
298
+ use super :: * ;
299
+
300
+ fn print_output_diff ( expected : & str , actual : & str ) {
301
+ println ! ( "Expected output:\n {}\n Actual output:\n {}" , expected, actual) ;
302
+ }
303
+
304
+ #[ test]
305
+ fn dim_1 ( ) {
306
+ let overflow: usize = 5 ;
307
+ let a = Array1 :: from_elem ( ( PRINT_ELEMENTS_LIMIT * 2 + overflow, ) , 1 ) ;
308
+ let mut expected_output = String :: from ( "[" ) ;
309
+ a. iter ( )
310
+ . take ( PRINT_ELEMENTS_LIMIT )
311
+ . for_each ( |elem| { expected_output. push_str ( format ! ( "{}, " , elem) . as_str ( ) ) } ) ;
312
+ expected_output. push_str ( "..." ) ;
313
+ a. iter ( )
314
+ . skip ( PRINT_ELEMENTS_LIMIT + overflow)
315
+ . for_each ( |elem| { expected_output. push_str ( format ! ( ", {}" , elem) . as_str ( ) ) } ) ;
316
+ expected_output. push ( ']' ) ;
317
+ let actual_output = format ! ( "{}" , a) ;
318
+
319
+ print_output_diff ( & expected_output, & actual_output) ;
320
+ assert_eq ! ( actual_output, expected_output) ;
321
+ }
322
+
323
+ #[ test]
324
+ fn dim_2_last_axis_overflow ( ) {
325
+ let overflow: usize = 3 ;
326
+ let a = Array2 :: from_elem ( ( PRINT_ELEMENTS_LIMIT , PRINT_ELEMENTS_LIMIT * 2 + overflow) , 1 ) ;
327
+ let mut expected_output = String :: from ( "[" ) ;
328
+
329
+ for i in 0 ..PRINT_ELEMENTS_LIMIT {
330
+ expected_output. push_str ( format ! ( "[{}" , a[ ( i, 0 ) ] ) . as_str ( ) ) ;
331
+ for j in 1 ..PRINT_ELEMENTS_LIMIT {
332
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
333
+ }
334
+ expected_output. push_str ( ", ..." ) ;
335
+ for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
336
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
337
+ }
338
+ expected_output. push_str ( if i < PRINT_ELEMENTS_LIMIT - 1 { "],\n " } else { "]" } ) ;
339
+ }
340
+ expected_output. push ( ']' ) ;
341
+ let actual_output = format ! ( "{}" , a) ;
342
+
343
+ print_output_diff ( & expected_output, & actual_output) ;
344
+ assert_eq ! ( actual_output, expected_output) ;
345
+ }
346
+
347
+ #[ test]
348
+ fn dim_2_non_last_axis_overflow ( ) {
349
+ let overflow: usize = 5 ;
350
+ let a = Array2 :: from_elem ( ( PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT ) , 1 ) ;
351
+ let mut expected_output = String :: from ( "[" ) ;
352
+
353
+ for i in 0 ..PRINT_ELEMENTS_LIMIT {
354
+ expected_output. push_str ( format ! ( "[{}" , a[ ( i, 0 ) ] ) . as_str ( ) ) ;
355
+ for j in 1 ..PRINT_ELEMENTS_LIMIT {
356
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
357
+ }
358
+ expected_output. push_str ( "],\n " ) ;
359
+ }
360
+ expected_output. push_str ( "...,\n " ) ;
361
+ for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
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 ( if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
367
+ "]"
368
+ } else {
369
+ "],\n "
370
+ } ) ;
371
+ }
372
+ expected_output. push ( ']' ) ;
373
+ let actual_output = format ! ( "{}" , a) ;
374
+
375
+ print_output_diff ( & expected_output, & actual_output) ;
376
+ assert_eq ! ( actual_output, expected_output) ;
377
+ }
378
+
379
+ #[ test]
380
+ fn dim_2_multi_directional_overflow ( ) {
381
+ let overflow: usize = 5 ;
382
+ let a = Array2 :: from_elem (
383
+ ( PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT * 2 + overflow) , 1
384
+ ) ;
385
+ let mut expected_output = String :: from ( "[" ) ;
386
+
387
+ for i in 0 ..PRINT_ELEMENTS_LIMIT {
388
+ expected_output. push_str ( format ! ( "[{}" , a[ ( i, 0 ) ] ) . as_str ( ) ) ;
389
+ for j in 1 ..PRINT_ELEMENTS_LIMIT {
390
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
391
+ }
392
+ expected_output. push_str ( ", ..." ) ;
393
+ for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
394
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
395
+ }
396
+ expected_output. push_str ( "],\n " ) ;
397
+ }
398
+ expected_output. push_str ( "...,\n " ) ;
399
+ for i in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
400
+ expected_output. push_str ( format ! ( "[{}" , a[ ( i, 0 ) ] ) . as_str ( ) ) ;
401
+ for j in 1 ..PRINT_ELEMENTS_LIMIT {
402
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
403
+ }
404
+ expected_output. push_str ( ", ..." ) ;
405
+ for j in PRINT_ELEMENTS_LIMIT + overflow..PRINT_ELEMENTS_LIMIT * 2 + overflow {
406
+ expected_output. push_str ( format ! ( ", {}" , a[ ( i, j) ] ) . as_str ( ) ) ;
407
+ }
408
+ expected_output. push_str ( if i == PRINT_ELEMENTS_LIMIT * 2 + overflow - 1 {
409
+ "]"
410
+ } else {
411
+ "],\n "
412
+ } ) ;
413
+ }
414
+ expected_output. push ( ']' ) ;
415
+ let actual_output = format ! ( "{}" , a) ;
416
+
417
+ print_output_diff ( & expected_output, & actual_output) ;
418
+ assert_eq ! ( actual_output, expected_output) ;
419
+ }
420
+ }
0 commit comments