summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2020-05-14 16:18:20 +0700
committerGitHub <[email protected]>2020-05-14 16:18:20 +0700
commitbe9f938bd33d26d07d1bd1ff479e22f440777a63 (patch)
tree7927bb25a31aa7bdf23f2dbab181ae0200a55d8f /test
parent7a5c0ee80298b3661073e11d89f6e0ec1e2afe83 (diff)
parent27299a5c47cb4bc87a222004fd245577a6946caa (diff)
Merge pull request #405 from hathach/follow-pr404
Follow pr404
Diffstat (limited to 'test')
-rw-r--r--test/test/test_fifo.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/test/test/test_fifo.c b/test/test/test_fifo.c
index 2cb8ada91..b6c22f569 100644
--- a/test/test/test_fifo.c
+++ b/test/test/test_fifo.c
@@ -54,6 +54,90 @@ void test_normal(void)
}
}
+void test_item_size(void)
+{
+ TU_FIFO_DEF(ff4, FIFO_SIZE, uint32_t, false);
+ tu_fifo_clear(&ff4);
+
+ uint32_t data[20];
+ for(uint32_t i=0; i<sizeof(data)/4; i++) data[i] = i;
+
+ tu_fifo_write_n(&ff4, data, 10);
+
+ uint32_t rd[10];
+ uint16_t rd_count;
+
+ // read 0 -> 4
+ rd_count = tu_fifo_read_n(&ff4, rd, 5);
+ TEST_ASSERT_EQUAL( 5, rd_count );
+ TEST_ASSERT_EQUAL_UINT32_ARRAY( data, rd, rd_count ); // 0 -> 4
+
+ tu_fifo_write_n(&ff4, data+10, 5);
+
+ // read 5 -> 14
+ rd_count = tu_fifo_read_n(&ff4, rd, 10);
+ TEST_ASSERT_EQUAL( 10, rd_count );
+ TEST_ASSERT_EQUAL_UINT32_ARRAY( data+5, rd, rd_count ); // 5 -> 14
+}
+
+void test_read_n(void)
+{
+ // prepare data
+ uint8_t data[20];
+ for(int i=0; i<sizeof(data); i++) data[i] = i;
+
+ for(uint8_t i=0; i < FIFO_SIZE; i++) tu_fifo_write(&ff, data+i);
+
+ uint8_t rd[10];
+ uint16_t rd_count;
+
+ // case 1: Read index + count < depth
+ // read 0 -> 4
+ rd_count = tu_fifo_read_n(&ff, rd, 5);
+ TEST_ASSERT_EQUAL( 5, rd_count );
+ TEST_ASSERT_EQUAL_MEMORY( data, rd, rd_count ); // 0 -> 4
+
+ // case 2: Read index + count > depth
+ // write 10, 11, 12
+ tu_fifo_write(&ff, data+10);
+ tu_fifo_write(&ff, data+11);
+ tu_fifo_write(&ff, data+12);
+
+ rd_count = tu_fifo_read_n(&ff, rd, 7);
+ TEST_ASSERT_EQUAL( 7, rd_count );
+
+ TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count ); // 5 -> 11
+
+ // Should only read until empty
+ TEST_ASSERT_EQUAL( 1, tu_fifo_read_n(&ff, rd, 100) );
+}
+
+void test_write_n(void)
+{
+ // prepare data
+ uint8_t data[20];
+ for(int i=0; i<sizeof(data); i++) data[i] = i;
+
+ // case 1: wr + count < depth
+ tu_fifo_write_n(&ff, data, 8); // wr = 8, count = 8
+
+ uint8_t rd[10];
+ uint16_t rd_count;
+
+ rd_count = tu_fifo_read_n(&ff, rd, 5); // wr = 8, count = 3
+ TEST_ASSERT_EQUAL( 5, rd_count );
+ TEST_ASSERT_EQUAL_MEMORY( data, rd, rd_count ); // 0 -> 4
+
+ // case 2: wr + count > depth
+ tu_fifo_write_n(&ff, data+8, 6); // wr = 3, count = 9
+
+ for(rd_count=0; rd_count<7; rd_count++) tu_fifo_read(&ff, rd+rd_count); // wr = 3, count = 2
+
+ TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count); // 5 -> 11
+
+ TEST_ASSERT_EQUAL(2, tu_fifo_count(&ff));
+}
+
void test_peek(void)
{
uint8_t temp;