From 9133f178161fab4580de884b2b26019781ef7e27 Mon Sep 17 00:00:00 2001 From: Pepijn Noltes Date: Mon, 25 Mar 2024 19:58:21 +0100 Subject: [PATCH] gh-674: Combine typed array list to string utils functions --- .../ConvertUtilsErrorInjectionTestSuite.cc | 8 +-- libs/utils/gtest/src/ConvertUtilsTestSuite.cc | 24 +++++--- libs/utils/include/celix_convert_utils.h | 56 +++---------------- libs/utils/src/celix_convert_utils.c | 52 +++++++---------- libs/utils/src/properties.c | 19 +++---- 5 files changed, 55 insertions(+), 104 deletions(-) diff --git a/libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc b/libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc index 6705acb15..c74c70199 100644 --- a/libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc +++ b/libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc @@ -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); } diff --git a/libs/utils/gtest/src/ConvertUtilsTestSuite.cc b/libs/utils/gtest/src/ConvertUtilsTestSuite.cc index aa7b6b05d..a991f6fc0 100644 --- a/libs/utils/gtest/src/ConvertUtilsTestSuite.cc +++ b/libs/utils/gtest/src/ConvertUtilsTestSuite.cc @@ -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); } @@ -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); @@ -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); @@ -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 @@ -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); @@ -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)); +} diff --git a/libs/utils/include/celix_convert_utils.h b/libs/utils/include/celix_convert_utils.h index 63b74df79..a30fdb62e 100644 --- a/libs/utils/include/celix_convert_utils.h +++ b/libs/utils/include/celix_convert_utils.h @@ -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. * @@ -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. * @@ -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. * @@ -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. * @@ -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 } diff --git a/libs/utils/src/celix_convert_utils.c b/libs/utils/src/celix_convert_utils.c index fe25ba829..18f87c4d7 100644 --- a/libs/utils/src/celix_convert_utils.c +++ b/libs/utils/src/celix_convert_utils.c @@ -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; @@ -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; @@ -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); @@ -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); } diff --git a/libs/utils/src/properties.c b/libs/utils/src/properties.c index 099809ce6..639afde97 100644 --- a/libs/utils/src/properties.c +++ b/libs/utils/src/properties.c @@ -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 { @@ -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;