Test Coverage - executing this code


 
Thread Tools Search this Thread
Top Forums Programming Test Coverage - executing this code
# 1  
Old 11-17-2009
Test Coverage - executing this code

Hi,

I am trying to gain test coverage on the 'fold' program in unix and am having difficulty executing the following code:

Code:
if(ferror (istream))
{
	error (0, errno, "%s", filename);
	if(!STREQ (filename, "-"))
		fclose (istream);
	return 1;
}
if (!STREQ (filename, "-") && fclose (istream) == EOF)
{
	error (0, errno, "%s", filename);
	return 1;
}

How could I cause an error to occur in the input stream using the fold command in the command prompt (i.e. fold < input).

Thank you!

Last edited by DukeNuke2; 11-19-2009 at 05:15 PM.. Reason: code tags please!
# 2  
Old 11-18-2009
What that checks is the possible errors the read() system call can return. One easy one is:

SIGINT to get EINTR - use a big file for input and try ctrl/c.
EISDIR - run fold on a directory

Chances are most of the above errors are caught long before read. By stat-ing the file. Or signal trapping.

Anyway read the man page for read to see what errors you can create.

Last edited by jim mcnamara; 11-19-2009 at 05:27 PM..
# 3  
Old 11-19-2009
Thanks! Oddly enough, the directory tip worked. I originally tried a nonexistent file, and that was caught before fold ran. But, the directory run works.

However, everything inside of an if(STREQ...) statement is still not covered. I do not understand this 'if' statement. I tried creating a directory named "-" to get past the ferror 'if' statement and evaulate to true on the STREQ(filename, "-") statement, but it did not work. Any ideas?

Last edited by Jakeman1086; 11-19-2009 at 05:10 PM..
# 4  
Old 11-19-2009
STREQ is usually defined something like this
Code:
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)

The idea is to improve string comparison speed that is highly likely to fail on the first character. In the case above, filenames do almost never start with a '-' character.
And it is faster than strcmp() all by itself in this application.

This could be supporting the case:
Code:
 cat somefile | fold --

if your fold implementation allows - as meaning read from stdin.
# 5  
Old 11-21-2009
I didn't completely understand what you were getting at, but if its just an efficient comparison and - is the beginning of stdin, anything that is not stdin will pass this line?

Focusing on this line:
if (!STREQ (filename, "-") && fclose (istream) == EOF)

I'll still have to deal with fclose(istream) == EOF. To me, this line means once a file has come to its end. So, it would evaluate to true at the end of every file.
So, does this mean to get into this code segment, I just need input from non-standard input into fold? If I'm right, how do I do this? I have limited experience with unix.

Thanks for all the help. Smilie
# 6  
Old 11-22-2009
No. fclose() == EOF means there was an error closing the file. EOF is defined as -1, and is also what fgetc() returns on EOF. The coder chose EOF which is unfortunate if you are not familiar with libc stdio functions. fclose calls close(), cleans up buffers, etc.

If you want to continue with your project you need to understand:

what each function returns on error, and what system call(s) the stdc function uses underneath.

what/how to cause the error

For example, in this case
This could happen when a file descriptor cannot be closed when the filesystem becomes dismounted or corrupted, as can happen with an NSF mount.

If you read the close 2 man page, and can 'create' any of the errors you see there and then fclose will barf.

Quote:
EBADF
fd isn't a valid open file descriptor.
EINTR
The close() call was interrupted by a signal.
EIO
An I/O error occurred.
You could corrupt the FILE * struct,
Code:
close(fileno(istream));

but this means you would have to add your code to the original.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Way to get Code/path , which is executing under a process?

Dear All, Please help me in finding solution for below problem. I need a command or script to get code or path(from which location code is being executed), which is executing under a process ID. I dont have google access here,Please help me in finding solution. Thank you. (3 Replies)
Discussion started by: subbarao12
3 Replies

2. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

3. Shell Programming and Scripting

Code coverage for Korn shell

hi can anyone suggest how to check the code coverage of the shell script (ksh). also i do not see any man page for shcov. what does shcov do.? please help. (1 Reply)
Discussion started by: anijan
1 Replies

4. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

5. Shell Programming and Scripting

executing code on files in the sorted order -help!

Say i have 2 files in the giving format: file1 1 2 3 4 1 2 3 4 1 2 3 4 file2 1 2 3 4 1 2 3 4 1 2 3 4 I have a PERL code (loaned by one of u -i forgot who - thanks!) that extracts the 2nd column from each file and append horizontally to a new file: perl -ane 'push @{$L->}, $F; close... (1 Reply)
Discussion started by: epi8
1 Replies
Login or Register to Ask a Question