TCA (Total Coverage Analysis) provides visibility into which parts of your program are actually tested and how often each block is executed. Running TCA in conjunction with Insure++ and a comprehensive test suite can dramatically improve the efficiency of your testing and guarantee faster delivery of more reliable programs.

In this section:

How TCA Works

Use the insure command instead of your normal compiler to process your application. Insure++ builds a temporary file that contains your original source code instrumented for coverage analysis. The file is passed to your normal compiler. When your application runs, it creates a database containing information about which blocks were executed. TCA analyzes the database and creates reports.

Preparing Code for Coverage Analysis

Coverage analysis is automatically enabled when you instrument and compile an application with Insure++. You can also perform coverage analysis without checking for runtime errors by compiling and linking with the -Zcov switch. If you compile with insure -Zcov, you must also link with the -Zcov option and vice versa. You cannot use the -Zcov option in only one stage of the build.

Example: Sorting Strings

To see this process in action, consider the code shown below, which is a modified version of a bubble sorting algorithm.

/*
* File: strsort.c
*/
#include <stdio.h>
#include <string.h>

bubble_sort(a, n, dir)
	char **a;
	int n, dir;
{
	int i, j;

	for(i=0; i<n; i++) {
		for(j=1; j<n-i; j++) {
			if(dir * strcmp(a[j-1], a[j]) > 0) {
				char *temp;
				temp = a[j-1];
				a[j-1] = a[j];
				a[j] = temp;
			}
		}
	}
}

main(argc, argv)
int argc;
char **argv;
{
	int i, dir, length, start;
	if (argc > 1 && argv[1][0] == '-') {
		if (argv[1][1] == 'a') {
			dir = 1; length = argc-2; start = 2;
		} else if (argv[1][1] == 'd') {
			dir = -1; length = argc-2; start = 2;
		}
	} else {
		dir = 1; length = argc; start = 1;
	}
	bubble_sort(argv+start, length, dir);
	for (i = 2; i < argc; i++)
		printf("%s ", argv[i]);
	printf("\n");
	return 0;
}

This program sorts a set of strings supplied as command line arguments in either ascending or descending order, according to the settings of the command line switches.

The following command sorts strings in ascending order:

strsort -a s1 s2 s3 ...

The following command sorts strings in descending order:

strsort -d s1 s2 s3 ...

The following command sorts strings in ascending order:

strsort s1 s2 s3 ...

If you want to try the commands shown in this section, you can use the source code supplied with the Insure++ examples by executing the following command:

cp /usr/local/insure/examples/c/strsort.c 

Use the following command to translate and compile this program on Unix with runtime error detection and coverage analysis enabled: 

insure /Zi strsort.c

In addition to compiling the program, a tca.map will be created. This file describes the layout of your source file. This file is located in the same directory that your executable was built into. 

You can analyze Insure++ coverage from the command line or through the TCA GUI. Choose File> Load in the TCA window and select the tca.log file located in your program's directory.

Use the following command to review code coverage information from the command line:

tca tca.log

The following examples show how tests can be performed on the application: 

$ strsort -a aaaa bbbb
aaaa bbbb
** TCA log data will be merged with tca.log **
$ strsort -a bbb aaa
aaa bbb
** TCA log data will be merged with tca.log **
$ strsort -d aaa bbbb
bbbb aaa
** TCA log data will be merged with tca.log **
$ strsort -d bbb aaa
bbb aaa
** TCA log data will be merged with tca.log **

Each time the application is executed, the coverage analysis module issues a message indicating that information is being added to a log file, which is created the first time the program is run. This file contains the coverage information for one or more test runs. Additionally, Insure++ issues no error messages during any execution of the program.

  • No labels