Skip to content

Commit 0b9f301

Browse files
author
Deepika
committed
Test to verify peek API
1 parent 9fa660e commit 0b9f301

File tree

1 file changed

+35
-2
lines changed
  • TESTS/mbedmicro-rtos-mbed/CircularBuffer

1 file changed

+35
-2
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)

0 commit comments

Comments
 (0)