-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestA.c
150 lines (125 loc) · 7.19 KB
/
testA.c
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
142
143
144
145
146
147
148
149
150
#include "sources/myfs.h"
#include <stdio.h>
#include <string.h>
void red() {
printf("\033[1;31m");
}
void green() {
printf("\033[1;32m");
}
void white() {
printf("\033[1;0m");
}
int main(int argc, char const *argv[])
{
printf("creating root..\n");
mymkfs(10000);
// ############### test1: open the same directory ###############
myDIR* dirp1 = myopendir("root/test1");
myDIR* dirp2 = myopendir("root/test1");
int t1 = dirp1->fd;
int t2 = dirp2->fd;
int t = (t1 == t2);
if(t == 1)
{
green();
printf("myopendir test PASSED\n");
}else{
red();
printf("myopendir test FAILED\n");
}
myDIR* dirp3 = myopendir("root/test2");
myclosedir(dirp3);
myclosedir(dirp2);
myclosedir(dirp1);
// ############### test2: open the same file with myopen ###############
int fd = myopen("root/test1/file1", O_CREAT);
int testfd = myopen("root/test1/file1", O_CREAT); // O_CREAT does not matter here as the file is created and will be found before created.
int fd2 = myopen("root/test2/file2",O_CREAT);
int fd3 = myopen("root/test1/file2",0); // flag O_CREAT is not needed here (same reason as in testfd).
int i = (fd == testfd);
if(i == 1)
{
green();
printf("myopen test PASSED\n");
}else{
red();
printf("myopen test FAILED\n");
}
white();
// ############### test3: open the same file in different directories ###############
i = (fd2 == fd3);
if(i == 1)
{
green();
printf("different directory open file PASSED\n");
}else{
red();
printf("different directory open file FAILED\n");
}
white();
// ############### test4: closing file changing fd ###############
testfd = myclose(testfd);
i = (fd != testfd);
if(i == 1)
{
green();
printf("myclose test PASSED\n");
}else{
red();
printf("myclose test FAILED\n");
}
white();
// ############### test5: write, read and seek operation on files ###############
char* test = "this is a test\n";
int ptr = mywrite(fd2, test, 25);
char buf[25];
char buf2[25];
mylseek(fd2, -25, SEEK_CUR);
myread(fd2, buf, 25);
if(strcmp(buf, test) ==0)
{
green();
printf("read test PASSED\n");
}else
{
red();
printf("read test FAILED\n");
}
white();
mylseek(fd2,-15,SEEK_CUR);
myread(fd2, buf2, 15);
if(strcmp(buf2, "test\n") ==0)
{
green();
printf("seek test PASSED\n\n");
}else
{
red();
printf("seek test FAILED\n\n");
}
white();
// ############### test6: testing write big file ###############
char* test2 = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$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$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$test";
int newfd = myopen("root/test1/bigfile", O_CREAT);
char *bigbuf = malloc(8);
mywrite(newfd,test2,strlen(test2));
mylseek(newfd,-8,SEEK_CUR);
myread(newfd,bigbuf,8);
if(strcmp(bigbuf, "$$$$test") == 0)
{
green();
printf("seek test big file PASSED\n\n");
}else
{
red();
printf("seek test big file FAILED\n\n");
}
white();
printf("our fs looks like this:\n");
printdir("root");
printdir("test1");
printdir("test2");
mymount(NULL, "testfile.txt", NULL,NULL,NULL); // write current file system (that is on the memory) into testfile.txt for future use
return 0;
}