2016-08-23 06:21:07 +08:00
|
|
|
//===- unittests/ADT/IListBaseTest.cpp - ilist_base unit tests ------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-23 06:21:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-08-30 09:37:58 +08:00
|
|
|
#include "llvm/ADT/ilist_base.h"
|
2016-08-23 06:21:07 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-09-12 00:20:53 +08:00
|
|
|
// Test fixture.
|
|
|
|
template <typename T> class IListBaseTest : public ::testing::Test {};
|
|
|
|
|
|
|
|
// Test variants with the same test.
|
|
|
|
typedef ::testing::Types<ilist_base<false>, ilist_base<true>>
|
|
|
|
IListBaseTestTypes;
|
|
|
|
TYPED_TEST_CASE(IListBaseTest, IListBaseTestTypes);
|
|
|
|
|
|
|
|
TYPED_TEST(IListBaseTest, insertBeforeImpl) {
|
|
|
|
typedef TypeParam list_base_type;
|
|
|
|
typedef typename list_base_type::node_base_type node_base_type;
|
2016-09-11 00:28:52 +08:00
|
|
|
|
|
|
|
node_base_type S, A, B;
|
2016-09-12 00:20:53 +08:00
|
|
|
|
2016-08-23 06:21:07 +08:00
|
|
|
// [S] <-> [S]
|
|
|
|
S.setPrev(&S);
|
|
|
|
S.setNext(&S);
|
|
|
|
|
|
|
|
// [S] <-> A <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S, A);
|
2016-08-23 06:21:07 +08:00
|
|
|
EXPECT_EQ(&A, S.getPrev());
|
|
|
|
EXPECT_EQ(&S, A.getPrev());
|
|
|
|
EXPECT_EQ(&A, S.getNext());
|
|
|
|
EXPECT_EQ(&S, A.getNext());
|
|
|
|
|
|
|
|
// [S] <-> A <-> B <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S, B);
|
2016-08-23 06:21:07 +08:00
|
|
|
EXPECT_EQ(&B, S.getPrev());
|
|
|
|
EXPECT_EQ(&A, B.getPrev());
|
|
|
|
EXPECT_EQ(&S, A.getPrev());
|
|
|
|
EXPECT_EQ(&A, S.getNext());
|
|
|
|
EXPECT_EQ(&B, A.getNext());
|
|
|
|
EXPECT_EQ(&S, B.getNext());
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:20:53 +08:00
|
|
|
TYPED_TEST(IListBaseTest, removeImpl) {
|
|
|
|
typedef TypeParam list_base_type;
|
|
|
|
typedef typename list_base_type::node_base_type node_base_type;
|
|
|
|
|
2016-09-11 00:28:52 +08:00
|
|
|
node_base_type S, A, B;
|
2016-08-23 06:21:07 +08:00
|
|
|
|
|
|
|
// [S] <-> A <-> B <-> [S]
|
|
|
|
S.setPrev(&S);
|
|
|
|
S.setNext(&S);
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S, A);
|
|
|
|
list_base_type::insertBeforeImpl(S, B);
|
2016-08-23 06:21:07 +08:00
|
|
|
|
|
|
|
// [S] <-> B <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::removeImpl(A);
|
2016-08-23 06:21:07 +08:00
|
|
|
EXPECT_EQ(&B, S.getPrev());
|
|
|
|
EXPECT_EQ(&S, B.getPrev());
|
|
|
|
EXPECT_EQ(&B, S.getNext());
|
|
|
|
EXPECT_EQ(&S, B.getNext());
|
|
|
|
EXPECT_EQ(nullptr, A.getPrev());
|
|
|
|
EXPECT_EQ(nullptr, A.getNext());
|
|
|
|
|
|
|
|
// [S] <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::removeImpl(B);
|
2016-08-23 06:21:07 +08:00
|
|
|
EXPECT_EQ(&S, S.getPrev());
|
|
|
|
EXPECT_EQ(&S, S.getNext());
|
|
|
|
EXPECT_EQ(nullptr, B.getPrev());
|
|
|
|
EXPECT_EQ(nullptr, B.getNext());
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:20:53 +08:00
|
|
|
TYPED_TEST(IListBaseTest, removeRangeImpl) {
|
|
|
|
typedef TypeParam list_base_type;
|
|
|
|
typedef typename list_base_type::node_base_type node_base_type;
|
|
|
|
|
2016-09-11 00:28:52 +08:00
|
|
|
node_base_type S, A, B, C, D;
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
|
|
|
|
// [S] <-> A <-> B <-> C <-> D <-> [S]
|
|
|
|
S.setPrev(&S);
|
|
|
|
S.setNext(&S);
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S, A);
|
|
|
|
list_base_type::insertBeforeImpl(S, B);
|
|
|
|
list_base_type::insertBeforeImpl(S, C);
|
|
|
|
list_base_type::insertBeforeImpl(S, D);
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
|
|
|
|
// [S] <-> A <-> D <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::removeRangeImpl(B, D);
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
EXPECT_EQ(&D, S.getPrev());
|
|
|
|
EXPECT_EQ(&A, D.getPrev());
|
|
|
|
EXPECT_EQ(&S, A.getPrev());
|
|
|
|
EXPECT_EQ(&A, S.getNext());
|
|
|
|
EXPECT_EQ(&D, A.getNext());
|
|
|
|
EXPECT_EQ(&S, D.getNext());
|
|
|
|
EXPECT_EQ(nullptr, B.getPrev());
|
|
|
|
EXPECT_EQ(nullptr, C.getNext());
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:20:53 +08:00
|
|
|
TYPED_TEST(IListBaseTest, removeRangeImplAllButSentinel) {
|
|
|
|
typedef TypeParam list_base_type;
|
|
|
|
typedef typename list_base_type::node_base_type node_base_type;
|
|
|
|
|
2016-09-11 00:28:52 +08:00
|
|
|
node_base_type S, A, B;
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
|
|
|
|
// [S] <-> A <-> B <-> [S]
|
|
|
|
S.setPrev(&S);
|
|
|
|
S.setNext(&S);
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S, A);
|
|
|
|
list_base_type::insertBeforeImpl(S, B);
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
|
|
|
|
// [S] <-> [S]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::removeRangeImpl(A, S);
|
ADT: Split out simple_ilist, a simple intrusive list
Split out a new, low-level intrusive list type with clear semantics.
Unlike iplist (and ilist), all operations on simple_ilist are intrusive,
and simple_ilist never takes ownership of its nodes. This enables an
intuitive API that has the right defaults for intrusive lists.
- insert() takes references (not pointers!) to nodes (in iplist/ilist,
passing a reference will cause the node to be copied).
- erase() takes only iterators (like std::list), and does not destroy
the nodes.
- remove() takes only references and has the same behaviour as erase().
- clear() does not destroy the nodes.
- The destructor does not destroy the nodes.
- New API {erase,remove,clear}AndDispose() take an extra Disposer
functor for callsites that want to call some disposal routine (e.g.,
std::default_delete).
This list is not currently configurable, and has no callbacks.
The initial motivation was to fix iplist<>::sort to work correctly (even
with callbacks in ilist_traits<>). iplist<> uses simple_ilist<>::sort
directly. The new test in unittests/IR/ModuleTest.cpp crashes without
this commit.
Fixing sort() via a low-level layer provided a good opportunity to:
- Unit test the low-level functionality thoroughly.
- Modernize the API, largely inspired by other intrusive list
implementations.
Here's a sketch of a longer-term plan:
- Create BumpPtrList<>, a non-intrusive list implemented using
simple_ilist<>, and use it for the Token list in
lib/Support/YAMLParser.cpp. This will factor out the only real use of
createNode().
- Evolve the iplist<> and ilist<> APIs in the direction of
simple_ilist<>, making allocation/deallocation explicit at call sites
(similar to simple_ilist<>::eraseAndDispose()).
- Factor out remaining calls to createNode() and deleteNode() and remove
the customization from ilist_traits<>.
- Transition uses of iplist<>/ilist<> that don't need callbacks over to
simple_ilist<>.
llvm-svn: 280107
2016-08-31 00:23:55 +08:00
|
|
|
EXPECT_EQ(&S, S.getPrev());
|
|
|
|
EXPECT_EQ(&S, S.getNext());
|
|
|
|
EXPECT_EQ(nullptr, A.getPrev());
|
|
|
|
EXPECT_EQ(nullptr, B.getNext());
|
2016-08-23 06:21:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:20:53 +08:00
|
|
|
TYPED_TEST(IListBaseTest, transferBeforeImpl) {
|
|
|
|
typedef TypeParam list_base_type;
|
|
|
|
typedef typename list_base_type::node_base_type node_base_type;
|
|
|
|
|
2016-09-11 00:28:52 +08:00
|
|
|
node_base_type S1, S2, A, B, C, D, E;
|
2016-08-23 06:21:07 +08:00
|
|
|
|
|
|
|
// [S1] <-> A <-> B <-> C <-> [S1]
|
|
|
|
S1.setPrev(&S1);
|
|
|
|
S1.setNext(&S1);
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S1, A);
|
|
|
|
list_base_type::insertBeforeImpl(S1, B);
|
|
|
|
list_base_type::insertBeforeImpl(S1, C);
|
2016-08-23 06:21:07 +08:00
|
|
|
|
|
|
|
// [S2] <-> D <-> E <-> [S2]
|
|
|
|
S2.setPrev(&S2);
|
|
|
|
S2.setNext(&S2);
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::insertBeforeImpl(S2, D);
|
|
|
|
list_base_type::insertBeforeImpl(S2, E);
|
2016-08-23 06:21:07 +08:00
|
|
|
|
|
|
|
// [S1] <-> C <-> [S1]
|
2016-09-11 00:28:52 +08:00
|
|
|
list_base_type::transferBeforeImpl(D, A, C);
|
2016-08-23 06:21:07 +08:00
|
|
|
EXPECT_EQ(&C, S1.getPrev());
|
|
|
|
EXPECT_EQ(&S1, C.getPrev());
|
|
|
|
EXPECT_EQ(&C, S1.getNext());
|
|
|
|
EXPECT_EQ(&S1, C.getNext());
|
|
|
|
|
|
|
|
// [S2] <-> A <-> B <-> D <-> E <-> [S2]
|
|
|
|
EXPECT_EQ(&E, S2.getPrev());
|
|
|
|
EXPECT_EQ(&D, E.getPrev());
|
|
|
|
EXPECT_EQ(&B, D.getPrev());
|
|
|
|
EXPECT_EQ(&A, B.getPrev());
|
|
|
|
EXPECT_EQ(&S2, A.getPrev());
|
|
|
|
EXPECT_EQ(&A, S2.getNext());
|
|
|
|
EXPECT_EQ(&B, A.getNext());
|
|
|
|
EXPECT_EQ(&D, B.getNext());
|
|
|
|
EXPECT_EQ(&E, D.getNext());
|
|
|
|
EXPECT_EQ(&S2, E.getNext());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|