Skip to content

Commit

Permalink
gh-87: Refactor properties and filter to use typed array lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Mar 10, 2024
1 parent 5bb3859 commit 0ad0e94
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 1,088 deletions.
29 changes: 13 additions & 16 deletions libs/utils/gtest/src/ConvertUtilsTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ TEST_F(ConvertUtilsTestSuite, ConvertToLongArrayTest) {
}

TEST_F(ConvertUtilsTestSuite, LongArrayToStringTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
celix_autoptr(celix_array_list_t) list = celix_arrayList_createLongArray();
celix_arrayList_addLong(list, 1L);
celix_arrayList_addLong(list, 2L);
celix_arrayList_addLong(list, 3L);
Expand All @@ -351,12 +351,12 @@ TEST_F(ConvertUtilsTestSuite, LongArrayToStringTest) {
EXPECT_STREQ("1,2,3", result);
free(result);

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

celix_autoptr(celix_array_list_t) singleEntryList = celix_arrayList_create();
celix_autoptr(celix_array_list_t) singleEntryList = celix_arrayList_createLongArray();
celix_arrayList_addLong(singleEntryList, 1L);
result = celix_utils_longArrayListToString(singleEntryList);
EXPECT_STREQ("1", result);
Expand All @@ -379,7 +379,7 @@ TEST_F(ConvertUtilsTestSuite, ConvertToDoubleArrayList) {
}

TEST_F(ConvertUtilsTestSuite, DoubleArrayToStringTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
celix_autoptr(celix_array_list_t) list = celix_arrayList_createDoubleArray();
celix_arrayList_addDouble(list, 0.1);
celix_arrayList_addDouble(list, 2.0);
celix_arrayList_addDouble(list, 3.3);
Expand Down Expand Up @@ -412,7 +412,7 @@ TEST_F(ConvertUtilsTestSuite, ConvertToBoolArrayList) {
}

TEST_F(ConvertUtilsTestSuite, BoolArrayToStringTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
celix_autoptr(celix_array_list_t) list = celix_arrayList_createBoolArray();
celix_arrayList_addBool(list, true);
celix_arrayList_addBool(list, false);
celix_arrayList_addBool(list, true);
Expand Down Expand Up @@ -470,7 +470,7 @@ TEST_F(ConvertUtilsTestSuite, ConvertToStringArrayList) {
}

TEST_F(ConvertUtilsTestSuite, StringArrayToStringTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
celix_autoptr(celix_array_list_t) list = celix_arrayList_createStringArray();
celix_arrayList_addString(list, "a");
celix_arrayList_addString(list, "b");
celix_arrayList_addString(list, "c");
Expand All @@ -485,15 +485,12 @@ TEST_F(ConvertUtilsTestSuite, StringArrayToStringTest) {
EXPECT_STREQ(R"(a,b,c,d\\\,,e)", result);

//Check if the result can be converted back to an equal list
celix_array_list_t* listResult;
celix_autoptr(celix_array_list_t) listResult;
celix_status_t convertState = celix_utils_convertStringToStringArrayList(result, nullptr, &listResult);
EXPECT_EQ(CELIX_SUCCESS, convertState);
EXPECT_TRUE(listResult != nullptr);

EXPECT_EQ(celix_arrayList_size(list), celix_arrayList_size(listResult));
for (int i = 0; i < celix_arrayList_size(list); ++i) {
EXPECT_STREQ((char*)celix_arrayList_getString(list, i), (char*)celix_arrayList_getString(listResult, i));
}
EXPECT_TRUE(celix_arrayList_equals(list, listResult));
free(result);
}

TEST_F(ConvertUtilsTestSuite, ConvertToVersionArrayList) {
Expand All @@ -516,10 +513,10 @@ TEST_F(ConvertUtilsTestSuite, VersionArrayToStringTest) {
celix_autoptr(celix_version_t) v1 = celix_version_create(1, 2, 3, nullptr);
celix_autoptr(celix_version_t) v2 = celix_version_create(2, 3, 4, nullptr);
celix_autoptr(celix_version_t) v3 = celix_version_create(3, 4, 5, "qualifier");
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
celix_arrayList_add(list, v1);
celix_arrayList_add(list, v2);
celix_arrayList_add(list, v3);
celix_autoptr(celix_array_list_t) list = celix_arrayList_createVersionArray();
celix_arrayList_addVersion(list, v1);
celix_arrayList_addVersion(list, v2);
celix_arrayList_addVersion(list, v3);

char* result = celix_utils_versionArrayListToString(list);
EXPECT_STREQ("1.2.3,2.3.4,3.4.5.qualifier", result);
Expand Down
10 changes: 5 additions & 5 deletions libs/utils/gtest/src/CxxPropertiesTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ TEST_F(CxxPropertiesTestSuite, ArrayListTest) {
EXPECT_EQ(0, props.size());

// Test set
props.setStrings("key1", {"value1", "value2"});
props.setLongs("key2", {1, 2});
props.setDoubles("key3", {1.1, 2.2});
props.setBooleans("key4", {true, false});
props.setVersions("key5", {celix::Version{1, 2, 3}, celix::Version{2, 3, 4}});
props.setVector("key1", std::vector<std::string>{"value1", "value2"});
props.setVector("key2", std::vector<long>{1, 2});
props.setVector("key3", std::vector<double>{1.1, 2.2});
props.setVector("key4", std::vector<bool>{true, false});
props.setVector("key5", std::vector<celix::Version>{celix::Version{1, 2, 3}, celix::Version{2, 3, 4}});
EXPECT_EQ(5, props.size());

// Test get type
Expand Down
52 changes: 34 additions & 18 deletions libs/utils/gtest/src/FilterTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,21 +603,30 @@ TEST_F(FilterTestSuite, UnmatchedTypeMatchTest) {
}

TEST_F(FilterTestSuite, MatchArrayTypesTest) {
const char* strings[] = {"a", "b", "c"};
const long longs[] = {1, 2, 3};
const double doubles[] = {1.0, 2.0, 3.0};
const bool bools[] = {true, true, true};
celix_autoptr(celix_version_t) v1 = celix_version_createVersionFromString("1.0.0");
celix_autoptr(celix_version_t) v2 = celix_version_createVersionFromString("2.0.0");
celix_autoptr(celix_version_t) v3 = celix_version_createVersionFromString("3.0.0");
const celix_version_t* versions[] = {v1, v2, v3};

//Given a string, long, double, bool and version array list.
celix_autoptr(celix_array_list_t) stringList = celix_arrayList_createStringArray();
celix_arrayList_addString(stringList, "a");
celix_arrayList_addString(stringList, "b");
celix_autoptr(celix_array_list_t) longList = celix_arrayList_createLongArray();
celix_arrayList_addLong(longList, 1);
celix_arrayList_addLong(longList, 2);
celix_autoptr(celix_array_list_t) doubleList = celix_arrayList_createDoubleArray();
celix_arrayList_addDouble(doubleList, 2.0);
celix_arrayList_addDouble(doubleList, 3.0);
celix_autoptr(celix_array_list_t) boolList = celix_arrayList_createBoolArray();
celix_arrayList_addBool(boolList, true);
celix_arrayList_addBool(boolList, true);
celix_autoptr(celix_array_list_t) versionList = celix_arrayList_createVersionArray();
celix_arrayList_assignVersion(versionList, celix_version_createVersionFromString("2.0.0"));
celix_arrayList_assignVersion(versionList, celix_version_createVersionFromString("3.0.0"));

//And a properties with these array lists
celix_autoptr(celix_properties_t) props = celix_properties_create();
celix_properties_setStrings(props, "strings", strings, 3);
celix_properties_setLongs(props, "longs", longs, 3);
celix_properties_setDoubles(props, "doubles", doubles, 3);
celix_properties_setBooleans(props, "bools", bools, 3);
celix_properties_setVersions(props, "versions", versions, 3);
celix_properties_setArrayList(props, "strings", stringList);
celix_properties_setArrayList(props, "longs", longList);
celix_properties_setArrayList(props, "doubles", doubleList);
celix_properties_setArrayList(props, "bools", boolList);
celix_properties_setArrayList(props, "versions", versionList);

// Check if match is true if any of the array elements match
celix_autoptr(celix_filter_t) filter1 = celix_filter_create("(strings=a)");
Expand Down Expand Up @@ -663,9 +672,13 @@ TEST_F(FilterTestSuite, MatchArrayTypesTest) {
}

TEST_F(FilterTestSuite, ApproxWithArrayAttributesTest) {
const char* strings[] = {"abcdef", "defghi", "ghijkl"};
celix_array_list_t* stringList = celix_arrayList_createStringArray();
celix_arrayList_addString(stringList, "abcdef");
celix_arrayList_addString(stringList, "defghi");
celix_arrayList_addString(stringList, "ghijkl");

celix_autoptr(celix_properties_t) props = celix_properties_create();
celix_properties_setStrings(props, "strings", strings, 3);
celix_properties_assignArrayList(props, "strings", stringList);

celix_autoptr(celix_filter_t) filter1 = celix_filter_create("(strings~=abc)");
EXPECT_TRUE(filter1 != nullptr);
Expand All @@ -685,9 +698,12 @@ TEST_F(FilterTestSuite, ApproxWithArrayAttributesTest) {
}

TEST_F(FilterTestSuite, SubStringWithArrayAttributesTest) {
const char* strings[] = {"John Doe", "Jane Doe", "John Smith"};
celix_array_list_t* stringList = celix_arrayList_createStringArray();
celix_arrayList_addString(stringList, "John Doe");
celix_arrayList_addString(stringList, "Jane Doe");
celix_arrayList_addString(stringList, "John Smith");
celix_autoptr(celix_properties_t) props = celix_properties_create();
celix_properties_setStrings(props, "strings", strings, 3);
celix_properties_assignArrayList(props, "strings", stringList);

celix_autoptr(celix_filter_t) filter1 = celix_filter_create("(strings=John*)");
EXPECT_TRUE(filter1 != nullptr);
Expand Down
133 changes: 24 additions & 109 deletions libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,29 @@ TEST_F(PropertiesErrorInjectionTestSuite, GetAsArrayWithArrayListCopyFailedTest)
//Given a properties set with a string array, long array, double array, bool array and version array
const char* str1 = "string1";
const char* str2 = "string2";
const char* stringsArray[] = {str1, str2};
long longsArray[] = {1, 2, 3};
double doublesArray[] = {1.1, 2.2, 3.3};
bool boolsArray[] = {true, false, true};
celix_autoptr(celix_array_list_t) stringList = celix_arrayList_createStringArray();
celix_arrayList_add(stringList, (void*)str1);
celix_arrayList_add(stringList, (void*)str2);
celix_autoptr(celix_array_list_t) longList = celix_arrayList_createLongArray();
celix_arrayList_addLong(longList, 1);
celix_arrayList_addLong(longList, 2);
celix_autoptr(celix_array_list_t) doubleList = celix_arrayList_createDoubleArray();
celix_arrayList_addDouble(doubleList, 1.1);
celix_arrayList_addDouble(doubleList, 2.2);
celix_autoptr(celix_array_list_t) boolList = celix_arrayList_createBoolArray();
celix_arrayList_addBool(boolList, true);
celix_arrayList_addBool(boolList, false);
celix_autoptr(celix_version_t) v = celix_version_create(1, 2, 3, "qualifier");
const celix_version_t* versionsArray[] = {v, v, v};
celix_autoptr(celix_array_list_t) versionList = celix_arrayList_createVersionArray();
celix_arrayList_add(versionList, v);
celix_arrayList_add(versionList, v);

celix_autoptr(celix_properties_t) props = celix_properties_create();
celix_properties_setStrings(props, "stringArray", stringsArray, 2);
celix_properties_setLongs(props, "longArray", longsArray, 3);
celix_properties_setDoubles(props, "doubleArray", doublesArray, 3);
celix_properties_setBooleans(props, "boolArray", boolsArray, 3);
celix_properties_setVersions(props, "versionArray", versionsArray, 3);
celix_properties_setArrayList(props, "stringArray", stringList);
celix_properties_setArrayList(props, "longArray", longList);
celix_properties_setArrayList(props, "doubleArray", doubleList);
celix_properties_setArrayList(props, "boolArray", boolList);
celix_properties_setArrayList(props, "versionArray", versionList);

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_getAsStringArrayList
celix_ei_expect_celix_arrayList_createWithOptions((void*)celix_properties_getAsStringArrayList, 1, nullptr);
Expand Down Expand Up @@ -352,106 +362,11 @@ TEST_F(PropertiesErrorInjectionTestSuite, SetArrayWithArrayListCopyFailedTest) {
//And a (empty) array list
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();

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

// Then the celix_properties_setLongArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setLongArrayList(props, "longArray", list));

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

// Then the celix_properties_setDoubleArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setDoubleArrayList(props, "doubleArray", list));

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

// Then the celix_properties_setBoolArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setBoolArrayList(props, "boolArray", list));

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_setVersionArrayList
celix_ei_expect_celix_arrayList_createWithOptions((void*)celix_properties_setVersionArrayList, 0, nullptr);

// Then the celix_properties_setVersionArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setVersionArrayList(props, "versionArray", list));

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_setStringArrayList
celix_ei_expect_celix_arrayList_createWithOptions((void*)celix_properties_setStringArrayList, 0, nullptr);

// Then the celix_properties_setStringArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setStringArrayList(props, "stringArray", list));


//Given a raw array for each type
long longsArray[] = {1, 2, 3};
double doublesArray[] = {1.1, 2.2, 3.3};
bool boolsArray[] = {true, false, true};
celix_autoptr(celix_version_t) v = celix_version_create(1, 2, 3, "qualifier");
const celix_version_t* versionsArray[] = {v, v, v};
const char* stringsArray[] = {"string", "string2"};


// When a celix_arrayList_create error injection is set for celix_properties_setLongs
celix_ei_expect_celix_arrayList_create((void*)celix_properties_setLongs, 0, nullptr);

// Then the celix_properties_setLongs call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setLongs(props, "longArray", longsArray, 3));

// When a celix_arrayList_create error injection is set for celix_properties_setDoubles
celix_ei_expect_celix_arrayList_create((void*)celix_properties_setDoubles, 0, nullptr);

// Then the celix_properties_setDoubles call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setDoubles(props, "doubleArray", doublesArray, 3));

// When a celix_arrayList_create error injection is set for celix_properties_setBooleans
celix_ei_expect_celix_arrayList_create((void*)celix_properties_setBooleans, 0, nullptr);

// Then the celix_properties_setBooleans call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setBooleans(props, "boolArray", boolsArray, 3));

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_setVersions
celix_ei_expect_celix_arrayList_createWithOptions((void*)celix_properties_setVersions, 0, nullptr);

// Then the celix_properties_setVersions call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setVersions(props, "versionArray", versionsArray, 3));

// When a celix_arrayList_createWithOptions error injection is set for celix_properties_setStrings
celix_ei_expect_celix_arrayList_createWithOptions((void*)celix_properties_setStrings, 0, nullptr);

// Then the celix_properties_setStrings call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setStrings(props, "stringArray", stringsArray, 3));


// When a celix_arrayList_addLong error injection is set for celix_properties_setLongArrayList
celix_ei_expect_celix_arrayList_addLong((void*)celix_properties_setLongs, 0, CELIX_ENOMEM);

// Then the celix_properties_setLongArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setLongs(props, "longArray", longsArray, 3));

// When a celix_arrayList_addDouble error injection is set for celix_properties_setDoubleArrayList
celix_ei_expect_celix_arrayList_addDouble((void*)celix_properties_setDoubles, 0, CELIX_ENOMEM);

// Then the celix_properties_setDoubleArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setDoubles(props, "doubleArray", doublesArray, 3));

// When a celix_arrayList_addBool error injection is set for celix_properties_setBoolArrayList
celix_ei_expect_celix_arrayList_addBool((void*)celix_properties_setBooleans, 0, CELIX_ENOMEM);

// Then the celix_properties_setBoolArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setBooleans(props, "boolArray", boolsArray, 3));

// When a celix_arrayList_add error injection is set for celix_properties_setVersionArrayList
celix_ei_expect_celix_arrayList_add((void*)celix_properties_setVersions, 0, CELIX_ENOMEM);

// Then the celix_properties_setVersionArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setVersions(props, "versionArray", versionsArray, 3));

// When a celix_arrayList_addString error injection is set for celix_properties_setStringArrayList
celix_ei_expect_celix_arrayList_addString((void*)celix_properties_setStrings, 0, CELIX_ENOMEM);

// Then the celix_properties_setStringArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setStrings(props, "stringArray", stringsArray, 3));
// Then the celix_properties_setArrayList call fails
EXPECT_EQ(CELIX_ENOMEM, celix_properties_setArrayList(props, "longArray", list));
}


Expand Down
Loading

0 comments on commit 0ad0e94

Please sign in to comment.