2018-05-22 16:58:12 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-04-28 14:26:55 +00:00
|
|
|
#include "src/heap/list.h"
|
2018-05-22 16:58:12 +00:00
|
|
|
#include "testing/gtest-support.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
2020-04-28 14:26:55 +00:00
|
|
|
namespace internal {
|
|
|
|
namespace heap {
|
2018-05-22 16:58:12 +00:00
|
|
|
|
|
|
|
class TestChunk {
|
|
|
|
public:
|
2020-04-28 14:26:55 +00:00
|
|
|
heap::ListNode<TestChunk>& list_node() { return list_node_; }
|
2020-05-13 12:39:56 +00:00
|
|
|
const heap::ListNode<TestChunk>& list_node() const { return list_node_; }
|
2020-04-28 14:26:55 +00:00
|
|
|
heap::ListNode<TestChunk> list_node_;
|
2018-05-22 16:58:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(List, InsertAtTailAndRemove) {
|
|
|
|
List<TestChunk> list;
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
TestChunk t1;
|
|
|
|
list.PushBack(&t1);
|
|
|
|
EXPECT_FALSE(list.Empty());
|
|
|
|
EXPECT_TRUE(list.Contains(&t1));
|
|
|
|
list.Remove(&t1);
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(List, InsertAtHeadAndRemove) {
|
|
|
|
List<TestChunk> list;
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
TestChunk t1;
|
|
|
|
list.PushFront(&t1);
|
|
|
|
EXPECT_FALSE(list.Empty());
|
|
|
|
list.Remove(&t1);
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(List, InsertMultipleAtTailAndRemoveFromTail) {
|
|
|
|
List<TestChunk> list;
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
const int kSize = 10;
|
|
|
|
TestChunk chunks[kSize];
|
|
|
|
for (int i = 0; i < kSize; i++) {
|
|
|
|
list.PushBack(&chunks[i]);
|
|
|
|
EXPECT_EQ(list.back(), &chunks[i]);
|
|
|
|
}
|
|
|
|
for (int i = kSize - 1; i > 0; i--) {
|
|
|
|
list.Remove(&chunks[i]);
|
|
|
|
EXPECT_EQ(list.back(), &chunks[i - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Remove(&chunks[0]);
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(List, InsertMultipleAtHeadAndRemoveFromHead) {
|
|
|
|
List<TestChunk> list;
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
const int kSize = 10;
|
|
|
|
TestChunk chunks[kSize];
|
|
|
|
for (int i = 0; i < kSize; i++) {
|
|
|
|
list.PushFront(&chunks[i]);
|
|
|
|
EXPECT_EQ(list.front(), &chunks[i]);
|
|
|
|
}
|
|
|
|
for (int i = kSize - 1; i > 0; i--) {
|
|
|
|
list.Remove(&chunks[i]);
|
|
|
|
EXPECT_EQ(list.front(), &chunks[i - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Remove(&chunks[0]);
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(List, InsertMultipleAtTailAndRemoveFromMiddle) {
|
|
|
|
List<TestChunk> list;
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
const int kSize = 10;
|
|
|
|
TestChunk chunks[kSize];
|
|
|
|
for (int i = 0; i < kSize; i++) {
|
|
|
|
list.PushBack(&chunks[i]);
|
|
|
|
EXPECT_EQ(list.back(), &chunks[i]);
|
|
|
|
}
|
|
|
|
int i, j;
|
|
|
|
for (i = kSize / 2 - 1, j = kSize / 2; i >= 0; i--, j++) {
|
|
|
|
list.Remove(&chunks[i]);
|
|
|
|
list.Remove(&chunks[j]);
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(list.Empty());
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:26:55 +00:00
|
|
|
} // namespace heap
|
|
|
|
} // namespace internal
|
2018-05-22 16:58:12 +00:00
|
|
|
} // namespace v8
|