Personal tools
You are here: Home Courses CS453 Automated SW Testing, Fall 14 example3.c

example3.c

example3.c — C source code, 2 kB (2463 bytes)

File contents

//Example3.c
#include <stdio.h>
#include <stdlib.h>

int main(int argn, char **args) {
	int input;
	if( argn != 2 ) {
		printf("Usage: Example3 <integer>\n");
		return -1;
	} else {
		int argConvertResult = sscanf ( args[1], "%d", &input);
		if( !argConvertResult ) {
			printf("Usage: Example3 <integer>\n");
			return -1;
		}
	}
		
	input = input < 0 ? 1 : input;
	
	//if-else if-else
	if( input < 10) {
		printf("Input is less than 10\n");
	} else if ( input == 15){
		printf("Input is 15\n");
	} else {
		input *= 10;
	}	
	
	//for	
	for( int i = 0 ; i < 10 ; i++ ) {
		input += i;
	}
	
	//while
	while( (input < 100) ) {
		input += input;
	}
	
	int temp = 0;
	
	//do-while
	do {
		input -= 1;
		//switch
		switch(input) {
			case 100: 
				return 1;
			case 200: 
				return 2;
			default:
				temp++;
		}
	} while( input > 0 );	
}

/*
== How to preprocess Example3.c ==
gcc -P -E example3.c -o example3.preprocessed.c

== How to compile Example3.c after instrumentation ==
gcc -o example3 -std=c99 example3.instrumented.c

== Result ==

./Example3 1
Input is less than 10

  807        0       1  argn != 2
  812        0       1  !argConvertResult
  817        0       1  input < 0
  818        1       0  input < 10
  820        0       0  input == 15
  825        1       1  i < 10
  828        1       1  (input < 100)
  832        1       0  input > 0
  835        1       0  100
  837        0       0  200
  839        1       0  default
Covered: 11 / Total: 19 = 57.894737%

./Example3 -1

  807        0       1  argn != 2
  812        0       1  !argConvertResult
  817        1       1  input < 0
  818        1       0  input < 10
  820        0       0  input == 15
  825        1       1  i < 10
  828        1       1  (input < 100)
  832        1       0  input > 0
  835        1       0  100
  837        0       0  200
  839        1       0  default
Covered: 12 / Total: 19 = 63.157895%

./Example3 15
Input is 15

  807        0       1  argn != 2
  812        0       1  !argConvertResult
  817        1       1  input < 0
  818        1       1  input < 10
  820        1       0  input == 15
  825        1       1  i < 10
  828        1       1  (input < 100)
  832        1       0  input > 0
  835        1       0  100
  837        0       0  200
  839        1       0  default
Covered: 14 / Total: 19 = 73.684211%
*/
Document Actions