From 75989673e5246756beda3a3d816575ce0a77f315 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 9 Dec 2022 15:39:57 +0700 Subject: [PATCH] add test_write_double_overflowed for fifo --- test/unit-test/test/test_fifo.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/unit-test/test/test_fifo.c b/test/unit-test/test/test_fifo.c index 5e639401d..55bc24b7e 100644 --- a/test/unit-test/test/test_fifo.c +++ b/test/unit-test/test/test_fifo.c @@ -138,6 +138,34 @@ void test_write_n(void) TEST_ASSERT_EQUAL(24, tu_fifo_count(ff)); } +void test_write_double_overflowed(void) +{ + tu_fifo_set_overwritable(ff, true); + + uint8_t rd_buf[FIFO_SIZE] = { 0 }; + uint8_t* buf = test_data; + + // full + buf += tu_fifo_write_n(ff, buf, FIFO_SIZE); + TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff)); + + // write more, should still full + buf += tu_fifo_write_n(ff, buf, FIFO_SIZE-8); + TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff)); + + // double overflowed: in total, write more than > 2*FIFO_SIZE + buf += tu_fifo_write_n(ff, buf, 16); + TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff)); + + // reading back should give back data from last FIFO_SIZE write + tu_fifo_read_n(ff, rd_buf, FIFO_SIZE); + + TEST_ASSERT_EQUAL_MEMORY(buf-16, rd_buf+FIFO_SIZE-16, 16); + + // TODO whole buffer should match, but we deliberately not implement it + // TEST_ASSERT_EQUAL_MEMORY(buf-FIFO_SIZE, rd_buf, FIFO_SIZE); +} + static uint16_t help_write(uint16_t total, uint16_t n) { tu_fifo_write_n(ff, test_data, n); @@ -149,7 +177,7 @@ static uint16_t help_write(uint16_t total, uint16_t n) return total; } -void test_write_overwritable(void) +void test_write_overwritable2(void) { tu_fifo_set_overwritable(ff, true);