@@ -229,55 +229,46 @@ mod formatting_with_omit {
229
229
use super :: * ;
230
230
use crate :: prelude:: * ;
231
231
232
- fn print_output_diff ( expected : & str , actual : & str ) {
233
- println ! ( "Expected output:\n {}\n Actual output:\n {}" , expected, actual) ;
232
+ fn assert_str_eq ( expected : & str , actual : & str ) {
233
+ assert_eq ! (
234
+ expected, actual,
235
+ "\n expected:\n {}\n actual:\n {}\n " ,
236
+ expected, actual,
237
+ ) ;
234
238
}
235
239
236
240
#[ test]
237
241
fn empty_arrays ( ) {
238
242
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) ;
243
246
}
244
247
245
248
#[ test]
246
249
fn zero_length_axes ( ) {
247
250
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) ;
252
254
}
253
255
254
256
#[ test]
255
257
fn dim_0 ( ) {
256
258
let element = 12 ;
257
259
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) ;
262
263
}
263
264
264
265
#[ test]
265
266
fn dim_1 ( ) {
266
267
let overflow: usize = 5 ;
267
268
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) ;
281
272
}
282
273
283
274
#[ test]
@@ -287,28 +278,12 @@ mod formatting_with_omit {
287
278
( PRINT_ELEMENTS_LIMIT , PRINT_ELEMENTS_LIMIT * 2 + overflow) ,
288
279
1 ,
289
280
) ;
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) ;
312
287
}
313
288
314
289
#[ test]
@@ -318,32 +293,16 @@ mod formatting_with_omit {
318
293
( PRINT_ELEMENTS_LIMIT * 2 + overflow, PRINT_ELEMENTS_LIMIT ) ,
319
294
1 ,
320
295
) ;
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) ;
347
306
}
348
307
349
308
#[ test]
@@ -356,49 +315,26 @@ mod formatting_with_omit {
356
315
) ,
357
316
1 ,
358
317
) ;
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) ;
393
328
}
394
329
395
330
#[ test]
396
331
fn dim_3_overflow_all ( ) {
397
332
let a = Array3 :: from_shape_fn ( ( 20 , 10 , 7 ) , |( i, j, k) | {
398
333
1000. + ( 100. * ( ( i as f64 ) . sqrt ( ) + ( j as f64 ) . sin ( ) + k as f64 ) ) . round ( ) / 100.
399
334
} ) ;
335
+ let actual = format ! ( "{:.2}" , a) ;
400
336
// Generated using NumPy with `np.set_printoptions(suppress=True, floatmode='maxprec_equal')`.
401
- let correct = "\
337
+ let expected = "\
402
338
[[[1000.00, 1001.00, 1002.00, ..., 1004.00, 1005.00, 1006.00],
403
339
[1000.84, 1001.84, 1002.84, ..., 1004.84, 1005.84, 1006.84],
404
340
[1000.91, 1001.91, 1002.91, ..., 1004.91, 1005.91, 1006.91],
@@ -448,7 +384,7 @@ mod formatting_with_omit {
448
384
[1005.02, 1006.02, 1007.02, ..., 1009.02, 1010.02, 1011.02],
449
385
[1005.35, 1006.35, 1007.35, ..., 1009.35, 1010.35, 1011.35],
450
386
[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 ) ;
452
388
}
453
389
454
390
#[ test]
@@ -458,8 +394,9 @@ mod formatting_with_omit {
458
394
/ 100.
459
395
+ 1000.
460
396
} ) ;
397
+ let actual = format ! ( "{:.2}" , a) ;
461
398
// Generated using NumPy with `np.set_printoptions(suppress=True, floatmode='maxprec_equal')`.
462
- let correct = "\
399
+ let expected = "\
463
400
[[[[1001.00, 1002.00, 1003.00, ..., 1006.00, 1007.00, 1008.00],
464
401
[1001.84, 1002.84, 1003.84, ..., 1006.84, 1007.84, 1008.84],
465
402
[1001.91, 1002.91, 1003.91, ..., 1006.91, 1007.91, 1008.91],
@@ -767,6 +704,6 @@ mod formatting_with_omit {
767
704
[9106.69, 9107.69, 9108.69, ..., 9111.69, 9112.69, 9113.69],
768
705
[9106.48, 9107.48, 9108.48, ..., 9111.48, 9112.48, 9113.48],
769
706
[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 ) ;
771
708
}
772
709
}
0 commit comments