Skip to content

Commit c56bf67

Browse files
committed
Issue 7655: align the bench printouts so that the numbers tend to be aligned.
(scratching an itch.) Rebased and updated. Fixed bug: omitted field init from embedded struct literal in a test. Fixed bug: int underflow during padding length calculation.
1 parent c8e77d5 commit c56bf67

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

src/libextra/test.rs

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ impl ToStr for TestName {
5757
}
5858
}
5959

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+
6077
// A function that runs a test. If the function returns successfully,
6178
// the test succeeds; if the function fails then the test fails. We
6279
// may need to come up with a more clever definition of test in order
@@ -70,6 +87,19 @@ pub enum TestFn {
7087
DynBenchFn(~fn(&mut BenchHarness))
7188
}
7289

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+
73103
// Structure passed to BenchFns
74104
pub struct BenchHarness {
75105
iterations: u64,
@@ -316,7 +346,8 @@ struct ConsoleTestState {
316346
ignored: uint,
317347
measured: uint,
318348
metrics: MetricMap,
319-
failures: ~[TestDesc]
349+
failures: ~[TestDesc],
350+
max_name_len: uint, // number of columns to fill when aligning names
320351
}
321352

322353
impl ConsoleTestState {
@@ -348,7 +379,8 @@ impl ConsoleTestState {
348379
ignored: 0u,
349380
measured: 0u,
350381
metrics: MetricMap::new(),
351-
failures: ~[]
382+
failures: ~[],
383+
max_name_len: 0u,
352384
}
353385
}
354386

@@ -411,8 +443,9 @@ impl ConsoleTestState {
411443
self.out.write_line(format!("\nrunning {} {}", len, noun));
412444
}
413445

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));
416449
}
417450

418451
pub fn write_result(&self, result: &TestResult) {
@@ -559,12 +592,12 @@ pub fn fmt_metrics(mm: &MetricMap) -> ~str {
559592

560593
pub fn fmt_bench_samples(bs: &BenchSamples) -> ~str {
561594
if bs.mb_s != 0 {
562-
format!("{} ns/iter (+/- {}) = {} MB/s",
595+
format!("{:>9} ns/iter (+/- {}) = {} MB/s",
563596
bs.ns_iter_summ.median as uint,
564597
(bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint,
565598
bs.mb_s)
566599
} else {
567-
format!("{} ns/iter (+/- {})",
600+
format!("{:>9} ns/iter (+/- {})",
568601
bs.ns_iter_summ.median as uint,
569602
(bs.ns_iter_summ.max - bs.ns_iter_summ.min) as uint)
570603
}
@@ -577,7 +610,7 @@ pub fn run_tests_console(opts: &TestOpts,
577610
debug2!("callback(event={:?})", event);
578611
match (*event).clone() {
579612
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),
581614
TeResult(test, result) => {
582615
st.write_log(&test, &result);
583616
st.write_result(&result);
@@ -607,6 +640,11 @@ pub fn run_tests_console(opts: &TestOpts,
607640
}
608641
}
609642
let st = @mut ConsoleTestState::new(opts);
643+
match tests.iter().map(|t| t.desc.name.to_str().len()).max() {
644+
Some(l) => { st.max_name_len = l; },
645+
None => {}
646+
}
647+
debug2!("tests max_name_len: {}", st.max_name_len);
610648
run_tests(opts, tests, |x| callback(&x, st));
611649
match opts.save_metrics {
612650
None => (),
@@ -646,7 +684,8 @@ fn should_sort_failures_before_printing_them() {
646684
ignored: 0u,
647685
measured: 0u,
648686
metrics: MetricMap::new(),
649-
failures: ~[test_b, test_a]
687+
failures: ~[test_b, test_a],
688+
max_name_len: 0u,
650689
};
651690

652691
st.write_failures();
@@ -662,7 +701,7 @@ fn use_color() -> bool { return get_concurrency() == 1; }
662701
#[deriving(Clone)]
663702
enum TestEvent {
664703
TeFiltered(~[TestDesc]),
665-
TeWait(TestDesc),
704+
TeWait(TestDesc, NamePadding),
666705
TeResult(TestDesc, TestResult),
667706
}
668707

@@ -704,15 +743,15 @@ fn run_tests(opts: &TestOpts,
704743
// We are doing one test at a time so we can print the name
705744
// of the test before we run it. Useful for debugging tests
706745
// that hang forever.
707-
callback(TeWait(test.desc.clone()));
746+
callback(TeWait(test.desc.clone(), test.testfn.padding()));
708747
}
709748
run_test(!opts.run_tests, test, ch.clone());
710749
pending += 1;
711750
}
712751

713752
let (desc, result) = p.recv();
714753
if concurrency != 1 {
715-
callback(TeWait(desc.clone()));
754+
callback(TeWait(desc.clone(), PadNone));
716755
}
717756
callback(TeResult(desc, result));
718757
pending -= 1;
@@ -721,7 +760,7 @@ fn run_tests(opts: &TestOpts,
721760
// All benchmarks run at the end, in serial.
722761
// (this includes metric fns)
723762
for b in filtered_benchs_and_metrics.move_iter() {
724-
callback(TeWait(b.desc.clone()));
763+
callback(TeWait(b.desc.clone(), b.testfn.padding()));
725764
run_test(!opts.run_benchmarks, b, ch.clone());
726765
let (test, result) = p.recv();
727766
callback(TeResult(test, result));

0 commit comments

Comments
 (0)