1

I want to test 2 C programs to check it's coverage (line coverage and branch coverage).

I am using gcovr in linux system.

To do this, I have written a Perl Script that will call gcc compiler and read input from the file, then gives test for the coverage of C program.

I have many test suites in the form of files that are given to C program, I am using Perl to automate the input and get the coverage using gcovr.

Of course I've used system command , and backtics in perl to call gcc and gcovr.

The first C program runs well, because the input to the program is read from arguments given.

But for the second C program, I got confused, because the program only accepts input from keyboard or simply put "it is using scanf".

So, how can we provide input to C programs that only accepts input from keyboard (scanf) through terminal linux or using perl with system command ?

Vikas Tawniya
  • 1,323
  • 1
  • 13
  • 22
Iman Tumorang
  • 131
  • 2
  • 9
  • 25

2 Answers2

0

scanf accepts input from stdin. You can redirect stdin to a file when you start your program and have the program get its input from that file.

Redirection is a feature of the OS, not a feature of a specific language.

Let's say you want to start your program and you know that this program will accept '10' 'y' and 'n' as input. All you have to do is to create a file with those lines in, by whatever way you can. Let's suppose that this file is named test_1.

You then start the program with tested_program < test_1 and the program will use '10', 'y' and 'n' as its input.

Richard St-Cyr
  • 970
  • 1
  • 8
  • 14
0

If using the Perl command system is not a strict requirement, you can consider using Open3 instead. It is more advanced than system, and you can specify what to use for STDIN, STDOUT and STDERR. So you can write the input to your C program in a text file, create a handle to that file, and use Open3 to call your C program specifying that file handle as first argument.