Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare files with Boost.Test?

I've two files that I need to compare. I would use something like this

BOOST_REQUIRE_EQUAL(filename1, filename2);
like image 819
Mhh Lecker Avatar asked Feb 22 '13 10:02

Mhh Lecker


People also ask

How can I compare two C files?

Step 1: Open both the file with pointer at the starting. Step 2: Fetch data from file as characters one by one. Step 3: Compare the characters. If the characters are different then return the line and position of the error character.

What is boost testing?

Boost unit testing framework (Boost. Test) is a part of the Boost library. It is a fully-functional and scalable framework, with wide range of assertion macros, XML output, and other features. Boost. Test itself lacks mocking functionality, but it can be combined with stand-alone mocking frameworks such as gmock.


2 Answers

You can use BOOST_CHECK_EQUAL_COLLECTIONS to compare file contents.

Code sample:

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

#include <fstream>
#include <iterator>

BOOST_AUTO_TEST_CASE( test )
{
    std::ifstream ifs1("data1.txt");
    std::ifstream ifs2("data2.txt");

    std::istream_iterator<char> b1(ifs1), e1;
    std::istream_iterator<char> b2(ifs2), e2;

    BOOST_CHECK_EQUAL_COLLECTIONS(b1, e1, b2, e2);
}
like image 132
Grigorii Chudnov Avatar answered Sep 27 '22 17:09

Grigorii Chudnov


Files? Read both and compare contents.

like image 27
ForEveR Avatar answered Sep 27 '22 16:09

ForEveR