File size: 3,156 Bytes
be7c937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <gtest/gtest.h>
#include "wayy_db/mmap_file.hpp"
#include "wayy_db/types.hpp"

#include <filesystem>
#include <cstring>

using namespace wayy_db;
namespace fs = std::filesystem;

class MmapFileTest : public ::testing::Test {
protected:
    void SetUp() override {
        test_dir_ = "/tmp/wayy_mmap_test_" + std::to_string(getpid());
        fs::create_directories(test_dir_);
    }

    void TearDown() override {
        fs::remove_all(test_dir_);
    }

    std::string test_dir_;
};

TEST_F(MmapFileTest, CreateAndWrite) {
    std::string path = test_dir_ + "/test.bin";

    {
        MmapFile file(path, MmapFile::Mode::Create, 1024);

        EXPECT_TRUE(file.is_open());
        EXPECT_EQ(file.size(), 1024);
        EXPECT_EQ(file.path(), path);

        // Write some data
        auto* data = static_cast<int*>(file.data());
        for (int i = 0; i < 256; ++i) {
            data[i] = i * 2;
        }

        file.sync();
    }

    // Verify data persisted
    {
        MmapFile file(path, MmapFile::Mode::ReadOnly);

        EXPECT_EQ(file.size(), 1024);

        auto* data = static_cast<const int*>(file.data());
        EXPECT_EQ(data[0], 0);
        EXPECT_EQ(data[100], 200);
        EXPECT_EQ(data[255], 510);
    }
}

TEST_F(MmapFileTest, ReadWrite) {
    std::string path = test_dir_ + "/rw.bin";

    // Create initial file
    {
        MmapFile file(path, MmapFile::Mode::Create, 100);
        std::memset(file.data(), 0, 100);
    }

    // Open for read-write and modify
    {
        MmapFile file(path, MmapFile::Mode::ReadWrite);
        auto* data = static_cast<uint8_t*>(file.data());
        data[50] = 42;
        file.sync();
    }

    // Verify modification
    {
        MmapFile file(path, MmapFile::Mode::ReadOnly);
        auto* data = static_cast<const uint8_t*>(file.data());
        EXPECT_EQ(data[50], 42);
    }
}

TEST_F(MmapFileTest, Resize) {
    std::string path = test_dir_ + "/resize.bin";

    MmapFile file(path, MmapFile::Mode::Create, 100);
    EXPECT_EQ(file.size(), 100);

    file.resize(500);
    EXPECT_EQ(file.size(), 500);

    // Can still write to expanded region
    auto* data = static_cast<uint8_t*>(file.data());
    data[400] = 123;
    file.sync();
}

TEST_F(MmapFileTest, MoveSemantics) {
    std::string path = test_dir_ + "/move.bin";

    MmapFile file1(path, MmapFile::Mode::Create, 256);
    void* original_data = file1.data();

    MmapFile file2 = std::move(file1);

    EXPECT_FALSE(file1.is_open());
    EXPECT_TRUE(file2.is_open());
    EXPECT_EQ(file2.data(), original_data);
    EXPECT_EQ(file2.size(), 256);
}

TEST_F(MmapFileTest, OpenNonexistent) {
    std::string path = test_dir_ + "/nonexistent.bin";

    EXPECT_THROW(
        MmapFile file(path, MmapFile::Mode::ReadOnly),
        WayyException
    );
}

TEST_F(MmapFileTest, CloseAndReopen) {
    std::string path = test_dir_ + "/close.bin";

    MmapFile file(path, MmapFile::Mode::Create, 100);
    EXPECT_TRUE(file.is_open());

    file.close();
    EXPECT_FALSE(file.is_open());

    file.open(path, MmapFile::Mode::ReadOnly);
    EXPECT_TRUE(file.is_open());
    EXPECT_EQ(file.size(), 100);
}