2010-05-12 21:35:19 +00:00
|
|
|
//===- llvm/unittest/ADT/APInt.cpp - APInt 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.h"
|
Add an assertion for a likely ilist::splice() contract violation.
The single-element ilist::splice() function supports a noop move:
List.splice(I, List, I);
The corresponding std::list function doesn't allow that, so add a unit
test to document that behavior.
This also means that
List.splice(I, List, F);
is somewhat surprisingly not equivalent to
List.splice(I, List, F, next(F));
This patch adds an assertion to catch the illegal case I == F above.
Alternatively, we could make I == F a legal noop, but that would make
ilist differ even more from std::list.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-18 19:28:37 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-01-02 10:26:28 +00:00
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2012-12-04 10:23:08 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <ostream>
|
2010-05-12 21:35:19 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Node : ilist_node<Node> {
|
|
|
|
int Value;
|
|
|
|
|
|
|
|
Node() {}
|
|
|
|
Node(int _Value) : Value(_Value) {}
|
2013-01-04 22:35:42 +00:00
|
|
|
~Node() { Value = -1; }
|
2010-05-12 21:35:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ilistTest, Basic) {
|
|
|
|
ilist<Node> List;
|
|
|
|
List.push_back(Node(1));
|
|
|
|
EXPECT_EQ(1, List.back().Value);
|
|
|
|
EXPECT_EQ(0, List.back().getPrevNode());
|
|
|
|
EXPECT_EQ(0, List.back().getNextNode());
|
|
|
|
|
|
|
|
List.push_back(Node(2));
|
|
|
|
EXPECT_EQ(2, List.back().Value);
|
|
|
|
EXPECT_EQ(2, List.front().getNextNode()->Value);
|
|
|
|
EXPECT_EQ(1, List.back().getPrevNode()->Value);
|
2010-05-13 18:35:02 +00:00
|
|
|
|
|
|
|
const ilist<Node> &ConstList = List;
|
|
|
|
EXPECT_EQ(2, ConstList.back().Value);
|
|
|
|
EXPECT_EQ(2, ConstList.front().getNextNode()->Value);
|
|
|
|
EXPECT_EQ(1, ConstList.back().getPrevNode()->Value);
|
2010-05-12 21:35:19 +00:00
|
|
|
}
|
|
|
|
|
Add an assertion for a likely ilist::splice() contract violation.
The single-element ilist::splice() function supports a noop move:
List.splice(I, List, I);
The corresponding std::list function doesn't allow that, so add a unit
test to document that behavior.
This also means that
List.splice(I, List, F);
is somewhat surprisingly not equivalent to
List.splice(I, List, F, next(F));
This patch adds an assertion to catch the illegal case I == F above.
Alternatively, we could make I == F a legal noop, but that would make
ilist differ even more from std::list.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-18 19:28:37 +00:00
|
|
|
TEST(ilistTest, SpliceOne) {
|
|
|
|
ilist<Node> List;
|
|
|
|
List.push_back(1);
|
|
|
|
|
|
|
|
// The single-element splice operation supports noops.
|
|
|
|
List.splice(List.begin(), List, List.begin());
|
|
|
|
EXPECT_EQ(1u, List.size());
|
|
|
|
EXPECT_EQ(1, List.front().Value);
|
2014-03-02 12:27:27 +00:00
|
|
|
EXPECT_TRUE(std::next(List.begin()) == List.end());
|
Add an assertion for a likely ilist::splice() contract violation.
The single-element ilist::splice() function supports a noop move:
List.splice(I, List, I);
The corresponding std::list function doesn't allow that, so add a unit
test to document that behavior.
This also means that
List.splice(I, List, F);
is somewhat surprisingly not equivalent to
List.splice(I, List, F, next(F));
This patch adds an assertion to catch the illegal case I == F above.
Alternatively, we could make I == F a legal noop, but that would make
ilist differ even more from std::list.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-18 19:28:37 +00:00
|
|
|
|
|
|
|
// Altenative noop. Move the first element behind itself.
|
|
|
|
List.push_back(2);
|
|
|
|
List.push_back(3);
|
2014-03-02 12:27:27 +00:00
|
|
|
List.splice(std::next(List.begin()), List, List.begin());
|
Add an assertion for a likely ilist::splice() contract violation.
The single-element ilist::splice() function supports a noop move:
List.splice(I, List, I);
The corresponding std::list function doesn't allow that, so add a unit
test to document that behavior.
This also means that
List.splice(I, List, F);
is somewhat surprisingly not equivalent to
List.splice(I, List, F, next(F));
This patch adds an assertion to catch the illegal case I == F above.
Alternatively, we could make I == F a legal noop, but that would make
ilist differ even more from std::list.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-18 19:28:37 +00:00
|
|
|
EXPECT_EQ(3u, List.size());
|
|
|
|
EXPECT_EQ(1, List.front().Value);
|
2014-03-02 12:27:27 +00:00
|
|
|
EXPECT_EQ(2, std::next(List.begin())->Value);
|
Add an assertion for a likely ilist::splice() contract violation.
The single-element ilist::splice() function supports a noop move:
List.splice(I, List, I);
The corresponding std::list function doesn't allow that, so add a unit
test to document that behavior.
This also means that
List.splice(I, List, F);
is somewhat surprisingly not equivalent to
List.splice(I, List, F, next(F));
This patch adds an assertion to catch the illegal case I == F above.
Alternatively, we could make I == F a legal noop, but that would make
ilist differ even more from std::list.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170443 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-18 19:28:37 +00:00
|
|
|
EXPECT_EQ(3, List.back().Value);
|
|
|
|
}
|
|
|
|
|
2013-01-04 22:35:42 +00:00
|
|
|
TEST(ilistTest, UnsafeClear) {
|
|
|
|
ilist<Node> List;
|
|
|
|
|
|
|
|
// Before even allocating a sentinel.
|
|
|
|
List.clearAndLeakNodesUnsafely();
|
|
|
|
EXPECT_EQ(0u, List.size());
|
|
|
|
|
|
|
|
// Empty list with sentinel.
|
|
|
|
ilist<Node>::iterator E = List.end();
|
|
|
|
List.clearAndLeakNodesUnsafely();
|
|
|
|
EXPECT_EQ(0u, List.size());
|
|
|
|
// The sentinel shouldn't change.
|
|
|
|
EXPECT_TRUE(E == List.end());
|
|
|
|
|
|
|
|
// List with contents.
|
|
|
|
List.push_back(1);
|
|
|
|
ASSERT_EQ(1u, List.size());
|
|
|
|
Node *N = List.begin();
|
|
|
|
EXPECT_EQ(1, N->Value);
|
|
|
|
List.clearAndLeakNodesUnsafely();
|
|
|
|
EXPECT_EQ(0u, List.size());
|
|
|
|
ASSERT_EQ(1, N->Value);
|
|
|
|
delete N;
|
|
|
|
|
|
|
|
// List is still functional.
|
|
|
|
List.push_back(5);
|
|
|
|
List.push_back(6);
|
|
|
|
ASSERT_EQ(2u, List.size());
|
|
|
|
EXPECT_EQ(5, List.front().Value);
|
|
|
|
EXPECT_EQ(6, List.back().Value);
|
|
|
|
}
|
|
|
|
|
2010-05-12 21:35:19 +00:00
|
|
|
}
|