Mihai93
Mihai93

Reputation: 41

How can I make a makefile to test some inputs?

So I have this C++ program and I have some inputs for it that I need to test.

I need to do a makefile for it to test them all, but I dont have the slightest clue how to do it, or what I need to write in my C++ program to get the inputs from the makefile.

I know the basics of makefiles, like the build, valgrind and clear commands, but this I dont have a clue how I can make....
I also need to create some .out files with the output of my program.

I know this might be a stupid and easy question, but I dont find the answer to both my questions(How can I make this makefile work and how I can parse the files from the makefile into my C program). EDIT: So this is how my makefile looks now. Now, how can I run them all at once? Can I do this?

Tarjan: g++ Tarjan.cpp -Wall -o Tarjan

run_test1: ./Tarjan input/input_test1.in output/output_test1.out

run_test2: ./Tarjan input/input_test2.in output/output_test2.out

run_test3: ./Tarjan input/input_test3.in output/output_test3.out

run_test4: ./Tarjan input/input_test4.in output/output_test4.out

run_test5: ./Tarjan input/input_test5.in output/output_test5.out

run_test6: ./Tarjan input/input_test6.in output/output_test6.out

run_test7: ./Tarjan input/input_test7.in output/output_test7.out

run_test8: ./Tarjan input/input_test8.in output/output_test8.out

run_test9: ./Tarjan input/input_test9.in output/output_test9.out

run_test10: ./Tarjan input/input_test10.in output/output_test10.out

clean: rm Tarjan rm output/output*.out

Upvotes: 0

Views: 1219

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106236

Keeping it as simple as possible, sounds like you'll need a Makefile similar to:

my_app: <tab> my_app.cpp
  <tab> g++ -Wall -g -o my_app my_app.cpp
  <tab> ./my_app < my_test_input

Then you can create a file called my_test_input with whatever your test input is, and read it from the standard input of the ./my_app process:

int an_int;
std::string a_space_separated_word;

if (std::cin >> an_int >> a_space_separated_word)
    // do something with the inputs...
else {
    std::cerr << "failed to read inputs\n";
    exit(EXIT_FAILURE);
}

std::string line;
while (getline(std::cin, line))
{
    ...get all the lines in the input into line, one by one...
}

Upvotes: 1

Related Questions