Skip to content

Commit 9932b4c

Browse files
authored
Merge pull request #6309 from deepikabhavnani/peek_api
Peek API to view data of buffer without popping
2 parents 7c272fa + 0b9f301 commit 9932b4c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

TESTS/mbedmicro-rtos-mbed/CircularBuffer/main.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,14 @@ void test_input_exceeds_capacity_push_2_pop_1_complex_type()
253253
}
254254
}
255255

256-
/* Test circular buffer - test pop(), empty(), full(), size() after CircularBuffer creation.
256+
/* Test circular buffer - test pop(), peek(), empty(), full(), size() after CircularBuffer creation.
257257
*
258258
* Given is a circular buffer.
259259
* When circular buffer is created.
260260
* Then circular buffer is empty:
261261
* - empty() returns true,
262262
* - pop() function returns false,
263+
* - peek() function returns false,
263264
* - full() function returns false,
264265
* - size() function returns 0,
265266
*
@@ -271,6 +272,7 @@ void test_pop_empty_full_size_after_creation()
271272

272273
TEST_ASSERT_TRUE(cb.empty());
273274
TEST_ASSERT_FALSE(cb.pop(data));
275+
TEST_ASSERT_FALSE(cb.peek(data));
274276
TEST_ASSERT_FALSE(cb.full());
275277
TEST_ASSERT_EQUAL(0, cb.size());
276278
}
@@ -411,6 +413,35 @@ void test_counter_type_buffer_size()
411413
TEST_ASSERT_EQUAL(100, data);
412414
}
413415

416+
/* Test circular buffer - peek should not update buffer data.
417+
*
418+
* When circular buffer peek operation is performed along with
419+
* push and pop, it should not update the buffer data elements.
420+
* Elements read using pop/peek operation should match.
421+
*
422+
*/
423+
void test_peek_no_pop()
424+
{
425+
CircularBuffer<int, 3, unsigned char> cb;
426+
int data = 0;
427+
int peek_data = 0;
428+
429+
for (uint32_t i = 0; i < 3; i++) {
430+
data = (0xAA + i);
431+
cb.push(data);
432+
cb.peek(peek_data);
433+
TEST_ASSERT_EQUAL(i + 1, cb.size());
434+
}
435+
436+
for (uint32_t i = 0; i < 3; i++) {
437+
TEST_ASSERT_TRUE(cb.peek(peek_data));
438+
TEST_ASSERT_EQUAL(0xAA + i, peek_data);
439+
TEST_ASSERT_TRUE(cb.pop(data));
440+
TEST_ASSERT_EQUAL(0xAA + i, data);
441+
TEST_ASSERT_EQUAL(3 - i - 1, cb.size());
442+
}
443+
}
444+
414445
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
415446
{
416447
greentea_case_failure_abort_handler(source, reason);
@@ -446,13 +477,15 @@ Case cases[] = {
446477

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

449-
Case("Test pop(), empty(), full(), size() after CircularBuffer creation.",
480+
Case("Test pop(), peek(), empty(), full(), size() after CircularBuffer creation.",
450481
test_pop_empty_full_size_after_creation, greentea_failure_handler),
451482

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

454485
Case("Input exceeds capacity(5) push 2, pop 1 - complex type.",
455486
test_input_exceeds_capacity_push_2_pop_1_complex_type<5, unsigned short>, greentea_failure_handler),
487+
488+
Case("peek() return data without popping the element.", test_peek_no_pop, greentea_failure_handler),
456489
};
457490

458491
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

platform/CircularBuffer.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class CircularBuffer {
8989

9090
/** Pop the transaction from the buffer
9191
*
92-
* @param data Data to be pushed to the buffer
92+
* @param data Data to be popped from the buffer
9393
* @return True if the buffer is not empty and data contains a transaction, false otherwise
9494
*/
9595
bool pop(T& data) {
@@ -154,6 +154,22 @@ class CircularBuffer {
154154
core_util_critical_section_exit();
155155
return elements;
156156
}
157+
158+
/** Peek into circular buffer without popping
159+
*
160+
* @param data Data to be peeked from the buffer
161+
* @return True if the buffer is not empty and data contains a transaction, false otherwise
162+
*/
163+
bool peek(T& data) const {
164+
bool data_updated = false;
165+
core_util_critical_section_enter();
166+
if (!empty()) {
167+
data = _pool[_tail];
168+
data_updated = true;
169+
}
170+
core_util_critical_section_exit();
171+
return data_updated;
172+
}
157173

158174
private:
159175
T _pool[BufferSize];

0 commit comments

Comments
 (0)