Skip to content

Commit df2976d

Browse files
authored
fix(test): ensure compatibility std::uniform_int_distribution for binary data generation & improve overall format (#587)
fix: ensure compatibility std::uniform_int_distribution for binary data generation & improve overall format
1 parent 50726c2 commit df2976d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

test/buffer/buffer_pool_manager_test.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "buffer/buffer_pool_manager.h"
1414

1515
#include <cstdio>
16+
#include <limits>
1617
#include <random>
1718
#include <string>
1819

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

3031
std::random_device r;
3132
std::default_random_engine rng(r());
32-
std::uniform_int_distribution<char> uniform_dist(0);
33+
34+
constexpr int lower_bound = static_cast<int>(std::numeric_limits<char>::min());
35+
constexpr int upper_bound = static_cast<int>(std::numeric_limits<char>::max());
36+
// No matter if `char` is signed or unsigned by default, this constraint must be met
37+
static_assert(upper_bound - lower_bound == 255);
38+
std::uniform_int_distribution<int> uniform_dist(lower_bound, upper_bound);
3339

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

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

68-
// Scenario: After unpinning pages {0, 1, 2, 3, 4} we should be able to create 5 new pages
74+
// Scenario: After unpinning pages {0, 1, 2, 3, 4}, we should be able to create 5 new pages
6975
for (int i = 0; i < 5; ++i) {
7076
EXPECT_EQ(true, bpm->UnpinPage(i, true));
7177
bpm->FlushPage(i);
7278
}
7379
for (int i = 0; i < 5; ++i) {
7480
EXPECT_NE(nullptr, bpm->NewPage(&page_id_temp));
81+
// Unpin the page here to allow future fetching
7582
bpm->UnpinPage(page_id_temp, false);
7683
}
84+
7785
// Scenario: We should be able to fetch the data we wrote a while ago.
7886
page0 = bpm->FetchPage(0);
87+
ASSERT_NE(nullptr, page0);
7988
EXPECT_EQ(0, memcmp(page0->GetData(), random_binary_data, BUSTUB_PAGE_SIZE));
8089
EXPECT_EQ(true, bpm->UnpinPage(0, true));
8190

@@ -128,10 +137,11 @@ TEST(BufferPoolManagerTest, DISABLED_SampleTest) {
128137

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

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

0 commit comments

Comments
 (0)