[ create a new paste ] login | about

Link: http://codepad.org/BYfnSnrV    [ raw code | fork ]

C, pasted on Dec 17:
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* Copyright (C) Hola 2012, 2013
 *
 * Welcome to TheCodeIL.com Challenge!
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>


/**
 * @brief            Copy a string. Allocate memory if need.
 *
 *   If buffer is not initialized (passed pointer to NULL as first parameter),
 *   str_cpy allocates buffer with @code len_src = strlen( src ) + 1 @endcode bytes of memory.
 *   
 *   If buffer is not valid pointer to memory block beginning, that leads to memory heap corruption.
 *   
 *   If src is not a valid string (or points to unallocated memory), it leads to undefined behaviuor.
 *   If src is NULL, the function deallocates dst.
 *   
 *   After execution if src is valid string, dst is allocated 
 *   with len_src bytes of memory and contains copy of src.
 *
 * @param[out] dst  Pointer to buffer where need to copy src.
 * @param[in]  src  String (null-terminated char array) that being copied.
 *
 * @return dst      destination
 */
char** str_cpy(char **dst, const char *src);

/**
 * @brief           Concatenate two strings.
 *
 *   If dst is not initialized (passed pointer to NULL as first parameter),
 *   or there is an empty string in dst, procedure just copies second string to dst using str_cpy.
 *   
 *   If buffer is not valid pointer to memory block beginning, that leads to memory heap corruption.
 *   
 *   If src is not a valid string (or points to unallocated memory), it leads to undefined behaviuor.
 *   If src is NULL pointer, dst will not be changed.
 *   
 *   After execution, if *dst and src were valid strings, buffer is allocated
 *   with (len_dst + len_src - 1) bytes of memory and contains null-terminated strcat(*dst, src).
 *
 * @param[in, out] dst  Pointer to buffer where the first part of string stores.
 * @param[in] src       The second part of string.
 *
 * @return dst     destination
 */
char** str_cat(char **dst, const char *src);

/**
 *	No need to implement
 */
void str_printf(char **dst, const char *fmt, ...);

/**
 *	No need to implement
 */
void str_free(char **str);


/**
 *  @return number of bytes of null-terminated string (with '\0')
 *     0, if not a pointer to char array passed (i.e. str = NULL)
 */
size_t str_len(const char *str);



int test00()
{
    char *s = NULL;
    str_cpy(&s, NULL);                             // check cpy NULL to NULL
    return s != NULL;
}


int test0()
{
    char *s = NULL;
    str_cat(&s, NULL);                             // check cat NULL to NULL
    return s != NULL;
}


int test1()
{
    int ans;
    char check[] = "Hola Hola";
    char *s = NULL;

    str_cpy(&s, check);                             // check allocation
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test2()
{
    int ans;
    char check[] = "Hola Hola";
    char *s = (char*)calloc(1, strlen(check)+1);

    str_cpy(&s, check);                             // check reallocation (needed more space)
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test3()
{
    int ans;
    char check[] = "Hola Hola";
    char *s = (char*)calloc(1, 1);

    str_cpy(&s, check);                             // simple str_cpy
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test4()
{
    int ans;
    char check[] = "Hola";
    char words[] = "Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cpy(&s, s);                                 // str_cpy with dst as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test5()
{
    int ans;
    char check[] = "Hola";
    char words[] = "Hola Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cpy(&s, s+5);                               // str_cpy with shifted dst as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test6()
{
    int ans;
    char check[] = "";
    char words[] = "Hola Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cpy(&s, "");                                // str_cpy with "" as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test7()
{
    char words[] = "Hola Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cpy(&s, NULL);                              // str_cpy with NULL as src
    return s != NULL;
}


int test8()
{
    int ans;
    char check[] = "Hola";
    char appnd[] = "Hola";
    char *s = NULL;

    str_cat(&s, appnd);                             // check allocation with str_cat
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test9()
{
    int ans;
    char check[] = "Hola";
    char appnd[] = "Hola";
    char *s = (char*)calloc(1, 1);

    str_cat(&s, appnd);                             // check reallocation with str_cat
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test10()
{
    int ans;
    char check[] = "Hola World";
    char words[] = "Hola";
    char appnd[] = " World";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cat(&s, appnd);                             // simple str_cat
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test11()
{
    int ans;
    char check[] = "Hola Hola ";
    char words[] = "Hola ";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cat(&s, s);                                 // str_cat with dst as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test12()
{
    int ans;
    char check[] = "Hola World World";
    char words[] = "Hola World";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cat(&s, s + 4);                             // str_cat with shifted dst as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test13()
{
    int ans;
    char check[] = "Hola";
    char words[] = "Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cat(&s, "");                                // str_cat with "" as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test14()
{
    int ans;
    char check[] = "Hola";
    char words[] = "Hola";
    char *s = (char*)calloc(1, strlen(words)+1);

    strcpy(s, words);
    str_cat(&s, NULL);                              // str_cat with NULL as src
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test15()                                        // complex test 1
{
    int ans;
    char check[] = "Hola World";
    char *s = NULL;

    str_cpy(&s, "Hola Hola");
    str_cpy(&s, s+5);
    str_cat(&s, " World");
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test16()                                        // complex test 2
{
    int ans;
    char check[] = "Hola ";
    char words[] = "Hola ";
    char *s = NULL;
    int i;

    str_cpy(&s, words);

    for ( i = 0; i < 5; ++i ) {
        str_cat(&s, s);
    }
    for ( i = 0; i < 31; ++i ) {
        str_cpy(&s, s+5);
    }
    ans = strcmp(s, check);
    str_free(&s);
    return ans;
}


int test17()
{
    int ans;
    char check[] = "World";
    char *s = NULL;                                 // nesting str_cpy with str_cpy
    ans = strcmp(*str_cpy(str_cpy(&s, "Hola "), "World"), check);
    str_free(&s);
    return ans;
}


int test18()
{
    int ans;
    char check[] = "Hola World";
    char *s = NULL;                                 // nesting str_cat with str_cpy
    ans = strcmp(*str_cat(str_cpy(&s, "Hola "), "World"), check);
    str_free(&s);
    return ans;
}


int test19()
{
    int ans;
    char check[] = "Hola World";
    char *s = NULL;                                 // nesting str_cat with str_cat
    ans = strcmp(*str_cat(str_cat(&s, "Hola "), "World"), check);
    str_free(&s);
    return ans;
}


int test20()
{
    int ans;
    char check[] = "World";
    char *s = NULL;                                 // nesting str_cpy with str_cat
    ans = strcmp(*str_cpy(str_cat(&s, "Hola "), "World"), check);
    str_free(&s);
    return ans;
}


int test21()
{
    int ans;
    char check[] = "World";
    char *s = NULL;
    char *d = NULL;
    ans = strcmp(*str_cpy(&s, *str_cpy(&d, "World")), check);
    str_free(&s);
    str_free(&d);
    return ans;
}


int test22()
{
    int ans;
    char check[] = "World";
    char *s = NULL;
    char *d = NULL;
    ans = strcmp(*str_cpy(&s, *str_cat(&d, "World")), check);
    str_free(&s);
    str_free(&d);
    return ans;
}


int test23()
{
    int ans;
    char check[] = "World";
    char *s = NULL;
    char *d = NULL;
    ans = strcmp(*str_cat(&s, *str_cpy(&d, "World")), check);
    str_free(&s);
    str_free(&d);
    return ans;
}


int test24()
{
    int ans;
    char check[] = "World";
    char *s = NULL;
    char *d = NULL;
    ans = strcmp(*str_cat(&s, *str_cat(&d, "World")), check);
    str_free(&s);
    str_free(&d);
    return ans;
}


int test25()
{
    int ans;
    char check[] = "Hola World";
    char *s = NULL;
    char *d = NULL;
    ans = strcmp(*str_cat(str_cpy(&s, "Hola "), *str_cpy(&d, "World")), check);
    str_free(&s);
    str_free(&d);
    return ans;
}




#define CHECK(x) \
    if ( !x ) printf("%s : succeed\n", #x); \
    else printf("%s : failed\n", #x);

int main(int argc, char *argv[])
{
    CHECK(test00());
    CHECK(test0());
    CHECK(test1());
    CHECK(test2());
    CHECK(test3());
    CHECK(test4());
    CHECK(test5());
    CHECK(test6());
    CHECK(test7());
    CHECK(test8());
    CHECK(test9());
    CHECK(test10());
    CHECK(test11());
    CHECK(test12());
    CHECK(test13());
    CHECK(test14());
    CHECK(test15());
    CHECK(test16());
    CHECK(test17());
    CHECK(test18());
    CHECK(test19());
    CHECK(test20());
    CHECK(test21());
    CHECK(test22());
    CHECK(test23());
    CHECK(test24());
    CHECK(test25());
    getchar();
    return 0;
}


Create a new paste based on this one


Comments: