2 lib - Read string from mc_pipe_stream
4 Copyright (C) 2021-2024
5 Free Software Foundation, Inc.
8 Andrew Borodin <aborodin@vmail.ru>, 2021
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib/util"
28 #include "tests/mctest.h"
32 /* --------------------------------------------------------------------------------------------- */
36 /* --------------------------------------------------------------------------------------------- */
38 static mc_pipe_stream_t stream
;
40 static char etalon_long_file_list
[BUF_1K
];
41 static size_t etalon_long_file_list_pos
;
43 /* --------------------------------------------------------------------------------------------- */
57 /* --------------------------------------------------------------------------------------------- */
59 /* @DataSource("data_source") */
61 static const struct data_source
64 const char *buf
; /* string to read */
67 int pos
[MAX_CHUNKS
]; /* ps.pos values */
68 const char *str
[MAX_CHUNKS
]; /* chunks */
69 size_t len
[MAX_CHUNKS
]; /* chunk lengths */
105 .str
= { "\\\\\\\n" },
112 .str
= { "\\\\\\\\\n" },
126 .str
= { "12345\n" },
133 .str
= { "12345\\\n" },
138 .buf
= "12345\\\\\n",
140 .str
= { "12345\\\\\n" },
145 .buf
= "12345\nabcd",
147 .str
= { "12345\n", "abcd" },
152 .buf
= "12345\\\nabcd",
154 .str
= { "12345\\\nabcd" },
159 .buf
= "12345\\\\\nabcd",
161 .str
= { "12345\\\\\n", "abcd" },
166 .buf
= "12345\\\\\\\nabcd",
168 .str
= { "12345\\\\\\\nabcd" },
173 .buf
= "12345\\\\\\\\\nabcd",
174 .pos
= { 0, 10, 14 },
175 .str
= { "12345\\\\\\\\\n", "abcd" },
180 .buf
= "12345\nabcd\n",
182 .str
= { "12345\n", "abcd\n" },
187 .buf
= "12345\nabcd\n~!@#$%^",
188 .pos
= { 0, 6, 11, 18 },
189 .str
= { "12345\n", "abcd\n", "~!@#$%^" },
190 .len
= { 6, 5, 7, 0 }
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 }
202 /* @Test(dataSource = "data_source") */
204 START_PARAMETRIZED_TEST (mc_pstream_get_string_test
, data_source
)
211 memset (&stream
, 0, sizeof (stream
));
212 stream
.len
= strlen (data
->buf
);
213 memmove (&stream
.buf
, data
->buf
, stream
.len
);
220 ck_assert_int_eq (stream
.pos
, data
->pos
[j
]);
222 ret
= mc_pstream_get_string (&stream
);
226 ck_assert_int_eq (ret
->len
, data
->len
[j
]);
227 mctest_assert_str_eq (ret
->str
, data
->str
[j
]);
229 g_string_free (ret
, TRUE
);
236 END_PARAMETRIZED_TEST
239 /* --------------------------------------------------------------------------------------------- */
246 p
= g_try_new0 (mc_pipe_t
, 1);
247 /* make less than sizeof (etalon_long_file_list) */
250 etalon_long_file_list_pos
= 0;
256 test_mc_pread (mc_pipe_t
*p
)
262 if (etalon_long_file_list_pos
>= sizeof (etalon_long_file_list
))
264 etalon_long_file_list_pos
= sizeof (etalon_long_file_list
);
265 p
->out
.len
= MC_PIPE_STREAM_EOF
;
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
;
274 etalon_long_file_list_pos
+= len
;
278 START_TEST (mc_pstream_get_long_file_list_test
)
283 GString
*result_long_file_list
= NULL
;
285 GString
*remain_file_name
= NULL
;
289 memset (etalon_long_file_list
, 'a', sizeof (etalon_long_file_list
) - 1);
290 /* create an \n-separated list */
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';
309 pip
= test_mc_popen ();
317 if (pip
->out
.len
== MC_PIPE_STREAM_EOF
)
320 while ((line
= mc_pstream_get_string (&pip
->out
)) != NULL
)
322 /* handle an \n-separated file list */
324 if (line
->str
[line
->len
- 1] == '\n')
326 /* entire file name or last chunk */
328 g_string_truncate (line
, line
->len
- 1);
330 /* join filename chunks */
331 if (remain_file_name
!= NULL
)
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
;
341 /* first or middle chunk of file name */
342 if (remain_file_name
== NULL
)
343 remain_file_name
= line
;
346 g_string_append_len (remain_file_name
, line
->str
, line
->len
);
347 g_string_free (line
, TRUE
);
353 /* collect file names to assemble the result string */
357 if (result_long_file_list
== NULL
)
358 result_long_file_list
= line
;
361 g_string_append_len (result_long_file_list
, line
->str
, line
->len
);
362 g_string_free (line
, TRUE
);
365 g_string_append_c (result_long_file_list
, '\n');
369 mctest_assert_str_eq (etalon_long_file_list
, result_long_file_list
->str
);
370 g_string_free (result_long_file_list
, TRUE
);
377 /* --------------------------------------------------------------------------------------------- */
384 tc_core
= tcase_create ("Core");
386 tcase_add_checked_fixture (tc_core
, setup
, teardown
);
388 /* Add new tests here: *************** */
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 /* *********************************** */
393 return mctest_run_all (tc_core
);
396 /* --------------------------------------------------------------------------------------------- */