1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Add lstComparatorZ().

Works just like lstComparatorStr() but with zero-terminated strings.
This commit is contained in:
David Steele 2021-07-30 18:08:04 -04:00
parent 8bca6946b4
commit 9abf6a2709
3 changed files with 28 additions and 0 deletions

View File

@ -97,6 +97,21 @@ lstComparatorStr(const void *item1, const void *item2)
FUNCTION_TEST_RETURN(strCmp(*(String **)item1, *(String **)item2));
}
/**********************************************************************************************************************************/
int
lstComparatorZ(const void *item1, const void *item2)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM_P(VOID, item1);
FUNCTION_TEST_PARAM_P(VOID, item2);
FUNCTION_TEST_END();
ASSERT(item1 != NULL);
ASSERT(item2 != NULL);
FUNCTION_TEST_RETURN(strcmp(*(char **)item1, *(char **)item2));
}
/***********************************************************************************************************************************
General function for a descending comparator that simply switches the parameters on the main comparator (which should be asc)
***********************************************************************************************************************************/

View File

@ -46,6 +46,9 @@ typedef int ListComparator(const void *item1, const void *item2);
// General purpose list comparator for Strings or structs with a String as the first member
int lstComparatorStr(const void *item1, const void *item2);
// General purpose list comparator for zero-terminated strings or structs with a zero-terminated string as the first member
int lstComparatorZ(const void *item1, const void *item2);
/***********************************************************************************************************************************
Constructors
***********************************************************************************************************************************/

View File

@ -180,6 +180,16 @@ testRun(void)
TEST_RESULT_INT(*(int *)lstGet(list, 1), 3, "sort value 1");
TEST_RESULT_INT(*(int *)lstGet(list, 2), 3, "sort value 2");
TEST_RESULT_INT(*(int *)lstGet(list, 3), 2, "sort value 3");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("lstComparatorZ()");
const char *string1 = "abc";
const char *string2 = "def";
TEST_RESULT_INT(lstComparatorZ(&string1, &string1), 0, "strings are equal");
TEST_RESULT_INT(lstComparatorZ(&string1, &string2), -1, "first string is less");
TEST_RESULT_INT(lstComparatorZ(&string2, &string1), 1, "first string is greater");
}
// *****************************************************************************************************************************