-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
223 lines (186 loc) · 5.22 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "libasm.h"
void test_strlen(void)
{
printf(BOLD "\nTesting ft_strlen\n" NC);
char *strings[] = {
"test12345",
"1",
"test",
"",
"\n",
"zzzzzzzzzzzzzzzzzzzzzzzz hey",
};
int numStrings = sizeof(strings) / sizeof(strings[0]);
for (int i = 0; i < numStrings; i++) {
int expected = strlen(strings[i]);
int result = ft_strlen(strings[i]);
printf(expected == result ? GREEN "OK" NC : RED "KO" NC);
printf("\t Expected: %d\tResult: %d\n", expected, result);
}
}
void test_strcpy(void)
{
printf(BOLD "\nTesting ft_strcpy\n" NC);
char *strings[] = {
"test12345",
"1",
"test",
"",
"zzzzzzzz",
};
int numStrings = sizeof(strings) / sizeof(strings[0]);
for (int i = 0; i < numStrings; i++) {
// Test return value
char dummy_dest[BUFFER_SIZE];
void *ref_ptr = strcpy(dummy_dest, strings[i]);
void *test_ptr = ft_strcpy(dummy_dest, strings[i]);
printf(ref_ptr == test_ptr ? GREEN "POINTER OK" NC : RED "POINTER KO" NC);
printf("\tExpected: %p\tResult: %p\n", ref_ptr, test_ptr);
// Test destination string
char ref_dest[BUFFER_SIZE];
char test_dest[BUFFER_SIZE];
strcpy(ref_dest, strings[i]);
ft_strcpy(test_dest, strings[i]);
printf(strcmp(ref_dest, test_dest) == 0 ? GREEN "COPY OK" NC : RED "COPY KO" NC);
printf("\t\tExpected: %s\tResult: %s\n", ref_dest, test_dest);
}
}
void test_strcmp(void)
{
printf(BOLD "\nTesting ft_strcmp\n" NC);
char *strings1[] = {
"test123456770797",
"1",
"test",
"",
"zzzzzzzz",
};
char *strings2[] = {
"test1234567",
"2",
"test2",
"",
"zzzz1zzz",
};
int numStrings = sizeof(strings1) / sizeof(strings1[0]);
for (int i = 0; i < numStrings; i++) {
int expected = strcmp(strings1[i], strings2[i]);
int result = ft_strcmp(strings1[i], strings2[i]);
printf(BOLD "%s" NC " vs "BOLD "%s" NC"\n", strings1[i], strings2[i]);
printf(expected == result ? GREEN "OK" NC : RED "KO" NC);
printf("\t Expected: %d\tResult: %d\n\n", expected, result);
}
}
void test_write(void)
{
printf(BOLD "\nTesting ft_write\n" NC);
char *strings[] = {
"test123456",
"1",
"test",
"",
"\0",
"zzzzzzzz",
};
int fds[] = {
-1,
open("test.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644),
10
};
int numStrings = sizeof(strings) / sizeof(strings[0]);
int numFds = sizeof(fds) / sizeof(fds[0]);
for (int i = 0; i < numStrings; i++) {
// Test return value and errno
printf(BOLD "\"%s\"\n" NC, strings[i]);
for (int j = 0; j < numFds; j++) {
errno = 0;
int ref_ret = write(fds[j], strings[i], strlen(strings[i]));
int ref_errno = errno;
errno = 0;
int test_ret = ft_write(fds[j], strings[i], strlen(strings[i]));
int test_errno = errno;
printf("fd: %d\t", fds[j]);
printf(ref_ret == test_ret ? GREEN "RETURN OK" NC : RED "RETURN KO" NC);
printf("\tExpected: %d\tResult: %d\n", ref_ret, test_ret);
printf(ref_errno == test_errno ? GREEN "\tERRNO OK" NC : RED "\tERRNO KO" NC);
printf("\tExpected: %d\tResult: %d\n", ref_errno, test_errno);
}
printf("\n");
}
// NULL pointer
printf(BOLD "NULL string\n\n" NC);
for (int j = 0; j < numFds; j++) {
errno = 0;
int ref_ret = write(fds[j], NULL, BUFFER_SIZE);
int ref_errno = errno;
errno = 0;
int test_ret = ft_write(fds[j], NULL, BUFFER_SIZE);
int test_errno = errno;
printf("fd: %d\t", fds[j]);
printf(ref_ret == test_ret ? GREEN "RETURN OK" NC : RED "RETURN KO" NC);
printf("\tExpected: %d\tResult: %d\n", ref_ret, test_ret);
printf(ref_errno == test_errno ? GREEN "\tERRNO OK" NC : RED "\tERRNO KO" NC);
printf("\tExpected: %d\tResult: %d\n", ref_errno, test_errno);
}
}
void test_read(void)
{
printf(BOLD "\nTesting ft_read\n" NC);
int fds[] = {
-1,
open("read.txt", O_RDONLY, 0644),
10
};
int numFds = sizeof(fds) / sizeof(fds[0]);
// Test return value and errno
for (int j = 0; j < numFds; j++) {
errno = 0;
char ref_buf[BUFFER_SIZE];
int ref_ret = read(fds[j], ref_buf, BUFFER_SIZE);
ref_buf[ref_ret == -1 ? 0 : ref_ret] = '\0';
int ref_errno = errno;
lseek(fds[j], 0, SEEK_SET); // Reset file pointer
errno = 0;
char test_buf[BUFFER_SIZE];
int test_ret = ft_read(fds[j], test_buf, BUFFER_SIZE);
test_buf[test_ret == -1 ? 0 : test_ret] = '\0';
int test_errno = errno;
printf("\nfd: %d\t", fds[j]);
printf(ref_ret == test_ret ? GREEN "RETURN OK" NC : RED "RETURN KO" NC);
printf("\tExpected: %d\tResult: %d\n\t", ref_ret, test_ret);
printf(ref_errno == test_errno ? GREEN "ERRNO OK" NC : RED "ERRNO KO" NC);
printf("\tExpected: %d\tResult: %d\n\t", ref_errno, test_errno);
printf(strcmp(ref_buf, test_buf) == 0 ? GREEN "COPY OK" NC : RED "COPY KO" NC);
printf("\t\tExpected: %s\tResult: %s\n", ref_buf, test_buf);
}
printf("\n");
}
void test_strdup(void)
{
printf(BOLD "\nTesting ft_strdup\n" NC);
char *strings[] = {
"test",
"1",
"0",
"",
"\0",
"zzzz",
};
int numStrings = sizeof(strings) / sizeof(strings[0]);
for (int i = 0; i < numStrings; i++) {
char *ref_ptr = strdup(strings[i]);
char *test_ptr = ft_strdup(strings[i]);
printf(strcmp(ref_ptr, test_ptr) == 0 ? GREEN "COPY OK" NC : RED "COPY KO" NC);
printf("\t\tExpected: %s\tResult: %s\n", ref_ptr, test_ptr);
}
}
int main(void)
{
test_strlen();
test_strcpy();
test_strcmp();
test_write();
test_read();
test_strdup();
return (0);
}