Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Added tests for STL utils #119

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion include/fiction/utils/stl_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#define FICTION_STL_UTILS_HPP

#include <functional>
#include <iterator>
#include <queue>
#include <type_traits>
#include <vector>

namespace fiction
Expand All @@ -29,11 +31,19 @@ namespace fiction
* @param last End of the range to examine.
* @param s_first Begin of the range to search for.
* @param s_last End of the range to search for.
* @return Iterator to the first position of the first shared 2-element sub-sequence shared between the two ranges.
* @return Iterator in the range `[first, last)` to the first position of the first 2-element sub-sequence shared
* between the two ranges, or `last` if no such shared sub-sequence exists.
*/
template <class InputIt, class ForwardIt>
InputIt find_first_two_of(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_last) noexcept
{
static_assert(
std::is_base_of<std::input_iterator_tag, typename std::iterator_traits<InputIt>::iterator_category>::value,
"InputIt must meet the requirements of LegacyInputIterator");
static_assert(
std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<ForwardIt>::iterator_category>::value,
"ForwardIt must meet the requirements of LegacyForwardIterator");

for (; first != last - 1; ++first)
{
for (ForwardIt it = s_first; it != s_last - 1; ++it)
Expand Down
78 changes: 78 additions & 0 deletions test/utils/stl_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Created by marcel on 07.02.23.
//

#include <catch2/catch_test_macros.hpp>

#include <fiction/algorithms/graph/generate_edge_intersection_graph.hpp>
#include <fiction/types.hpp>
#include <fiction/utils/routing_utils.hpp>
#include <fiction/utils/stl_utils.hpp>

#include <array>
#include <iterator>
#include <vector>

using namespace fiction;

TEST_CASE("Test find_first_two_of with vector input", "[find_first_two_of]")
{
static const std::vector<int> v1{0, 1, 1, 2, 3, 3};
static const std::vector<int> v2{1, 2, 3, 3};

auto it = find_first_two_of(v1.begin(), v1.end(), v2.begin(), v2.end());
CHECK(*it == 1);
CHECK(*(it + 1) == 2);

static const std::vector<int> v3{1, 2, 4, 3};
it = find_first_two_of(v1.begin(), v1.end(), v3.begin(), v3.end());
CHECK(it == std::next(v1.begin(), 2));
}

TEST_CASE("Test find_first_two_of with array input", "[find_first_two_of]")
{
static constexpr const std::array a1{0, 1, 1, 2, 3, 3};
static constexpr const std::array a2{1, 2, 3, 3};

auto it = find_first_two_of(std::begin(a1), std::end(a1), std::begin(a2), std::end(a2));
marcelwa marked this conversation as resolved.
Show resolved Hide resolved
CHECK(*it == 1);
CHECK(*(std::next(it, 1)) == 2);

static constexpr const std::array a3{1, 2, 4, 3};
it = find_first_two_of(std::begin(a1), std::end(a1), std::begin(a3), std::end(a3));
CHECK(it == std::next(std::begin(a1), 2));
}

TEST_CASE("Test find_first_two_of with different iterator types", "[find_first_two_of]")
{
static const std::vector<int> v1{0, 1, 1, 2, 3, 3};
static constexpr const std::array a2{1, 2, 3, 3};

auto it = find_first_two_of(v1.begin(), v1.end(), std::begin(a2), std::end(a2));
CHECK(*it == 1);
CHECK(*(it + 1) == 2);

static constexpr const std::array a3{1, 2, 4, 3};
it = find_first_two_of(v1.begin(), v1.end(), std::begin(a3), std::end(a3));
CHECK(it == std::next(v1.begin(), 2));
}

TEST_CASE("Test find_first_two_of with layout_coordinate_paths", "[first_first_two_of]")
{
static const layout_coordinate_path<cart_gate_clk_lyt> p1{{1, 1}, {2, 1}};
static const layout_coordinate_path<cart_gate_clk_lyt> p2{{0, 1}, {1, 1}, {2, 1}};

// regular iterators
const auto it1 = find_first_two_of(p1.begin(), p1.end(), p2.begin(), p2.end());
const auto it2 = find_first_two_of(p2.begin(), p2.end(), p1.begin(), p1.end());

CHECK(it1 == p1.begin());
CHECK(it2 == std::next(p2.begin(), 1));

// const iterators via std::cbegin and std::cend
const auto it3 = find_first_two_of(std::cbegin(p1), std::cend(p1), std::cbegin(p2), std::cend(p2));
const auto it4 = find_first_two_of(std::cbegin(p2), std::cend(p2), std::cbegin(p1), std::cend(p1));

CHECK(it3 == p1.begin());
CHECK(it4 == std::next(p2.begin(), 1));
}