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
|
#include "libft.h"
#include "test_utils.h"
_S_CRASH (ft_split_null, ft_split (NULL, ' '))
static void
_s_free_split (char **arr)
{
size_t i;
if (!arr)
return;
i = 0;
while (arr[i])
free (arr[i++]);
free (arr);
}
static size_t
_s_arr_len (char **arr)
{
size_t n;
n = 0;
while (arr[n])
n++;
return (n);
}
static void
_s_test_split (void)
{
int i;
char label[128];
_s_section ("ft_split");
/* NULL crashes */
_s_check ("NULL crashes", _s_crashes (_s_crash_ft_split_null));
/* basic split */
{
char **arr = ft_split ("hello world foo", ' ');
_s_check ("basic count", arr && _s_arr_len (arr) == 3);
_s_check ("basic [0]", arr && strcmp (arr[0], "hello") == 0);
_s_check ("basic [1]", arr && strcmp (arr[1], "world") == 0);
_s_check ("basic [2]", arr && strcmp (arr[2], "foo") == 0);
_s_check ("basic NULL-term", arr && arr[3] == NULL);
_s_free_split (arr);
}
/* leading delimiters */
{
char **arr = ft_split (" hello", ' ');
_s_check ("leading delim count", arr && _s_arr_len (arr) == 1);
_s_check ("leading delim [0]", arr && strcmp (arr[0], "hello") == 0);
_s_free_split (arr);
}
/* trailing delimiters */
{
char **arr = ft_split ("hello ", ' ');
_s_check ("trailing delim count", arr && _s_arr_len (arr) == 1);
_s_check ("trailing delim [0]", arr && strcmp (arr[0], "hello") == 0);
_s_free_split (arr);
}
/* consecutive delimiters */
{
char **arr = ft_split ("a,,b,,c", ',');
_s_check ("consec count", arr && _s_arr_len (arr) == 3);
_s_check ("consec [0]", arr && strcmp (arr[0], "a") == 0);
_s_check ("consec [1]", arr && strcmp (arr[1], "b") == 0);
_s_check ("consec [2]", arr && strcmp (arr[2], "c") == 0);
_s_free_split (arr);
}
/* no delimiter in string */
{
char **arr = ft_split ("hello", ' ');
_s_check ("no delim count", arr && _s_arr_len (arr) == 1);
_s_check ("no delim [0]", arr && strcmp (arr[0], "hello") == 0);
_s_free_split (arr);
}
/* empty string */
{
char **arr = ft_split ("", ' ');
_s_check ("empty count", arr && _s_arr_len (arr) == 0);
_s_check ("empty NULL-term", arr && arr[0] == NULL);
_s_free_split (arr);
}
/* string is only delimiters */
{
char **arr = ft_split (" ", ' ');
_s_check ("only delims count", arr && _s_arr_len (arr) == 0);
_s_check ("only delims NULL-term", arr && arr[0] == NULL);
_s_free_split (arr);
}
/* single character string, is delimiter */
{
char **arr = ft_split (",", ',');
_s_check ("single delim count", arr && _s_arr_len (arr) == 0);
_s_free_split (arr);
}
/* single character string, not delimiter */
{
char **arr = ft_split ("a", ',');
_s_check ("single non-delim count", arr && _s_arr_len (arr) == 1);
_s_check ("single non-delim [0]", arr && strcmp (arr[0], "a") == 0);
_s_free_split (arr);
}
/* delimiter '\0' — whole string is one word */
{
char **arr = ft_split ("hello", '\0');
_s_check ("nul delim count", arr && _s_arr_len (arr) == 1);
_s_check ("nul delim [0]", arr && strcmp (arr[0], "hello") == 0);
_s_free_split (arr);
}
/* each word is independently allocated */
{
char **arr = ft_split ("a b c", ' ');
_s_check ("independent ptrs", arr && arr[0] != arr[1] && arr[1] != arr[2]);
_s_free_split (arr);
}
/* randomized: build string from words, split, verify */
for (i = 0; i < _S_RAND_ITERS; i++)
{
int nwords = rand () % 10 + 1;
int pads = rand () % 5;
char **words = malloc (nwords * sizeof (char *));
char *src;
char **arr;
size_t total;
int j, k;
int ok;
if (!words)
continue;
/* generate random words (letters only) */
total = 0;
ok = 1;
for (j = 0; j < nwords; j++)
{
int wlen = rand () % 20 + 1;
words[j] = malloc (wlen + 1);
if (!words[j])
{
ok = 0;
break;
}
for (k = 0; k < wlen; k++)
words[j][k] = 'A' + rand () % 26;
words[j][wlen] = '\0';
total += wlen;
}
if (!ok)
{
for (k = 0; k < j; k++)
free (words[k]);
free (words);
continue;
}
/* build: pads + word + delimiters + ... + pads */
total += (nwords - 1) + 2 * pads;
src = malloc (total + 1);
if (!src)
{
for (j = 0; j < nwords; j++)
free (words[j]);
free (words);
continue;
}
k = 0;
for (j = 0; j < pads; j++)
src[k++] = ',';
for (j = 0; j < nwords; j++)
{
size_t wlen = strlen (words[j]);
memcpy (src + k, words[j], wlen);
k += wlen;
if (j < nwords - 1)
src[k++] = ',';
}
for (j = 0; j < pads; j++)
src[k++] = ',';
src[k] = '\0';
arr = ft_split (src, ',');
snprintf (label, sizeof (label), "random nwords=%d pads=%d", nwords,
pads);
if (!arr || (int)_s_arr_len (arr) != nwords)
{
_s_check (label, 0);
}
else
{
int match = 1;
for (j = 0; j < nwords; j++)
{
if (strcmp (arr[j], words[j]) != 0)
{
match = 0;
break;
}
}
_s_check (label, match && arr[nwords] == NULL);
}
for (j = 0; j < nwords; j++)
free (words[j]);
free (words);
free (src);
_s_free_split (arr);
}
}
int
main (void)
{
srand (time (NULL));
_s_test_split ();
_s_print_results ();
return (_s_fail != 0);
}
|