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++. See Options Used by TCA 129 for details on how to disable coverage analysis.

On Unix systems, you can perform coverage analysis without runtime error checking 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:

copy c:\insure\examples\c\strsort.c . (Windows)

cp /usr/local/insure/examples/c/strsort.c . (Unix)

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

insure -g -o strsort 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. We can now perform a set of tests on the application. A few samples are shown below. The statements beginning with the inject prompt are the Windows commands executed. The statements beginning with the $ symbol are the Unix commands executed. The remaining text is the response of the system.

Tests on Windows
C:\> inject strsort.exe -a aaaa bbbb aaaa bbbb
** TCA log data will be merged with tca.log **
C:\> inject strsort.exe -a bbb aaa aaa bbb
**TCA log data will be merged with tca.log **
C:\> inject strsort.exe -d aaa bbbb bbbb aaa
** TCA log data will be merged with tca.log **
C:\> inject strsort.exe -d bbb aaa bbb aaa
** TCA log data will be merged with tca.log **
Tests on Unix
$ 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