Skip to content

Commit

Permalink
gh-674: Add missing equals callback check in array list equals
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Feb 25, 2024
1 parent a8937f1 commit 9ae479e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
20 changes: 17 additions & 3 deletions libs/utils/gtest/src/ArrayListTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_F(ArrayListTestSuite, ArrayListWithEqualsTest) {
const char* sb = (char*)b.voidPtrVal;
return celix_utils_stringEquals(sa, sb);
};
auto* list = celix_arrayList_createWithOptions(&opts);
celix_autoptr(celix_array_list_t) list = celix_arrayList_createWithOptions(&opts);
EXPECT_EQ(0, celix_arrayList_size(list));

celix_arrayList_add(list, (void*)"val0");
Expand Down Expand Up @@ -80,8 +80,6 @@ TEST_F(ArrayListTestSuite, ArrayListWithEqualsTest) {
EXPECT_EQ(celix_arrayList_size(list), 2);
EXPECT_STREQ((char*)celix_arrayList_get(list, 0), "val1");
EXPECT_STREQ((char*)celix_arrayList_get(list, 1), "val3");

celix_arrayList_destroy(list);
}

template<typename T>
Expand Down Expand Up @@ -280,6 +278,21 @@ TEST_F(ArrayListTestSuite, EqualCheckTest) {
celix_autoptr(celix_array_list_t) list5 = celix_arrayList_createDoubleArray(); //different type than list1
celix_arrayList_addDouble(list5, 1.0);

// And 2 custom pointer list, with different equals callbacks
celix_array_list_create_options_t opts{};
opts.elementType = CELIX_ARRAY_LIST_ELEMENT_TYPE_POINTER;
opts.equalsCallback = [](celix_array_list_entry_t, celix_array_list_entry_t) -> bool {
return true; //dummy equals callback
};
celix_autoptr(celix_array_list_t) list6 = celix_arrayList_createWithOptions(&opts);
celix_arrayList_add(list6, (void*)1);
opts.equalsCallback = [](celix_array_list_entry_t, celix_array_list_entry_t) -> bool {
return false; //dummy equals callback
};
celix_autoptr(celix_array_list_t) list7 = celix_arrayList_createWithOptions(&opts);
celix_arrayList_add(list7, (void*)1);


//The lists can be checked for equality
EXPECT_TRUE(celix_arrayList_equals(list1, list2));
EXPECT_TRUE(celix_arrayList_equals(list1, list1));
Expand All @@ -290,6 +303,7 @@ TEST_F(ArrayListTestSuite, EqualCheckTest) {
EXPECT_FALSE(celix_arrayList_equals(list1, list3));
EXPECT_FALSE(celix_arrayList_equals(list1, list4));
EXPECT_FALSE(celix_arrayList_equals(list1, list5));
EXPECT_FALSE(celix_arrayList_equals(list6, list7));
}

TEST_F(ArrayListTestSuite, CopyArrayTest) {
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/include/celix_array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ void celix_arrayList_sort(celix_array_list_t *list);
* - The array list have the same equals callback
* - The array list have the same values at the same index
*
* Note that the remove callback and compare callback are ignored.
* Note that the remove, compare and copy callbacks are ignored.
*
* If both array list are NULL, they are considered equal.
*
Expand Down
3 changes: 3 additions & 0 deletions libs/utils/src/array_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ bool celix_arrayList_equals(const celix_array_list_t* listA, const celix_array_l
if (!listA || !listB) {
return false;
}
if (listA->elementType != listB->elementType) {
return false;
}
if (listA->size != listB->size) {
return false;
}
Expand Down

0 comments on commit 9ae479e

Please sign in to comment.