Skip to content

Commit

Permalink
fix: ensure compatibility std::uniform_int_distribution for binary da…
Browse files Browse the repository at this point in the history
…ta generation & improve overall format
  • Loading branch information
xzhseh committed Jul 19, 2023
1 parent 32b059b commit 16b1cbe
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/buffer/buffer_pool_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "buffer/buffer_pool_manager.h"

#include <cstdio>
#include <limits>
#include <random>
#include <string>

Expand All @@ -29,7 +30,12 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {

std::random_device r;
std::default_random_engine rng(r());
std::uniform_int_distribution<char> uniform_dist(0);

constexpr int lower_bound = static_cast<int>(std::numeric_limits<char>::min());
constexpr int upper_bound = static_cast<int>(std::numeric_limits<char>::max());
// No matter if `char` is signed or unsigned by default, this constraint must be met
static_assert(upper_bound - lower_bound == 255);
std::uniform_int_distribution<int> uniform_dist(lower_bound, upper_bound);

auto *disk_manager = new DiskManager(db_name);
auto *bpm = new BufferPoolManager(buffer_pool_size, disk_manager, k);
Expand All @@ -44,7 +50,7 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
char random_binary_data[BUSTUB_PAGE_SIZE];
// Generate random binary data
for (char &i : random_binary_data) {
i = uniform_dist(rng);
i = static_cast<char>(uniform_dist(rng));
}

// Insert terminal characters both in the middle and at end
Expand All @@ -65,17 +71,20 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
EXPECT_EQ(nullptr, bpm->NewPage(&page_id_temp));
}

// Scenario: After unpinning pages {0, 1, 2, 3, 4} we should be able to create 5 new pages
// Scenario: After unpinning pages {0, 1, 2, 3, 4}, we should be able to create 5 new pages
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(true, bpm->UnpinPage(i, true));
bpm->FlushPage(i);
}
for (int i = 0; i < 5; ++i) {
EXPECT_NE(nullptr, bpm->NewPage(&page_id_temp));
// Unpin the page here to allow future fetching
bpm->UnpinPage(page_id_temp, false);
}

// Scenario: We should be able to fetch the data we wrote a while ago.
page0 = bpm->FetchPage(0);
ASSERT_NE(nullptr, page0);
EXPECT_EQ(0, memcmp(page0->GetData(), random_binary_data, BUSTUB_PAGE_SIZE));
EXPECT_EQ(true, bpm->UnpinPage(0, true));

Expand Down Expand Up @@ -128,10 +137,11 @@ TEST(BufferPoolManagerTest, DISABLED_SampleTest) {

// Scenario: We should be able to fetch the data we wrote a while ago.
page0 = bpm->FetchPage(0);
ASSERT_NE(nullptr, page0);
EXPECT_EQ(0, strcmp(page0->GetData(), "Hello"));

// Scenario: If we unpin page 0 and then make a new page, all the buffer pages should
// now be pinned. Fetching page 0 should fail.
// now be pinned. Fetching page 0 again should fail.
EXPECT_EQ(true, bpm->UnpinPage(0, true));
EXPECT_NE(nullptr, bpm->NewPage(&page_id_temp));
EXPECT_EQ(nullptr, bpm->FetchPage(0));
Expand Down

0 comments on commit 16b1cbe

Please sign in to comment.