Skip to content

Peek API to view data of buffer without popping #6309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2018
Merged
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
37 changes: 35 additions & 2 deletions TESTS/mbedmicro-rtos-mbed/CircularBuffer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,14 @@ void test_input_exceeds_capacity_push_2_pop_1_complex_type()
}
}

/* Test circular buffer - test pop(), empty(), full(), size() after CircularBuffer creation.
/* Test circular buffer - test pop(), peek(), empty(), full(), size() after CircularBuffer creation.
*
* Given is a circular buffer.
* When circular buffer is created.
* Then circular buffer is empty:
* - empty() returns true,
* - pop() function returns false,
* - peek() function returns false,
* - full() function returns false,
* - size() function returns 0,
*
Expand All @@ -271,6 +272,7 @@ void test_pop_empty_full_size_after_creation()

TEST_ASSERT_TRUE(cb.empty());
TEST_ASSERT_FALSE(cb.pop(data));
TEST_ASSERT_FALSE(cb.peek(data));
TEST_ASSERT_FALSE(cb.full());
TEST_ASSERT_EQUAL(0, cb.size());
}
Expand Down Expand Up @@ -411,6 +413,35 @@ void test_counter_type_buffer_size()
TEST_ASSERT_EQUAL(100, data);
}

/* Test circular buffer - peek should not update buffer data.
*
* When circular buffer peek operation is performed along with
* push and pop, it should not update the buffer data elements.
* Elements read using pop/peek operation should match.
*
*/
void test_peek_no_pop()
{
CircularBuffer<int, 3, unsigned char> cb;
int data = 0;
int peek_data = 0;

for (uint32_t i = 0; i < 3; i++) {
data = (0xAA + i);
cb.push(data);
cb.peek(peek_data);
TEST_ASSERT_EQUAL(i + 1, cb.size());
}

for (uint32_t i = 0; i < 3; i++) {
TEST_ASSERT_TRUE(cb.peek(peek_data));
TEST_ASSERT_EQUAL(0xAA + i, peek_data);
TEST_ASSERT_TRUE(cb.pop(data));
TEST_ASSERT_EQUAL(0xAA + i, data);
TEST_ASSERT_EQUAL(3 - i - 1, cb.size());
}
}

utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
{
greentea_case_failure_abort_handler(source, reason);
Expand Down Expand Up @@ -446,13 +477,15 @@ Case cases[] = {

Case("reset() clears the buffer.", test_reset<uint32_t, 5>, greentea_failure_handler),

Case("Test pop(), empty(), full(), size() after CircularBuffer creation.",
Case("Test pop(), peek(), empty(), full(), size() after CircularBuffer creation.",
test_pop_empty_full_size_after_creation, greentea_failure_handler),

Case("Test CounterType/BufferSize boarder case.", test_counter_type_buffer_size, greentea_failure_handler),

Case("Input exceeds capacity(5) push 2, pop 1 - complex type.",
test_input_exceeds_capacity_push_2_pop_1_complex_type<5, unsigned short>, greentea_failure_handler),

Case("peek() return data without popping the element.", test_peek_no_pop, greentea_failure_handler),
};

utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
Expand Down
18 changes: 17 additions & 1 deletion platform/CircularBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CircularBuffer {

/** Pop the transaction from the buffer
*
* @param data Data to be pushed to the buffer
* @param data Data to be popped from the buffer
* @return True if the buffer is not empty and data contains a transaction, false otherwise
*/
bool pop(T& data) {
Expand Down Expand Up @@ -154,6 +154,22 @@ class CircularBuffer {
core_util_critical_section_exit();
return elements;
}

/** Peek into circular buffer without popping
*
* @param data Data to be peeked from the buffer
* @return True if the buffer is not empty and data contains a transaction, false otherwise
*/
bool peek(T& data) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, does data need to be const if peek is a const member function?

Copy link
Author

Choose a reason for hiding this comment

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

const member function is not allowed to modify the object elements, which is not done in case of this API. But we are passing data as return value hence it cannot be const.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be an issue if it was a const reference (T& const data). But since it's a reference-to-const (const T& data), it should be fine.

Does this compile as is with a const CircularBuffer?

Copy link
Author

Choose a reason for hiding this comment

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

Not all members of CircularBuffer are const, without this API also it won't compile as const CircularBuffer.

(const T& data) in case of template class says value of data variable cannot be modified in the function. We are passing the data value, hence it cannot be const. I tried compilation with const and it failed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, that's annoying if there's a similar issue in other functions.

const T& data can be assigned with issue.

bool data_updated = false;
core_util_critical_section_enter();
if (!empty()) {
data = _pool[_tail];
data_updated = true;
}
core_util_critical_section_exit();
return data_updated;
}

private:
T _pool[BufferSize];
Expand Down