Skip to content

Commit

Permalink
gh-674: Refactor array list type for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Apr 1, 2024
1 parent 4d94de2 commit be6ca79
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 220 deletions.
17 changes: 17 additions & 0 deletions libs/utils/gtest/src/ArrayListTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,20 @@ TEST_F(ArrayListTestSuite, ReallocTest) {
EXPECT_EQ(i, celix_arrayList_getLong(list, i));
}
}

TEST_F(ArrayListTestSuite, ElementTypeToStringTest) {
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_POINTER",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_POINTER));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL",
celix_arrayList_elementTypeToString(CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL));
EXPECT_STREQ("CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED",
celix_arrayList_elementTypeToString((celix_array_list_element_type_t)100 /*non existing*/));
}
10 changes: 5 additions & 5 deletions libs/utils/gtest/src/CxxPropertiesTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ TEST_F(CxxPropertiesTestSuite, ArrayListTest) {
EXPECT_EQ(5, props.size());

// Test get type
EXPECT_EQ(props.getType("key1"), celix::Properties::ValueType::StringArray);
EXPECT_EQ(props.getType("key2"), celix::Properties::ValueType::LongArray);
EXPECT_EQ(props.getType("key3"), celix::Properties::ValueType::DoubleArray);
EXPECT_EQ(props.getType("key4"), celix::Properties::ValueType::BooleanArray);
EXPECT_EQ(props.getType("key5"), celix::Properties::ValueType::VersionArray);
EXPECT_EQ(props.getType("key1"), celix::Properties::ValueType::Vector);
EXPECT_EQ(props.getType("key2"), celix::Properties::ValueType::Vector);
EXPECT_EQ(props.getType("key3"), celix::Properties::ValueType::Vector);
EXPECT_EQ(props.getType("key4"), celix::Properties::ValueType::Vector);
EXPECT_EQ(props.getType("key5"), celix::Properties::ValueType::Vector);

// Test getAs with valid key
auto strings = props.getAsStringVector("key1");
Expand Down
10 changes: 5 additions & 5 deletions libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
celix_properties_setArrayList(props, "versionArray", versionList);

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_getAsStringArrayList
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsStringArrayList, 0, nullptr);
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsStringArrayList, 1, nullptr);

// Then the celix_properties_getAsStringArrayList call fails
celix_array_list_t* strings = nullptr;
Expand All @@ -320,7 +320,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
ASSERT_EQ(nullptr, strings);

//When a celix_arrayList_copy error injection is set for celix_properties_getAsLongArrayList
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsLongArrayList, 0, nullptr);
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsLongArrayList, 1, nullptr);

// Then the celix_properties_getAsLongArrayList call fails
celix_array_list_t* longs = nullptr;
Expand All @@ -329,7 +329,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
ASSERT_EQ(nullptr, longs);

//When a celix_arrayList_copy error injection is set for celix_properties_getAsDoubleArrayList
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsDoubleArrayList, 0, nullptr);
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsDoubleArrayList, 1, nullptr);

// Then the celix_properties_getAsDoubleArrayList call fails
celix_array_list_t* doubles = nullptr;
Expand All @@ -338,7 +338,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
ASSERT_EQ(nullptr, doubles);

//When a celix_arrayList_copy error injection is set for celix_properties_getAsBoolArrayList
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsBoolArrayList, 0, nullptr);
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsBoolArrayList, 1, nullptr);

// Then the celix_properties_getAsBoolArrayList call fails
celix_array_list_t* bools = nullptr;
Expand All @@ -347,7 +347,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
ASSERT_EQ(nullptr, bools);

//When a celix_arrayList_createWithOptions error injection is set for celix_properties_getAsVersionArrayList
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsVersionArrayList, 0, nullptr);
celix_ei_expect_celix_arrayList_copy((void*)celix_properties_getAsVersionArrayList, 1, nullptr);

// Then the celix_properties_getAsVersionArrayList call fails
celix_array_list_t* versions = nullptr;
Expand Down
42 changes: 42 additions & 0 deletions libs/utils/gtest/src/PropertiesTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,50 @@ TEST_F(PropertiesTestSuite, GetTypedArrayListTest) {
EXPECT_EQ(doubleList, retrievedDoubleList2);
EXPECT_EQ(boolList, retrievedBoolList2);
EXPECT_EQ(versionList, retrievedVersionList2);

//When using the celix_properties_getArrayList function to retrieve the array lists
const auto* retrievedStringList3 = celix_properties_getArrayList(props, "stringList", nullptr);
const auto* retrievedLongList3 = celix_properties_getArrayList(props, "longList", nullptr);
const auto* retrievedDoubleList3 = celix_properties_getArrayList(props, "doubleList", nullptr);
const auto* retrievedBoolList3 = celix_properties_getArrayList(props, "boolList", nullptr);
const auto* retrievedVersionList3 = celix_properties_getArrayList(props, "versionList", nullptr);

//Then the retrieved array lists should be the same as the original array lists
EXPECT_TRUE(celix_arrayList_equals(stringList, retrievedStringList3));
EXPECT_TRUE(celix_arrayList_equals(longList, retrievedLongList3));
EXPECT_TRUE(celix_arrayList_equals(doubleList, retrievedDoubleList3));
EXPECT_TRUE(celix_arrayList_equals(boolList, retrievedBoolList3));
EXPECT_TRUE(celix_arrayList_equals(versionList, retrievedVersionList3));
}

TEST_F(PropertiesTestSuite, GetAsTypedArrayListWithInvalidDefault) {
celix_autoptr(celix_array_list_t) ptrList = celix_arrayList_createPointerArray();
celix_autoptr(celix_properties_t) props = celix_properties_create();

celix_array_list_t* outList = nullptr;

EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_getAsStringArrayList(nullptr, "aKey", ptrList, &outList));
EXPECT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_getAsLongArrayList(nullptr, "aKey", ptrList, &outList));
EXPECT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_getAsDoubleArrayList(nullptr, "aKey", ptrList, &outList));
EXPECT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_getAsBoolArrayList(nullptr, "aKey", ptrList, &outList));
EXPECT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, celix_properties_getAsVersionArrayList(nullptr, "aKey", ptrList, &outList));
EXPECT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();
}


TEST_F(PropertiesTestSuite, SetArrayListWithIllegalArgumentsTest) {
//Given a properties object
celix_autoptr(celix_properties_t) props = celix_properties_create();
Expand Down
31 changes: 10 additions & 21 deletions libs/utils/include/celix/Properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,14 @@ namespace celix {
* @brief Enum representing the possible types of a property value.
*/
enum class ValueType {
Unset, /**< Property value is not set. */
String, /**< Property value is a string. */
Long, /**< Property value is a long integer. */
Double, /**< Property value is a double. */
Bool, /**< Property value is a boolean. */
Version, /**< Property value is a Celix version. */
LongArray, /**< Property value is an array of long integers. */
DoubleArray, /**< Property value is an array of doubles. */
BooleanArray, /**< Property value is an array of booleans. */
VersionArray, /**< Property value is an array of Celix versions. */
StringArray /**< Property value is an array of strings. */
Unset, /**< Property value is not set. */
String, /**< Property value is a string. */
Long, /**< Property value is a long integer. */
Double, /**< Property value is a double. */
Bool, /**< Property value is a boolean. */
Version, /**< Property value is a Celix version. */
Vector, /**< Property value is a vector of long integers, doubles, booleans, celix::Version or
string. */
};

class ValueRef {
Expand Down Expand Up @@ -995,16 +992,8 @@ namespace celix {
return ValueType::Bool;
case CELIX_PROPERTIES_VALUE_TYPE_VERSION:
return ValueType::Version;
case CELIX_PROPERTIES_VALUE_TYPE_LONG_ARRAY:
return ValueType::LongArray;
case CELIX_PROPERTIES_VALUE_TYPE_DOUBLE_ARRAY:
return ValueType::DoubleArray;
case CELIX_PROPERTIES_VALUE_TYPE_BOOL_ARRAY:
return ValueType::BooleanArray;
case CELIX_PROPERTIES_VALUE_TYPE_VERSION_ARRAY:
return ValueType::VersionArray;
case CELIX_PROPERTIES_VALUE_TYPE_STRING_ARRAY:
return ValueType::StringArray;
case CELIX_PROPERTIES_VALUE_TYPE_ARRAY_LIST:
return ValueType::Vector;
default: /*unset*/
return ValueType::Unset;
}
Expand Down
9 changes: 9 additions & 0 deletions libs/utils/include/celix_array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ bool celix_arrayList_equals(const celix_array_list_t* listA, const celix_array_l
CELIX_UTILS_EXPORT
celix_array_list_t* celix_arrayList_copy(const celix_array_list_t* list);

/**
* @brief Returns the element type as a string.
* The returned string is owned by the celix_arrayList library and should not be freed.
* @param type The element type.
* @return The element type as a string. Returns "Undefined" if the element type is not recognized.
*/
CELIX_UTILS_EXPORT
const char* celix_arrayList_elementTypeToString(celix_array_list_element_type_t type);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit be6ca79

Please sign in to comment.