Skip to content

Commit

Permalink
Refactor other dynType_sequence methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Jan 8, 2024
1 parent 78b7d2d commit 84b4578
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
48 changes: 48 additions & 0 deletions libs/dfi/gtest/src/dyn_type_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libs/dfi/include/dyn_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 17 additions & 26 deletions libs/dfi/src/dyn_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libs/dfi/src/json_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 84b4578

Please sign in to comment.