13
13
#include " buffer/buffer_pool_manager.h"
14
14
15
15
#include < cstdio>
16
+ #include < limits>
16
17
#include < random>
17
18
#include < string>
18
19
@@ -29,7 +30,12 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
29
30
30
31
std::random_device r;
31
32
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);
33
39
34
40
auto *disk_manager = new DiskManager (db_name);
35
41
auto *bpm = new BufferPoolManager (buffer_pool_size, disk_manager, k);
@@ -44,7 +50,7 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
44
50
char random_binary_data[BUSTUB_PAGE_SIZE];
45
51
// Generate random binary data
46
52
for (char &i : random_binary_data) {
47
- i = uniform_dist (rng);
53
+ i = static_cast < char >( uniform_dist (rng) );
48
54
}
49
55
50
56
// Insert terminal characters both in the middle and at end
@@ -65,17 +71,20 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
65
71
EXPECT_EQ (nullptr , bpm->NewPage (&page_id_temp));
66
72
}
67
73
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
69
75
for (int i = 0 ; i < 5 ; ++i) {
70
76
EXPECT_EQ (true , bpm->UnpinPage (i, true ));
71
77
bpm->FlushPage (i);
72
78
}
73
79
for (int i = 0 ; i < 5 ; ++i) {
74
80
EXPECT_NE (nullptr , bpm->NewPage (&page_id_temp));
81
+ // Unpin the page here to allow future fetching
75
82
bpm->UnpinPage (page_id_temp, false );
76
83
}
84
+
77
85
// Scenario: We should be able to fetch the data we wrote a while ago.
78
86
page0 = bpm->FetchPage (0 );
87
+ ASSERT_NE (nullptr , page0);
79
88
EXPECT_EQ (0 , memcmp (page0->GetData (), random_binary_data, BUSTUB_PAGE_SIZE));
80
89
EXPECT_EQ (true , bpm->UnpinPage (0 , true ));
81
90
@@ -128,10 +137,11 @@ TEST(BufferPoolManagerTest, DISABLED_SampleTest) {
128
137
129
138
// Scenario: We should be able to fetch the data we wrote a while ago.
130
139
page0 = bpm->FetchPage (0 );
140
+ ASSERT_NE (nullptr , page0);
131
141
EXPECT_EQ (0 , strcmp (page0->GetData (), " Hello" ));
132
142
133
143
// 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.
135
145
EXPECT_EQ (true , bpm->UnpinPage (0 , true ));
136
146
EXPECT_NE (nullptr , bpm->NewPage (&page_id_temp));
137
147
EXPECT_EQ (nullptr , bpm->FetchPage (0 ));
0 commit comments