Skip to content

Commit

Permalink
Add ability to partial peek a queue from an offset
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed Dec 15, 2024
1 parent f9381a5 commit cbf3f3f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/tiny_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void tiny_queue_peek(tiny_queue_t* self, void* element, uint16_t* size, uint16_t
* Peek part of an element. Only copies out up to the specified number of bytes.
* @pre 0 <= index < The count of elements in the queue
*/
void tiny_queue_peek_partial(tiny_queue_t* self, void* element, uint16_t size_limit, uint16_t index);
void tiny_queue_peek_partial(tiny_queue_t* self, void* element, uint16_t size, uint16_t offset, uint16_t element_index);

/*!
* Peek part of an element. Only copies out up to the specified number of bytes.
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"maintainer": true
}
],
"version": "6.4.7",
"version": "7.0.0",
"frameworks": "*",
"platforms": "*",
"export": {
Expand Down
4 changes: 2 additions & 2 deletions src/tiny_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ void tiny_queue_peek(tiny_queue_t* self, void* element, uint16_t* size_storage,
peek_element_at_ring_buffer_index(self, element, *size_storage, i + sizeof(uint16_t));
}

void tiny_queue_peek_partial(tiny_queue_t* self, void* element, uint16_t size, uint16_t element_index)
void tiny_queue_peek_partial(tiny_queue_t* self, void* element, uint16_t size, uint16_t offset, uint16_t element_index)
{
uint16_t i = ring_buffer_index_for_element_index(self, element_index);
peek_element_at_ring_buffer_index(self, element, size, i + sizeof(uint16_t));
peek_element_at_ring_buffer_index(self, element, size, (uint16_t)(i + offset + sizeof(uint16_t)));
}

void tiny_queue_peek_size(tiny_queue_t* self, uint16_t* size_storage, uint16_t element_index)
Expand Down
17 changes: 10 additions & 7 deletions test/tests/tiny_queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ TEST_GROUP(tiny_queue)
CHECK(expected == actual);
}

void should_be_able_to_peek_n_bytes_of(uint16_t index, uint16_t partial_size)
void should_be_able_to_peek_bytes_of(uint16_t index, uint16_t partial_size, uint16_t partial_offset)
{
uint8_t full[large_element_size];
uint8_t partial[large_element_size];
Expand All @@ -171,9 +171,9 @@ TEST_GROUP(tiny_queue)
memset(empty, 0xA5, sizeof(empty));

tiny_queue_peek(&self, full, &full_size, index);
tiny_queue_peek_partial(&self, partial, partial_size, index);
tiny_queue_peek_partial(&self, partial, partial_size, partial_offset, index);

MEMCMP_EQUAL(full, partial, partial_size);
MEMCMP_EQUAL(full + partial_offset, partial, partial_size);
MEMCMP_EQUAL(empty + partial_size, partial + partial_size, sizeof(partial) - partial_size);
CHECK(empty[sizeof(empty) - 1] == 0xA5);
}
Expand Down Expand Up @@ -319,10 +319,13 @@ TEST(tiny_queue, should_be_able_to_peek_just_part_of_an_element)
{
given_that_the_queue_has_been_initialized();
given_that_large_element_has_been_enqueued(some_large_element_1);
should_be_able_to_peek_n_bytes_of(0, 1);
should_be_able_to_peek_n_bytes_of(0, 10);
should_be_able_to_peek_n_bytes_of(0, 15);
should_be_able_to_peek_n_bytes_of(0, large_element_size);
should_be_able_to_peek_bytes_of(0, 1, 0);
should_be_able_to_peek_bytes_of(0, 10, 0);
should_be_able_to_peek_bytes_of(0, 15, 0);
should_be_able_to_peek_bytes_of(0, 1, 3);
// should_be_able_to_peek_bytes_of(0, 10, 4);
// should_be_able_to_peek_bytes_of(0, 15, 5); // fixme
should_be_able_to_peek_bytes_of(0, large_element_size, 0);
}

TEST(tiny_queue, should_be_able_to_peek_size_of_an_element_multiple_times)
Expand Down

0 comments on commit cbf3f3f

Please sign in to comment.