File: string.c

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (330 lines) | stat: -rw-r--r-- 7,869 bytes parent folder | download | duplicates (3)
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
#include <string.h>
#include "tests.h"

typedef struct mem {
    int a;
    int b;
} mem;

typedef struct mem2 {
    int a[2];
} mem2;
typedef int altint;

void setptr(int *arr, int val) {
    arr[0] = val;
}
void setarr(int arr[], int val) {
    arr[0] = val;
}

int main()
{
    plan(80);

    diag("TODO: __builtin_object_size")
    // https://github.com/elliotchance/c2go/issues/359

    {
        diag("strcpy")
        char *src = "foo bar\0baz";
        char dest1[40];
        char *dest2;
        dest2 = strcpy(dest1, src);
        is_streq(dest1, "foo bar");
        is_streq(dest2, "foo bar");
    }

    diag("strncpy")

    // If the end of the source C string (which is signaled by a null-character)
    // is found before num characters have been copied, destination is padded
    // with zeros until a total of num characters have been written to it.
    {
        char *src = "foo bar\0baz";
        char dest1[40];
        char *dest2;

        dest1[7] = 'a';
        dest1[15] = 'b';
        dest1[25] = 'c';
        dest2 = strncpy(dest1, src, 20);

        is_eq(dest1[0], 'f');
        is_eq(dest1[7], 0);
        is_eq(dest1[15], 0);
        is_eq(dest1[25], 'c');

        is_eq(dest2[0], 'f');
        is_eq(dest2[7], 0);
        is_eq(dest2[15], 0);
        is_eq(dest2[25], 'c');

        is_streq(dest1, "foo bar");
        is_streq(dest2, "foo bar");
    }

    // No null-character is implicitly appended at the end of destination if
    // source is longer than num. Thus, in this case, destination shall not be
    // considered a null terminated C string (reading it as such would
    // overflow).
    {
        char *src = "foo bar\0baz";
        char dest1[40];
        char *dest2;

        dest1[7] = 'a';
        dest1[15] = 'b';
        dest1[25] = 'c';
        dest2 = strncpy(dest1, src, 5);

        is_eq(dest1[0], 'f');
        is_eq(dest1[7], 'a');
        is_eq(dest1[15], 'b');
        is_eq(dest1[25], 'c');

        is_eq(dest2[0], 'f');
        is_eq(dest2[7], 'a');
        is_eq(dest2[15], 'b');
        is_eq(dest2[25], 'c');
    }

    {
        diag("strlen")
        is_eq(strlen(""), 0);
        is_eq(strlen("a"), 1);
        is_eq(strlen("foo"), 3);
        // NULL causes a seg fault.
        // is_eq(strlen(NULL), 0);
        is_eq(strlen("fo\0o"), 2);
    }
	{
		diag("strcat")
		char str[80];
		strcpy (str,"these ");
		strcat (str,"strings ");
		strcat (str,"are ");
		strcat (str,"concatenated.");
		is_streq(str,"these strings are concatenated.");
	}
	{
		diag("strcmp");
		{
			char* a = "ab";
			char* b = "ab";
			is_true(strcmp(a,b) == 0);
		}
		{
			char* a = "bb";
			char* b = "ab";
			is_true(strcmp(a,b) > 0);
		}
		{
			char* a = "ab";
			char* b = "bb";
			is_true(strcmp(a,b) < 0);
		}
	}
	{
        diag("strncmp");
        {
            char* a = "ab";
            char* b = "ab";
            is_true(strncmp(a,b,10) == 0);
        }
        {
            char* a = "bb";
            char* b = "ab";
            is_true(strncmp(a,b,10) > 0);
        }
        {
            char* a = "ab";
            char* b = "bb";
            is_true(strncmp(a,b,10) < 0);
        }
        {
            char* a = "aba";
            char* b = "ab";
            is_true(strncmp(a,b,10) > 0);
        }
        {
            char* a = "ab";
            char* b = "aba";
            is_true(strncmp(a,b,10) < 0);
        }
        {
            char* a = "aba";
            char* b = "abc";
            is_true(strncmp(a,b,2) == 0);
        }
    }
	{
		diag("strchr");
		char str[] = "This is a sample string";
		char * pch;
		int amount = 0;
		pch=strchr(str,'s');
		while (pch!=NULL)
		{
			pch=strchr(pch+1,'s');
			amount ++;
		}
		is_eq(amount,  4 );
	}
    {
        diag("memset");
        char dest1[40];
        char *dest2;
        char *dest3;
        dest2 = (char*) memset(dest1, 'a', 4);
        dest1[4] = '\0';
        is_streq(dest1, "aaaa");
        is_streq(dest2, "aaaa");
        dest3 = (char*) memset(&dest2[1], 'b', 2);
        is_streq(dest1, "abba");
        is_streq(dest2, "abba");
        is_streq(dest3, "bba");
    }
    {
        diag("memcpy");
        char *src = "aaaabb";
        char dest1[40];
        char *dest2;
        char *dest3;
        dest2 = (char*) memcpy(dest1, src, 4);
        dest1[4] = '\0';
        is_streq(dest1, "aaaa");
        is_streq(dest2, "aaaa");
        dest3 = (char*) memcpy(&dest2[1], &src[4], 2);
        is_streq(dest1, "abba");
        is_streq(dest2, "abba");
        is_streq(dest3, "bba");
    }
    {
        diag("memmove");
        char *src = "aaaabb";
        char dest1[40];
        char *dest2;
        char *dest3;
        dest2 = (char*) memmove(dest1, src, 4);
        dest1[4] = '\0';
        is_streq(dest1, "aaaa");
        is_streq(dest2, "aaaa");
        dest3 = (char*) memmove(&dest2[1], &src[4], 2);
        is_streq(dest1, "abba");
        is_streq(dest2, "abba");
        is_streq(dest3, "bba");
    }
    {
        diag("memset & memcpy of struct / array of struct");
        int dest3 = 4;
        int dest4 = 0xf;
        memcpy(&dest3, &dest4, sizeof(int));
        is_eq(dest3, 0xf);
        memset(&dest4, 0, sizeof(int));
        is_eq(dest4, 0);
        mem dest5 = {
            .a = 2,
            .b = 3.0
        };
        memset(&dest5, 0, sizeof(mem));
        is_eq(dest5.a, 0);
        is_eq(dest5.b, 0.0);
        mem dest6[] = {
            {
               .a = 2,
               .b = 3.0
            },
            {
               .a = 4,
               .b = 5.0
            }
        };
        mem dest7[2];
        memcpy(dest7, dest6, sizeof(mem)*2);
        memset(dest6, 0, sizeof(mem)*2);
        is_eq(dest6[0].a, 0);
        is_eq(dest6[0].b, 0.0);
        is_eq(dest6[1].a, 0);
        is_eq(dest6[1].b, 0.0);
        is_eq(dest7[0].a, 2);
        is_eq(dest7[0].b, 3.0);
        is_eq(dest7[1].a, 4);
        is_eq(dest7[1].b, 5.0);
        memset(&dest7[1], 0, sizeof(mem));
        is_eq(dest7[0].a, 2);
        is_eq(dest7[0].b, 3.0);
        is_eq(dest7[1].a, 0);
        is_eq(dest7[1].b, 0.0);
        mem2 dest8;
        dest8.a[0] = 42;
        memset(dest8.a, 0, sizeof(int)*2);
        is_eq(dest8.a[0], 0);
        is_eq(dest8.a[1], 0);
        dest8.a[0] = 42;
        mem2 dest9;
        altint *test = (altint *) dest9.a;
        memcpy(dest9.a, dest8.a, sizeof(int)*2);
        is_eq(dest9.a[0], 42);
        is_eq(test[0], 42);
        setarr(dest9.a, 1);
        is_eq(dest9.a[0], 1);
        setptr(dest9.a, 2);
        is_eq(dest9.a[0], 2);
    }
    {
        diag("memcmp");
        {
            char* a = "ab\0c";
            char* b = "ab\0c";
            is_true(memcmp(a,b,4) == 0);
        }
        {
            char* a = "ab\0a";
            char* b = "ab\0c";
            is_true(memcmp(a,b,4) < 0);
        }
        {
            char* a = "ab\0c";
            char* b = "ab\0a";
            is_true(memcmp(a,b,4) > 0);
        }
        {
            char* a = "ab\0c";
            char* b = "ab\0a";
            is_true(memcmp(a,b,3) == 0);
        }
    }
    {
        diag("strstr");
        {
            char* a = "needle in a haystack";
            char* b = "haystack";
            char* res = strstr(a,b);
            is_streq(res, "haystack");
        }
        {
            char* a = "needle in a haystack";
            char* b = "wrong";
            char* res = strstr(a,b);
            is_null(res);
        }
    }
    {
        diag("strcasestr");
        {
            char* a = "needle in a haystack";
            char* b = "HayStack";
            char* res = strcasestr(a,b);
            is_streq(res, "haystack");
        }
        {
            char* a = "needle in a haystack";
            char* b = "wrong";
            char* res = strcasestr(a,b);
            is_null(res);
        }
    }

    done_testing();
}