-
Notifications
You must be signed in to change notification settings - Fork 2
/
testB.c
156 lines (146 loc) · 7.63 KB
/
testB.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
#include "sources/mystdio.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[])
{
mymount("testfile.txt", NULL, NULL, NULL, NULL); // loads "testfile.txt" to memory
printfs_mem();
// ############### test1: myfopen actual data allocation on a small file ###############
myFILE *fptr1 = myfopen("root/test1/file2", "r+");
if(strcmp(fptr1->data, "this is a test\n") == 0)
{
green();
printf("myfopen small file PASSED\n");
}else
{
red();
printf("my fopen small file FAILED\n");
}
white();
myfclose(fptr1);
// ############### test2: myfopen actual data allocation on a big file ###############
fptr1 = myfopen("root/test1/bigfile", "r+");
char* test2 = "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$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$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$test";
if(strcmp(fptr1->data, test2) == 0)
{
green();
printf("myfopen big file PASSED\n");
}else
{
red();
printf("my fopen big file FAILED\n");
}
white();
myfclose(fptr1);
// ############### test3: myfread ###############
fptr1 = myfopen("root/test1/bigfile", "r+");
char testres[5];
myfread(testres,1,4, fptr1);
if(strcmp("$$$$", testres) == 0)
{
green();
printf("myfread PASSED\n");
}else
{
red();
printf("myfread FAILED\n%s == %s\n", "$$$$", testres);
}
white();
// ############### test3: myflseek ###############
myfseek(fptr1, strlen(test2)-4, SEEK_SET);
char *testres2 = malloc(6);
myfread(testres2,1,4, fptr1);
if(strcmp("test", testres2) == 0)
{
green();
printf("myfseek SET PASSED\n");
}else
{
red();
printf("myfseek SET FAILED\n");
}
white();
myfseek(fptr1, -5, SEEK_END);
myfread(testres2,1,5, fptr1);
if(strcmp("$test", testres2) == 0)
{
green();
printf("myfseek END PASSED\n");
}else
{
red();
printf("myfseek END FAILED\n");
}
free(testres2);
white();
myfclose(fptr1);
// ############### test4: myfopen write mode ###############
fptr1 = myfopen("root/test1/file2", "w");
if(strcmp(" ", fptr1->data) == 0)
{
green();
printf("myfopen write mode PASSED\n");
}else
{
red();
printf("myfopen write mode FAILED\na%sa", fptr1->data);
}
white();
myfclose(fptr1);
// ############### test5: myfwrite ###############
myFILE *fptr5 = myfopen("root/test1/file2", "w");
char* test5 = "test5";
myfwrite(test5, 1, 6, fptr5);
myfseek(fptr5, -26, SEEK_CUR);
char* testres3 = malloc(6);
myfread(testres3, 1, 6, fptr5);
if(strcmp(test5, testres3) == 0)
{
green();
printf("myfwrite write mode PASSED\n");
}else
{
red();
printf("myfwrite write mode FAILED\n");
}
white();
myfclose(fptr5);
// ############### test6: myfprintf ###############
myFILE *fptr6 = myfopen("root/test1/file2", "r+");
myfprintf(fptr6, "this %d was an int and this is %c char", 222222, 'a');
if(strcmp("this 222222 was an int and this is a char", fptr6->data) == 0)
{
green();
printf("myfprintf PASSED\n");
}else
{
red();
printf("myfprintf FAILED\n%s == %s\n", "this 222222 was an int and this is a char", fptr6->data);
}
white();
// ############### test7: myfscanf ###############
int x;
char a;
myfscanf(fptr6, "this %d was an int and this is %c char", &x, &a);
if(x==222222&&a=='a')
{
green();
printf("myfscanf PASSED\n");
}else
{
red();
printf("myfscanf FAILED\n%d == %d\n%c == %c\n", x, 222222, a, 'a');
}
myfclose(fptr6);
destroyfs();
return 0;
}