forked from sameenjalal/Linux-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fs.c
201 lines (170 loc) · 4.25 KB
/
test_fs.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Use this to test your filesystem.
// Your program must make sure the filesystem is formatted first.
// You must also call open_fs() first.
// Any error will terminate the test.
#include "api.h"
#include <stdlib.h>
#include <stdio.h>
#define TEST_SET_SIZE 1000000
#define BLOCKS 800
#define DISK_NAME "ssjfs_disk"
void test_fs();
int main(){
format_fs(DISK_NAME, BLOCKS);
open_fs(DISK_NAME);
test_fs();
close_fs(DISK_NAME);
return 0;
}
void test_fs()
{
int return_value;
int file_number;
printf("Creating file 1\n");
return_value = file_create("/test1");
if(return_value == SUCCESS)
{
printf("Successfully created file1...\n\n");
}
else
{
printf("Could not create file1, failed test%d...\n\n", return_value);
return;
}
printf("Creating file 2\n");
return_value = file_create("/test2");
if(return_value == SUCCESS)
{
printf("Successfully created file2...\n\n");
}
else
{
printf("Could not create file2, failed test%d...\n\n", return_value);
return;
}
printf("Creating file 3\n");
return_value = file_create("/test3");
if(return_value == SUCCESS)
{
printf("Successfully created file3...\n\n");
}
else
{
printf("Could not create file3, failed test%d...\n\n", return_value);
return;
}
printf("Printing root dir...should see /test1 and /test2...\n");
file_printdir("/");
printf("\n\nCREATING directory\n");
return_value = file_mkdir("/test_dir");
if(return_value == SUCCESS)
{
printf("Successfully created dir...\n");
}
else
{
printf("Could not create dir, failed test...%d\n", return_value);
}
printf("\n\nCREATING file in the directory\n");
return_value = file_create("/test_dir/test_file");
if(return_value == SUCCESS)
{
printf("Successfully created test_file...\n");
}
else
{
printf("Could not create file, failed test...%d\n", return_value);
return;
}
printf("opening file /test_dir/test_file\n\n");
file_number = return_value = file_open("/test_dir/test_file");
if(return_value >= 0)
{
printf("Successfully opened file, file_number = %i...\n", return_value);
}
else
{
printf("Could not open file, failed test...%d\n", return_value);
return;
}
printf("Doing read/write test...\n");
printf("Populating array...\n");
// Generate some test data.
long unsigned test_array[TEST_SET_SIZE];
test_array[0] = 0;
test_array[1] = 1;
int i;
for(i=2; i<TEST_SET_SIZE; i++)
{
test_array[i] = test_array[i-1] + test_array[i-2];
}
printf("Writing array to file...\n");
for(i=0; i<TEST_SET_SIZE; i++)
{
return_value = file_write(file_number, &test_array[i], sizeof(long unsigned));
if(return_value != sizeof(long unsigned))
{
printf("Error while writing...%d\n", return_value);
return;
}
}
printf("Wrote to file...\n");
printf("lseeking back to beginning...\n");
return_value = file_lseek(file_number, 0, LSEEK_ABSOLUTE);
if(return_value == 0)
{
printf("lseek success...\n");
}
else
{
printf("lseek error...\n");
return;
}
long unsigned* restored_array = malloc(sizeof(long unsigned*)*TEST_SET_SIZE);
for(i=0; i<TEST_SET_SIZE; i++)
{
return_value = file_read(file_number, &restored_array[i], sizeof(long unsigned));
if(return_value != sizeof(long unsigned))
{
printf("Error while reading...\n");
return;
}
}
printf("Read from file...\n");
for(i=0; i<TEST_SET_SIZE; i++)
{
if(test_array[i] == restored_array[i])
{
//printf("Good...test_array[%i] = %lu, restored_array[%i] = %lu...\n", i, test_array[i], i, restored_array[i]);
}
else
{
printf("Error during compare...test_array[%i] = %lu, restored_array[%i] = %lu...\n", i, test_array[i], i, restored_array[i]);
return;
}
}
printf("Checked, array good...\n");
printf("Deleting file...\n");
return_value = file_delete("/test_dir/test_file");
if(return_value == SUCCESS)
{
printf("Successfully deleted file...\n");
}
else
{
printf("Error deleting file...\n");
return;
}
printf("Removing dir...\n");
return_value = file_rmdir("/test_dir");
if(return_value == SUCCESS)
{
printf("Successfully removed dir...\n");
}
else
{
printf("Error removing dir...\n");
return;
}
printf("Passed basic test...\n");
}