-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesystemTest.cc
88 lines (72 loc) · 2.16 KB
/
FilesystemTest.cc
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
#include <unistd.h>
#include "Filesystem.hh"
#include "Strings.hh"
#include "UnitTest.hh"
using namespace std;
int main(int argc, char* argv[]) {
{
auto results = list_directory(".");
#ifdef WINDOWS
expect_eq(1, results.count("FilesystemTest.exe"));
#else
expect_eq(1, results.count("FilesystemTest"));
#endif
expect_eq(0, results.count("the-test-will-fail-if-this-file-exists"));
expect_eq(0, results.count("."));
expect_eq(0, results.count(".."));
}
{
string filename("FilesystemTest-data");
string symlink_name("FilesystemTest-link");
try {
string data("0123456789");
save_file(filename, data);
expect_eq("0123456789", load_file(filename));
expect_eq(data.size(), (size_t)stat(filename).st_size);
#ifndef WINDOWS
expect_eq(data.size(), (size_t)lstat(filename).st_size);
symlink(filename.c_str(), symlink_name.c_str());
expect_eq(data.size(), (size_t)stat(symlink_name).st_size);
expect_eq(filename.size(), (size_t)lstat(symlink_name).st_size);
#endif
{
auto f = fopen_unique(filename, "r+b");
expect_eq(data.size(), (size_t)fstat(f.get()).st_size);
expect_eq(data.size(), (size_t)fstat(fileno(f.get())).st_size);
fseek(f.get(), 5, SEEK_SET);
fwrite(data.data(), 1, data.size(), f.get());
}
#ifndef WINDOWS
expect_eq(data.size() + 5, (size_t)stat(symlink_name).st_size);
expect_eq(data.substr(0, 5) + data, load_file(symlink_name));
#endif
} catch (...) {
remove(filename.c_str());
#ifndef WINDOWS
remove(symlink_name.c_str());
#endif
throw;
}
remove(filename.c_str());
#ifndef WINDOWS
remove(symlink_name.c_str());
#endif
}
#ifndef WINDOWS
{
auto p = pipe();
writex(p.second, "omg", 3);
expect_eq("omg", readx(p.first, 3));
Poll poll;
poll.add(p.first, POLLIN);
poll.add(p.second, POLLOUT);
unordered_map<int, short> expected_result({{p.second, POLLOUT}});
expect_eq(expected_result, poll.poll());
poll.remove(p.first, true);
poll.remove(p.second, true);
}
#endif
// TODO: test get_user_home_directory
printf("%s: all tests passed\n", argv[0]);
return 0;
}