- tests/lib/utilunix__mc_pstream_get_string.c
This source file includes following definitions.
- setup
- teardown
- START_PARAMETRIZED_TEST
- test_mc_popen
- test_mc_pread
- START_TEST
- main
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 #define TEST_SUITE_NAME "/lib/util"
27
28 #include "tests/mctest.h"
29
30 #include "lib/util.h"
31
32
33
34 #define MAX_CHUNKS 8
35
36
37
38 static mc_pipe_stream_t stream;
39
40 static char etalon_long_file_list[BUF_1K];
41 static size_t etalon_long_file_list_pos;
42
43
44
45
46 static void
47 setup (void)
48 {
49 }
50
51
52 static void
53 teardown (void)
54 {
55 }
56
57
58
59
60
61 static const struct data_source
62 {
63
64 const char *buf;
65
66
67 int pos[MAX_CHUNKS];
68 const char *str[MAX_CHUNKS];
69 size_t len[MAX_CHUNKS];
70 }
71 data_source[] =
72 {
73
74 {
75 .buf = "",
76 .pos = { 0 },
77 .str = { "" },
78 .len = { 0 }
79 },
80
81 {
82 .buf = "\n",
83 .pos = { 0, 1 },
84 .str = { "\n" },
85 .len = { 1, 0 }
86 },
87
88 {
89 .buf = "\\\n",
90 .pos = { 0, 2 },
91 .str = { "\\\n" },
92 .len = { 2, 0 }
93 },
94
95 {
96 .buf = "\\\\\n",
97 .pos = { 0, 3 },
98 .str = { "\\\\\n" },
99 .len = { 3, 0 }
100 },
101
102 {
103 .buf = "\\\\\\\n",
104 .pos = { 0, 4 },
105 .str = { "\\\\\\\n" },
106 .len = { 4, 0 }
107 },
108
109 {
110 .buf = "\\\\\\\\\n",
111 .pos = { 0, 5 },
112 .str = { "\\\\\\\\\n" },
113 .len = { 5, 0 }
114 },
115
116 {
117 .buf = "12345",
118 .pos = { 0, 5 },
119 .str = { "12345" },
120 .len = { 5, 0 }
121 },
122
123 {
124 .buf = "12345\n",
125 .pos = { 0, 6 },
126 .str = { "12345\n" },
127 .len = { 6, 0 }
128 },
129
130 {
131 .buf = "12345\\\n",
132 .pos = { 0, 7 },
133 .str = { "12345\\\n" },
134 .len = { 7, 0 }
135 },
136
137 {
138 .buf = "12345\\\\\n",
139 .pos = { 0, 8 },
140 .str = { "12345\\\\\n" },
141 .len = { 8, 0 }
142 },
143
144 {
145 .buf = "12345\nabcd",
146 .pos = { 0, 6, 10 },
147 .str = { "12345\n", "abcd" },
148 .len = { 6, 4, 0 }
149 },
150
151 {
152 .buf = "12345\\\nabcd",
153 .pos = { 0, 11 },
154 .str = { "12345\\\nabcd" },
155 .len = { 11, 0 }
156 },
157
158 {
159 .buf = "12345\\\\\nabcd",
160 .pos = { 0, 8, 12 },
161 .str = { "12345\\\\\n", "abcd" },
162 .len = { 8, 4, 0 }
163 },
164
165 {
166 .buf = "12345\\\\\\\nabcd",
167 .pos = { 0, 13 },
168 .str = { "12345\\\\\\\nabcd" },
169 .len = { 13, 0 }
170 },
171
172 {
173 .buf = "12345\\\\\\\\\nabcd",
174 .pos = { 0, 10, 14 },
175 .str = { "12345\\\\\\\\\n", "abcd" },
176 .len = { 10, 4, 0 }
177 },
178
179 {
180 .buf = "12345\nabcd\n",
181 .pos = { 0, 6, 11 },
182 .str = { "12345\n", "abcd\n" },
183 .len = { 6, 5, 0 }
184 },
185
186 {
187 .buf = "12345\nabcd\n~!@#$%^",
188 .pos = { 0, 6, 11, 18 },
189 .str = { "12345\n", "abcd\n", "~!@#$%^" },
190 .len = { 6, 5, 7, 0 }
191 },
192
193 {
194 .buf = "12345\nabcd\n~!@#$%^\n",
195 .pos = { 0, 6, 11, 19 },
196 .str = { "12345\n", "abcd\n", "~!@#$%^\n" },
197 .len = { 6, 5, 8, 0 }
198 }
199 };
200
201
202
203
204 START_PARAMETRIZED_TEST (mc_pstream_get_string_test, data_source)
205
206 {
207
208 int j = 0;
209
210
211 memset (&stream, 0, sizeof (stream));
212 stream.len = strlen (data->buf);
213 memmove (&stream.buf, data->buf, stream.len);
214
215
216 do
217 {
218 GString *ret;
219
220 ck_assert_int_eq (stream.pos, data->pos[j]);
221
222 ret = mc_pstream_get_string (&stream);
223 if (ret == NULL)
224 break;
225
226 ck_assert_int_eq (ret->len, data->len[j]);
227 mctest_assert_str_eq (ret->str, data->str[j]);
228
229 g_string_free (ret, TRUE);
230
231 j++;
232 }
233 while (TRUE);
234 }
235
236 END_PARAMETRIZED_TEST
237
238
239
240
241 static mc_pipe_t *
242 test_mc_popen (void)
243 {
244 mc_pipe_t *p;
245
246 p = g_try_new0 (mc_pipe_t, 1);
247
248 p->out.len = 128;
249
250 etalon_long_file_list_pos = 0;
251
252 return p;
253 }
254
255 static void
256 test_mc_pread (mc_pipe_t *p)
257 {
258 size_t len;
259
260 p->out.pos = 0;
261
262 if (etalon_long_file_list_pos >= sizeof (etalon_long_file_list))
263 {
264 etalon_long_file_list_pos = sizeof (etalon_long_file_list);
265 p->out.len = MC_PIPE_STREAM_EOF;
266 return;
267 }
268
269 len = sizeof (etalon_long_file_list) - etalon_long_file_list_pos;
270 len = MIN (len, (size_t) p->out.len);
271 memmove (p->out.buf, etalon_long_file_list + etalon_long_file_list_pos, len);
272 p->out.len = (ssize_t) len;
273
274 etalon_long_file_list_pos += len;
275 }
276
277
278 START_TEST (mc_pstream_get_long_file_list_test)
279
280
281 {
282
283 GString *result_long_file_list = NULL;
284 mc_pipe_t *pip;
285 GString *remain_file_name = NULL;
286
287
288
289 memset (etalon_long_file_list, 'a', sizeof (etalon_long_file_list) - 1);
290
291 etalon_long_file_list[5] = '\n';
292 etalon_long_file_list[25] = '\n';
293 etalon_long_file_list[50] = '\n';
294 etalon_long_file_list[75] = '\n';
295 etalon_long_file_list[127] = '\n';
296 etalon_long_file_list[200] = '\n';
297 etalon_long_file_list[310] = '\n';
298 etalon_long_file_list[325] = '\n';
299 etalon_long_file_list[360] = '\n';
300 etalon_long_file_list[512] = '\n';
301 etalon_long_file_list[701] = '\n';
302 etalon_long_file_list[725] = '\n';
303 etalon_long_file_list[800] = '\n';
304 etalon_long_file_list[sizeof (etalon_long_file_list) - 2] = '\n';
305 etalon_long_file_list[sizeof (etalon_long_file_list) - 1] = '\0';
306
307
308
309 pip = test_mc_popen ();
310
311 while (TRUE)
312 {
313 GString *line;
314
315 test_mc_pread (pip);
316
317 if (pip->out.len == MC_PIPE_STREAM_EOF)
318 break;
319
320 while ((line = mc_pstream_get_string (&pip->out)) != NULL)
321 {
322
323
324 if (line->str[line->len - 1] == '\n')
325 {
326
327
328 g_string_truncate (line, line->len - 1);
329
330
331 if (remain_file_name != NULL)
332 {
333 g_string_append_len (remain_file_name, line->str, line->len);
334 g_string_free (line, TRUE);
335 line = remain_file_name;
336 remain_file_name = NULL;
337 }
338 }
339 else
340 {
341
342 if (remain_file_name == NULL)
343 remain_file_name = line;
344 else
345 {
346 g_string_append_len (remain_file_name, line->str, line->len);
347 g_string_free (line, TRUE);
348 }
349
350 line = NULL;
351 }
352
353
354 if (line == NULL)
355 continue;
356
357 if (result_long_file_list == NULL)
358 result_long_file_list = line;
359 else
360 {
361 g_string_append_len (result_long_file_list, line->str, line->len);
362 g_string_free (line, TRUE);
363 }
364
365 g_string_append_c (result_long_file_list, '\n');
366 }
367 }
368
369 mctest_assert_str_eq (etalon_long_file_list, result_long_file_list->str);
370 g_string_free (result_long_file_list, TRUE);
371
372 }
373
374 END_TEST
375
376
377
378
379 int
380 main (void)
381 {
382 TCase *tc_core;
383
384 tc_core = tcase_create ("Core");
385
386 tcase_add_checked_fixture (tc_core, setup, teardown);
387
388
389 mctest_add_parameterized_test (tc_core, mc_pstream_get_string_test, data_source);
390 tcase_add_test (tc_core, mc_pstream_get_long_file_list_test);
391
392
393 return mctest_run_all (tc_core);
394 }
395
396