Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [lab1] fix unittests #5

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion test/unittest/buffer_pool_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ void test1()
}
ASSERT_EQ(count, 10);

// 先释放 record_page_handle 对 bp 的引用,避免析构顺序错误
rc = record_page_handle.cleanup();
ASSERT_EQ(rc, RC::SUCCESS);

bp->close_file();
delete bpm;
}
Expand Down Expand Up @@ -74,6 +78,9 @@ void test2()
}
ASSERT_EQ(count, 10);

rc = record_page_handle.cleanup();
ASSERT_EQ(rc, RC::SUCCESS);

bp->close_file();
delete bpm;
}
Expand All @@ -92,4 +99,4 @@ int main(int argc, char **argv)
// 调用RUN_ALL_TESTS()运行所有测试用例
// main函数返回RUN_ALL_TESTS()的运行结果
return RUN_ALL_TESTS();
}
}
21 changes: 19 additions & 2 deletions test/unittest/frame_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,36 @@ TEST(test_buffer, test_frame_manager)
*/
Frame *frame_get = frame_manager.get(file_desc, size - 1);
ASSERT_EQ(frame_get, used_list.back());
frame_get->unpin();

/**
* 测试3:FrameManager的purge_frames方法
*/
Frame *frame_fail = frame_manager.alloc(file_desc, ++size); // 分配失败
ASSERT_EQ(frame_fail, nullptr);

// 先 unpin 一些 Frame 便于后续驱逐
for (int i = 0; i < 3; i++) {
used_list.front()->unpin();
used_list.pop_front();
}

auto evict_action = [&](Frame *frame) { return RC::SUCCESS; };
frame_manager.evict_frames(3, evict_action); // 驱逐3个Frame
ASSERT_EQ(used_list.size() - 3, frame_manager.frame_num());
ASSERT_EQ(used_list.size(), frame_manager.frame_num());

frame_manager.evict_frames(1, evict_action); // 驱逐失败,没有 pin_count=0 的 Frame
ASSERT_EQ(used_list.size(), frame_manager.frame_num());

Frame *item2 = frame_manager.alloc(file_desc, size); // 分配成功
ASSERT_NE(item2, nullptr);
item2->unpin();

// 清理所有的 Frame
while (!used_list.empty()) {
used_list.front()->unpin();
used_list.pop_front();
}

frame_manager.cleanup();
}
Expand All @@ -59,4 +76,4 @@ int main(int argc, char **argv)
// 调用RUN_ALL_TESTS()运行所有测试用例
// main函数返回RUN_ALL_TESTS()的运行结果
return RUN_ALL_TESTS();
}
}