How to examine data that was sent standard out

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in C/C++test
Data sent to standard out from a file
1. Create a simple function that will accept two arguments, a filename and mode

File: try.c
-------------cut----------------------
/* try.c */

#include <stdio.h>

/* the foo function will accept the name of the file and the mode as arguments*/
/* and pass the values to fopen, then prints the contents of the file to */
/* the terminal window */

void foo(char* filename, char* mode)
{
FILE *fp;
char t[500];

fp=fopen(filename, mode);
if (!fp)
return;
while (fgets(t,500,fp)!=NULL)
printf("%s",t);
fclose(fp);
}

---------------cut----------------------


2. Place the text file somewhere on your system.
-----------------------cut--------------------
This is a Test
-----------------------cut--------------------



3. Load the try.c file into the C++Test GUI

4. Build the Test harness and generate some test cases
> Test (menu) => Test Using => Configuration => Built-in => UnitTesting => Generate Unit Tests

5. After the test cases are played open the coverage window and examine the foo function. You will notice that while loop was never exercised.

6. The next step is to examine the stub configuration, and determine if C++Test was using the original function. C++Test by default changes harmful functions
like fopen to Safe Definition, so as not to accidentally corrupt any data files.

7. In order to exercise the while loop you must change the fopen sub from safe definition to user defined definition. Then make a call to fopen from with in the stub


-----------------------cut-----------------------
FILE * CppTest_Stub_fopen (__CPTR_TestContext __CPTR_test_context, const char * _MT_var0, const char * _MT_var1)
{
/* pass the arguments from test case to fopen */

return fopen(_MT_var0, _MT_var1);

}

----------------------cut-----------------------


8. Modify the test case to pass the location of the file, and the mode as an argument to the user defined stub
----------cut---------------------------------------
void test_foo_0()
{
/* Pre-condition initialization */
/* Initializing argument 1 (filename) */
/* Use the full path to the text file */
char * _filename = "\test.txt";

/* Initializing argument 2 (mode) */
char * _mode = "r";

{
/* Tested function call */
foo(_filename, _mode);

/* Post-condition check */
// CPPTEST_NOT_VALIDATED()
}

}

----------------------cut-----------------------------------------

9. Play the test case and you should see 100% line coverage for the foo function


10. Use the C++Test Stream API to examine the data that was sent to standard out

----------cut---------------------------------------
void test_foo_0()
{
/* Pre-condition initialization */

/* Create the CppTest_StreamRedirect objects */
CppTest_StreamRedirect* output_stream = CppTest_RedirectStdOutput();
CppTest_StreamRedirect* input_stream = CppTest_RedirectStdInput("2\n4\n");


/* Initializing argument 1 (filename) */
/* Use the full path to the text file */
char * _filename = "\test.txt";

/* Initializing argument 2 (mode) */
char * _mode = "r";

{
/* Tested function call */
foo(_filename, _mode);

/* Post-condition check */

/* Output the contents of the file to the screen */

unsigned int a;
CPPTEST_MESSAGE(CppTest_StreamReadData(output_stream, &a))

/* Delete the CppTest_StreamRedirect objects */
CppTest_StreamReset(output_stream);
CppTest_StreamReset(input_stream);

// CPPTEST_NOT_VALIDATED()
}

}

----------------------cut-----------------------------------------

11. C++Test will now send the contents of the test.txt file to the Test Cases/Results tab
Sign In or Register to comment.