You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pn...@apache.org on 2023/05/02 09:50:52 UTC
[celix] 04/04: Add error injection test suite for array list
This is an automated email from the ASF dual-hosted git repository.
pnoltes pushed a commit to branch feature/array_list_return_status_on_modification
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 78ff2f06a6107f71c47e4b30a41bad5080c2ac9d
Author: Pepijn Noltes <pe...@gmail.com>
AuthorDate: Mon May 1 19:38:33 2023 +0200
Add error injection test suite for array list
---
libs/utils/gtest/CMakeLists.txt | 5 ++-
.../gtest/src/ArrayListErrorInjectionTestSuite.cc | 49 ++++++++++++++++++++++
libs/utils/gtest/src/ArrayListTestSuite.cc | 27 ++++++++++++
3 files changed, 79 insertions(+), 2 deletions(-)
diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt
index acb38bdb..2d8a47f5 100644
--- a/libs/utils/gtest/CMakeLists.txt
+++ b/libs/utils/gtest/CMakeLists.txt
@@ -82,8 +82,9 @@ if (LINKER_WRAP_SUPPORTED)
src/FileUtilsErrorInjectionTestSuite.cc
src/ConvertUtilsErrorInjectionTestSuite.cc
src/IpUtilsErrorInjectionTestSuite.cc
- )
- target_link_libraries(test_utils_with_ei PRIVATE Celix::zip_ei Celix::stdio_ei Celix::stat_ei Celix::fts_ei Celix::utils_obj Celix::utils_ei Celix::ifaddrs_ei GTest::gtest GTest::gtest_main)
+ src/ArrayListErrorInjectionTestSuite.cc
+ )
+ target_link_libraries(test_utils_with_ei PRIVATE Celix::zip_ei Celix::stdio_ei Celix::stat_ei Celix::fts_ei Celix::utils_obj Celix::utils_ei Celix::ifaddrs_ei Celix::malloc_ei GTest::gtest GTest::gtest_main)
target_include_directories(test_utils_with_ei PRIVATE ../src) #for version_private (needs refactoring of test)
celix_deprecated_utils_headers(test_utils_with_ei)
add_dependencies(test_utils_with_ei test_utils_resources)
diff --git a/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
new file mode 100644
index 00000000..6e17fbf8
--- /dev/null
+++ b/libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include "celix_array_list.h"
+#include "malloc_ei.h"
+
+class ArrayListErrorInjectionTestSuite : public ::testing::Test {
+public:
+ ArrayListErrorInjectionTestSuite() = default;
+ ~ArrayListErrorInjectionTestSuite() noexcept override {
+ celix_ei_expect_realloc(nullptr, 0, nullptr);
+ }
+};
+
+TEST_F(ArrayListErrorInjectionTestSuite, TestAddFunctions) {
+ //Given an array list with a capacity of 10 (whitebox knowledge)
+ auto* list = celix_arrayList_create();
+
+ //When adding 10 elements, no error is expected
+ for (int i = 0; i < 10; ++i) {
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addInt(list, i));
+ }
+ EXPECT_EQ(10, celix_arrayList_size(list));
+
+ //And realloc is primed to fail
+ celix_ei_expect_realloc(CELIX_EI_UNKNOWN_CALLER, 1, nullptr);
+
+ //Then adding an element should fail
+ EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addInt(list, 10));
+ EXPECT_EQ(10, celix_arrayList_size(list));
+}
diff --git a/libs/utils/gtest/src/ArrayListTestSuite.cc b/libs/utils/gtest/src/ArrayListTestSuite.cc
index 55cd8eae..acb86d14 100644
--- a/libs/utils/gtest/src/ArrayListTestSuite.cc
+++ b/libs/utils/gtest/src/ArrayListTestSuite.cc
@@ -230,3 +230,30 @@ TEST_F(ArrayListTestSuite, TestSortForArrayList) {
celix_arrayList_destroy(list);
}
+
+TEST_F(ArrayListTestSuite, TestReturnStatusAddFunctions) {
+ auto* list = celix_arrayList_create();
+ ASSERT_TRUE(list != nullptr);
+ EXPECT_EQ(0, celix_arrayList_size(list));
+
+ //no error, return status is CELIX_SUCCESS
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addInt(list, 1));
+ EXPECT_EQ(1, celix_arrayList_size(list));
+
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addLong(list, 2L));
+ EXPECT_EQ(2, celix_arrayList_size(list));
+
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addFloat(list, 3.0f));
+ EXPECT_EQ(3, celix_arrayList_size(list));
+
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addDouble(list, 4.0));
+ EXPECT_EQ(4, celix_arrayList_size(list));
+
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addBool(list, true));
+ EXPECT_EQ(5, celix_arrayList_size(list));
+
+ EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_add(list, (void*)0x42));
+ EXPECT_EQ(6, celix_arrayList_size(list));
+
+ celix_arrayList_destroy(list);
+}
\ No newline at end of file