-
Notifications
You must be signed in to change notification settings - Fork 0
/
p-testmemfs.cc
141 lines (100 loc) · 2.93 KB
/
p-testmemfs.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
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
#include "u-lib.hh"
void process_main() {
int f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
char buf[200];
ssize_t n = sys_read(f, buf, 1);
assert_eq(n, 1);
assert_memeq(buf, "W", 1);
n = sys_read(f, buf, 2);
assert_eq(n, 2);
assert_memeq(buf, "he", 2);
n = sys_read(f, buf, 3);
assert_eq(n, 3);
assert_memeq(buf, "n p", 3);
int f2 = sys_open("emerson.txt", OF_READ);
assert(f2 > 2 && f2 != f);
n = sys_read(f2, buf, 5);
assert_eq(n, 5);
assert_memeq(buf, "When ", 5);
n = sys_read(f, buf, 5);
assert_eq(n, 5);
assert_memeq(buf, "iped ", 5);
int f3 = 3;
while (f3 == f || f3 == f2) {
++f3;
}
int r = sys_dup2(f, f3);
assert_ge(r, 0);
n = sys_read(f, buf, 10);
assert_eq(n, 10);
assert_memeq(buf, "a tiny voi", 10);
n = sys_read(f3, buf, 10);
assert_eq(n, 10);
assert_memeq(buf, "ce hard by", 10);
n = sys_read(f, buf, 10);
assert_eq(n, 10);
assert_memeq(buf, ",\nGay and ", 10);
r = sys_close(f);
assert_eq(r, 0);
n = sys_read(f, buf, 10);
assert_eq(n, E_BADF);
n = sys_read(f2, buf, 10);
assert_eq(n, 10);
assert_memeq(buf, "piped a ti", 10);
n = sys_read(f3, buf, 10);
assert_eq(n, 10);
assert_memeq(buf, "polite, a ", 10);
n = sys_read(f3, buf, sizeof(buf));
assert_eq(n, 79);
assert_memeq(buf, "cheerful cry,\n", 14);
assert_memeq(buf + 72, "throat\n", 7);
n = sys_read(f3, buf, sizeof(buf));
assert_eq(n, 0);
n = sys_read(f3, buf, sizeof(buf));
assert_eq(n, 0);
r = sys_close(f2);
assert_eq(r, 0);
r = sys_close(f3);
assert_eq(r, 0);
r = sys_open(nullptr, OF_READ);
assert_eq(r, E_FAULT);
extern char end[];
char* page = reinterpret_cast<char*>(
round_up(reinterpret_cast<uintptr_t>(end), PAGESIZE)
) + PAGESIZE;
r = sys_page_alloc(page);
assert_eq(r, 0);
strcpy(page, "emerson.txt");
f = sys_open(page, OF_READ);
assert_gt(f, 2);
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, "When", 4);
memcpy(page + PAGESIZE - 11, "emerson.txt", 11);
f2 = sys_open(page + PAGESIZE - 11, OF_READ);
assert_eq(f2, E_FAULT);
// Some tests for write
char write_buf[200];
int w = sys_open("test.txt", OF_WRITE | OF_CREATE);
assert_gt(w, 2);
strcpy(write_buf, "JAMES");
int res = sys_write(w, write_buf, 4);
assert_eq(res, 4);
res = sys_read(w, buf, 4);
assert_eq(res, E_BADF);
int r1 = sys_open("test.txt", OF_READ);
assert_gt(r1, w);
res = sys_read(r1, buf, 4);
assert_eq(res, 4);
assert_memeq(buf, "JAME", 4);
res = sys_write(r1, write_buf, 4);
assert_eq(res, E_BADF);
res = sys_write(w, write_buf + 4, 1);
assert_eq(res, 1);
res = sys_read(r1, buf, 1);
assert_eq(res, 1);
assert_memeq(buf, "S", 1);
console_printf("testmemfs succeeded.\n");
sys_exit(0);
}