2021-03-29 00:55:19 +03:00
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include "config_ac.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "test_common.h"
|
|
|
|
#include "string_calls.h"
|
|
|
|
|
|
|
|
#define RESULT_LEN 1024
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_src_is_null__returns_empty_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char **src = NULL;
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, " ", src, 0);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_src_has_null_item__returns_joined_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, NULL };
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, " ", src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_value ");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_src_has_one_item__returns_copied_source_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *expected_value = "test_value";
|
|
|
|
const char *src[] = { expected_value };
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, " ", src, 1);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, expected_value);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_src_has_two_items__returns_joined_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, test_value };
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, " ", src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_value test_value");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_joiner_is_empty_string__returns_joined_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, test_value };
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, "", src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_valuetest_value");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_joiner_is_NULL__returns_joined_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, test_value };
|
|
|
|
char result[RESULT_LEN];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, NULL, src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_valuetest_value");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_destination_is_NULL__returns_NULL)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value };
|
|
|
|
char *result = NULL;
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, 0, " ", src, 1);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_ptr_eq(result, NULL);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_destination_is_shorter_than_src__returns_partial_src_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value };
|
|
|
|
char result[5];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, 5, " ", src, 1);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_destination_is_shorter_than_src_plus_joiner__returns_partial_joined_string)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, test_value};
|
|
|
|
char result[16];
|
|
|
|
result[0] = '\0';
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, 16, " joiner ", src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_value join");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_destination_has_contents__returns_joined_string_with_content_overwritten)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
|
|
|
const char *test_value = "test_value";
|
|
|
|
const char *src[] = { test_value, test_value};
|
|
|
|
char result[RESULT_LEN] = "1234567890";
|
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
g_strnjoin(result, RESULT_LEN, " ", src, 2);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_str_eq(result, "test_value test_value");
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_strnjoin__when_always__then_doesnt_write_beyond_end_of_destination)
|
|
|
|
{
|
|
|
|
/* setup */
|
|
|
|
|
2021-05-08 19:58:11 +03:00
|
|
|
const char *src[] = { "a", "b", "c"};
|
|
|
|
char result[5 + 1 + 1]; /* a-b-c + term null + guard value */
|
2021-03-29 00:55:19 +03:00
|
|
|
|
|
|
|
/* test */
|
|
|
|
|
|
|
|
result[6] = '\x7f';
|
|
|
|
g_strnjoin(result, 5 + 1, "-", src, 3);
|
|
|
|
|
|
|
|
/* verify */
|
|
|
|
|
|
|
|
ck_assert_int_eq(result[6], '\x7f');
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-08-26 17:31:38 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
START_TEST(test_bm2str__no_bits_defined)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char buff[64];
|
|
|
|
|
|
|
|
static const struct bitmask_string bits[] =
|
|
|
|
{
|
|
|
|
BITMASK_STRING_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
rv = g_bitmask_to_str(0xffff, bits, ',', buff, sizeof(buff));
|
|
|
|
|
|
|
|
ck_assert_str_eq(buff, "0xffff");
|
|
|
|
ck_assert_int_eq(rv, 6);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_bm2str__all_bits_defined)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char buff[64];
|
|
|
|
|
|
|
|
static const struct bitmask_string bits[] =
|
|
|
|
{
|
|
|
|
{1 << 0, "BIT_0"},
|
|
|
|
{1 << 1, "BIT_1"},
|
|
|
|
{1 << 6, "BIT_6"},
|
|
|
|
{1 << 7, "BIT_7"},
|
|
|
|
BITMASK_STRING_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
int bitmask = 1 << 0 | 1 << 1 | 1 << 6 | 1 << 7;
|
|
|
|
|
|
|
|
rv = g_bitmask_to_str(bitmask, bits, '|', buff, sizeof(buff));
|
|
|
|
|
|
|
|
ck_assert_str_eq(buff, "BIT_0|BIT_1|BIT_6|BIT_7");
|
|
|
|
ck_assert_int_eq(rv, (6 * 4) - 1);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_bm2str__some_bits_undefined)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char buff[64];
|
|
|
|
|
|
|
|
static const struct bitmask_string bits[] =
|
|
|
|
{
|
|
|
|
{1 << 0, "BIT_0"},
|
|
|
|
{1 << 1, "BIT_1"},
|
|
|
|
{1 << 6, "BIT_6"},
|
|
|
|
{1 << 7, "BIT_7"},
|
|
|
|
BITMASK_STRING_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
int bitmask = 1 << 0 | 1 << 1 | 1 << 16;
|
|
|
|
|
|
|
|
rv = g_bitmask_to_str(bitmask, bits, ':', buff, sizeof(buff));
|
|
|
|
|
|
|
|
ck_assert_str_eq(buff, "BIT_0:BIT_1:0x10000");
|
|
|
|
ck_assert_int_eq(rv, (6 * 2) + 7);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_bm2str__overflow_all_bits_defined)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char buff[10];
|
|
|
|
|
|
|
|
static const struct bitmask_string bits[] =
|
|
|
|
{
|
|
|
|
{1 << 0, "BIT_0"},
|
|
|
|
{1 << 1, "BIT_1"},
|
|
|
|
{1 << 6, "BIT_6"},
|
|
|
|
{1 << 7, "BIT_7"},
|
|
|
|
BITMASK_STRING_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
int bitmask = 1 << 0 | 1 << 1 | 1 << 6 | 1 << 7;
|
|
|
|
|
|
|
|
rv = g_bitmask_to_str(bitmask, bits, '+', buff, sizeof(buff));
|
|
|
|
|
|
|
|
ck_assert_str_eq(buff, "BIT_0+BIT");
|
|
|
|
ck_assert_int_eq(rv, (4 * 6) - 1);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_bm2str__overflow_some_bits_undefined)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char buff[16];
|
|
|
|
|
|
|
|
static const struct bitmask_string bits[] =
|
|
|
|
{
|
|
|
|
{1 << 0, "BIT_0"},
|
|
|
|
{1 << 1, "BIT_1"},
|
|
|
|
{1 << 6, "BIT_6"},
|
|
|
|
{1 << 7, "BIT_7"},
|
|
|
|
BITMASK_STRING_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
int bitmask = 1 << 0 | 1 << 1 | 1 << 16;
|
|
|
|
|
|
|
|
rv = g_bitmask_to_str(bitmask, bits, '#', buff, sizeof(buff));
|
|
|
|
|
|
|
|
ck_assert_str_eq(buff, "BIT_0#BIT_1#0x1");
|
|
|
|
ck_assert_int_eq(rv, (6 * 2) + 7);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-03-29 00:55:19 +03:00
|
|
|
/******************************************************************************/
|
|
|
|
Suite *
|
|
|
|
make_suite_test_string(void)
|
|
|
|
{
|
|
|
|
Suite *s;
|
|
|
|
TCase *tc_strnjoin;
|
2021-08-26 17:31:38 +03:00
|
|
|
TCase *tc_bm2str;
|
2021-03-29 00:55:19 +03:00
|
|
|
|
|
|
|
s = suite_create("String");
|
|
|
|
|
|
|
|
tc_strnjoin = tcase_create("strnjoin");
|
|
|
|
suite_add_tcase(s, tc_strnjoin);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_src_is_null__returns_empty_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_src_has_null_item__returns_joined_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_src_has_one_item__returns_copied_source_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_src_has_two_items__returns_joined_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_joiner_is_empty_string__returns_joined_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_joiner_is_NULL__returns_joined_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_destination_is_NULL__returns_NULL);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_destination_is_shorter_than_src__returns_partial_src_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_destination_is_shorter_than_src_plus_joiner__returns_partial_joined_string);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_destination_has_contents__returns_joined_string_with_content_overwritten);
|
|
|
|
tcase_add_test(tc_strnjoin, test_strnjoin__when_always__then_doesnt_write_beyond_end_of_destination);
|
|
|
|
|
2021-08-26 17:31:38 +03:00
|
|
|
tc_bm2str = tcase_create("bm2str");
|
|
|
|
suite_add_tcase(s, tc_bm2str);
|
|
|
|
tcase_add_test(tc_bm2str, test_bm2str__no_bits_defined);
|
|
|
|
tcase_add_test(tc_bm2str, test_bm2str__all_bits_defined);
|
|
|
|
tcase_add_test(tc_bm2str, test_bm2str__some_bits_undefined);
|
|
|
|
tcase_add_test(tc_bm2str, test_bm2str__overflow_all_bits_defined);
|
|
|
|
tcase_add_test(tc_bm2str, test_bm2str__overflow_some_bits_undefined);
|
|
|
|
|
2021-03-29 00:55:19 +03:00
|
|
|
return s;
|
|
|
|
}
|