Skip to content

Commit

Permalink
Merge pull request #12 from cmazakas/fix/obj-tests
Browse files Browse the repository at this point in the history
fix function_ref Callable constructor
  • Loading branch information
pdimov authored Jun 21, 2024
2 parents 6a0f933 + 2a5e803 commit 915a207
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/boost/compat/function_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct function_ref_base {
template <class F>
function_ref_base(obj_tag, F&& fn) noexcept
: thunk_{}, invoke_(&invoke_object_holder<Const, NoEx, F, R, Args...>::invoke_object) {
thunk_.pobj_ = static_cast<void*>(std::addressof(fn));
thunk_.pobj_ = const_cast<void*>(static_cast<void const*>(std::addressof(fn)));
}

function_ref_base(const function_ref_base&) noexcept = default;
Expand Down
47 changes: 47 additions & 0 deletions test/function_ref_obj_noexcept_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,36 @@ int main() {

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&&>));

compat::function_ref<S2> fv2(f1);

BOOST_TEST_EQ(fv2(1), 1);

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&&>));

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const&&>));

compat::function_ref<S4> fv4(f1);

BOOST_TEST_EQ(fv4(1, 2, 3), 123);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&&>));
}
{
using S1 = int() const noexcept;
Expand All @@ -95,25 +106,57 @@ int main() {

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&&>));

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&&>));

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S3>, F1 const&&>));

compat::function_ref<S4> fv4(f1);

BOOST_TEST_EQ(fv4(1, 2, 3), 123);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&&>));
}

{
using S2 = int(int) noexcept;
using S4 = int(int, int, int) noexcept;

auto& fref = f1;

compat::function_ref<S2> fv2(fref);
BOOST_TEST_EQ(fv2(1), 1);

auto const& cfref = f1;
compat::function_ref<S4> fv4(cfref);
BOOST_TEST_EQ(fv4(1, 2, 3), 123);
}

{
using S4 = int(int, int, int) const noexcept;

auto const& cfref = f1;
compat::function_ref<S4> fv4(cfref);
BOOST_TEST_EQ(fv4(1, 2, 3), 123);
}
}

Expand All @@ -123,13 +166,17 @@ int main() {

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F2 const&&>));

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F2 const&&>));
}

// invoke_r
Expand Down
77 changes: 77 additions & 0 deletions test/function_ref_obj_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,39 @@ int main() {
compat::function_ref<S1> fv1(f);

BOOST_TEST_EQ(fv1(), -1);

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&&>));

compat::function_ref<S2> fv2(f);

BOOST_TEST_EQ(fv2(1), 1);

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&>));

compat::function_ref<S3> fv3(f);

BOOST_TEST_EQ(fv3(1, 2), 12);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const&&>));

compat::function_ref<S4> fv4(f);

BOOST_TEST_EQ(fv4(1, 2, 3), 123);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&&>));
}

{
Expand All @@ -93,29 +102,89 @@ int main() {

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S1>, F1 const&&>));

BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1&&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&>));
BOOST_TEST_TRAIT_FALSE((std::is_constructible<compat::function_ref<S2>, F1 const&&>));

compat::function_ref<S3> fv3(f);

BOOST_TEST_EQ(fv3(1, 2), 12);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S3>, F1 const&&>));

compat::function_ref<S4> fv4(f);

BOOST_TEST_EQ(fv4(1, 2, 3), 123);

BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1&&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&>));
BOOST_TEST_TRAIT_TRUE((std::is_constructible<compat::function_ref<S4>, F1 const&&>));
}

{
using S1 = int();
using S2 = int(int);

auto& fref = f;

compat::function_ref<S1> fv1(fref);
BOOST_TEST_EQ(fv1(), -1);

compat::function_ref<S2> fv2(fref);
BOOST_TEST_EQ(fv2(1), 1);
}

{
using S1 = int();
using S2 = int(int);

compat::function_ref<S1> fv1(std::move(f));
BOOST_TEST_EQ(fv1(), -1);

compat::function_ref<S2> fv2(std::move(f));
BOOST_TEST_EQ(fv2(1), 1);
}

{
using S3 = int(int, int) const;
using S4 = int(int, int, int) const;

auto const& fref = f;

compat::function_ref<S3> fv3(fref);
BOOST_TEST_EQ(fv3(1, 2), 12);

compat::function_ref<S4> fv4(fref);
BOOST_TEST_EQ(fv4(1, 2, 3), 123);
}

{
using S3 = int(int, int) const;
using S4 = int(int, int, int) const;

auto const&& fref = std::move(f);

compat::function_ref<S3> fv3(fref);
BOOST_TEST_EQ(fv3(1, 2), 12);

compat::function_ref<S4> fv4(fref);
BOOST_TEST_EQ(fv4(1, 2, 3), 123);
}
}

Expand All @@ -125,12 +194,20 @@ int main() {
compat::function_ref<int(int, int)> fv1(g);
BOOST_TEST_EQ(fv1(3, 2), 321);

auto& gref = g;
compat::function_ref<int(int, int)> rfv1(gref);
BOOST_TEST_EQ(rfv1(3, 2), 321);

compat::function_ref<int(int, int)> fv2(std::move(g));
BOOST_TEST_EQ(fv2(3, 2), 321);

compat::function_ref<int(int, int) const> fv3(g);
BOOST_TEST_EQ(fv3(3, 2), 322);

auto const& gcref = g;
compat::function_ref<int(int, int) const> crfv3(gcref);
BOOST_TEST_EQ(fv3(3, 2), 322);

compat::function_ref<int(int, int) const> fv4(std::move(g));
BOOST_TEST_EQ(fv4(3, 2), 322);
}
Expand Down

0 comments on commit 915a207

Please sign in to comment.