DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C programmer's productivity tools

Improving test coverage with lprof

It is difficult to write test suites that fully exercise programs if you have no way of determining how much of the code is exercised. The lprof tool removes the guesswork by showing which lines of code are executed. This allows the tester to know exactly what has been tested. It also makes it easier to refine and improve tests.

To measure how well a given test suite tests a program, profile the program and look at the summary output, to see how much of the code is exercised. More specifically, you can examine individual functions that do not have 100% coverage (that is, not all lines in the function were executed). This analysis may suggest ways of improving the tests.

The following two examples illustrate why certain functions may not have 100% coverage. The first example demonstrates how to uncover a feature that is usually missed in the test suite. The second example shows how to uncover a function that is never called.

Example 1: searching for untested features

First, examine a source listing to see what parts of the code are not executed. The portion of code profiled in the following example processes command-line options.

   while((c=getopt(argc, argv, "blcnsvi")) != EOF)
         [32]		switch(c) {
             		case 'v':
     [U] [34]			vflag++;
             			break;
             		case 'c':
         [37]			cflag++;
             			break;
             		case 'n':
         [40]			nflag++;
             			break;
             		case 'b':
         [43]			bflag++;
             			break;
             		case 's':
         [46]			sflag++;
             			break;
             		case 'l':
         [49]			lflag++;
             			break;
             		case 'i':
         [52]			iflag++;
             			break;
             		case '?':
     [U] [55]			errflg++;
         [56]			}
The output shows that the code for the -v option was never executed. To correct this, create a test that exercises the option.

Example 2: functions that are never called

Consider the following lprof summary.

   Coverage Data Source: test.cnt
   Date of Coverage Data Source: Wed Mar  6 11:11:58 1991
   Object: myprog

percent lines total function covered covered lines name

91.5 97 106 compile 100.0 18 18 step 100.0 73 73 advance 100.0 4 4 getrnge 42.9 12 28 main 100.0 29 29 execute 100.0 19 19 succeed 42.9 3 7 putdata 0.0 0 19 regerr 100.0 21 21 fgetl

85.2 276 324 TOTAL

None of the lines in the function regerr are executed. To find out why, invoke cscope (described later in this chapter) and request a list of the functions that call it. If cscope reports that no function calls regerr, it may be possible to delete it from the code.
Next topic: cscope
Previous topic: Improving performance with prof and lprof

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003