Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions libs/utils/gtest/src/ArrayListErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,18 @@ TEST_F(ArrayListErrorInjectionTestSuite, CopyArrayListFailureTest) {
// And a celix_err is expected
EXPECT_EQ(3, celix_err_getErrorCount());
}


TEST_F(ArrayListErrorInjectionTestSuite, InitialCapacityOptionUsed) {
celix_array_list_create_options_t opts{};
opts.elementType = CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING;
opts.initialCapacity = 1; // smaller than number of elements we will add
celix_autoptr(celix_array_list_t) list = celix_arrayList_createWithOptions(&opts);

// First add fits in initial capacity
EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addString(list, "v1"));

// Fail the next realloc to verify that a second add triggers a reallocation
celix_ei_expect_realloc((void*)celix_arrayList_addString, 2, nullptr, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to move this line before EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addString(list, "v1")); to make it clear the first operation does not trigger memory allocation.

EXPECT_EQ(CELIX_ENOMEM, celix_arrayList_addString(list, "v2"));
}
12 changes: 12 additions & 0 deletions libs/utils/gtest/src/ArrayListTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,15 @@ TEST_F(ArrayListTestSuite, ElementTypeToStringTest) {
EXPECT_STREQ("Undefined",
celix_arrayList_elementTypeToString((celix_array_list_element_type_t)100 /*non existing*/));
}

TEST_F(ArrayListTestSuite, InitialCapacityOptionTest) {
celix_array_list_create_options_t opts{};
opts.elementType = CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING;
opts.initialCapacity = 1; // smaller than number of elements we will add
celix_autoptr(celix_array_list_t) list = celix_arrayList_createWithOptions(&opts);

// First add fits in initial capacity
EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addString(list, "v1"));
// Second add requires realloc
EXPECT_EQ(CELIX_SUCCESS, celix_arrayList_addString(list, "v2"));
}
60 changes: 33 additions & 27 deletions libs/utils/include/celix_array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#include <stdbool.h>

#include "celix_array_list_type.h"
#include "celix_utils_export.h"
#include "celix_cleanup.h"
#include "celix_errno.h"
#include "celix_utils_export.h"
#include "celix_version_type.h"

#ifndef CELIX_ARRAY_LIST_H_
Expand All @@ -32,12 +32,13 @@
* Init macro so that the opts are correctly initialized for C++ compilers
*/
#ifdef __cplusplus
#define CELIX_OPTS_INIT {}
#define CELIX_OPTS_INIT \
{ \
}
#else
#define CELIX_OPTS_INIT
#endif


#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -57,7 +58,7 @@ typedef enum celix_array_list_element_type {
CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING = 2, /**< Represents a string element type where the array list is the owner */
CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG = 3, /**< Represents a long integer element type. */
CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE = 4, /**< Represents a double element type. */
CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL = 5, /**< Represents a boolean element type. */
CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL = 5, /**< Represents a boolean element type. */
CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION = 6, /**< Represents a celix_version_t* element type. */
} celix_array_list_element_type_t;

Expand All @@ -75,10 +76,10 @@ typedef union celix_array_list_entry {
CELIX_ARRAY_LIST_ELEMENT_TYPE_STRING_REF or CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
long int longVal; /**< A long integer value when the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_LONG or
CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
double doubleVal; /**< A double value when the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE or
CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
bool boolVal; /**< A boolean value when the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL or
CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
double doubleVal; /**< A double value when the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_DOUBLE or
CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
bool boolVal; /**< A boolean value when the element type is CELIX_ARRAY_LIST_ELEMENT_TYPE_BOOL or
CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
const celix_version_t* versionVal; /**< A celix_version_t* value when the element type is
CELIX_ARRAY_LIST_ELEMENT_TYPE_VERSION or CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED. */
} celix_array_list_entry_t;
Expand Down Expand Up @@ -252,9 +253,14 @@ typedef struct celix_array_list_create_options {
*/
#define CELIX_EMPTY_ARRAY_LIST_CREATE_OPTIONS \
{ \
.elementType = CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED, .simpleRemovedCallback = NULL, \
.removedCallbackData = NULL, .removedCallback = NULL, .equalsCallback = NULL, \
.compareCallback = NULL, .copyCallback = NULL, \
.elementType = CELIX_ARRAY_LIST_ELEMENT_TYPE_UNDEFINED, \
.simpleRemovedCallback = NULL, \
.removedCallbackData = NULL, \
.removedCallback = NULL, \
.equalsCallback = NULL, \
.compareCallback = NULL, \
.copyCallback = NULL, \
.initialCapacity = 0, \
}
#endif

Expand All @@ -280,21 +286,21 @@ celix_array_list_t* celix_arrayList_createWithOptions(const celix_array_list_cre
* @note If a (simple) removed callback is configured, the callback will be called for every array list entry.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_destroy(celix_array_list_t *list);
void celix_arrayList_destroy(celix_array_list_t* list);

CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(celix_array_list_t, celix_arrayList_destroy)

/**
* @brief Return the element type of the array list.
*/
CELIX_UTILS_EXPORT
celix_array_list_element_type_t celix_arrayList_getElementType(const celix_array_list_t *list);
celix_array_list_element_type_t celix_arrayList_getElementType(const celix_array_list_t* list);

/**
* @brief Returns the size of the array list.
*/
CELIX_UTILS_EXPORT
int celix_arrayList_size(const celix_array_list_t *list);
int celix_arrayList_size(const celix_array_list_t* list);

/**
* @brief Returns the value for the provided index.
Expand All @@ -307,7 +313,7 @@ int celix_arrayList_size(const celix_array_list_t *list);
* @return Returns the pointer value for the index. Returns NULL if index is out of bound.
*/
CELIX_UTILS_EXPORT
void* celix_arrayList_get(const celix_array_list_t *list, int index);
void* celix_arrayList_get(const celix_array_list_t* list, int index);

/**
* @brief Returns the value for the provided index.
Expand All @@ -320,7 +326,7 @@ void* celix_arrayList_get(const celix_array_list_t *list, int index);
* @return Returns the string value for the index. Returns NULL if index is out of bound.
*/
CELIX_UTILS_EXPORT
const char* celix_arrayList_getString(const celix_array_list_t *list, int index);
const char* celix_arrayList_getString(const celix_array_list_t* list, int index);

/**
* @brief Returns the value for the provided index.
Expand All @@ -333,7 +339,7 @@ const char* celix_arrayList_getString(const celix_array_list_t *list, int index)
* @return Returns the long value for the index. Returns 0 if index is out of bound.
*/
CELIX_UTILS_EXPORT
long int celix_arrayList_getLong(const celix_array_list_t *list, int index);
long int celix_arrayList_getLong(const celix_array_list_t* list, int index);

/**
* @brief Returns the value for the provided index.
Expand All @@ -346,7 +352,7 @@ long int celix_arrayList_getLong(const celix_array_list_t *list, int index);
* @return Returns the double value for the index. Returns 0 if index is out of bound.
*/
CELIX_UTILS_EXPORT
double celix_arrayList_getDouble(const celix_array_list_t *list, int index);
double celix_arrayList_getDouble(const celix_array_list_t* list, int index);

/**
* @brief Returns the value for the provided index.
Expand All @@ -359,7 +365,7 @@ double celix_arrayList_getDouble(const celix_array_list_t *list, int index);
* @return Returns the bool value for the index. Returns false if index is out of bound.
*/
CELIX_UTILS_EXPORT
bool celix_arrayList_getBool(const celix_array_list_t *list, int index);
bool celix_arrayList_getBool(const celix_array_list_t* list, int index);

/**
* @brief Returns the value for the provided index.
Expand All @@ -372,7 +378,7 @@ bool celix_arrayList_getBool(const celix_array_list_t *list, int index);
* @return Returns the version value for the index. Returns NULL if index is out of bound.
*/
CELIX_UTILS_EXPORT
const celix_version_t* celix_arrayList_getVersion(const celix_array_list_t *list, int index);
const celix_version_t* celix_arrayList_getVersion(const celix_array_list_t* list, int index);

/**
* @brief Returns the entry for the provided index.
Expand All @@ -382,7 +388,7 @@ const celix_version_t* celix_arrayList_getVersion(const celix_array_list_t *list
* @return Returns the entry for the index. Returns NULL if index is out of bound.
*/
CELIX_UTILS_EXPORT
celix_array_list_entry_t celix_arrayList_getEntry(const celix_array_list_t *list, int index);
celix_array_list_entry_t celix_arrayList_getEntry(const celix_array_list_t* list, int index);

/**
* @brief add pointer entry to the back of the array list.
Expand Down Expand Up @@ -529,22 +535,22 @@ celix_status_t celix_arrayList_assignVersion(celix_array_list_t* list, celix_ver
* @return The index of the entry or -1 if the entry is not found.
*/
CELIX_UTILS_EXPORT
int celix_arrayList_indexOf(celix_array_list_t *list, celix_array_list_entry_t entry);
int celix_arrayList_indexOf(celix_array_list_t* list, celix_array_list_entry_t entry);

/**
* @brief Removes an entry at the provided index.
* If the provided index < 0 or out of bound, nothing will be removed.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_removeAt(celix_array_list_t *list, int index);
void celix_arrayList_removeAt(celix_array_list_t* list, int index);

/**
* @brief Clear all entries in the array list.
*
* @note If a (simple) removed callback is configured, the callback will be called for every array list entry.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_clear(celix_array_list_t *list);
void celix_arrayList_clear(celix_array_list_t* list);

/**
* @brief Remove the first entry from array list which matches the provided value.
Expand All @@ -556,7 +562,7 @@ void celix_arrayList_clear(celix_array_list_t *list);
* If there was no equals callback provided a direct memory compare will be done.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_removeEntry(celix_array_list_t *list, celix_array_list_entry_t entry);
void celix_arrayList_removeEntry(celix_array_list_t* list, celix_array_list_entry_t entry);

/**
* @brief Remove the first pointer entry from array list which matches the provided value.
Expand Down Expand Up @@ -634,14 +640,14 @@ void celix_arrayList_removeVersion(celix_array_list_t* list, const celix_version
* @brief Sort the array list using the provided sort function.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_sortEntries(celix_array_list_t *list, celix_array_list_compare_entries_fp compare);
void celix_arrayList_sortEntries(celix_array_list_t* list, celix_array_list_compare_entries_fp compare);

/**
* @brief Sort the array list using the array list configured compare function.
* Note that undefined the array list compare function can be NULL and in that case the array list will not be sorted.
*/
CELIX_UTILS_EXPORT
void celix_arrayList_sort(celix_array_list_t *list);
void celix_arrayList_sort(celix_array_list_t* list);

/**
* @brief Check if the array list are equal.
Expand Down
Loading
Loading