Skip to content

Commit

Permalink
#674 Add additional array list ei tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Feb 5, 2024
1 parent 1f324c5 commit 2105123
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
43 changes: 43 additions & 0 deletions libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
#include <gtest/gtest.h>

#include "celix_array_list.h"
#include "celix_array_list_ei.h"
#include "celix_err.h"
#include "celix_utils_ei.h"
#include "celix_version_ei.h"
#include "malloc_ei.h"

class ArrayListErrorInjectionTestSuite : public ::testing::Test {
public:
ArrayListErrorInjectionTestSuite() = default;
~ArrayListErrorInjectionTestSuite() noexcept override {
celix_ei_expect_realloc(nullptr, 0, nullptr);
celix_ei_expect_calloc(nullptr, 0, nullptr);
celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
celix_ei_expect_celix_version_copy(nullptr, 0, nullptr);
}
};

Expand All @@ -49,3 +56,39 @@ TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) {

celix_arrayList_destroy(list);
}

TEST_F(ArrayListErrorInjectionTestSuite, AddStringAndAddVersionFailureTest) {
// Given a string array list
celix_autoptr(celix_array_list_t) stringList = celix_arrayList_createStringArray();
// When an error is injected for celix_utils_strdup
celix_ei_expect_celix_utils_strdup((void*)celix_arrayList_addString, 0, nullptr);
// Then adding a string should fail
EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addString(stringList, "test"));

// Given a version array list
celix_autoptr(celix_array_list_t) versionList = celix_arrayList_createVersionArray();
// When an error is injected for celix_version_copy
celix_ei_expect_celix_version_copy((void*)celix_arrayList_addVersion, 0, nullptr);
// Then adding a version should fail
EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addVersion(versionList, NULL));
}

TEST_F(ArrayListErrorInjectionTestSuite, CopyArrayListFailureTest) {
// Given a string array list with 10 elements (whitebox knowledge)
celix_autoptr(celix_array_list_t) stringList = celix_arrayList_createStringArray();
celix_arrayList_addString(stringList, "test1");

// When an error is injected for calloc
celix_ei_expect_calloc((void*)celix_arrayList_copy, 1, nullptr);
// Then copying an array list should fail
EXPECT_EQ(nullptr, celix_arrayList_copy(stringList));
// And a celix_err is expected
EXPECT_EQ(1, celix_err_getErrorCount());

// When an error is injected for celix_utils_strdup
celix_ei_expect_celix_utils_strdup((void*)celix_arrayList_addString, 0, nullptr);
// Then copying an array list should fail
EXPECT_EQ(nullptr, celix_arrayList_copy(stringList));
// And a celix_err is expected
EXPECT_EQ(2, celix_err_getErrorCount());
}
18 changes: 9 additions & 9 deletions libs/utils/gtest/src/ArrayListTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class ArrayListTestSuite : public ::testing::Test {
public:
};

TEST_F(ArrayListTestSuite, TestCreateDestroyHashMap) {
TEST_F(ArrayListTestSuite, CreateDestroyArrayListTest) {
auto* list = celix_arrayList_create();
EXPECT_TRUE(list != nullptr);
EXPECT_EQ(0, celix_arrayList_size(list));
celix_arrayList_destroy(list);
}

TEST_F(ArrayListTestSuite, TestArrayListWithEquals) {
TEST_F(ArrayListTestSuite, ArrayListWithEqualsTest) {
celix_array_list_create_options_t opts{};
opts.equalsCallback = [](celix_array_list_entry_t a, celix_array_list_entry_t b) -> bool {
const char* sa = (char*)a.voidPtrVal;
Expand Down Expand Up @@ -109,7 +109,7 @@ void testArrayListForTemplateType(const std::vector<T>& entries,
celix_arrayList_destroy(list);
}

TEST_F(ArrayListTestSuite, TestDifferentEntyTypesForArrayList) {
TEST_F(ArrayListTestSuite, TestDifferentEntyTypesForArrayListTest) {
std::vector<int> intEntries{1, 2, 3, 4, 5};
testArrayListForTemplateType<int>(intEntries,
celix_arrayList_createIntArray,
Expand Down Expand Up @@ -224,7 +224,7 @@ TEST_F(ArrayListTestSuite, VersionArrayList) {
EXPECT_EQ(2, celix_arrayList_size(versionList));
}

TEST_F(ArrayListTestSuite, SortTypedArrayLists) {
TEST_F(ArrayListTestSuite, SortTypedArrayListsTest) {
// Given a ptr, string, int, long, uint, ulong, float, double, bool, size and version list
// with unsorted values (including duplicates)
celix_autoptr(celix_array_list_t) ptrList = celix_arrayList_createPointerArray();
Expand Down Expand Up @@ -376,7 +376,7 @@ TEST_F(ArrayListTestSuite, SortTypedArrayLists) {
EXPECT_EQ(0, celix_version_compareToMajorMinor(celix_arrayList_getVersion(versionList, 4), 3, 2));
}

TEST_F(ArrayListTestSuite, EqualCheck) {
TEST_F(ArrayListTestSuite, EqualCheckTest) {
//Given a selection of long list and a double list
celix_autoptr(celix_array_list_t) list1 = celix_arrayList_createLongArray();
celix_arrayList_addLong(list1, 1L);
Expand Down Expand Up @@ -437,7 +437,7 @@ TEST_F(ArrayListTestSuite, CopyArrayTest) {
EXPECT_TRUE(celix_arrayList_equals(versionList, versionListCopy));
}

TEST_F(ArrayListTestSuite, TestSimpleRemovedCallbacksForArrayList) {
TEST_F(ArrayListTestSuite, SimpleRemovedCallbacksForArrayListTest) {
celix_array_list_create_options_t opts{};
opts.simpleRemovedCallback = free;
auto* list = celix_arrayList_createWithOptions(&opts);
Expand All @@ -449,7 +449,7 @@ TEST_F(ArrayListTestSuite, TestSimpleRemovedCallbacksForArrayList) {
celix_arrayList_destroy(list); //will call free for every entry
}

TEST_F(ArrayListTestSuite, TestRemovedCallbacksForArrayList) {
TEST_F(ArrayListTestSuite, RemovedCallbacksForArrayListTest) {
int count = 0 ;
celix_array_list_create_options_t opts{};
opts.removedCallbackData = &count;
Expand All @@ -473,7 +473,7 @@ TEST_F(ArrayListTestSuite, TestRemovedCallbacksForArrayList) {

}

TEST_F(ArrayListTestSuite, TestSortForArrayList) {
TEST_F(ArrayListTestSuite, SortForArrayListTest) {
auto* list = celix_arrayList_create();
celix_arrayList_addInt(list, 3);
celix_arrayList_addInt(list, 2);
Expand All @@ -497,7 +497,7 @@ TEST_F(ArrayListTestSuite, TestSortForArrayList) {
celix_arrayList_destroy(list);
}

TEST_F(ArrayListTestSuite, TestReturnStatusAddFunctions) {
TEST_F(ArrayListTestSuite, ReturnStatusAddFunctionsTest) {
auto* list = celix_arrayList_create();
ASSERT_TRUE(list != nullptr);
EXPECT_EQ(0, celix_arrayList_size(list));
Expand Down
2 changes: 2 additions & 0 deletions libs/utils/include/celix_array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ bool celix_arrayList_equals(const celix_array_list_t* listA, const celix_array_l
* if the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION, the versions will be copied.
* For all other element types the values will be copied.
*
* If a NULL is returned and the original array list is not NULL, a error message is logged to celix_err.
*
* @param[in] list The array list to copy.
* @return A new array list with the same element type and values as the original array list or NULL if the original
* array list is NULL or out of memory.
Expand Down
25 changes: 6 additions & 19 deletions libs/utils/src/array_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static int celix_arrayList_compareStringEntries(celix_array_list_entry_t a, celi
}

static bool celix_arrayList_stringEquals(celix_array_list_entry_t a, celix_array_list_entry_t b) {
return celix_arrayList_compareStringEntries(a, b) == 0;
return celix_utils_stringEquals(a.stringVal, b.stringVal);
}

static int celix_arrayList_compareIntEntries(celix_array_list_entry_t a, celix_array_list_entry_t b) {
Expand Down Expand Up @@ -325,13 +325,6 @@ celix_array_list_t* celix_arrayList_createVersionArray() {
return celix_arrayList_createTypedArray(CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION);
}


celix_array_list_t* celix_arrayList_createWithEquals(celix_arrayList_equals_fp equals) {
celix_array_list_create_options_t opts = CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS;
opts.equalsCallback = equals;
return celix_arrayList_createWithOptions(&opts);
}

void celix_arrayList_destroy(celix_array_list_t *list) {
if (list != NULL) {
celix_arrayList_clear(list);
Expand Down Expand Up @@ -752,20 +745,14 @@ celix_array_list_t* celix_arrayList_copy(const celix_array_list_t* list) {

for (int i = 0; i < celix_arrayList_size(list); ++i) {
celix_array_list_entry_t entry = list->elementData[i];
celix_status_t status;
if (copy->elementType == CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING) {
entry.stringVal = celix_utils_strdup(entry.stringVal);
if (entry.stringVal == NULL) {
celix_err_push("Failed to copy string entry. Out of memory.");
return NULL;
}
status = celix_arrayList_addString(copy, entry.stringVal);
} else if (copy->elementType == CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION) {
entry.versionVal = celix_version_copy(entry.versionVal);
if (entry.versionVal == NULL) {
celix_err_push("Failed to copy version entry. Out of memory.");
return NULL;
}
status = celix_arrayList_addVersion(copy, entry.versionVal);
} else {
status = celix_arrayList_addEntry(copy, entry);
}
celix_status_t status = celix_arrayList_addEntry(copy, entry);
if (status != CELIX_SUCCESS) {
celix_err_push("Failed to add entry to copy list.");
return NULL;
Expand Down

0 comments on commit 2105123

Please sign in to comment.