-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_test.c
More file actions
94 lines (82 loc) · 1.98 KB
/
file_test.c
File metadata and controls
94 lines (82 loc) · 1.98 KB
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
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"
#define BSIZE 512
#define NINDIRECT (BSIZE / sizeof(uint))
#define NDBLINDIRECT ((NINDIRECT) * (NINDIRECT))
#define NUM_BYTES ((NDBLINDIRECT) * (BSIZE))
#define NUM_TEST3 2
char buf[NUM_BYTES], buf2[NUM_BYTES];
char filename[16] = "test_file0";
const int len = 10;
void failed(const char *msg)
{
printf(1, msg);
printf(1, "Test failed!!\n");
exit();
}
void test1(int first)
{
int fd;
if (first)
printf(1, "Test 1: Write %d bytes\n", NUM_BYTES);
fd = open(filename, O_CREATE | O_WRONLY);
if (fd < 0)
failed("File open error\n");
if (write(fd, buf, NUM_BYTES) < 0)
failed("File write error\n");
if (close(fd) < 0)
failed("File close error\n");
if (first)
printf(1, "Test 1 passed\n\n");
}
void test2(int first)
{
int i, fd;
for (i = 0; i < NUM_BYTES; i++)
buf2[i] = 0;
if (first)
printf(1, "Test 2: Read %d bytes\n", NUM_BYTES);
fd = open(filename, O_RDONLY);
if (fd < 0)
failed("File open error\n");
if (read(fd, buf2, NUM_BYTES) < 0)
failed("File read error\n");
for (i = 0; i < NUM_BYTES; i++) {
if (buf2[i] != (i % 26) + 'a') {
printf(1, "%dth character, expected %c, found %c\n", i, (i % 26) + 'a', buf2[i]);
failed("");
}
}
if (close(fd) < 0)
failed("File close error\n");
if (unlink(filename) < 0)
failed("File unlink error\n");
if ((fd = open(filename, O_RDONLY)) >= 0)
failed("File not erased\n");
if (first)
printf(1, "Test 2 passed\n\n");
}
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < NUM_BYTES; i++)
buf[i] = (i % 26) + 'a';
test1(1);
test2(1);
printf(1, "Test 3: repeating test 1 & 2\n");
for (i = 0; i < NUM_TEST3; i++)
{
printf(1, "Loop %d: ", i + 1);
filename[len - 1] = (i % 10) + '0';
printf(1, "1.. ");
test1(0);
printf(1, "2.. ");
test2(0);
printf(1, "ok\n");
}
printf(1, "Test 3 passed\n");
printf(1, "All tests passed!!\n");
exit();
}