Add uc_strv_reverse

This commit is contained in:
Nils O. Selåsdal
2015-12-10 19:13:16 +01:00
parent 9b70b64729
commit db78689015
3 changed files with 55 additions and 0 deletions
+5
View File
@@ -101,6 +101,11 @@ void uc_strv_destroy(struct UCStrv *strv);
*/
int uc_strv_append_all(struct UCStrv *restrict a, struct UCStrv *restrict b, int copy);
/** Reverse the order of strings in a UCStrv
*
* @param strv strv to reverse.
*/
void uc_strv_reverse(struct UCStrv *strv);
#ifdef __cplusplus
}
#endif
+11
View File
@@ -130,3 +130,14 @@ int uc_strv_append_all(struct UCStrv *restrict a, struct UCStrv *restrict b, int
return 0;
}
void uc_strv_reverse(struct UCStrv *strv)
{
for (size_t i = 0; i < strv->cnt / 2; i++) {
size_t k = strv->cnt - i - 1;
char *tmp = strv->strings[i];
strv->strings[i] = strv->strings[k];
strv->strings[k] = tmp;
}
}
+39
View File
@@ -123,6 +123,44 @@ START_TEST (test_strv_append_all_nocopy)
END_TEST
START_TEST (test_strv_reverse)
struct UCStrv strv;
uc_strv_init(&strv);
uc_strv_append(&strv, "string1");
uc_strv_reverse(&strv);
ck_assert_str_eq("string1", strv.strings[0]);
uc_strv_destroy(&strv);
uc_strv_init(&strv);
uc_strv_append(&strv, "string1");
uc_strv_append(&strv, "string2");
uc_strv_reverse(&strv);
ck_assert_str_eq("string2", strv.strings[0]);
ck_assert_str_eq("string1", strv.strings[1]);
uc_strv_destroy(&strv);
uc_strv_init(&strv);
uc_strv_append(&strv, "string1");
uc_strv_append(&strv, "string2");
uc_strv_append(&strv, "string3");
uc_strv_reverse(&strv);
ck_assert_str_eq("string3", strv.strings[0]);
ck_assert_str_eq("string2", strv.strings[1]);
ck_assert_str_eq("string1", strv.strings[2]);
uc_strv_destroy(&strv);
END_TEST
Suite *strv_suite(void)
{
@@ -135,6 +173,7 @@ Suite *strv_suite(void)
tcase_add_test(tc, test_strv_null_terminate);
tcase_add_test(tc, test_strv_append_all_copy);
tcase_add_test(tc, test_strv_append_all_nocopy);
tcase_add_test(tc, test_strv_reverse);
suite_add_tcase(s, tc);