FIFO protection (#1285)
[ExpressLRS.git] / src / test / fifo_native / test_fifo.cpp
blob926001258cfc4b07bf03ccb7ab24f90153142f5e
1 #include <cstdint>
2 #include <FIFO.h>
3 #include <unity.h>
4 #include <set>
6 using namespace std;
8 FIFO f;
9 void init()
11 f.flush();
12 for(int i=0;i<FIFO_SIZE/2;i++) {
13 f.push(i);
14 TEST_ASSERT_EQUAL(i, f.pop());
16 for(int i=0;i<FIFO_SIZE-1;i++) {
17 f.push(i);
21 void test_fifo_pop_wrap(void)
23 init();
24 for(int i=0;i<FIFO_SIZE-1;i++) {
25 TEST_ASSERT_EQUAL(i, f.pop());
29 void test_fifo_popBytes_wrap(void)
31 init();
32 uint8_t buf[FIFO_SIZE] = {0};
33 f.popBytes(buf, FIFO_SIZE-1);
34 for(int i=0;i<FIFO_SIZE-1;i++) {
35 TEST_ASSERT_EQUAL(i, buf[i]);
39 void test_fifo_ensure()
41 f.flush();
42 // Push 25 9-byte packets with a 1 byte length header
43 for (int i = 0; i < 25; i++)
45 f.push(9); // 9 bytes;
46 for(int x = 0 ; x<9 ; x++)
47 f.push(i);
49 TEST_ASSERT_EQUAL(250, f.size());
51 // Ensure we can push a 99 byte packet with a prefix (i.e. 100 bytes)
52 f.ensure(100);
53 TEST_ASSERT_EQUAL(150, f.size()); // make sure that we've popped the right amount to fit our jumbo packet
54 TEST_ASSERT_EQUAL(9, f.pop()); // check that we have a header len
55 for (int x = 0; x < 9; x++)
56 TEST_ASSERT_EQUAL(10, f.pop()); // and that all the bytes in the head packet are what we expect
59 int main(int argc, char **argv)
61 UNITY_BEGIN();
62 RUN_TEST(test_fifo_pop_wrap);
63 RUN_TEST(test_fifo_popBytes_wrap);
64 RUN_TEST(test_fifo_ensure);
65 UNITY_END();
67 return 0;