This is an automated email from the ASF dual-hosted git repository.
pnoltes pushed a commit to branch feature/674-add-element-type-to-array-list
in repository https://gitbox.apache.org/repos/asf/celix.git
The following commit(s) were added to
refs/heads/feature/674-add-element-type-to-array-list by this push:
new 5097e483 #674 Add add/assign nullptr test for array list
5097e483 is described below
commit 5097e4831f20dbe3c947c0fb327327a1ad3cd3e3
Author: Pepijn Noltes
AuthorDate: Wed Feb 7 19:25:38 2024 +0100
#674 Add add/assign nullptr test for array list
---
.../gtest/src/ArrayListErrorInjectionTestSuite.cc | 26 +++-
libs/utils/gtest/src/ArrayListTestSuite.cc | 64
libs/utils/include/celix_array_list.h | 43 +-
libs/utils/src/array_list.c| 69 +++---
4 files changed, 162 insertions(+), 40 deletions(-)
diff --git a/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
index 309d54d0..f808d3af 100644
--- a/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
+++ b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
@@ -20,7 +20,6 @@
#include
#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"
@@ -34,10 +33,28 @@ public:
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);
+celix_err_resetErrors();
}
};
-TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) {
+TEST_F(ArrayListErrorInjectionTestSuite, CreateTest) {
+//Given an error is injected for calloc (used for the array struct)
+celix_ei_expect_calloc(CELIX_EI_UNKNOWN_CALLER, 1, nullptr);
+//Then creating an array list should fail
+EXPECT_EQ(nullptr, celix_arrayList_create());
+//And an error is logged to the celix_err
+EXPECT_EQ(1, celix_err_getErrorCount());
+
+//Given an error is injected for malloc (used for the element data)
+celix_ei_expect_malloc(CELIX_EI_UNKNOWN_CALLER, 0, nullptr);
+//Then creating an array list should fail
+EXPECT_EQ(nullptr, celix_arrayList_create());
+//And an error is logged to the celix_err
+EXPECT_EQ(2, celix_err_getErrorCount());
+}
+
+
+TEST_F(ArrayListErrorInjectionTestSuite, AddFunctionsTest) {
//Given an array list with a capacity of 10 (whitebox knowledge)
auto* list = celix_arrayList_create();
@@ -53,6 +70,8 @@ TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) {
//Then adding an element should fail
EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addInt(list, 10));
EXPECT_EQ(10, celix_arrayList_size(list));
+//And an error is logged to the celix_err
+EXPECT_EQ(1, celix_err_getErrorCount());
celix_arrayList_destroy(list);
}
@@ -70,7 +89,8 @@ TEST_F(ArrayListErrorInjectionTestSuite,
AddStringAndAddVersionFailureTest) {
// 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));
+celix_autoptr(celix_version_t) version =
celix_version_createVersionFromString("1.0.0");
+EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addVersion(versionList, version));
}
TEST_F(ArrayListErrorInjectionTestSuite, CopyArrayListFailureTest) {
diff --git a/libs/utils/gtest/src/ArrayListTestSuite.cc
b/libs/utils/gtest/src/ArrayListTestSuite.cc
index 0bc61c26..e820f714 100644
--- a/libs/utils/gtest/src/ArrayListTestSuite.cc
+++ b/libs/utils/gtest/src/ArrayListTestSuite.cc
@@ -528,3 +528,67 @@ TEST_F(ArrayListTestSuite, AutoCleanupTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
EXPECT_NE(nullptr, list);
}
+
+TEST_F(ArrayListTestSuite, AddNullTest) {
+// Given an undefined type, string, string ref and version list
+celix_autoptr(celix_array_list_t) list = celix_arrayList_create();
+celix_autoptr(celix_array_list_t) stringList =
celix_arrayList_createStringArray();
+celix_autoptr(celix_array_list_t) stringRefList =
celix_arrayList_createStringRefArray();
+celix_autoptr(celix_array_list_t) versionList =
celix_arrayList_createVersionArray();
+
+// When adding a null value to the lists
+celix_arrayList_add(list, nullptr);
+celix_arrayList_addString(stringList, nullptr);
+celix_arrayList_addString(stringRefList, nullptr);
+celix_arrayList_addVersion(versionList, nullptr);
+
+// Then the lists contain the null value
+EXPECT_EQ(1, celix_arrayList_size(list));
+EXPECT_EQ(nullptr, celix_arrayList_get(list, 0));
+EXPECT_EQ(1, celix_arrayList_size(stringList));
+EXPECT_EQ(nullptr, celix_arrayList_getString(stringList, 0));
+