Comment #0 by bearophile_hugs — 2010-05-03T05:24:40Z
In the output text file of the code coverage the counts before the lines can become misaligned if very long loops execute some lines many times.
For example this D2 code:
import std.stdio: writeln;
int sqr(int x) {
return x * x;
}
void main() {
int tot;
for(int i; i < 100000000; i++)
tot += 1;
writeln("hello world ", tot);
}
Produces this misaligned code coverage output (dmd 2.044) that's harder to read:
|import std.stdio: writeln;
|int sqr(int x) {
0000000| return x * x;
|}
|void main() {
1| int tot;
200000002| for(int i; i < 100000000; i++)
100000000| tot += 1;
1| writeln("hello world ", tot);
|}
test.d is 80% covered
So I suggest three changes in this file:
1) To use ============ instead of "0000000" because among the other numbers my eyes spot a sequence of equal signs better than a sequence of zeros.
2) Automatic column size management, so if the numbers grow the code coverage output keeps its vertical alignment.
3) Formatting counts with underscores to separate thousands and improve readability of large counts: 1_000_000.
I have used this little (a bit cryptic) Python script to do such processing (but I'd like dmd to do something similar by itself):
filename = "some_file_name"
def thousands(n, separator="_"):
sign = "-" if n < 0 else ""
n = str(abs(n))[::-1]
parts = [n[i:i+3] for i in xrange(0, len(n), 3)]
return sign + separator.join(parts)[::-1]
lines = file(filename+ ".lst").readlines()[:-1]
parts = [line.split("|", 1) for line in lines]
for i, line in enumerate(parts):
line[0] = line[0].strip()
if line[0] and line[0] != "0000000":
line[0] = thousands(int(line[0]))
n = max(len(p1) for p1, p2 in parts) # len of the maximum number
for p1, p2 in parts:
p2b = "|" + p2.rstrip()
if p1:
if p1 == "0000000":
print ("=" * n) + p2b
else:
print p1.rjust(n) + p2b
else:
print (" " * n) + p2b
The output of that Python script when remove_last=None :
|import std.stdio: writeln;
|int sqr(int x) {
===========| return x * x;
|}
|void main() {
1| int tot;
200_000_002| for(int i; i < 100000000; i++)
100_000_000| tot += 1;
1| writeln("hello world ", tot);
|}
Extra: I have often found that I further like to divide the counts by one thousand or even one million, to better show only very large counts):
|import std.stdio: writeln;
|int sqr(int x) {
===| return x * x;
|}
|void main() {
| int tot;
200| for(int i; i < 100000000; i++)
100| tot += 1;
| writeln("hello world ", tot);
|}
Comment #1 by bearophile_hugs — 2010-05-05T02:13:59Z
Improved version of the Python script:
filename = "test"
counts_divisor_thousands = 0 # 0, 1 or 2
def thousands(n, separator="_"):
sign = "-" if n < 0 else ""
n = str(abs(n))[::-1]
parts = [n[i:i+3] for i in xrange(0, len(n), 3)]
return sign + separator.join(parts)[::-1]
lines = [l.rstrip() for l in file(filename+ ".lst")][:-1]
parts = [l.split("|", 1) for l in lines]
divisor = 10 ** (counts_divisor_thousands * 3)
for count, code in parts:
count = count.strip()
if count and count != "0000000":
count = thousands(int(count) / divisor)
# len of the maximum count
ncols = max(len(count) for count, code in parts)
for count, code in parts:
if count:
if count == "0000000":
print ("=" * ncols) + "|" + code
else:
print count.rjust(ncols) + "|" + code
else:
print (" " * ncols) + "|" + code
Comment #2 by kennytm — 2011-06-08T13:34:26Z
The coverage output is a druntime issue (and phobos for 1.x). For example, to display '=======' instead of '0000000', you apply this patch:
diff --git a/src/rt/cover.d b/src/rt/cover.d
index 2ed5696..c72ee2f 100644
--- a/src/rt/cover.d
+++ b/src/rt/cover.d
@@ -184,7 +184,7 @@ shared static ~this()
if( c.valid[i] )
{
nno++;
- fprintf( flst, "0000000|%.*s\n", line.length, line.ptr );
+ fprintf( flst, "=======|%.*s\n", line.length, line.ptr );
}
else
{
Comment #3 by jcrapuchettes — 2014-06-06T22:30:44Z