@@ -57,6 +57,23 @@ impl ToStr for TestName {
57
57
}
58
58
}
59
59
60
+ #[ deriving( Clone ) ]
61
+ enum NamePadding { PadNone , PadOnLeft , PadOnRight }
62
+
63
+ impl TestDesc {
64
+ fn padded_name ( & self , column_count : uint , align : NamePadding ) -> ~str {
65
+ use std:: num:: Saturating ;
66
+ let name = self . name . to_str ( ) ;
67
+ let fill = column_count. saturating_sub ( name. len ( ) ) ;
68
+ let pad = " " . repeat ( fill) ;
69
+ match align {
70
+ PadNone => name,
71
+ PadOnLeft => pad. append ( name) ,
72
+ PadOnRight => name. append ( pad) ,
73
+ }
74
+ }
75
+ }
76
+
60
77
// A function that runs a test. If the function returns successfully,
61
78
// the test succeeds; if the function fails then the test fails. We
62
79
// may need to come up with a more clever definition of test in order
@@ -70,6 +87,19 @@ pub enum TestFn {
70
87
DynBenchFn ( ~fn ( & mut BenchHarness ) )
71
88
}
72
89
90
+ impl TestFn {
91
+ fn padding ( & self ) -> NamePadding {
92
+ match self {
93
+ & StaticTestFn ( * ) => PadNone ,
94
+ & StaticBenchFn ( * ) => PadOnRight ,
95
+ & StaticMetricFn ( * ) => PadOnRight ,
96
+ & DynTestFn ( * ) => PadNone ,
97
+ & DynMetricFn ( * ) => PadOnRight ,
98
+ & DynBenchFn ( * ) => PadOnRight ,
99
+ }
100
+ }
101
+ }
102
+
73
103
// Structure passed to BenchFns
74
104
pub struct BenchHarness {
75
105
iterations : u64 ,
@@ -316,7 +346,8 @@ struct ConsoleTestState {
316
346
ignored : uint ,
317
347
measured : uint ,
318
348
metrics : MetricMap ,
319
- failures : ~[ TestDesc ]
349
+ failures : ~[ TestDesc ] ,
350
+ max_name_len : uint , // number of columns to fill when aligning names
320
351
}
321
352
322
353
impl ConsoleTestState {
@@ -348,7 +379,8 @@ impl ConsoleTestState {
348
379
ignored : 0 u,
349
380
measured : 0 u,
350
381
metrics : MetricMap :: new ( ) ,
351
- failures : ~[ ]
382
+ failures : ~[ ] ,
383
+ max_name_len : 0 u,
352
384
}
353
385
}
354
386
@@ -411,8 +443,9 @@ impl ConsoleTestState {
411
443
self . out . write_line ( format ! ( "\n running {} {}" , len, noun) ) ;
412
444
}
413
445
414
- pub fn write_test_start ( & self , test : & TestDesc ) {
415
- self . out . write_str ( format ! ( "test {} ... " , test. name. to_str( ) ) ) ;
446
+ pub fn write_test_start ( & self , test : & TestDesc , align : NamePadding ) {
447
+ let name = test. padded_name ( self . max_name_len , align) ;
448
+ self . out . write_str ( format ! ( "test {} ... " , name) ) ;
416
449
}
417
450
418
451
pub fn write_result ( & self , result : & TestResult ) {
@@ -559,12 +592,12 @@ pub fn fmt_metrics(mm: &MetricMap) -> ~str {
559
592
560
593
pub fn fmt_bench_samples ( bs : & BenchSamples ) -> ~str {
561
594
if bs. mb_s != 0 {
562
- format ! ( "{} ns/iter (+/- {}) = {} MB/s" ,
595
+ format ! ( "{:>9 } ns/iter (+/- {}) = {} MB/s" ,
563
596
bs. ns_iter_summ. median as uint,
564
597
( bs. ns_iter_summ. max - bs. ns_iter_summ. min) as uint,
565
598
bs. mb_s)
566
599
} else {
567
- format ! ( "{} ns/iter (+/- {})" ,
600
+ format ! ( "{:>9 } ns/iter (+/- {})" ,
568
601
bs. ns_iter_summ. median as uint,
569
602
( bs. ns_iter_summ. max - bs. ns_iter_summ. min) as uint)
570
603
}
@@ -577,7 +610,7 @@ pub fn run_tests_console(opts: &TestOpts,
577
610
debug2 ! ( "callback(event={:?})" , event) ;
578
611
match ( * event) . clone ( ) {
579
612
TeFiltered ( ref filtered_tests) => st. write_run_start ( filtered_tests. len ( ) ) ,
580
- TeWait ( ref test) => st. write_test_start ( test) ,
613
+ TeWait ( ref test, padding ) => st. write_test_start ( test, padding ) ,
581
614
TeResult ( test, result) => {
582
615
st. write_log ( & test, & result) ;
583
616
st. write_result ( & result) ;
@@ -607,6 +640,20 @@ pub fn run_tests_console(opts: &TestOpts,
607
640
}
608
641
}
609
642
let st = @mut ConsoleTestState :: new ( opts) ;
643
+ fn len_if_padded ( t : & TestDescAndFn ) -> uint {
644
+ match t. testfn . padding ( ) {
645
+ PadNone => 0 u,
646
+ PadOnLeft | PadOnRight => t. desc . name . to_str ( ) . len ( ) ,
647
+ }
648
+ }
649
+ match tests. iter ( ) . max_by ( |t|len_if_padded ( * t) ) {
650
+ Some ( t) => {
651
+ let n = t. desc . name . to_str ( ) ;
652
+ debug2 ! ( "Setting max_name_len from: {}" , n) ;
653
+ st. max_name_len = n. len ( ) ;
654
+ } ,
655
+ None => { }
656
+ }
610
657
run_tests ( opts, tests, |x| callback ( & x, st) ) ;
611
658
match opts. save_metrics {
612
659
None => ( ) ,
@@ -646,7 +693,8 @@ fn should_sort_failures_before_printing_them() {
646
693
ignored : 0 u,
647
694
measured : 0 u,
648
695
metrics : MetricMap :: new ( ) ,
649
- failures : ~[ test_b, test_a]
696
+ failures : ~[ test_b, test_a] ,
697
+ max_name_len : 0 u,
650
698
} ;
651
699
652
700
st. write_failures ( ) ;
@@ -662,7 +710,7 @@ fn use_color() -> bool { return get_concurrency() == 1; }
662
710
#[ deriving( Clone ) ]
663
711
enum TestEvent {
664
712
TeFiltered ( ~[ TestDesc ] ) ,
665
- TeWait ( TestDesc ) ,
713
+ TeWait ( TestDesc , NamePadding ) ,
666
714
TeResult ( TestDesc , TestResult ) ,
667
715
}
668
716
@@ -704,15 +752,15 @@ fn run_tests(opts: &TestOpts,
704
752
// We are doing one test at a time so we can print the name
705
753
// of the test before we run it. Useful for debugging tests
706
754
// that hang forever.
707
- callback ( TeWait ( test. desc . clone ( ) ) ) ;
755
+ callback ( TeWait ( test. desc . clone ( ) , test . testfn . padding ( ) ) ) ;
708
756
}
709
757
run_test ( !opts. run_tests , test, ch. clone ( ) ) ;
710
758
pending += 1 ;
711
759
}
712
760
713
761
let ( desc, result) = p. recv ( ) ;
714
762
if concurrency != 1 {
715
- callback ( TeWait ( desc. clone ( ) ) ) ;
763
+ callback ( TeWait ( desc. clone ( ) , PadNone ) ) ;
716
764
}
717
765
callback ( TeResult ( desc, result) ) ;
718
766
pending -= 1 ;
@@ -721,7 +769,7 @@ fn run_tests(opts: &TestOpts,
721
769
// All benchmarks run at the end, in serial.
722
770
// (this includes metric fns)
723
771
for b in filtered_benchs_and_metrics. move_iter ( ) {
724
- callback ( TeWait ( b. desc . clone ( ) ) ) ;
772
+ callback ( TeWait ( b. desc . clone ( ) , b . testfn . padding ( ) ) ) ;
725
773
run_test ( !opts. run_benchmarks , b, ch. clone ( ) ) ;
726
774
let ( test, result) = p. recv ( ) ;
727
775
callback ( TeResult ( test, result) ) ;
0 commit comments