Skip to content

Commit

Permalink
Add bytearray append
Browse files Browse the repository at this point in the history
This commit adds a function to append one bytearray onto another.
  • Loading branch information
jmcph4 committed Jan 28, 2019
1 parent 6d589d7 commit e5237cd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/az_bytearray.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,29 @@ az_status_t az_bytearray_slice(az_bytearray_t bytearray, uintmax_t start,
return AZ_STATUS_OK;
}

/**
* Appends <code>back</code> onto <code>front</code>.
*
* @param back
* the bytearray to be added onto the end of <code>front</code>
* @param front
* the bytearray to be appended to
* @return an <code>az_status_t</code> type indicating success of operation
* @throw AZ_ERR_ILLEGAL_PARAM
* if <code>front == NULL</code>
*
* */
az_status_t az_bytearray_append(az_bytearray_t back, az_bytearray_t* front)
{
if(front == NULL) /* null guard */
{
return AZ_ERR_ILLEGAL_PARAM;
}

front->data = realloc(front->data, front->len + back.len);
memcpy(&front->data[front->len], back.data, back.len);
front->len += back.len;

return AZ_STATUS_OK;
}

1 change: 1 addition & 0 deletions src/az_bytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ az_status_t az_bytearray_equal(az_bytearray_t a, az_bytearray_t b, bool* equal);
/* Operations */
az_status_t az_bytearray_slice(az_bytearray_t bytearray, uintmax_t start,
uintmax_t end, az_bytearray_t** slice);
az_status_t az_bytearray_append(az_bytearray_t back, az_bytearray_t* front);

#endif /* AZ_BYTEARRAY_H_ */

28 changes: 28 additions & 0 deletions tests/test_az_bytearray.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ void test_az_bytearray_slice_normal(void)
TEST_ASSERT_TRUE(slices_equal);
}

void test_az_bytearray_append_normal(void)
{
uint8_t bytes[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

az_bytearray_t a;
az_bytearray_init(4, bytes, &a);

az_bytearray_t b;
az_bytearray_init(4, &bytes[4], &b);

az_bytearray_t expected_bytearray;
az_bytearray_init(8, bytes, &expected_bytearray);

az_bytearray_t actual_bytearray;
az_bytearray_init(4, bytes, &actual_bytearray);

az_status_t res = az_bytearray_append(b, &actual_bytearray);

bool equal = false;
az_bytearray_equal(expected_bytearray, actual_bytearray, &equal);

TEST_ASSERT_EQUAL_INT(AZ_STATUS_OK, res);
TEST_ASSERT_TRUE(equal);
}

int main(void)
{
UNITY_BEGIN();
Expand All @@ -126,6 +151,9 @@ int main(void)
/* az_bytearray_slice */
RUN_TEST(test_az_bytearray_slice_normal);

/* az_bytearray_append */
RUN_TEST(test_az_bytearray_append_normal);

return UNITY_END();
}

0 comments on commit e5237cd

Please sign in to comment.