-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.c
377 lines (332 loc) · 10.2 KB
/
main2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ihermell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/06 18:08:24 by ihermell #+# #+# */
/* Updated: 2014/11/07 12:03:44 by ihermell ### ########.fr */
/* */
/* ************************************************************************** */
#include "srcs/libft.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void test_memalloc()
{
int i;
unsigned char *tmp;
dprintf(1, "Memalloc ");
tmp = (unsigned char*)ft_memalloc(15);
i = -1;
while (++i < 15)
{
if (tmp[i] != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m La memoire n'a pas ete initialisee a 0.i\n");
return;
}
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_memdel()
{
void *tmp = malloc(50);
dprintf(1, "Memdel ");
ft_memdel(&tmp);
if (tmp != NULL) {
dprintf(1, "\x1b[31mFail\x1b[0m Le pointeur n'a pas ete mis a NULL.");
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strnew()
{
int i;
char *tmp;
dprintf(1, "Strnew ");
tmp = ft_strnew(15);
i = -1;
while (++i < 15)
{
if (tmp[i] != '\0') {
dprintf(1, "\x1b[31mFail\x1b[0m La chaine n'a pas ete initialisee a \\0.\n");
return;
}
}
if (tmp[i] != '\0') {
dprintf(1, "\x1b[31mFail\x1b[0m Le \\0 de fin n'a pas ete ajoute.\n");
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strdel()
{
char *tmp = malloc(50);
dprintf(1, "Strdel ");
ft_strdel(&tmp);
if (tmp != NULL) {
dprintf(1, "\x1b[31mFail\x1b[0m Le pointeur n'a pas ete mis a NULL.");
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strclr()
{
char *tmp = (char*)malloc(13);
int i = -1;
dprintf(1, "Strclr ");
strcpy(tmp, "Motherfucker");
ft_strclr(tmp);
while (++i < 13) {
if (tmp[i] != '\0') {
dprintf(1, "\x1b[31mFail\x1b[0m Les caracteres de la chaine n'ont pas ete assignes a 0.\n");
return;
}
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void char_to_upper(char *c)
{
*c = toupper(*c);
}
void test_striter()
{
char tmp[] = "MotherFucker";
dprintf(1, "Striter ");
ft_striter(tmp, &char_to_upper);
if (strcmp(tmp, "MOTHERFUCKER") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : MotherFucker\n f : char_to_toupper(char *c) *c = toupper(*c);\n\n Valeur attendue : MOTHERFUCKER\n Valeur obtenue : %s\n", tmp);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void char_to_index(unsigned int i, char *c)
{
*c = i % 10 + 48;
}
void test_striteri()
{
char tmp[] = "MotherFucker";
dprintf(1, "Striteri ");
ft_striteri(tmp, &char_to_index);
if (strcmp(tmp, "012345678901") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : MotherFucker\n f : char_to_index(unsigned int index, char *c)\n *c = i modulo 10 + 48;\n\n Valeur attendue : 012345678901\n Valeur obtenue : %s\n", tmp);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
char yolo(char c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
return (c);
}
void test_strmap()
{
char tmp[] = "MotherFucker";
char *dst;
dprintf(1, "Strmap ");
dst = ft_strmap(tmp, &yolo);
if (strcmp(tmp, "MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m La chaine originale a ete modifiee ; une nouvelle chaine fresh doit etre cree.\n");
return;
}
if (strcmp(dst, "MOTHERFUCKER") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : MotherFucker\n f : man toupper\n\n Valeur attendue : MOTHERFUCKER\n Valeur obtenue : %s\n", dst);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
char char_to_i(unsigned int i, char c)
{
(void) c;
return((int)i % 10 + 48);
}
void test_strmapi()
{
char tmp[] = "MotherFucker";
char *dst;
dprintf(1, "Strmapi ");
dst = ft_strmapi(tmp, &char_to_i);
if (strcmp(tmp, "MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m La chaine originale a ete modifiee ; une nouvelle chaine fresh doit etre cree.\n");
return;
}
if (strcmp(dst, "012345678901") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : MotherFucker\n f : char_to_index(unsigned int index, char c)\n return (i modulo 10 + 48);\n\n Valeur attendue : 012345678901\n Valeur obtenue : %s\n", dst);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strequ()
{
char str[4][15] = {"", " ", "Hello", "Motherfucker"};
int i = -1;
int j = -1;
dprintf(1, "Strequ ");
while (++i < 4) {
j = -1;
while (++j < 4) {
if (strcmp(str[i], str[j]) == 0 && ft_strequ(str[i], str[j]) != 1) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s1 : '%s'\n s2 : '%s'\n\n Valeur attendue : 1\n Valeur obtenue : %d\n", str[i], str[j],ft_strequ(str[i], str[j]));
return;
}
if (strcmp(str[i], str[j]) != 0 && ft_strequ(str[i], str[j]) != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s1 : '%s'\n s2 : '%s'\n\n Valeur attendue : 0\n Valeur obtenue : %d\n", str[i], str[j],ft_strequ(str[i], str[j]));
return;
}
}
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strnequ()
{
char str[4][15] = {"", " ", "Hello", "Motherfucker"};
int i = -1;
int j;
int k;
dprintf(1, "Strnequ ");
while (++i < 4) {
j = -1;
while (++j < 4) {
k = -1;
while (++k < 15) {
if (strncmp(str[i], str[j], k) == 0 && ft_strnequ(str[i], str[j], k) != 1) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s1 : '%s'\n s2 : '%s'\n k : %d\n\n Valeur attendue : 1\n Valeur obtenue : %d\n", str[i], str[j],k,ft_strnequ(str[i], str[j], k));
return;
}
if (strncmp(str[i], str[j], k) != 0 && ft_strnequ(str[i], str[j], k) != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s1 : '%s'\n s2 : '%s'\n k : %d\n\n Valeur attendue : 0\n Valeur obtenue : %d\n", str[i], str[j],k,ft_strnequ(str[i], str[j], k));
return;
}
}
}
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strsub()
{
char tmp[] = "Hello MotherFucker";
char *dst;
dprintf(1, "Strsub ");
dst = ft_strsub(tmp, 6, 12);
if (strcmp(tmp, "Hello MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m La chaine originale a ete modifiee ; vous devez retourne une copie fresh.\n");
return;
}
if (strcmp(dst, "MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : Hello MotherFucker\n start : 6\n len : 12\n\n Valeur attendue : MotherFucker\n Valeur obtenue : %s\n", dst);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strjoin()
{
char *tmp = "Hello ";
char *tmp2 = "MotherFucker";
char *dst;
dprintf(1, "Strjoin ");
dst = ft_strjoin(tmp, tmp2);
if (strcmp(dst, "Hello MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s1 : 'Hello ' s2 : 'MotherFucker'\n\n Valeur attendue : Hello MotherFucker\n Valeur obtenue : %s\n", dst);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strtrim()
{
char *tmp = "\n\n\t\t Hello\t\n MotherFucker \n\n \t\t";
char *tmp2 = "Hello MotherFucker";
dprintf(1, "Strtrim ");
char *dst = ft_strtrim(tmp);
char *dst2 = ft_strtrim(tmp2);
if (strcmp(dst, "Hello\t\n MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : \n\n\t\t Hello\t\n MotherFucker \n\n \t\t\n\n Valeur attendue : Hello\t\n MotherFucker\n Valeur obtenue : %s\n", dst);
return;
}
if (strcmp(dst2, "Hello MotherFucker") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : Hello MotherFucker\n\n Valeur attendue : Hello MotherFucker\n Valeur obtenue : %s\n", dst2);
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_strsplit()
{
char *tmp = "*salut**les*koupins";
char *tmp2 = "salut**les*koupins*";
char **tab;
char **tab2;
dprintf(1, "Strsplit ");
tab = ft_strsplit(tmp, '*');
tab2 = ft_strsplit(tmp2, '*');
if (strcmp(tab[0], "salut") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : *salut**les*koupins\n\n Valeur attendue (1er element du tableau): salut Valeur obtenue : %s\n", tab[0]);
return;
}
if (strcmp(tab[1], "les") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : *salut**les*koupins\n\n Valeur attendue (2er element du tableau): les Valeur obtenue : %s\n", tab[1]);
return;
}
if (strcmp(tab[2], "koupins") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : *salut**les*koupins\n\n Valeur attendue (3er element du tableau): koupins Valeur obtenue : %s\n", tab[2]);
return;
}
if (tab[3] != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m Le tableau n'est pas termine par 0.n");
return;
}
if (strcmp(tab2[0], "salut") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : salut**les*koupins*\n\n Valeur attendue (1er element du tableau): salut Valeur obtenue : %s\n", tab2[0]);
return;
}
if (strcmp(tab2[1], "les") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : salut**les*koupins*\n\n Valeur attendue (2er element du tableau): les Valeur obtenue : %s\n", tab2[1]);
return;
}
if (strcmp(tab2[2], "koupins") != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n s : salut**les*koupins*\n\n Valeur attendue (3er element du tableau): koupins Valeur obtenue : %s\n", tab2[2]);
return;
}
if (tab2[3] != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m Le tableau n'est pas termine par 0.n");
return;
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
void test_itoa()
{
int ints[4] = {0, -3, 256, -87459};
char str[4][10] = {"0", "-3", "256", "-87459"};
int i = -1;
char *tmp;
dprintf(1, "Itoa ");
while (++i < 4) {
tmp = ft_itoa(ints[i]);
if (strcmp(tmp, str[i]) != 0) {
dprintf(1, "\x1b[31mFail\x1b[0m\n Params:\n int : %d\n\n Valeur attendue : %s\n Valeur obtenue : %s\n", ints[i], str[i], tmp);
return;
}
}
dprintf(1, "\x1b[32mOK\x1b[0m\n");
}
int main()
{
test_memalloc();
test_memdel();
test_strnew();
test_strdel();
test_strclr();
test_striter();
test_striteri();
test_strmap();
test_strmapi();
test_strequ();
test_strnequ();
test_strsub();
test_strjoin();
test_strtrim();
test_strsplit();
test_itoa();
return (0);
}