/// \file ZipMapIterator.cpp /// \brief Tests for ZipMapIterator // // This file is distributed under the MIT License. See LICENSE.md for details. // // Standard includes #include #include #include // Boost includes #define BOOST_TEST_MODULE ZipMapIterator bool init_unit_test(); #include // Local libraries includes #include "revng/ADT/SmallMap.h" #include "revng/ADT/ZipMapIterator.h" #include "revng/UnitTestHelpers/UnitTestHelpers.h" using namespace llvm; template static void compare(T &Left, T &Right, std::vector, Optional>> &&Expected) { using KE = KeyContainer; using pointer = typename KE::pointer; std::vector> Result; std::copy(zipmap_begin(Left, Right), zipmap_end(Left, Right), std::back_inserter(Result)); revng_assert(Result.size() == Expected.size()); auto FindLeft = [&Expected, &Left](unsigned I) { return Expected[I].first ? KE::find(Left, *Expected[I].first) : nullptr; }; auto FindRight = [&Expected, &Right](unsigned I) { return Expected[I].second ? KE::find(Right, *Expected[I].second) : nullptr; }; for (unsigned I = 0; I < Result.size(); I++) { revng_assert(Result[I] == std::make_pair(FindLeft(I), FindRight(I))); } } template void run() { Map A, B; const Map &ARef = A; const Map &BRef = B; using KC = KeyContainer; KC::insert(A, 1); KC::insert(A, 2); KC::insert(A, 4); KC::insert(A, 5); KC::sort(A); KC::insert(B, 1); KC::insert(B, 3); KC::insert(B, 4); KC::insert(B, 7); KC::sort(B); compare(ARef, BRef, { { { 1 }, { 1 } }, { { 2 }, {} }, { {}, { 3 } }, { { 4 }, { 4 } }, { { 5 }, {} }, { {}, { 7 } }, }); KC::insert(A, 0); KC::sort(A); compare(A, B, { { { 0 }, {} }, { { 1 }, { 1 } }, { { 2 }, {} }, { {}, { 3 } }, { { 4 }, { 4 } }, { { 5 }, {} }, { {}, { 7 } }, }); KC::insert(B, -1); KC::sort(B); compare(A, B, { { {}, { -1 } }, { { 0 }, {} }, { { 1 }, { 1 } }, { { 2 }, {} }, { {}, { 3 } }, { { 4 }, { 4 } }, { { 5 }, {} }, { {}, { 7 } }, }); } BOOST_AUTO_TEST_CASE(TestStdMap) { run>(); } BOOST_AUTO_TEST_CASE(TestStdSet) { run>(); } BOOST_AUTO_TEST_CASE(TestStdVectorPair) { run>>(); } BOOST_AUTO_TEST_CASE(TestSmallMap) { run>(); }