-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
428 lines (368 loc) · 9.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include <stdlib.h>
# include <fcntl.h>
int ft_bzero(void *s, size_t n);
char* ft_strcat(char * s1, const char * s2);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_puts(const char *s);
size_t ft_strlen(const char *s);
void * ft_memset(void *b, int c, size_t len);
void * ft_memcpy(void * dst, const void * src, size_t n);
char * ft_strdup(const char *s1);
void ft_cat(int fd);
char * ft_strchr(const char *s, int c);
char * ft_strrchr(const char *s, int c);
char * ft_strcpy(char *dst, const char *src);
int ft_isupper(int c);
int ft_islower(int c);
# define MIN ( -4200 )
# define MAX ( 5000 )
# define STR_MAX_SIZE ( 10000 )
# define random(min, max)( ( rand() % ( max - min ) ) + min )
static size_t random_str( char *buff )
{
size_t len = random( 1, STR_MAX_SIZE );
int i;
for ( i = 0; i < len; ++i )
buff[i] = random( 1, 256 );
buff[i] = 0;
return ( len );
}
void printmem(char *mem, int size)
{
int i = 0;
while(i < size)
printf("[%c]", mem[i++]);
printf("\n");
}
void check_puts(char *s)
{
puts(s);
ft_puts(s);
printf("\033[32mOk\n\033[0m");
}
void check_isDigit()
{
int i = 0;
printf( "--------\nTest ft_isdigit:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isdigit( i ) != isdigit( i ) )
{
printf("[%d] -> result: %d, expected result: %d\n", i, ft_isdigit( i ), isdigit( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isAlpha()
{
int i = 0;
printf( "--------\nTest ft_isalpha:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isalpha( i ) != isalpha( i ) )
{
printf("[%d] -> result: %d, expected result: %d\n", i, ft_isalpha( i ), isalpha( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isAlnum()
{
int i = 0;
printf( "--------\nTest ft_isalnum:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isalnum( i ) != isalnum( i ) )
{
printf("[%d] -> result: %d, expected result: %d\n", i, ft_isalnum( i ), isalnum( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isAscii()
{
int i = 0;
printf( "--------\nTest ft_isascii:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isascii( i ) != isascii( i ) )
{
printf("[%d] -> result: %d, expected result: %d\n", i, ft_isascii( i ), isascii( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isprint()
{
int i = 0;
printf( "--------\nTest ft_isprint:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isprint( i ) != isprint( i ) )
{
printf("[%d] -> result: %d, expected result: %d\n", i, ft_isprint( i ), isprint( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_tolower()
{
int i = 0;
printf( "--------\nTest ft_tolower:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_tolower( i ) != tolower( i ) )
{
printf("[%d] -> result: %c, expected result: %c\n", i, ft_tolower( i ), tolower( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_toupper()
{
int i = 0;
printf( "--------\nTest ft_toupper:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_toupper( i ) != toupper( i ) )
{
printf("[%d] -> result: %c, expected result: %c\n", i, ft_toupper( i ), toupper( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_strchr()
{
printf( "--------\nTest ft_strchr:\n" );
printf("strchr: %s, ft_strchr: %s \n", strchr("yolo", 'y'), ft_strchr("yolo", 'y'));
printf("strchr: %s, ft_strchr: %s \n", strchr("abcdef", 'c'), ft_strchr("abcdef", 'c'));
printf("\033[32mOk\n\033[0m");
}
void check_strrchr()
{
printf( "--------\nTest ft_strrchr:\n" );
printf("strrchr: %s, ft_strrchr: %s \n", strrchr("yoloooooooooooo loilol kikou", 'y'), ft_strrchr("yoloooooooooooo loilol kikou", 'y'));
printf("strrchr: %s, ft_strrchr: %s \n", strrchr("mama just killed a man", 'k'), ft_strrchr("mama just killed a man", 'k'));
printf("\033[32mOk\n\033[0m");
}
void check_strlen()
{
int i = 0;
printf( "--------\nTest ft_strlen:\n" );
char str[STR_MAX_SIZE];
size_t len;
size_t ft_len;
for ( i = MIN; i < MAX; i++ )
{
len = random_str( str );
if ( ( ft_len = ft_strlen( str ) ) != len )
{
printf("[%s] -> result: %d, expected result: %d\n", str, ft_len, len );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isLower()
{
int i = 0;
printf( "--------\nTest ft_islower:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_islower( i ) != islower( i ) )
{
printf("[%d] -> result: %c, expected result: %c\n", i, ft_islower( i ), islower( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
void check_isupper()
{
int i = 0;
printf( "--------\nTest ft_isupper:\n" );
for ( i = MIN; i < MAX; i++ )
{
if ( ft_isupper( i ) != isupper( i ) )
{
printf("[%d] -> result: %c, expected result: %c\n", i, ft_isupper( i ), isupper( i ) );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
}
int main()
{
check_isupper();
check_isLower();
puts("--------\nTest ft_puts:\n");
check_puts("great succes !");
check_puts(0);
check_isDigit();
check_isAlpha();
check_isAlnum();
check_isAscii();
check_isprint();
check_tolower();
check_toupper();
int i = 0;
srand( 42 );
printf( "--------\nTest ft_bzero:\n" );
char str_bezro[STR_MAX_SIZE];
char str_ft_bezro[STR_MAX_SIZE];
size_t len_bzero;
for ( i = MIN; i < MAX; i++ )
{
len_bzero = random_str( str_bezro );
len_bzero -= random( 0, len_bzero );
strcpy( str_ft_bezro, str_bezro );
ft_bzero( str_ft_bezro, len_bzero );
bzero( str_bezro, len_bzero );
if ( memcmp( str_ft_bezro, str_bezro, len_bzero ) != 0 )
{
printf("result: \n%s\nexpected result: \n%s\n", str_ft_bezro, str_bezro );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
printf( "--------\nTest ft_strcat:\n" );
char str_strcat[STR_MAX_SIZE * 2];
char str_ft_strcat[STR_MAX_SIZE * 2];
char str_strcat_s2[STR_MAX_SIZE];
char *ret_strcat;
char *ret_ft_strcat;
size_t len_strcat;
size_t len_strcat_s2;
for ( i = MIN; i < MAX; i++ )
{
len_strcat_s2 = random_str( str_strcat_s2 );
len_strcat = random_str( str_strcat );
strcpy( str_ft_strcat, str_strcat );
ret_ft_strcat = ft_strcat( str_ft_strcat, str_strcat_s2 );
ret_strcat = strcat( str_strcat, str_strcat_s2 );
if ( memcmp( str_strcat, str_ft_strcat, len_strcat + len_strcat_s2 ) != 0 )
{
printmem( str_strcat, len_strcat );
printmem( str_ft_strcat, len_strcat );
printf("result: \n%s\nexpected result: \n%s\n", str_strcat, str_ft_strcat );
exit( 0 );
}
if ( memcmp( ret_strcat, ret_ft_strcat, len_strcat + len_strcat_s2 ) != 0 )
{
printmem( ret_strcat, len_strcat );
printmem( ret_ft_strcat, len_strcat );
printf("result: \n%s\nexpected result: \n%s\n", ret_strcat, ret_ft_strcat );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
check_strlen();
check_strchr();
check_strrchr();
printf( "--------\nTest ft_memset:\n" );
char str_memset[STR_MAX_SIZE];
char str_ft_memset[STR_MAX_SIZE];
size_t len_memset;
for ( i = MIN; i < MAX; i++ )
{
len_memset = random_str( str_memset );
len_memset -= random( 0, len_memset );
strcpy( str_ft_memset, str_memset );
ft_memset( str_ft_memset, i, len_memset );
memset( str_memset, i, len_memset );
if ( memcmp( str_ft_memset, str_memset, len_bzero ) != 0 )
{
printf("result: \n%s\nexpected result: \n%s\n", str_ft_memset, str_memset );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
printf( "--------\nTest ft_memcpy:\n" );
char str_memcpy[STR_MAX_SIZE];
char str_ft_memcpy[STR_MAX_SIZE];
char str_memcpy_s2[STR_MAX_SIZE];
size_t len_memcpy;
void *ret_memcpy;
void *ret_ft_memcpy;
for ( i = MIN; i < MAX; i++ )
{
len_memcpy = random_str( str_memcpy_s2 );
ret_ft_memcpy = ft_memcpy( str_ft_memcpy, str_memcpy_s2, len_memcpy );
ret_memcpy = memcpy( str_memcpy, str_memcpy_s2, len_memcpy );
if ( memcmp( str_ft_memcpy, str_memcpy, len_memcpy ) != 0 )
{
printf("result: \n%s\nexpected result: \n%s\n", str_ft_memcpy, str_memcpy );
exit( 0 );
}
if ( ret_ft_memcpy != str_ft_memcpy )
{
printf("result: \n%d\nexpected result: \n%d\n", (size_t)ret_ft_memcpy, (size_t)str_ft_memcpy );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
printf( "--------\nTest ft_strdup:\n" );
char str_strdup[STR_MAX_SIZE];
size_t len_strdup;
char *ret_ft_strdup;
char *ret_strdup;
for ( i = MIN; i < MAX; i++ )
{
len_strdup = random_str( str_strdup );
ret_ft_strdup = ft_strdup( str_strdup );
ret_strdup = strdup( str_strdup );
if ( memcmp( ret_ft_strdup, ret_strdup, len_strdup ) != 0 )
{
printf("result: \n%s\nexpected result: \n%s\n", ret_ft_strdup, ret_strdup );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
printf( "--------\nTest ft_strcpy:\n" );
char str_ft_strcpy[STR_MAX_SIZE];
char str_strcpy[STR_MAX_SIZE];
char str_strcpy_s2[STR_MAX_SIZE];
char *ret_strcpy;
char *ret_ft_strcpy;
int len_strcpy;
for ( i = MIN; i < MAX; i++ )
{
len_strcpy = random_str( str_strcpy_s2 );
ret_strcpy = strcpy( str_strcpy, str_strcpy_s2 );
ret_ft_strcpy = ft_strcpy( str_ft_strcpy, str_strcpy_s2 );
if ( memcmp( str_ft_strcpy, str_strcpy, len_strcpy ) != 0 )
{
printf("result: \n%s\n, expected result: \n%s\n", str_ft_strcpy, str_strcpy );
exit( 0 );
}
if ( memcmp( ret_strcpy, ret_ft_strcpy, len_strcpy ) != 0 )
{
printf("result: \n%s\n, expected result: \n%s\n", ret_strcpy, ret_ft_strcpy );
exit( 0 );
}
}
printf("\033[32mOk\n\033[0m");
printf( "--------\nTest ft_cat:\n" );
ft_cat( 4 );
ft_cat( 8 );
ft_cat( -4 );
ft_cat( open( "ft_bzero.s", O_RDONLY ) );
ft_cat( open( "Makefile", O_RDONLY ) );
return 0;
}