-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDramCircularListTest.cc
164 lines (134 loc) · 4.56 KB
/
DramCircularListTest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "DramCircularList.h"
#include <gtest/gtest.h>
class CircularListDramTest : public ::testing::Test {
protected:
DramCircularLinkedList* list;
CircularListDramTest() {
}
virtual ~CircularListDramTest() {
}
virtual void SetUp() {
list = new DramCircularLinkedList();
}
virtual void TearDown() {
delete list;
}
};
// 测试空链表的创建
TEST_F(CircularListDramTest, CreateEmptyList) {
EXPECT_EQ(list->head, nullptr);
}
// 测试插入单个节点
TEST_F(CircularListDramTest, InsertSingleNode) {
list->insertNode("key1", "data1");
EXPECT_NE(list->head, nullptr);
EXPECT_EQ(list->head->prev, list->head);
EXPECT_EQ(list->head->next, list->head);
}
// 测试插入多个节点
TEST_F(CircularListDramTest, InsertMultipleNodes) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
EXPECT_NE(list->head, nullptr);
EXPECT_NE(list->head->next, list->head);
EXPECT_NE(list->head->prev, list->head);
EXPECT_EQ(list->head->prev->next, list->head);
EXPECT_EQ(list->head->next->prev, list->head);
}
// 测试删除节点
TEST_F(CircularListDramTest, DeleteNode) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
list->deleteNode(list->head->next); // 删除第二个插入的节点
EXPECT_EQ(list->head->next, list->head);
EXPECT_EQ(list->head->prev, list->head);
}
TEST_F(CircularListDramTest, TestLoop) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
list->insertNode("key3", "data3");
DramNode* start = list->head;
DramNode* iter = start->next;
// 正向遍历
bool loopedForward = false;
while (iter != start) {
iter = iter->next;
if (iter == start) {
loopedForward = true;
break;
}
}
EXPECT_TRUE(loopedForward);
// 反向遍历
iter = start->prev;
bool loopedBackward = false;
while (iter != start) {
iter = iter->prev;
if (iter == start) {
loopedBackward = true;
break;
}
}
EXPECT_TRUE(loopedBackward);
}
TEST_F(CircularListDramTest, ClearList) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
list->insertNode("key3", "data3");
// 逐个删除所有节点
while (list->head != nullptr && list->head->next != list->head) {
list->deleteNode(list->head);
}
// 最后删除剩余的最后一个节点
list->deleteNode(list->head);
// 验证链表是否为空
EXPECT_EQ(list->head, nullptr);
}
//test enum
TEST_F(CircularListDramTest, NodeStatusTransition) {
list->insertNode("key", "data");
DramNode* node = list->head;
node->setStatus(DramNode::Once_read);
EXPECT_EQ(node->getStatus(), DramNode::Once_read);
node->setStatus(DramNode::Twice_read);
EXPECT_EQ(node->getStatus(), DramNode::Twice_read);
node->setStatus(DramNode::Twice_read);
EXPECT_EQ(node->getStatus(), DramNode::Twice_read);
}
// 测试插入节点后currentSize的更新
TEST_F(CircularListDramTest, CurrentSizeAfterInsertion) {
list->insertNode("key1", "data1");
size_t expectedSize = sizeof(DramNode) + strlen("key1") + 1 + strlen("data1") + 1;
EXPECT_EQ(list->currentSize, expectedSize);
list->insertNode("key2", "data2");
expectedSize += sizeof(DramNode) + strlen("key2") + 1 + strlen("data2") + 1;
EXPECT_EQ(list->currentSize, expectedSize);
}
// 测试删除节点后currentSize的更新
TEST_F(CircularListDramTest, CurrentSizeAfterDeletion) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
size_t initialSize = list->currentSize;
list->deleteNode(list->head->next); // 删除"key2", "data2"
size_t expectedSizeAfterDeletion = initialSize - (sizeof(DramNode) + strlen("key2") + 1 + strlen("data2") + 1);
EXPECT_EQ(list->currentSize, expectedSizeAfterDeletion);
}
// 测试清空链表后currentSize是否为0
TEST_F(CircularListDramTest, CurrentSizeAfterClearingList) {
list->insertNode("key1", "data1");
list->insertNode("key2", "data2");
list->insertNode("key3", "data3");
// 逐个删除所有节点
while (list->head != nullptr && list->head->next != list->head) {
list->deleteNode(list->head);
}
// 最后删除剩余的最后一个节点
list->deleteNode(list->head);
// 验证链表已清空且currentSize为0
EXPECT_EQ(list->head, nullptr);
EXPECT_EQ(list->currentSize, 0);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}