From 84b45788ee1200abcdf72b4171b38d6d4152ad4e Mon Sep 17 00:00:00 2001 From: PengZheng Date: Mon, 8 Jan 2024 21:35:11 +0800 Subject: [PATCH] Refactor other dynType_sequence methods. --- libs/dfi/gtest/src/dyn_type_tests.cpp | 48 +++++++++++++++++++++++++++ libs/dfi/include/dyn_type.h | 2 +- libs/dfi/src/dyn_type.c | 43 ++++++++++-------------- libs/dfi/src/json_serializer.c | 2 +- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/libs/dfi/gtest/src/dyn_type_tests.cpp b/libs/dfi/gtest/src/dyn_type_tests.cpp index f3cb50790..cd7e266a4 100644 --- a/libs/dfi/gtest/src/dyn_type_tests.cpp +++ b/libs/dfi/gtest/src/dyn_type_tests.cpp @@ -268,6 +268,7 @@ TEST_F(DynTypeTests, SequenceWithPointerTest) { ASSERT_EQ(0, rc); ASSERT_TRUE(seq != NULL); + dynType_sequence_init(type, seq); rc = dynType_sequence_alloc(type, seq, 1); ASSERT_EQ(0, rc); struct item **loc = NULL; @@ -341,6 +342,53 @@ TEST_F(DynTypeTests, SequenceReserve) { dynType_destroy(type); } +TEST_F(DynTypeTests, FillSequenceTest) { + struct double_sequence { + uint32_t cap; + uint32_t len; + double* buf; + }; + + dyn_type *type = NULL; + int rc = 0; + rc = dynType_parseWithStr("[D", NULL, NULL, &type); + ASSERT_EQ(0, rc); + + struct double_sequence *seq = NULL; + rc = dynType_alloc(type, (void **)&seq); + ASSERT_EQ(0, rc); + ASSERT_TRUE(seq != NULL); + + rc = dynType_sequence_alloc(type, seq, 1); + ASSERT_EQ(0, rc); + + void* loc; + rc = dynType_sequence_increaseLengthAndReturnLastLoc(type, seq, (void **)&loc); + ASSERT_EQ(0, rc); + + double val = 2.0; + dynType_simple_setValue(dynType_sequence_itemType(type), loc, &val); + ASSERT_EQ(val, seq->buf[0]); + + rc = dynType_sequence_increaseLengthAndReturnLastLoc(type, seq, (void **)&loc); + ASSERT_NE(0, rc); + ASSERT_STREQ("Cannot increase sequence length beyond capacity (1)", celix_err_popLastError()); + + rc = dynType_sequence_reserve(type, seq, 2); + ASSERT_EQ(0, rc); + rc = dynType_sequence_locForIndex(type, seq, 1, (void **)&loc); + ASSERT_NE(0, rc); + ASSERT_STREQ("Requesting index (1) outsize defined length (1) but within capacity", celix_err_popLastError()); + + + rc = dynType_sequence_locForIndex(type, seq, 2, (void **)&loc); + ASSERT_NE(0, rc); + ASSERT_STREQ("Requested index (2) is greater than capacity (2) of sequence", celix_err_popLastError()); + + dynType_free(type, seq); + dynType_destroy(type); +} + TEST_F(DynTypeTests, EnumTest) { dyn_type *type = NULL; int rc = 0; diff --git a/libs/dfi/include/dyn_type.h b/libs/dfi/include/dyn_type.h index 635004b27..7ae474719 100644 --- a/libs/dfi/include/dyn_type.h +++ b/libs/dfi/include/dyn_type.h @@ -311,7 +311,7 @@ CELIX_DFI_EXPORT int dynType_sequence_reserve(const dyn_type* type, void* inst, * @return 0 if successful. * @retval 1 if the index is out of bounds. */ -CELIX_DFI_EXPORT int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, int index, void** valLoc); +CELIX_DFI_EXPORT int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, uint32_t index, void** valLoc); /** * @brief Increase the length of the sequence by one and return the value location for the last element. diff --git a/libs/dfi/src/dyn_type.c b/libs/dfi/src/dyn_type.c index e629900b5..474009205 100644 --- a/libs/dfi/src/dyn_type.c +++ b/libs/dfi/src/dyn_type.c @@ -651,9 +651,11 @@ static void dynType_deepFree(const dyn_type* type, void* loc, bool alsoDeleteSel case DYN_TYPE_SIMPLE: //nop break; +//LCOV_EXCL_START default: - celix_err_pushf("Unexpected switch case. cannot free dyn type %c\n", type->descriptor); + assert(0 && "Unexpected switch case. cannot free dyn type"); break; +//LCOV_EXCL_STOP } if (alsoDeleteSelf) { @@ -666,7 +668,7 @@ static void dynType_freeSequenceType(const dyn_type* type, void* seqLoc) { struct generic_sequence* seq = seqLoc; const dyn_type* itemType = dynType_sequence_itemType(type); void* itemLoc = NULL; - int i; + uint32_t i; for (i = 0; i < seq->len; ++i) { dynType_sequence_locForIndex(type, seqLoc, i, &itemLoc); dynType_deepFree(itemType, itemLoc, false); @@ -688,50 +690,39 @@ uint32_t dynType_sequence_length(const void *seqLoc) { return seq->len; } -int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, int index, void** out) { +int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, uint32_t index, void** out) { assert(type->type == DYN_TYPE_SEQUENCE); - int status = OK; - struct generic_sequence* seq = seqLoc; size_t itemSize = dynType_size(type->sequence.itemType); if (index >= seq->cap) { - status = ERROR; - celix_err_pushf("Requested index (%i) is greater than capacity (%u) of sequence", index, seq->cap); + celix_err_pushf("Requested index (%u) is greater than capacity (%u) of sequence", index, seq->cap); + return ERROR; } if (index >= seq->len) { - status = ERROR; - celix_err_pushf("Requesting index (%i) outsize defined length (%u) but within capacity", index, seq->len); + celix_err_pushf("Requesting index (%u) outsize defined length (%u) but within capacity", index, seq->len); + return ERROR; } - if (status == OK) { - char* valLoc = seq->buf + (index * itemSize); - (*out) = valLoc; - } + char* valLoc = seq->buf + (index * itemSize); + (*out) = valLoc; - return status; + return OK; } int dynType_sequence_increaseLengthAndReturnLastLoc(const dyn_type* type, void* seqLoc, void** valLoc) { assert(type->type == DYN_TYPE_SEQUENCE); - int status = OK; struct generic_sequence* seq = seqLoc; - int lastIndex = seq->len; - if (seq->len < seq->cap) { - seq->len += 1; - } else { - status = ERROR; + uint32_t lastIndex = seq->len; + if (seq->len >= seq->cap) { celix_err_pushf("Cannot increase sequence length beyond capacity (%u)", seq->cap); + return ERROR; } - - if (status == OK) { - status = dynType_sequence_locForIndex(type, seqLoc, lastIndex, valLoc); - } - - return status; + seq->len += 1; + return dynType_sequence_locForIndex(type, seqLoc, lastIndex, valLoc); } const dyn_type* dynType_sequence_itemType(const dyn_type* type) { diff --git a/libs/dfi/src/json_serializer.c b/libs/dfi/src/json_serializer.c index 34d4f577b..a98e3ea85 100644 --- a/libs/dfi/src/json_serializer.c +++ b/libs/dfi/src/json_serializer.c @@ -437,7 +437,7 @@ static int jsonSerializer_writeSequence(const dyn_type *type, void *input, json_ const dyn_type *itemType = dynType_sequence_itemType(type); uint32_t len = dynType_sequence_length(input); - int i = 0; + uint32_t i = 0; void *itemLoc = NULL; json_t *item = NULL; for (i = 0; i < len; i += 1) {