Skip to content

Commit 7fb461d

Browse files
committed
Clean up tests
1 parent a47baef commit 7fb461d

File tree

2 files changed

+110
-89
lines changed

2 files changed

+110
-89
lines changed

test/buffer/buffer_pool_manager_test.cpp

+76-72
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include <cstdio>
14-
#include <deque>
1514
#include <filesystem>
1615

1716
#include "buffer/buffer_pool_manager.h"
@@ -27,36 +26,37 @@ const size_t FRAMES = 10;
2726
// Note that this test assumes you are using the an LRU-K replacement policy.
2827
const size_t K_DIST = 5;
2928

29+
void CopyString(char *dest, const std::string &src) {
30+
BUSTUB_ENSURE(src.length() + 1 <= BUSTUB_PAGE_SIZE, "CopyString src too long");
31+
snprintf(dest, BUSTUB_PAGE_SIZE, "%s", src.c_str());
32+
}
33+
3034
TEST(BufferPoolManagerTest, DISABLED_VeryBasicTest) {
3135
// A very basic test.
3236

3337
auto disk_manager = std::make_shared<DiskManager>(db_fname);
3438
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get(), K_DIST);
3539

36-
page_id_t pid = bpm->NewPage();
37-
38-
char str[] = "Hello, world!";
40+
const page_id_t pid = bpm->NewPage();
41+
const std::string str = "Hello, world!";
3942

4043
// Check `WritePageGuard` basic functionality.
4144
{
4245
auto guard = bpm->WritePage(pid);
43-
char *data = guard.GetDataMut();
44-
snprintf(data, sizeof(str), "%s", str);
45-
EXPECT_STREQ(data, str);
46+
CopyString(guard.GetDataMut(), str);
47+
EXPECT_STREQ(guard.GetData(), str.c_str());
4648
}
4749

4850
// Check `ReadPageGuard` basic functionality.
4951
{
50-
auto guard = bpm->ReadPage(pid);
51-
const char *data = guard.GetData();
52-
EXPECT_STREQ(data, str);
52+
const auto guard = bpm->ReadPage(pid);
53+
EXPECT_STREQ(guard.GetData(), str.c_str());
5354
}
5455

5556
// Check `ReadPageGuard` basic functionality (again).
5657
{
57-
auto guard = bpm->ReadPage(pid);
58-
const char *data = guard.GetData();
59-
EXPECT_STREQ(data, str);
58+
const auto guard = bpm->ReadPage(pid);
59+
EXPECT_STREQ(guard.GetData(), str.c_str());
6060
}
6161

6262
ASSERT_TRUE(bpm->DeletePage(pid));
@@ -66,31 +66,34 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinEasyTest) {
6666
auto disk_manager = std::make_shared<DiskManager>(db_fname);
6767
auto bpm = std::make_shared<BufferPoolManager>(2, disk_manager.get(), 5);
6868

69-
page_id_t pageid0;
70-
page_id_t pageid1;
69+
const page_id_t pageid0 = bpm->NewPage();
70+
const page_id_t pageid1 = bpm->NewPage();
71+
72+
const std::string str0 = "page0";
73+
const std::string str1 = "page1";
74+
const std::string str0updated = "page0updated";
75+
const std::string str1updated = "page1updated";
7176

7277
{
73-
pageid0 = bpm->NewPage();
7478
auto page0_write_opt = bpm->CheckedWritePage(pageid0);
7579
ASSERT_TRUE(page0_write_opt.has_value());
76-
WritePageGuard page0_write = std::move(page0_write_opt.value());
77-
strcpy(page0_write.GetDataMut(), "page0"); // NOLINT
80+
auto page0_write = std::move(page0_write_opt.value());
81+
CopyString(page0_write.GetDataMut(), str0);
7882

79-
pageid1 = bpm->NewPage();
8083
auto page1_write_opt = bpm->CheckedWritePage(pageid1);
8184
ASSERT_TRUE(page1_write_opt.has_value());
82-
WritePageGuard page1_write = std::move(page1_write_opt.value());
83-
strcpy(page1_write.GetDataMut(), "page1"); // NOLINT
85+
auto page1_write = std::move(page1_write_opt.value());
86+
CopyString(page1_write.GetDataMut(), str1);
8487

8588
ASSERT_EQ(1, bpm->GetPinCount(pageid0));
8689
ASSERT_EQ(1, bpm->GetPinCount(pageid1));
8790

88-
page_id_t temp_page_id1 = bpm->NewPage();
89-
auto temp_page1_opt = bpm->CheckedReadPage(temp_page_id1);
91+
const auto temp_page_id1 = bpm->NewPage();
92+
const auto temp_page1_opt = bpm->CheckedReadPage(temp_page_id1);
9093
ASSERT_FALSE(temp_page1_opt.has_value());
9194

92-
page_id_t temp_page_id2 = bpm->NewPage();
93-
auto temp_page2_opt = bpm->CheckedWritePage(temp_page_id2);
95+
const auto temp_page_id2 = bpm->NewPage();
96+
const auto temp_page2_opt = bpm->CheckedWritePage(temp_page_id2);
9497
ASSERT_FALSE(temp_page2_opt.has_value());
9598

9699
ASSERT_EQ(1, bpm->GetPinCount(pageid0));
@@ -103,12 +106,12 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinEasyTest) {
103106
}
104107

105108
{
106-
page_id_t temp_page_id1 = bpm->NewPage();
107-
auto temp_page1_opt = bpm->CheckedReadPage(temp_page_id1);
109+
const auto temp_page_id1 = bpm->NewPage();
110+
const auto temp_page1_opt = bpm->CheckedReadPage(temp_page_id1);
108111
ASSERT_TRUE(temp_page1_opt.has_value());
109112

110-
page_id_t temp_page_id2 = bpm->NewPage();
111-
auto temp_page2_opt = bpm->CheckedWritePage(temp_page_id2);
113+
const auto temp_page_id2 = bpm->NewPage();
114+
const auto temp_page2_opt = bpm->CheckedWritePage(temp_page_id2);
112115
ASSERT_TRUE(temp_page2_opt.has_value());
113116

114117
ASSERT_FALSE(bpm->GetPinCount(pageid0).has_value());
@@ -118,15 +121,15 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinEasyTest) {
118121
{
119122
auto page0_write_opt = bpm->CheckedWritePage(pageid0);
120123
ASSERT_TRUE(page0_write_opt.has_value());
121-
WritePageGuard page0_write = std::move(page0_write_opt.value());
122-
ASSERT_EQ(0, strcmp(page0_write.GetData(), "page0"));
123-
strcpy(page0_write.GetDataMut(), "page0updated"); // NOLINT
124+
auto page0_write = std::move(page0_write_opt.value());
125+
EXPECT_STREQ(page0_write.GetData(), str0.c_str());
126+
CopyString(page0_write.GetDataMut(), str0updated);
124127

125128
auto page1_write_opt = bpm->CheckedWritePage(pageid1);
126129
ASSERT_TRUE(page1_write_opt.has_value());
127-
WritePageGuard page1_write = std::move(page1_write_opt.value());
128-
ASSERT_EQ(0, strcmp(page1_write.GetData(), "page1"));
129-
strcpy(page1_write.GetDataMut(), "page1updated"); // NOLINT
130+
auto page1_write = std::move(page1_write_opt.value());
131+
EXPECT_STREQ(page1_write.GetData(), str1.c_str());
132+
CopyString(page1_write.GetDataMut(), str1updated);
130133

131134
ASSERT_EQ(1, bpm->GetPinCount(pageid0));
132135
ASSERT_EQ(1, bpm->GetPinCount(pageid1));
@@ -138,13 +141,13 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinEasyTest) {
138141
{
139142
auto page0_read_opt = bpm->CheckedReadPage(pageid0);
140143
ASSERT_TRUE(page0_read_opt.has_value());
141-
ReadPageGuard page0_read = std::move(page0_read_opt.value());
142-
ASSERT_EQ(0, strcmp(page0_read.GetData(), "page0updated"));
144+
const auto page0_read = std::move(page0_read_opt.value());
145+
EXPECT_STREQ(page0_read.GetData(), str0updated.c_str());
143146

144147
auto page1_read_opt = bpm->CheckedReadPage(pageid1);
145148
ASSERT_TRUE(page1_read_opt.has_value());
146-
ReadPageGuard page1_read = std::move(page1_read_opt.value());
147-
ASSERT_EQ(0, strcmp(page1_read.GetData(), "page1updated"));
149+
const auto page1_read = std::move(page1_read_opt.value());
150+
EXPECT_STREQ(page1_read.GetData(), str1updated.c_str());
148151

149152
ASSERT_EQ(1, bpm->GetPinCount(pageid0));
150153
ASSERT_EQ(1, bpm->GetPinCount(pageid1));
@@ -162,12 +165,13 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinMediumTest) {
162165
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get(), K_DIST);
163166

164167
// Scenario: The buffer pool is empty. We should be able to create a new page.
165-
page_id_t pid0 = bpm->NewPage();
168+
const auto pid0 = bpm->NewPage();
166169
auto page0 = bpm->WritePage(pid0);
167170

168171
// Scenario: Once we have a page, we should be able to read and write content.
169-
snprintf(page0.GetDataMut(), BUSTUB_PAGE_SIZE, "Hello");
170-
EXPECT_EQ(0, strcmp(page0.GetDataMut(), "Hello"));
172+
const std::string hello = "Hello";
173+
CopyString(page0.GetDataMut(), hello);
174+
EXPECT_STREQ(page0.GetData(), hello.c_str());
171175

172176
page0.Drop();
173177

@@ -176,58 +180,58 @@ TEST(BufferPoolManagerTest, DISABLED_PagePinMediumTest) {
176180

177181
// Scenario: We should be able to create new pages until we fill up the buffer pool.
178182
for (size_t i = 0; i < FRAMES; i++) {
179-
auto pid = bpm->NewPage();
183+
const auto pid = bpm->NewPage();
180184
auto page = bpm->WritePage(pid);
181185
pages.push_back(std::move(page));
182186
}
183187

184188
// Scenario: All of the pin counts should be 1.
185189
for (const auto &page : pages) {
186-
auto pid = page.GetPageId();
190+
const auto pid = page.GetPageId();
187191
EXPECT_EQ(1, bpm->GetPinCount(pid));
188192
}
189193

190194
// Scenario: Once the buffer pool is full, we should not be able to create any new pages.
191195
for (size_t i = 0; i < FRAMES; i++) {
192-
auto pid = bpm->NewPage();
193-
auto fail = bpm->CheckedWritePage(pid);
196+
const auto pid = bpm->NewPage();
197+
const auto fail = bpm->CheckedWritePage(pid);
194198
ASSERT_FALSE(fail.has_value());
195199
}
196200

197201
// Scenario: Drop the first 5 pages to unpin them.
198202
for (size_t i = 0; i < FRAMES / 2; i++) {
199-
page_id_t pid = pages[0].GetPageId();
203+
const auto pid = pages[0].GetPageId();
200204
EXPECT_EQ(1, bpm->GetPinCount(pid));
201205
pages.erase(pages.begin());
202206
EXPECT_EQ(0, bpm->GetPinCount(pid));
203207
}
204208

205209
// Scenario: All of the pin counts of the pages we haven't dropped yet should still be 1.
206210
for (const auto &page : pages) {
207-
auto pid = page.GetPageId();
211+
const auto pid = page.GetPageId();
208212
EXPECT_EQ(1, bpm->GetPinCount(pid));
209213
}
210214

211215
// Scenario: After unpinning pages {1, 2, 3, 4, 5}, we should be able to create 4 new pages and bring them into
212216
// memory. Bringing those 4 pages into memory should evict the first 4 pages {1, 2, 3, 4} because of LRU.
213217
for (size_t i = 0; i < ((FRAMES / 2) - 1); i++) {
214-
auto pid = bpm->NewPage();
218+
const auto pid = bpm->NewPage();
215219
auto page = bpm->WritePage(pid);
216220
pages.push_back(std::move(page));
217221
}
218222

219223
// Scenario: There should be one frame available, and we should be able to fetch the data we wrote a while ago.
220224
{
221-
ReadPageGuard original_page = bpm->ReadPage(pid0);
222-
EXPECT_EQ(0, strcmp(original_page.GetData(), "Hello"));
225+
const auto original_page = bpm->ReadPage(pid0);
226+
EXPECT_STREQ(original_page.GetData(), hello.c_str());
223227
}
224228

225229
// Scenario: Once we unpin page 0 and then make a new page, all the buffer pages should now be pinned. Fetching page 0
226230
// again should fail.
227-
auto last_pid = bpm->NewPage();
228-
auto last_page = bpm->ReadPage(last_pid);
231+
const auto last_pid = bpm->NewPage();
232+
const auto last_page = bpm->ReadPage(last_pid);
229233

230-
auto fail = bpm->CheckedReadPage(pid0);
234+
const auto fail = bpm->CheckedReadPage(pid0);
231235
ASSERT_FALSE(fail.has_value());
232236

233237
// Shutdown the disk manager and remove the temporary file we created.
@@ -241,15 +245,15 @@ TEST(BufferPoolManagerTest, DISABLED_PageAccessTest) {
241245
auto disk_manager = std::make_shared<DiskManager>(db_fname);
242246
auto bpm = std::make_shared<BufferPoolManager>(1, disk_manager.get(), K_DIST);
243247

244-
auto pid = bpm->NewPage();
248+
const auto pid = bpm->NewPage();
245249
char buf[BUSTUB_PAGE_SIZE];
246250

247251
auto thread = std::thread([&]() {
248252
// The writer can keep writing to the same page.
249253
for (size_t i = 0; i < rounds; i++) {
250254
std::this_thread::sleep_for(std::chrono::milliseconds(5));
251255
auto guard = bpm->WritePage(pid);
252-
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
256+
CopyString(guard.GetDataMut(), std::to_string(i));
253257
}
254258
});
255259

@@ -258,7 +262,7 @@ TEST(BufferPoolManagerTest, DISABLED_PageAccessTest) {
258262
std::this_thread::sleep_for(std::chrono::milliseconds(10));
259263

260264
// While we are reading, nobody should be able to modify the data.
261-
auto guard = bpm->ReadPage(pid);
265+
const auto guard = bpm->ReadPage(pid);
262266

263267
// Save the data we observe.
264268
memcpy(buf, guard.GetData(), BUSTUB_PAGE_SIZE);
@@ -267,7 +271,7 @@ TEST(BufferPoolManagerTest, DISABLED_PageAccessTest) {
267271
std::this_thread::sleep_for(std::chrono::milliseconds(10));
268272

269273
// Check that the data is unmodified.
270-
EXPECT_EQ(0, strcmp(guard.GetData(), buf));
274+
EXPECT_STREQ(guard.GetData(), buf);
271275
}
272276

273277
thread.join();
@@ -279,33 +283,33 @@ TEST(BufferPoolManagerTest, DISABLED_ContentionTest) {
279283

280284
const size_t rounds = 100000;
281285

282-
auto pid = bpm->NewPage();
286+
const auto pid = bpm->NewPage();
283287

284288
auto thread1 = std::thread([&]() {
285289
for (size_t i = 0; i < rounds; i++) {
286290
auto guard = bpm->WritePage(pid);
287-
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
291+
CopyString(guard.GetDataMut(), std::to_string(i));
288292
}
289293
});
290294

291295
auto thread2 = std::thread([&]() {
292296
for (size_t i = 0; i < rounds; i++) {
293297
auto guard = bpm->WritePage(pid);
294-
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
298+
CopyString(guard.GetDataMut(), std::to_string(i));
295299
}
296300
});
297301

298302
auto thread3 = std::thread([&]() {
299303
for (size_t i = 0; i < rounds; i++) {
300304
auto guard = bpm->WritePage(pid);
301-
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
305+
CopyString(guard.GetDataMut(), std::to_string(i));
302306
}
303307
});
304308

305309
auto thread4 = std::thread([&]() {
306310
for (size_t i = 0; i < rounds; i++) {
307311
auto guard = bpm->WritePage(pid);
308-
strcpy(guard.GetDataMut(), std::to_string(i).c_str()); // NOLINT
312+
CopyString(guard.GetDataMut(), std::to_string(i));
309313
}
310314
});
311315

@@ -319,8 +323,8 @@ TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
319323
auto disk_manager = std::make_shared<DiskManager>(db_fname);
320324
auto bpm = std::make_shared<BufferPoolManager>(FRAMES, disk_manager.get(), K_DIST);
321325

322-
auto pid0 = bpm->NewPage();
323-
auto pid1 = bpm->NewPage();
326+
const auto pid0 = bpm->NewPage();
327+
const auto pid1 = bpm->NewPage();
324328

325329
auto guard0 = bpm->WritePage(pid0);
326330

@@ -332,7 +336,7 @@ TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
332336
start.store(true);
333337

334338
// Attempt to write to page 0.
335-
auto guard0 = bpm->WritePage(pid0);
339+
const auto guard0 = bpm->WritePage(pid0);
336340
});
337341

338342
// Wait for the other thread to begin before we start the test.
@@ -347,7 +351,7 @@ TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
347351
// Think about what might happen if you hold a certain "all-encompassing" latch for too long...
348352

349353
// While holding page 0, take the latch on page 1.
350-
auto guard1 = bpm->WritePage(pid1);
354+
const auto guard1 = bpm->WritePage(pid1);
351355

352356
// Let the child thread have the page 0 since we're done with it.
353357
guard0.Drop();
@@ -357,8 +361,8 @@ TEST(BufferPoolManagerTest, DISABLED_DeadlockTest) {
357361

358362
TEST(BufferPoolManagerTest, DISABLED_EvictableTest) {
359363
// Test if the evictable status of a frame is always correct.
360-
size_t rounds = 1000;
361-
size_t num_readers = 8;
364+
const size_t rounds = 1000;
365+
const size_t num_readers = 8;
362366

363367
auto disk_manager = std::make_shared<DiskManager>(db_fname);
364368
// Only allocate 1 frame of memory to the buffer pool manager.
@@ -372,9 +376,9 @@ TEST(BufferPoolManagerTest, DISABLED_EvictableTest) {
372376
bool signal = false;
373377

374378
// This page will be loaded into the only available frame.
375-
page_id_t winner_pid = bpm->NewPage();
379+
const auto winner_pid = bpm->NewPage();
376380
// We will attempt to load this page into the occupied frame, and it should fail every time.
377-
page_id_t loser_pid = bpm->NewPage();
381+
const auto loser_pid = bpm->NewPage();
378382

379383
std::vector<std::thread> readers;
380384
for (size_t j = 0; j < num_readers; j++) {
@@ -387,7 +391,7 @@ TEST(BufferPoolManagerTest, DISABLED_EvictableTest) {
387391
}
388392

389393
// Read the page in shared mode.
390-
auto read_guard = bpm->ReadPage(winner_pid);
394+
const auto read_guard = bpm->ReadPage(winner_pid);
391395

392396
// Since the only frame is pinned, no thread should be able to bring in a new page.
393397
ASSERT_FALSE(bpm->CheckedReadPage(loser_pid).has_value());

0 commit comments

Comments
 (0)