c++boostboost.test

Boost.Test - How to check two vectors on intersection?


I'm looking for the best way to check two vectors for intersections.

A nested loop like

for (auto const & first: first_vector)
  for (auto const & next: next_vector)
    if first == next
      return false;

could do the job but it does not look like a Boost.Test-ish way. Neither a look at Boost.Test helps a lot as the only test case predefined is BOOST_CHECK_EQUAL_COLLECTIONS

Update

BOOST_FIXTURE_TEST_CASE(paths,fixture_t)
{
  for(int i=0,j=vids.size(); i!=j; i++)
  {
    for(int p=0,q=vids.size(); p!=q; p++)
    {
      if (i == p)
      {
        continue;
      }
      if (i < p)
      {
        BOOST_TEST_CONTEXT("Equal match at positions " << i << " and " << p)

        BOOST_TEST(vids[i] != vids[p]);
      }
    }
  }
}

Solution

  • In unit tests you usually don't care about performance of validating code as long as it is reasonable.

    I would just write a template function sets_intersect taking two vectors, inserting one of them into std::hash_set and traversing the second vector until the first match. Or just sorting them both and checking with std::set_intersection.

    The final syntax will be something like BOOST_CHECK(sets_intersect(vec1, vec2)), which looks human friendly enough.