J
jaysome
/*
Does main1() have dead code that can never achieve 100% decision
coverage? And is main2() a valid way of fixing it so that there is no
dead code and the assert() never fires off and 100% decision coverage
can be achieved?
My answers are YES and YES. What are yours?
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILENAME "foo.dat"
static int main1(void);
static int main2(void);
static int main1(void)
{
FILE *fp;
char line[132];
fp = fopen(INPUT_FILENAME, "r");
if ( !fp )
{
fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
return EXIT_FAILURE;
}
if ( fgets(line, sizeof line, fp) )
{
size_t length = strlen(line);
if ( length > 0 )
{
printf("length == %lu\n", (unsigned long)length);
}
else
{
/* can this line ever be reached?*/
printf("length == 0\n");
}
}
return EXIT_SUCCESS;
}
static int main2(void)
{
FILE *fp;
char line[132];
fp = fopen(INPUT_FILENAME, "r");
if ( !fp )
{
fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
return EXIT_FAILURE;
}
if ( fgets(line, sizeof line, fp) )
{
size_t length = strlen(line);
assert(length > 0);/*always true*/
printf("length == %lu\n", (unsigned long)length);
}
return EXIT_SUCCESS;
}
int main(void)
{
int status;
status = main1();
printf("main1() returned %d\n", status);
status = main2();
printf("main2() returned %d\n", status);
return 0;
}
/*
Regards
--
jay
Using of this superb tool today prompted my question:
http://www.bullseye.com/
*/
Does main1() have dead code that can never achieve 100% decision
coverage? And is main2() a valid way of fixing it so that there is no
dead code and the assert() never fires off and 100% decision coverage
can be achieved?
My answers are YES and YES. What are yours?
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILENAME "foo.dat"
static int main1(void);
static int main2(void);
static int main1(void)
{
FILE *fp;
char line[132];
fp = fopen(INPUT_FILENAME, "r");
if ( !fp )
{
fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
return EXIT_FAILURE;
}
if ( fgets(line, sizeof line, fp) )
{
size_t length = strlen(line);
if ( length > 0 )
{
printf("length == %lu\n", (unsigned long)length);
}
else
{
/* can this line ever be reached?*/
printf("length == 0\n");
}
}
return EXIT_SUCCESS;
}
static int main2(void)
{
FILE *fp;
char line[132];
fp = fopen(INPUT_FILENAME, "r");
if ( !fp )
{
fprintf(stderr, "Error opening %s\n", INPUT_FILENAME);
return EXIT_FAILURE;
}
if ( fgets(line, sizeof line, fp) )
{
size_t length = strlen(line);
assert(length > 0);/*always true*/
printf("length == %lu\n", (unsigned long)length);
}
return EXIT_SUCCESS;
}
int main(void)
{
int status;
status = main1();
printf("main1() returned %d\n", status);
status = main2();
printf("main2() returned %d\n", status);
return 0;
}
/*
Regards
--
jay
Using of this superb tool today prompted my question:
http://www.bullseye.com/
*/