Skip to content

Commit 2e6e114

Browse files
committed
added a unit test
1 parent 77dff91 commit 2e6e114

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

llvm/unittests/ADT/FunctionExtrasTest.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,23 @@ class Incomplete {};
310310
Incomplete incompleteFunction() { return {}; }
311311
const Incomplete incompleteFunctionConst() { return {}; }
312312

313+
// Check that we can store a pointer-sized payload inline in the unique_function.
314+
TEST(UniqueFunctionTest, InlineStorageWorks) {
315+
// We do assume a couple of implementation details of the unique_function here:
316+
// - It can store certain small-enough payload inline
317+
// - Inline storage size is at least >= sizeof(void*)
318+
void *ptr;
319+
unique_function<void(void *)> UniqueFunctionWithInlineStorage{
320+
[ptr](void *self) {
321+
auto mid = reinterpret_cast<uintptr_t>(&ptr);
322+
auto beg = reinterpret_cast<uintptr_t>(self);
323+
auto end = reinterpret_cast<uintptr_t>(self) +
324+
sizeof(unique_function<void(void *)>);
325+
// Make sure the address of the captured pointer lies somewhere within
326+
// the unique_function object.
327+
EXPECT_TRUE(mid >= beg && mid < end);
328+
}};
329+
UniqueFunctionWithInlineStorage(&UniqueFunctionWithInlineStorage);
330+
}
331+
313332
} // anonymous namespace

0 commit comments

Comments
 (0)