12 {
14
15
16
17
18
19 {
23 "ramfs_system_test: open /hello.txt failed");
25
26 const char kMsg[] = "Hello, ramfs!";
27 auto write_result =
vfs::Write(file, kMsg,
sizeof(kMsg) - 1);
28 EXPECT_TRUE(write_result.has_value(),
"ramfs_system_test: write failed");
29 EXPECT_EQ(write_result.value(),
sizeof(kMsg) - 1,
30 "ramfs_system_test: write byte count mismatch");
31 klog::Info(
"ramfs_system_test: wrote {} bytes", write_result.value());
32
33
36 "ramfs_system_test: seek to start failed");
37 EXPECT_EQ(seek_result.value(),
static_cast<uint64_t
>(0),
38 "ramfs_system_test: seek position mismatch");
39
40
41 char buf[64] = {};
42 auto read_result =
vfs::Read(file, buf,
sizeof(buf) - 1);
43 EXPECT_TRUE(read_result.has_value(),
"ramfs_system_test: read failed");
44 EXPECT_EQ(read_result.value(),
sizeof(kMsg) - 1,
45 "ramfs_system_test: read byte count mismatch");
47 "ramfs_system_test: read content mismatch");
48 klog::Info(
"ramfs_system_test: read back: {}", buf);
49
51 }
52
53
54 {
57 "ramfs_system_test: re-open for seek test failed");
59
60
63 "ramfs_system_test: seek to offset 7 failed");
64 EXPECT_EQ(seek_result.value(),
static_cast<uint64_t
>(7),
65 "ramfs_system_test: seek offset 7 mismatch");
66
67 char buf[32] = {};
68 auto read_result =
vfs::Read(file, buf, 5);
70 "ramfs_system_test: partial read failed");
71
72 EXPECT_EQ(read_result.value(),
static_cast<size_t>(5),
73 "ramfs_system_test: partial read count mismatch");
75 "ramfs_system_test: partial read content mismatch");
76 klog::Info(
"ramfs_system_test: partial read from offset 7: {}", buf);
77
79 }
80
81
82 {
85 "ramfs_system_test: mkdir /testdir failed");
86 klog::Info(
"ramfs_system_test: mkdir /testdir ok");
87
88
89 auto inner =
93 "ramfs_system_test: open /testdir/inner.txt failed");
95
96
100 "ramfs_system_test: open /testdir as dir failed");
101 vfs::File* dir_file = dir_file_result.value();
102
104 auto readdir_result =
vfs::ReadDir(dir_file, entries, 8);
106 "ramfs_system_test: readdir failed");
107
108 EXPECT_GT(readdir_result.value(),
static_cast<size_t>(2),
109 "ramfs_system_test: readdir should return > 2 entries");
110 klog::Info(
"ramfs_system_test: readdir returned {} entries",
111 readdir_result.value());
112
114 }
115
116
117 {
120 "ramfs_system_test: unlink /hello.txt failed");
121 klog::Info(
"ramfs_system_test: unlink /hello.txt ok");
122
125 "ramfs_system_test: /hello.txt should be gone after unlink");
126 klog::Info(
"ramfs_system_test: confirmed /hello.txt no longer exists");
127 }
128
129
130 {
131
132 auto unlink_result =
vfs::Unlink(
"/testdir/inner.txt");
134 "ramfs_system_test: unlink /testdir/inner.txt failed");
135
138 "ramfs_system_test: rmdir /testdir failed");
139 klog::Info(
"ramfs_system_test: rmdir /testdir ok");
140 }
141
142
143 {
148 EXPECT_TRUE(f1.has_value(),
"ramfs_system_test: open fileA failed");
149 EXPECT_TRUE(f2.has_value(),
"ramfs_system_test: open fileB failed");
150
151 const char kDataA[] = "AAAA";
152 const char kDataB[] = "BBBB";
155
158
159 char buf1[8] = {};
160 char buf2[8] = {};
163
165 "ramfs_system_test: fileA data corrupted by fileB");
167 "ramfs_system_test: fileB data corrupted by fileA");
168 klog::Info(
"ramfs_system_test: two files are independent");
169
174 }
175
176 klog::Info(
"ramfs_system_test: all tests passed");
177 return true;
178}
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto Read(File *file, void *buf, size_t count) -> Expected< size_t >
从文件读取数据
auto Write(File *file, const void *buf, size_t count) -> Expected< size_t >
向文件写入数据
auto Open(const char *path, OpenFlags flags) -> Expected< File * >
打开文件
auto ReadDir(File *file, DirEntry *dirent, size_t count) -> Expected< size_t >
读取目录内容
auto MkDir(const char *path) -> Expected< void >
创建目录
auto Close(File *file) -> Expected< void >
关闭文件
auto Unlink(const char *path) -> Expected< void >
删除文件
auto RmDir(const char *path) -> Expected< void >
删除目录
auto Seek(File *file, int64_t offset, SeekWhence whence) -> Expected< uint64_t >
调整文件偏移量
File — 打开的文件实例(每次 open 产生一个)
#define EXPECT_TRUE(cond, msg)
#define EXPECT_FALSE(cond, msg)
#define EXPECT_GT(val1, val2, msg)
#define EXPECT_EQ(val1, val2, msg)