2

I want to add a unittest using gtest to test if my code can generate a file that are same with the reference file. Does gtest has a function to take two files and compare them?

Jonas Stein
  • 5,932
  • 5
  • 35
  • 67
superd
  • 135
  • 10

1 Answers1

3

Does gtest has a function to take two files and compare them?

No, there's no such function in gtest.

You can read your generated file into a std::string, and compare that one against one you declare in your testcase:

 std::ifstream t("generated_file.txt");
 std::string genfile((std::istreambuf_iterator<char>(t)),
                      std::istreambuf_iterator<char>());
 std::string expectedOutput = R"xxx(Expected
 output
 goes
 here
 verbatim
 )xxx";

 ASSERT_EQUAL(expectedOutput,genfile);
  • You might use this way: https://stackoverflow.com/a/2602258/1463922 - I mean for big files your way might be overkill – PiotrNycz Feb 05 '18 at 16:20