D Code Coverage Analysis
Jun. 13th, 2008 11:19 amFor the D programming language, the compiler has a -cov command line switch to generate coverage information. Here's what the report looks like for the sieve program:
The numbers to the left of the | indicate how many times that line was executed. (If there is more than one statement on a line, the number is the sum of the execution counts of each of those statements.) The last line is the percent of the statements that were executed, in this case every executable statement was executed at least once.
What can this information be used for?
For references: http://dobbscodetalk.com/index.php?option=com_myblog&show=Coverage-Analysis.html&Itemid=29
|/* Eratosthenes Sieve prime number calculation. */
|
|import std.stdio;
|
|bool flags[8191];
|
|int main()
5|{ int i, prime, k, count, iter;
|
1| writefln("10 iterations");
1| for (iter = 1;
11| iter <= 10;
10| iter++)
10| { count = 0;
10| flags[] = true;
163840| for (i = 0; i < flags.length; i++)
81910| { if (flags[i])
18990| { prime = i + i + 3;
18990| k = i + prime;
168980| while (k < flags.length)
| {
149990| flags[k] = false;
149990| k += prime;
| }
18990| count += 1;
| }
| }
| }
1| writefln("%d primes", count);
1| return 0;
|}
sieve.d is 100% covered
|
|import std.stdio;
|
|bool flags[8191];
|
|int main()
5|{ int i, prime, k, count, iter;
|
1| writefln("10 iterations");
1| for (iter = 1;
11| iter <= 10;
10| iter++)
10| { count = 0;
10| flags[] = true;
163840| for (i = 0; i < flags.length; i++)
81910| { if (flags[i])
18990| { prime = i + i + 3;
18990| k = i + prime;
168980| while (k < flags.length)
| {
149990| flags[k] = false;
149990| k += prime;
| }
18990| count += 1;
| }
| }
| }
1| writefln("%d primes", count);
1| return 0;
|}
sieve.d is 100% covered
The numbers to the left of the | indicate how many times that line was executed. (If there is more than one statement on a line, the number is the sum of the execution counts of each of those statements.) The last line is the percent of the statements that were executed, in this case every executable statement was executed at least once.
What can this information be used for?
- to improve the program's performance, by finding ways to reduce the execution count of expensive operations.
- to find the most likely path of execution through a function, so the layout of the function can be optimized for that.
- to find dead code, which is code that can never be executed. Dead code tends to accumulate in older projects that have been successively maintained. Once dead code is identified, it can be removed or commented out, to streamline the source code.
- most importantly, it identifies code that isn't executed by the test suite. Test cases can then be crafted specifically to fill in those gaps.
For references: http://dobbscodetalk.com/index.php?option=com_myblog&show=Coverage-Analysis.html&Itemid=29