gio/tests: add test for single escape character

Converts the existing test to a loop and adds another test case which
tests a single stop character, \0 in this case.
This commit is contained in:
Christian Hergert 2024-10-04 12:46:11 -07:00
parent e7e5ddd2ae
commit 760a6f6477

View File

@ -274,9 +274,51 @@ test_read_until (void)
G_GNUC_END_IGNORE_DEPRECATIONS
static char *
escape_data_string (const char *str,
size_t len)
{
char *escaped = g_memdup2 (str, len + 1);
for (size_t i = 0; i < len; i++)
{
if (escaped[i] == '\0')
escaped[i] = '?';
}
return escaped;
}
static void
test_read_upto (void)
{
const struct {
int n_repeats;
const char *data_string;
size_t data_string_len;
size_t data_part_len;
const char *data_sep;
size_t data_sep_len;
} vectors[] = {
{ 10, " part1 # part2 $ part3 \0 part4 ", 32, 7, "#$\0^", 4 },
{ 20, "{\"key\": \"value\"}\0", 17, 16, "\0", 1 },
};
#undef REPEATS
#undef DATA_STRING
#undef DATA_PART_LEN
#undef DATA_SEP
#undef DATA_SEP_LEN
#define REPEATS vectors[n].n_repeats
#define DATA_STRING vectors[n].data_string
#define DATA_STRING_LEN vectors[n].data_string_len
#define DATA_PART_LEN vectors[n].data_part_len
#define DATA_SEP vectors[n].data_sep
#define DATA_SEP_LEN vectors[n].data_sep_len
for (guint n = 0; n < G_N_ELEMENTS (vectors); n++)
{
const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
GInputStream *stream;
GInputStream *base_stream;
GError *error = NULL;
@ -285,23 +327,17 @@ test_read_upto (void)
int i;
guchar stop_char;
#undef REPEATS
#undef DATA_STRING
#undef DATA_PART_LEN
#undef DATA_SEP
#undef DATA_SEP_LEN
#define REPEATS 10 /* number of rounds */
#define DATA_STRING " part1 # part2 $ part3 \0 part4 ^"
#define DATA_PART_LEN 7 /* number of characters between separators */
#define DATA_SEP "#$\0^"
#define DATA_SEP_LEN 4
const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
char *escaped_data = escape_data_string (DATA_STRING, DATA_STRING_LEN);
char *escaped_sep = escape_data_string (DATA_SEP, DATA_SEP_LEN);
g_test_message ("Test vector %u: %s and %s", n, escaped_data, escaped_sep);
g_free (escaped_data);
g_free (escaped_sep);
base_stream = g_memory_input_stream_new ();
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
for (i = 0; i < REPEATS; i++)
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, DATA_STRING_LEN, NULL);
/* Test stop characters */
error = NULL;
@ -329,6 +365,8 @@ test_read_upto (void)
g_object_unref (base_stream);
g_object_unref (stream);
}
}
enum TestDataType {
TEST_DATA_BYTE = 0,
TEST_DATA_INT16,