diff options
Diffstat (limited to 'llvm/unittests/ADT/ilistTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/ilistTest.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/ilistTest.cpp b/llvm/unittests/ADT/ilistTest.cpp index c2ad3d0dce3..baf7c7907cb 100644 --- a/llvm/unittests/ADT/ilistTest.cpp +++ b/llvm/unittests/ADT/ilistTest.cpp @@ -149,4 +149,81 @@ TEST(ilistTest, HasGetNextTrait) { "Empty does not have a getNext(Node*)"); } +struct CreateSentinel { + Node *createSentinel(); +}; +TEST(ilistTest, HasCreateSentinelTrait) { + static_assert(ilist_detail::HasCreateSentinel<CreateSentinel>::value, + "CreateSentinel has a getNext(Node*)"); + static_assert( + ilist_detail::HasObsoleteCustomization<CreateSentinel, Node>::value, + "Empty should be obsolete because of createSentinel()"); + + // Negative test for HasCreateSentinel. + static_assert(!ilist_detail::HasCreateSentinel<Empty>::value, + "Empty does not have a createSentinel()"); +} + +struct NodeWithCallback : ilist_node<NodeWithCallback> { + int Value = 0; + bool IsInList = false; + + NodeWithCallback() = default; + NodeWithCallback(int Value) : Value(Value) {} + NodeWithCallback(const NodeWithCallback &) = delete; +}; + +} // end namespace + +namespace llvm { +template <> +struct ilist_traits<NodeWithCallback> + : public ilist_node_traits<NodeWithCallback> { + void addNodeToList(NodeWithCallback *N) { N->IsInList = true; } + void removeNodeFromList(NodeWithCallback *N) { N->IsInList = false; } +}; +} // end namespace llvm + +namespace { + +TEST(ilistTest, addNodeToList) { + ilist<NodeWithCallback> L; + NodeWithCallback N(7); + ASSERT_FALSE(N.IsInList); + + L.insert(L.begin(), &N); + ASSERT_EQ(1u, L.size()); + ASSERT_EQ(&N, &*L.begin()); + ASSERT_TRUE(N.IsInList); + + L.remove(&N); + ASSERT_EQ(0u, L.size()); + ASSERT_FALSE(N.IsInList); +} + +struct PrivateNode : private ilist_node<PrivateNode> { + friend struct llvm::ilist_node_access; + + int Value = 0; + + PrivateNode() = default; + PrivateNode(int Value) : Value(Value) {} + PrivateNode(const PrivateNode &) = delete; +}; + +TEST(ilistTest, privateNode) { + // Instantiate various APIs to be sure they're callable when ilist_node is + // inherited privately. + ilist<NodeWithCallback> L; + NodeWithCallback N(7); + L.insert(L.begin(), &N); + ++L.begin(); + (void)*L.begin(); + (void)(L.begin() == L.end()); + + ilist<NodeWithCallback> L2; + L2.splice(L2.end(), L); + L2.remove(&N); +} + } // end namespace |