Skip to content

Commit

Permalink
gh-674: Combine typed array list to string utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Mar 25, 2024
1 parent 70d0ae0 commit 9133f17
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 104 deletions.
8 changes: 4 additions & 4 deletions libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ TEST_F(ConvertUtilsWithErrorInjectionTestSuite, LongArrayToStringTest) {
celix_arrayList_addLong(list, 2L);

//Given an error injection for opem_memstream
celix_ei_expect_open_memstream((void*)celix_utils_longArrayListToString, 1, nullptr);
celix_ei_expect_open_memstream((void*)celix_utils_arrayListToString, 1, nullptr);
//When calling celix_utils_longArrayListToString
char* result = celix_utils_longArrayListToString(list);
char* result = celix_utils_arrayListToString(list);
//Then the result is null
EXPECT_EQ(nullptr, result);

//Given an error injection for fputs
celix_ei_expect_fputs((void*)celix_utils_longArrayListToString, 1, -1);
celix_ei_expect_fputs((void*)celix_utils_arrayListToString, 1, -1);
//When calling celix_utils_longArrayListToString
result = celix_utils_longArrayListToString(list);
result = celix_utils_arrayListToString(list);
//Then the result is null
EXPECT_EQ(nullptr, result);
}
Expand Down
24 changes: 15 additions & 9 deletions libs/utils/gtest/src/ConvertUtilsTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,18 @@ TEST_F(ConvertUtilsTestSuite, LongArrayToStringTest) {
celix_arrayList_addLong(list, 2L);
celix_arrayList_addLong(list, 3L);

char* result = celix_utils_longArrayListToString(list);
char* result = celix_utils_arrayListToString(list);
EXPECT_STREQ("1,2,3", result);
free(result);

celix_autoptr(celix_array_list_t) emptyList = celix_arrayList_createLongArray();
result = celix_utils_longArrayListToString(emptyList);
result = celix_utils_arrayListToString(emptyList);
EXPECT_STREQ("", result);
free(result);

celix_autoptr(celix_array_list_t) singleEntryList = celix_arrayList_createLongArray();
celix_arrayList_addLong(singleEntryList, 1L);
result = celix_utils_longArrayListToString(singleEntryList);
result = celix_utils_arrayListToString(singleEntryList);
EXPECT_STREQ("1", result);
free(result);
}
Expand All @@ -384,7 +384,7 @@ TEST_F(ConvertUtilsTestSuite, DoubleArrayToStringTest) {
celix_arrayList_addDouble(list, 2.0);
celix_arrayList_addDouble(list, 3.3);

char* result = celix_utils_doubleArrayListToString(list); //note result is not limited to 2 decimals, so using strstr
char* result = celix_utils_arrayListToString(list); //note result is not limited to 2 decimals, so using strstr
EXPECT_TRUE(strstr(result, "0.1") != nullptr);
EXPECT_TRUE(strstr(result, "2.0") != nullptr);
EXPECT_TRUE(strstr(result, "3.3") != nullptr);
Expand Down Expand Up @@ -417,7 +417,7 @@ TEST_F(ConvertUtilsTestSuite, BoolArrayToStringTest) {
celix_arrayList_addBool(list, false);
celix_arrayList_addBool(list, true);

char* result = celix_utils_boolArrayListToString(list);
char* result = celix_utils_arrayListToString(list);
EXPECT_STREQ("true,false,true", result);
free(result);

Expand Down Expand Up @@ -475,13 +475,13 @@ TEST_F(ConvertUtilsTestSuite, StringArrayToStringTest) {
celix_arrayList_addString(list, "b");
celix_arrayList_addString(list, "c");

char* result = celix_utils_stringArrayListToString(list);
char* result = celix_utils_arrayListToString(list);
EXPECT_STREQ("a,b,c", result);
free(result);

celix_arrayList_addString(list, "d\\,");
celix_arrayList_addString(list, "e");
result = celix_utils_stringArrayListToString(list);
result = celix_utils_arrayListToString(list);
EXPECT_STREQ(R"(a,b,c,d\\\,,e)", result);

//Check if the result can be converted back to an equal list
Expand Down Expand Up @@ -518,7 +518,7 @@ TEST_F(ConvertUtilsTestSuite, VersionArrayToStringTest) {
celix_arrayList_addVersion(list, v2);
celix_arrayList_addVersion(list, v3);

char* result = celix_utils_versionArrayListToString(list);
char* result = celix_utils_arrayListToString(list);
EXPECT_STREQ("1.2.3,2.3.4,3.4.5.qualifier", result);
free(result);

Expand All @@ -527,4 +527,10 @@ TEST_F(ConvertUtilsTestSuite, VersionArrayToStringTest) {
// tested, we only test a few cases here.
}


TEST_F(ConvertUtilsTestSuite, InvalidArgumentsForArrayToStringTest) {
EXPECT_EQ(nullptr, celix_utils_arrayListToString(nullptr));
celix_autoptr(celix_array_list_t) list1 = celix_arrayList_create(); //unsupported undefined type
EXPECT_EQ(nullptr, celix_utils_arrayListToString(list1));
celix_autoptr(celix_array_list_t) list2 = celix_arrayList_createPointerArray(); //unsupported pointer type
EXPECT_EQ(nullptr, celix_utils_arrayListToString(list2));
}
56 changes: 9 additions & 47 deletions libs/utils/include/celix_convert_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ celix_status_t celix_utils_convertStringToLongArrayList(const char* val,
const celix_array_list_t* defaultValue,
celix_array_list_t** list);

/**
* @brief Convert a celix_array_list_t* with long entries to a string.
*
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG.
*
* @param[in] list The list to convert.
* @return The string representation of the list. The returned string is allocated and should be freed.
*/
CELIX_UTILS_EXPORT
char* celix_utils_longArrayListToString(const celix_array_list_t* list);

/**
* @brief Convert a string to a celix_array_list_t* with double entries.
*
Expand All @@ -130,17 +119,6 @@ celix_status_t celix_utils_convertStringToDoubleArrayList(const char* val,
const celix_array_list_t* defaultValue,
celix_array_list_t** list);

/**
* @brief Convert a celix_array_list_t* with double entries to a string.
*
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE.
*
* @param[in] list The list to convert.
* @return The string representation of the list. The returned string is allocated and should be freed.
*/
CELIX_UTILS_EXPORT
char* celix_utils_doubleArrayListToString(const celix_array_list_t* list);

/**
* @brief Convert a string to a celix_array_list_t* with boolean entries.
*
Expand All @@ -157,17 +135,6 @@ celix_status_t celix_utils_convertStringToBoolArrayList(const char* val,
const celix_array_list_t* defaultValue,
celix_array_list_t** list);

/**
* @brief Convert a celix_array_list_t* with boolean entries to a string.
*
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL.
*
* @param[in] list The list to convert.
* @return The string representation of the list. The returned string is allocated and should be freed.
*/
CELIX_UTILS_EXPORT
char* celix_utils_boolArrayListToString(const celix_array_list_t* list);

/**
* @brief Convert a string to a celix_array_list_t* with string entries.
*
Expand All @@ -190,17 +157,6 @@ celix_status_t celix_utils_convertStringToStringArrayList(const char* val,
const celix_array_list_t* defaultValue,
celix_array_list_t** list);

/**
* @brief Convert a celix_array_list_t* with string entries to a string.
*
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING.
*
* @param[in] list The list to convert.
* @return The string representation of the list. The returned string is allocated and should be freed.
*/
CELIX_UTILS_EXPORT
char* celix_utils_stringArrayListToString(const celix_array_list_t* list);

/**
* @brief Convert a string to a celix_array_list_t* with celix_version_t* entries.
*
Expand All @@ -222,15 +178,21 @@ celix_status_t celix_utils_convertStringToVersionArrayList(const char* val,
celix_array_list_t** list);

/**
* @brief Convert a celix_array_list_t* with version entries to a string.
* @brief Convert a celix_array_list_t* with a string, bool, long, double of celix_version_t entries to a string.
*
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION.
* @note The array list must an array list of the type CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING,
* CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL, CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG, CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE,
* or CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION.
*
* @param[in] list The list to convert.
* @return The string representation of the list. The returned string is allocated and should be freed.
* @retrunvalue NULL if the list is NULL or if the list entry type is CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED or
* CELIX_ARRAY_LIST_ELEMENT_TYPE_POINTER.
*/
CELIX_UTILS_EXPORT
char* celix_utils_versionArrayListToString(const celix_array_list_t* list);
char* celix_utils_arrayListToString(const celix_array_list_t* list);



#ifdef __cplusplus
}
Expand Down
52 changes: 19 additions & 33 deletions libs/utils/src/celix_convert_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ celix_status_t celix_utils_convertStringToVersionArrayList(const char* val,
* @param printCb The callback to use for printing the list entries.
* @return The string representation of the list or NULL if an error occurred.
*/
static char* celix_utils_arrayListToString(const celix_array_list_t* list,
static char* celix_utils_arrayListToStringInternal(const celix_array_list_t* list,
int (*printCb)(FILE* stream, const celix_array_list_entry_t* entry)) {
char* result = NULL;
size_t len;
Expand Down Expand Up @@ -326,35 +326,14 @@ static int celix_utils_printLongEntry(FILE* stream, const celix_array_list_entry
return fprintf(stream, "%li", entry->longVal);
}

char* celix_utils_longArrayListToString(const celix_array_list_t* list) {
if (list) {
assert(celix_arrayList_getElementType(list) == CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG);
}
return celix_utils_arrayListToString(list, celix_utils_printLongEntry);
}

static int celix_utils_printDoubleEntry(FILE* stream, const celix_array_list_entry_t* entry) {
return fprintf(stream, "%lf", entry->doubleVal);
}

char* celix_utils_doubleArrayListToString(const celix_array_list_t* list) {
if (list) {
assert(celix_arrayList_getElementType(list) == CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE);
}
return celix_utils_arrayListToString(list, celix_utils_printDoubleEntry);
}

static int celix_utils_printBoolEntry(FILE* stream, const celix_array_list_entry_t* entry) {
return fprintf(stream, "%s", entry->boolVal ? "true" : "false");
}

char* celix_utils_boolArrayListToString(const celix_array_list_t* list) {
if (list) {
assert(celix_arrayList_getElementType(list) == CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL);
}
return celix_utils_arrayListToString(list, celix_utils_printBoolEntry);
}

static int celix_utils_printStrEntry(FILE* stream, const celix_array_list_entry_t* entry) {
const char* str = entry->stringVal;
int rc = 0;
Expand All @@ -373,13 +352,6 @@ static int celix_utils_printStrEntry(FILE* stream, const celix_array_list_entry_
return rc;
}

char* celix_utils_stringArrayListToString(const celix_array_list_t* list) {
if (list) {
assert(celix_arrayList_getElementType(list) == CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING);
}
return celix_utils_arrayListToString(list, celix_utils_printStrEntry);
}

static int celix_utils_printVersionEntry(FILE* stream, const celix_array_list_entry_t* entry) {
celix_version_t* version = entry->voidPtrVal;
int major = celix_version_getMajor(version);
Expand All @@ -392,9 +364,23 @@ static int celix_utils_printVersionEntry(FILE* stream, const celix_array_list_en
return fprintf(stream, "%i.%i.%i.%s", major, minor, micro, q);
}

char* celix_utils_versionArrayListToString(const celix_array_list_t* list) {
if (list) {
assert(celix_arrayList_getElementType(list) == CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION);
char* celix_utils_arrayListToString(const celix_array_list_t* list) {
if (!list) {
return NULL;
}
celix_array_list_element_type_t elType = celix_arrayList_getElementType(list);
switch (elType) {
case CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG:
return celix_utils_arrayListToStringInternal(list, celix_utils_printLongEntry);
case CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE:
return celix_utils_arrayListToStringInternal(list, celix_utils_printDoubleEntry);
case CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL:
return celix_utils_arrayListToStringInternal(list, celix_utils_printBoolEntry);
case CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING:
return celix_utils_arrayListToStringInternal(list, celix_utils_printStrEntry);
case CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION:
return celix_utils_arrayListToStringInternal(list, celix_utils_printVersionEntry);
default:
return NULL;
}
return celix_utils_arrayListToString(list, celix_utils_printVersionEntry);
}
19 changes: 8 additions & 11 deletions libs/utils/src/properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ static celix_status_t celix_properties_fillEntry(celix_properties_t* properties,
memcpy(entry, prototype, sizeof(*entry));
entry->value = NULL;
if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION) {
bool written = celix_version_fillString(entry->typed.versionValue, convertedValueBuffer, sizeof(convertedValueBuffer));
bool written =
celix_version_fillString(entry->typed.versionValue, convertedValueBuffer, sizeof(convertedValueBuffer));
if (written) {
entry->value = celix_properties_createString(properties, convertedValueBuffer);
} else {
Expand All @@ -162,16 +163,12 @@ static celix_status_t celix_properties_fillEntry(celix_properties_t* properties,
}
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_BOOL) {
entry->value = entry->typed.boolValue ? CELIX_PROPERTIES_BOOL_TRUE_STRVAL : CELIX_PROPERTIES_BOOL_FALSE_STRVAL;
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_LONG_ARRAY) {
entry->value = celix_utils_longArrayListToString(entry->typed.arrayValue);
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_DOUBLE_ARRAY) {
entry->value = celix_utils_doubleArrayListToString(entry->typed.arrayValue);
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_BOOL_ARRAY) {
entry->value = celix_utils_boolArrayListToString(entry->typed.arrayValue);
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_STRING_ARRAY) {
entry->value = celix_utils_stringArrayListToString(entry->typed.arrayValue);
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION_ARRAY) {
entry->value = celix_utils_versionArrayListToString(entry->typed.arrayValue);
} else if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_LONG_ARRAY ||
entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_DOUBLE_ARRAY ||
entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_BOOL_ARRAY ||
entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_STRING_ARRAY ||
entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION_ARRAY) {
entry->value = celix_utils_arrayListToString(entry->typed.arrayValue);
} else /*string value*/ {
assert(entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_STRING);
entry->value = entry->typed.strValue;
Expand Down

0 comments on commit 9133f17

Please sign in to comment.