Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning argv for testing purpose

Tags:

c++

I have to modify previously written C++ code and for program takes some command line arguments. Other people will be doing review and will be testing this code, to ease them, I have written this...

int main (int argc, char *argv[]) 
{
    // To do testing just uncomment the below line.
#define TESTING
#ifdef TESTING  
    argc = ARGUMENT_COUNT;
    argv[1] = new char[strlen(INPUT_FILE) + 1 ];
    strcpy(argv[1], INPUT_FILE);
    argv[2] = new char[strlen(MERGE_FILE) + 1 ];
    strcpy(argv[2], MERGE_FILE);
    .
    .
    .
#endif

My question: is there any other better way to handle this type of testing where command line is involved and the same variable argv is used everywhere.

Note: I dont have IDE support. I am using vi editor on a remote server.

like image 518
NJMR Avatar asked Oct 28 '25 14:10

NJMR


1 Answers

Put the code that processes the command line arguments into a separate function, maybe even in a method in a class that stores the values and provides them to your application.
Then, call this function/method from your main() function. Finally, implement test program(s) with test cases/functions which also call the function with the prepared test data and check for the expected result.

This way, production implementation and tests are clearly separated, no need to use a hack to provide the test data etc.

like image 127
Rene Avatar answered Oct 30 '25 03:10

Rene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!