llvm/unittests/ADT/IListBaseTest.cpp
Duncan P. N. Exon Smith f1f27bb03e 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<>.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280107 91177308-0d34-0410-b5e6-96231b3b80d8
2016-08-30 16:23:55 +00:00

143 lines
3.5 KiB
C++

//===- unittests/ADT/IListBaseTest.cpp - ilist_base unit tests ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/ilist_base.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(IListBaseTest, insertBeforeImpl) {
ilist_node_base S, A, B;
// [S] <-> [S]
S.setPrev(&S);
S.setNext(&S);
// [S] <-> A <-> [S]
ilist_base::insertBeforeImpl(S, A);
EXPECT_EQ(&A, S.getPrev());
EXPECT_EQ(&S, A.getPrev());
EXPECT_EQ(&A, S.getNext());
EXPECT_EQ(&S, A.getNext());
// [S] <-> A <-> B <-> [S]
ilist_base::insertBeforeImpl(S, B);
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());
}
TEST(IListBaseTest, removeImpl) {
ilist_node_base S, A, B;
// [S] <-> A <-> B <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
// [S] <-> B <-> [S]
ilist_base::removeImpl(A);
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]
ilist_base::removeImpl(B);
EXPECT_EQ(&S, S.getPrev());
EXPECT_EQ(&S, S.getNext());
EXPECT_EQ(nullptr, B.getPrev());
EXPECT_EQ(nullptr, B.getNext());
}
TEST(IListBaseTest, removeRangeImpl) {
ilist_node_base S, A, B, C, D;
// [S] <-> A <-> B <-> C <-> D <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
ilist_base::insertBeforeImpl(S, C);
ilist_base::insertBeforeImpl(S, D);
// [S] <-> A <-> D <-> [S]
ilist_base::removeRangeImpl(B, D);
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());
}
TEST(IListBaseTest, removeRangeImplAllButSentinel) {
ilist_node_base S, A, B;
// [S] <-> A <-> B <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
// [S] <-> [S]
ilist_base::removeRangeImpl(A, S);
EXPECT_EQ(&S, S.getPrev());
EXPECT_EQ(&S, S.getNext());
EXPECT_EQ(nullptr, A.getPrev());
EXPECT_EQ(nullptr, B.getNext());
}
TEST(IListBaseTest, transferBeforeImpl) {
ilist_node_base S1, S2, A, B, C, D, E;
// [S1] <-> A <-> B <-> C <-> [S1]
S1.setPrev(&S1);
S1.setNext(&S1);
ilist_base::insertBeforeImpl(S1, A);
ilist_base::insertBeforeImpl(S1, B);
ilist_base::insertBeforeImpl(S1, C);
// [S2] <-> D <-> E <-> [S2]
S2.setPrev(&S2);
S2.setNext(&S2);
ilist_base::insertBeforeImpl(S2, D);
ilist_base::insertBeforeImpl(S2, E);
// [S1] <-> C <-> [S1]
ilist_base::transferBeforeImpl(D, A, C);
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