Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility::metrics and simple tests (#214)
- Loading branch information
Showing
4 changed files
with
266 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2023 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#include <chrono> | ||
#include <utility> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <cpp-sort/sorter_facade.h> | ||
#include <cpp-sort/utility/adapter_storage.h> | ||
#include <cpp-sort/utility/metrics_tools.h> | ||
|
||
namespace | ||
{ | ||
struct foo_tag {}; | ||
|
||
template<typename Sorter> | ||
struct foo_metric_impl | ||
{ | ||
using tag_t = foo_tag; | ||
using metric_t = cppsort::utility::metric<int, tag_t>; | ||
|
||
foo_metric_impl() = default; | ||
|
||
constexpr explicit foo_metric_impl(Sorter&& sorter): | ||
cppsort::utility::adapter_storage<Sorter>(std::move(sorter)) | ||
{} | ||
|
||
template<typename ForwardIterator> | ||
auto operator()(ForwardIterator first, ForwardIterator last) const | ||
-> metric_t | ||
{ | ||
return metric_t(42); | ||
} | ||
}; | ||
|
||
template<typename Sorter> | ||
struct foo_metric: | ||
cppsort::sorter_facade<foo_metric_impl<Sorter>> | ||
{ | ||
foo_metric() = default; | ||
|
||
constexpr explicit foo_metric(Sorter sorter): | ||
cppsort::sorter_facade<foo_metric_impl<Sorter>>(std::move(sorter)) | ||
{} | ||
}; | ||
|
||
struct bar_tag {}; | ||
|
||
template<typename Sorter> | ||
struct bar_metric_impl | ||
{ | ||
using tag_t = foo_tag; | ||
using metric_t = cppsort::utility::metric<double, tag_t>; | ||
|
||
bar_metric_impl() = default; | ||
|
||
constexpr explicit bar_metric_impl(Sorter&& sorter): | ||
cppsort::utility::adapter_storage<Sorter>(std::move(sorter)) | ||
{} | ||
|
||
template<typename ForwardIterator> | ||
auto operator()(ForwardIterator first, ForwardIterator last) const | ||
-> metric_t | ||
{ | ||
return metric_t(145.0); | ||
} | ||
}; | ||
|
||
template<typename Sorter> | ||
struct bar_metric: | ||
cppsort::sorter_facade<bar_metric_impl<Sorter>> | ||
{ | ||
bar_metric() = default; | ||
|
||
constexpr explicit bar_metric(Sorter sorter): | ||
cppsort::sorter_facade<bar_metric_impl<Sorter>>(std::move(sorter)) | ||
{} | ||
}; | ||
} | ||
|
||
TEST_CASE("simple utility::metric tests", "[utility][metrics]") | ||
{ | ||
using namespace cppsort::utility; | ||
using namespace std::chrono_literals; | ||
|
||
struct time_tag {}; | ||
|
||
constexpr metric<std::chrono::seconds, time_tag> m1(5s); | ||
constexpr metric<std::chrono::milliseconds, time_tag> m2(5000ms); | ||
constexpr metric<std::chrono::milliseconds, time_tag> m3(4000ms); | ||
STATIC_CHECK( m1 == m2 ); | ||
STATIC_CHECK( m3 != m2 ); | ||
STATIC_CHECK( m3 < m1 ); | ||
} | ||
|
||
TEST_CASE("simple utility::metrics tests", "[utility][metrics]") | ||
{ | ||
using std::get; | ||
using namespace cppsort::utility; | ||
|
||
using metric1 = metric<int, foo_tag>; | ||
using metric2 = metric<float, bar_tag>; | ||
|
||
metrics<metric1, metric2> mm(metric1(42), metric2(140.0)); | ||
auto& m1 = get<foo_tag>(mm); | ||
CHECK( m1 == 42 ); | ||
auto m2 = get<bar_tag>(mm); | ||
CHECK( m2 == 140.0 ); | ||
|
||
get<foo_tag>(mm) = 35; | ||
CHECK( m1 == 35 ); | ||
} |