-
Notifications
You must be signed in to change notification settings - Fork 58
/
p-testwritefs3.cc
112 lines (77 loc) · 2.53 KB
/
p-testwritefs3.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
#include "u-lib.hh"
void process_main() {
printf("Starting testwritefs3 (assuming clean file system)...\n");
// read file
printf("%s:%d: read...\n", __FILE__, __LINE__);
int f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
char buf[200];
memset(buf, 0, sizeof(buf));
ssize_t n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "When piped a tiny voice hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
// create file
printf("%s:%d: create...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_WRITE);
assert_lt(f, 0);
assert_eq(f, E_NOENT);
f = sys_open("geisel.txt", OF_WRITE | OF_CREATE);
assert_gt(f, 2);
n = sys_write(f, "Why, girl, you're insane!\n"
"Elephants don't hatch chickadee eggs!\n", 64);
assert_eq(n, 64);
sys_close(f);
// read back
printf("%s:%d: read created...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 64);
assert_memeq(buf, "Why, girl, you're insane!\n"
"Elephants don't hatch chickadee eggs!\n", 64);
sys_close(f);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
int r = sys_sync(1);
assert_ge(r, 0);
// read back
printf("%s:%d: reread...\n", __FILE__, __LINE__);
f = sys_open("geisel.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 64);
assert_memeq(buf, "Why, girl, you're insane!\n"
"Elephants don't hatch chickadee eggs!\n", 64);
sys_close(f);
// read & write same file
f = sys_open("geisel.txt", OF_READ);
assert_gt(f, 2);
int wf = sys_open("geisel.txt", OF_WRITE);
assert_gt(wf, 2);
assert_ne(wf, f);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, "Why,", 4);
n = sys_write(wf, "Am I scaring you tonight?", 25);
assert_eq(n, 25);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 25);
assert_eq(n, 25);
assert_memeq(buf, " scaring you tonight?\nEle", 25);
n = sys_write(wf, "!", 1);
assert_eq(n, 1);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 5);
assert_eq(n, 5);
assert_memeq(buf, "phant", 5);
sys_close(f);
sys_close(wf);
printf("testwritefs3 succeeded.\n");
sys_exit(0);
}