-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_manager_tests.cpp
More file actions
397 lines (308 loc) · 12.7 KB
/
storage_manager_tests.cpp
File metadata and controls
397 lines (308 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* @file storage_manager_tests.cpp
* @brief Unit tests for StorageManager - low-level disk I/O and page-level access
*/
#include <gtest/gtest.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <vector>
#include "storage/storage_manager.hpp"
using namespace cloudsql::storage;
namespace {
static void cleanup_file(const std::string& dir, const std::string& name) {
std::remove((dir + "/" + name).c_str());
}
class StorageManagerTests : public ::testing::Test {
protected:
void SetUp() override {
sm_ = std::make_unique<StorageManager>("./test_data");
sm_->create_dir_if_not_exists();
}
void TearDown() override { sm_.reset(); }
std::unique_ptr<StorageManager> sm_;
};
TEST_F(StorageManagerTests, OpenCloseBasic) {
const std::string filename = "open_close_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_TRUE(sm_->close_file(filename));
}
TEST_F(StorageManagerTests, OpenNonExistentCreatesFile) {
const std::string filename = "new_file_test.db";
cleanup_file("./test_data", filename);
ASSERT_FALSE(sm_->file_exists(filename));
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_TRUE(sm_->file_exists(filename));
}
TEST_F(StorageManagerTests, OpenTwiceReturnsTrue) {
const std::string filename = "double_open_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_TRUE(sm_->close_file(filename));
}
TEST_F(StorageManagerTests, CloseNonExistentReturnsFalse) {
ASSERT_FALSE(sm_->close_file("nonexistent_file.db"));
}
TEST_F(StorageManagerTests, ReadWritePageBasic) {
const std::string filename = "page_rw_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
char write_buf[StorageManager::PAGE_SIZE];
char read_buf[StorageManager::PAGE_SIZE];
std::memset(write_buf, 0, StorageManager::PAGE_SIZE);
std::memset(read_buf, 0, StorageManager::PAGE_SIZE);
// Write pattern to page 0
for (int i = 0; i < 16; ++i) {
write_buf[i * 16] = static_cast<char>(i);
}
ASSERT_TRUE(sm_->write_page(filename, 0, write_buf));
// Read back and verify
ASSERT_TRUE(sm_->read_page(filename, 0, read_buf));
ASSERT_EQ(std::memcmp(write_buf, read_buf, StorageManager::PAGE_SIZE), 0);
}
TEST_F(StorageManagerTests, ReadBeyondEOFFillsZeros) {
const std::string filename = "beyond_eof_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
char read_buf[StorageManager::PAGE_SIZE];
std::memset(read_buf, 0xFF, StorageManager::PAGE_SIZE); // Fill with sentinel
// Read page 10 from empty file - should zero-fill
ASSERT_TRUE(sm_->read_page(filename, 10, read_buf));
// Verify all zeros
for (size_t i = 0; i < StorageManager::PAGE_SIZE; ++i) {
EXPECT_EQ(read_buf[i], 0) << "Byte at index " << i << " was not zero";
}
}
TEST_F(StorageManagerTests, PartialReadReturnsFalse) {
const std::string filename = "partial_read_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
// Write a small amount of data
char write_buf[StorageManager::PAGE_SIZE];
std::memset(write_buf, 0xAB, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(filename, 0, write_buf));
// Try to read the small write as a full page should succeed (EOF handling fills zeros)
char read_buf[StorageManager::PAGE_SIZE];
ASSERT_TRUE(sm_->read_page(filename, 0, read_buf));
}
TEST_F(StorageManagerTests, AllocatePageOnEmptyFile) {
const std::string filename = "allocate_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_EQ(sm_->allocate_page(filename), 0U);
}
TEST_F(StorageManagerTests, AllocatePageSequential) {
const std::string filename = "allocate_seq_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
// allocate_page returns next page index based on file size
// But it does NOT write to file - you need to write_page
ASSERT_EQ(sm_->allocate_page(filename), 0U);
// Write a page, then allocate should give next index
char buf[StorageManager::PAGE_SIZE];
std::memset(buf, 0, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(filename, 0, buf));
ASSERT_EQ(sm_->allocate_page(filename), 1U);
}
TEST_F(StorageManagerTests, CreateDirIfNotExistsBasic) {
// Directory should already exist from SetUp
ASSERT_TRUE(sm_->create_dir_if_not_exists());
}
TEST_F(StorageManagerTests, CreateDirAlreadyExists) {
// create_dir_if_not_exists should return true even if dir exists
ASSERT_TRUE(sm_->create_dir_if_not_exists());
ASSERT_TRUE(sm_->create_dir_if_not_exists());
}
TEST_F(StorageManagerTests, FileExistsAfterOpen) {
const std::string filename = "exists_test.db";
cleanup_file("./test_data", filename);
ASSERT_FALSE(sm_->file_exists(filename));
ASSERT_TRUE(sm_->open_file(filename));
ASSERT_TRUE(sm_->file_exists(filename));
}
TEST_F(StorageManagerTests, GetFullPath) {
const std::string path = sm_->get_full_path("test.db");
EXPECT_EQ(path, "./test_data/test.db");
}
TEST_F(StorageManagerTests, MultipleFilesOpen) {
const std::string file1 = "multi1.db";
const std::string file2 = "multi2.db";
const std::string file3 = "multi3.db";
cleanup_file("./test_data", file1);
cleanup_file("./test_data", file2);
cleanup_file("./test_data", file3);
ASSERT_TRUE(sm_->open_file(file1));
ASSERT_TRUE(sm_->open_file(file2));
ASSERT_TRUE(sm_->open_file(file3));
ASSERT_TRUE(sm_->file_exists(file1));
ASSERT_TRUE(sm_->file_exists(file2));
ASSERT_TRUE(sm_->file_exists(file3));
}
TEST_F(StorageManagerTests, StatsAccurateAfterOperations) {
const std::string filename = "stats_test.db";
cleanup_file("./test_data", filename);
const auto& stats = sm_->get_stats();
auto initial_pages_read = stats.pages_read.load();
auto initial_pages_written = stats.pages_written.load();
ASSERT_TRUE(sm_->open_file(filename));
char buf[StorageManager::PAGE_SIZE];
std::memset(buf, 0, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(filename, 0, buf));
ASSERT_TRUE(sm_->read_page(filename, 0, buf));
EXPECT_GT(stats.pages_written.load(), initial_pages_written);
}
TEST_F(StorageManagerTests, WriteAndReadDifferentPages) {
const std::string filename = "diff_pages_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
char page0[StorageManager::PAGE_SIZE];
char page1[StorageManager::PAGE_SIZE];
char page5[StorageManager::PAGE_SIZE];
char read_buf[StorageManager::PAGE_SIZE];
std::memset(page0, 0xAA, StorageManager::PAGE_SIZE);
std::memset(page1, 0xBB, StorageManager::PAGE_SIZE);
std::memset(page5, 0xCC, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(filename, 0, page0));
ASSERT_TRUE(sm_->write_page(filename, 1, page1));
ASSERT_TRUE(sm_->write_page(filename, 5, page5));
ASSERT_TRUE(sm_->read_page(filename, 0, read_buf));
EXPECT_EQ(std::memcmp(page0, read_buf, StorageManager::PAGE_SIZE), 0);
ASSERT_TRUE(sm_->read_page(filename, 1, read_buf));
EXPECT_EQ(std::memcmp(page1, read_buf, StorageManager::PAGE_SIZE), 0);
ASSERT_TRUE(sm_->read_page(filename, 5, read_buf));
EXPECT_EQ(std::memcmp(page5, read_buf, StorageManager::PAGE_SIZE), 0);
}
// ============= New Tests =============
/**
* @brief Verifies PAGE_SIZE constant value
*/
TEST_F(StorageManagerTests, PageSizeConstant) {
EXPECT_EQ(StorageManager::PAGE_SIZE, 4096U);
}
/**
* @brief Verifies read_page auto-opens file if not already open
*/
TEST_F(StorageManagerTests, ReadNonOpenedFileAutoOpens) {
const std::string filename = "non_opened_read.db";
cleanup_file("./test_data", filename);
// StorageManager auto-opens files, so read should succeed
char buf[StorageManager::PAGE_SIZE];
EXPECT_TRUE(sm_->read_page(filename, 0, buf));
}
/**
* @brief Verifies write_page auto-opens file if not already open
*/
TEST_F(StorageManagerTests, WriteNonOpenedFileAutoOpens) {
const std::string filename = "non_opened_write.db";
cleanup_file("./test_data", filename);
char buf[StorageManager::PAGE_SIZE];
std::memset(buf, 0xAB, StorageManager::PAGE_SIZE);
// StorageManager auto-opens files, so write should succeed
EXPECT_TRUE(sm_->write_page(filename, 0, buf));
}
/**
* @brief Verifies data persists across file open/close cycle
*/
TEST_F(StorageManagerTests, DataPersistenceAcrossOpenClose) {
const std::string filename = "persist_test.db";
cleanup_file("./test_data", filename);
// Write data to page 0
ASSERT_TRUE(sm_->open_file(filename));
char write_buf[StorageManager::PAGE_SIZE];
for (int i = 0; i < StorageManager::PAGE_SIZE; ++i) {
write_buf[i] = static_cast<char>(i & 0xFF);
}
ASSERT_TRUE(sm_->write_page(filename, 0, write_buf));
ASSERT_TRUE(sm_->close_file(filename));
// Reopen and read - data should persist
ASSERT_TRUE(sm_->open_file(filename));
char read_buf[StorageManager::PAGE_SIZE];
ASSERT_TRUE(sm_->read_page(filename, 0, read_buf));
EXPECT_EQ(std::memcmp(write_buf, read_buf, StorageManager::PAGE_SIZE), 0);
ASSERT_TRUE(sm_->close_file(filename));
}
/**
* @brief Verifies stats track bytes_read and bytes_written accurately
*/
TEST_F(StorageManagerTests, StatsBytesAccurate) {
const std::string filename = "stats_bytes_test.db";
cleanup_file("./test_data", filename);
const auto& stats = sm_->get_stats();
auto initial_bytes_read = stats.bytes_read.load();
auto initial_bytes_written = stats.bytes_written.load();
ASSERT_TRUE(sm_->open_file(filename));
char buf[StorageManager::PAGE_SIZE];
std::memset(buf, 0xAB, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(filename, 0, buf));
ASSERT_TRUE(sm_->read_page(filename, 0, buf));
EXPECT_EQ(stats.bytes_written.load(), initial_bytes_written + StorageManager::PAGE_SIZE);
EXPECT_EQ(stats.bytes_read.load(), initial_bytes_read + StorageManager::PAGE_SIZE);
}
/**
* @brief Verifies data isolation between multiple files
*/
TEST_F(StorageManagerTests, MultipleFilesDataIsolation) {
const std::string file1 = "isolate1.db";
const std::string file2 = "isolate2.db";
cleanup_file("./test_data", file1);
cleanup_file("./test_data", file2);
ASSERT_TRUE(sm_->open_file(file1));
ASSERT_TRUE(sm_->open_file(file2));
char buf1[StorageManager::PAGE_SIZE];
char buf2[StorageManager::PAGE_SIZE];
char read_buf[StorageManager::PAGE_SIZE];
std::memset(buf1, 0x11, StorageManager::PAGE_SIZE);
std::memset(buf2, 0x22, StorageManager::PAGE_SIZE);
ASSERT_TRUE(sm_->write_page(file1, 0, buf1));
ASSERT_TRUE(sm_->write_page(file2, 0, buf2));
// Read from file1 - should have file1's data
ASSERT_TRUE(sm_->read_page(file1, 0, read_buf));
EXPECT_EQ(std::memcmp(buf1, read_buf, StorageManager::PAGE_SIZE), 0);
// Read from file2 - should have file2's data
ASSERT_TRUE(sm_->read_page(file2, 0, read_buf));
EXPECT_EQ(std::memcmp(buf2, read_buf, StorageManager::PAGE_SIZE), 0);
}
/**
* @brief Verifies deallocate_page stub doesn't crash
*/
TEST_F(StorageManagerTests, DeallocatePageStub) {
const std::string filename = "dealloc_test.db";
cleanup_file("./test_data", filename);
// deallocate_page is a stub but shouldn't crash
EXPECT_NO_THROW(StorageManager::deallocate_page(filename, 0));
}
/**
* @brief Verifies complex byte patterns persist correctly
*/
TEST_F(StorageManagerTests, ReadAfterWriteDifferentPatterns) {
const std::string filename = "patterns_test.db";
cleanup_file("./test_data", filename);
ASSERT_TRUE(sm_->open_file(filename));
// Write alternating pattern
char page[StorageManager::PAGE_SIZE];
for (size_t i = 0; i < StorageManager::PAGE_SIZE; ++i) {
page[i] = (i % 2 == 0) ? 0xAA : 0x55;
}
ASSERT_TRUE(sm_->write_page(filename, 0, page));
char read_buf[StorageManager::PAGE_SIZE];
ASSERT_TRUE(sm_->read_page(filename, 0, read_buf));
EXPECT_EQ(std::memcmp(page, read_buf, StorageManager::PAGE_SIZE), 0);
}
/**
* @brief Verifies files_opened stat increments correctly
*/
TEST_F(StorageManagerTests, FilesOpenedStatAccurate) {
const std::string file1 = "stat_file1.db";
const std::string file2 = "stat_file2.db";
cleanup_file("./test_data", file1);
cleanup_file("./test_data", file2);
const auto& stats = sm_->get_stats();
auto initial_files_opened = stats.files_opened.load();
ASSERT_TRUE(sm_->open_file(file1));
ASSERT_TRUE(sm_->open_file(file2));
EXPECT_EQ(stats.files_opened.load(), initial_files_opened + 2);
}
} // namespace