Skip to content

Commit fbad5e0

Browse files
committed
add test for in-place mutation of SBO-stored callables
1 parent 7ce75ac commit fbad5e0

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

test/move_only_function_test.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#include <boost/core/lightweight_test.hpp>
1414
#include <boost/core/lightweight_test_trait.hpp>
1515

16-
#include <functional>
16+
#include <array>
17+
#include <cstdint>
1718
#include <memory>
1819
#include <type_traits>
1920

@@ -845,11 +846,39 @@ static void test_conv()
845846
}
846847
}
847848

849+
static void test_mutable_lambda()
850+
{
851+
{
852+
// Within SBO limits.
853+
int captured = 0;
854+
move_only_function<int()> func = [captured]() mutable { return ++captured; };
855+
856+
BOOST_TEST_EQ( func(), 1 );
857+
BOOST_TEST_EQ( func(), 2 );
858+
859+
move_only_function<int()> func2(std::move(func));
860+
BOOST_TEST_EQ( func2(), 3 );
861+
}
862+
863+
{
864+
// Too large for SBO.
865+
std::array<std::uint8_t, 256> captured = {{}};
866+
move_only_function<int()> func = [captured]() mutable { return ++captured[0]; };
867+
868+
BOOST_TEST_EQ( func(), 1 );
869+
BOOST_TEST_EQ( func(), 2 );
870+
871+
move_only_function<int()> func2(std::move(func));
872+
BOOST_TEST_EQ( func2(), 3 );
873+
}
874+
}
875+
848876
int main()
849877
{
850878
test_call();
851879
test_traits();
852880
test_conv();
881+
test_mutable_lambda();
853882

854883
return boost::report_errors();
855884
}

0 commit comments

Comments
 (0)