commence breakage
[inav.git] / src / test / unit / circular_queue_unittest.cc
blob64dd5d04880e89884e7649ed95854b66d2f7cfe1
1 #include "unittest_macros.h"
2 #include "gtest/gtest.h"
3 #include <stdint.h>
5 #include <stdlib.h>
6 #include <time.h>
8 extern "C" {
9 #include "common/circular_queue.h"
11 #include <queue>
12 #include <climits>
15 TEST(CircularQueue, ElementsAreCorrect){
16 srand (time(NULL));
18 circularBuffer_t buffer;
20 const size_t bufferSize = 16;
21 uint8_t buff[bufferSize];
23 circularBufferInit(&buffer, buff, bufferSize, sizeof(char));
25 const uint8_t testBurst = 3;
27 const uint8_t dataToInsertSize[testBurst] = {5, 10, 3};
28 const uint8_t dataToRemoveSize[testBurst] = {3, 7, 2};
30 std::queue <uint8_t>queue;
32 for(uint8_t j=0; j<testBurst; j++){
34 for(uint8_t i = 0;i < dataToInsertSize[j]; i++)
36 uint8_t value = rand() % 255;
37 queue.push(value);
38 circularBufferPushElement(&buffer, &value);
40 for(uint8_t i = 0;i < dataToRemoveSize[j]; i++)
42 uint8_t value;
43 circularBufferPopHead(&buffer, &value);
44 EXPECT_EQ(queue.front(),value);
45 queue.pop();
47 EXPECT_EQ(circularBufferCountElements(&buffer),queue.size());
50 EXPECT_EQ(circularBufferIsEmpty(&buffer),queue.empty());
54 TEST(CircularQueue, CheckIsEmptyAndIsFull16){ //check with uint16_t
55 srand (time(NULL));
57 circularBuffer_t buffer;
59 typedef uint16_t tested_type;
61 const size_t bufferSize = 16 * sizeof(tested_type);
62 uint8_t buff[bufferSize];
64 circularBufferInit(&buffer, buff, bufferSize, sizeof(tested_type));
66 const int testBurst = 3;
68 const int dataToInsertSize[testBurst] = {10, 8, 4};
69 const int dataToRemoveSize[testBurst] = {3, 3, 0};
70 //At the end we have 0 elements
72 std::queue <tested_type>queue;
74 EXPECT_EQ(circularBufferIsEmpty(&buffer),true);
75 EXPECT_EQ(circularBufferIsFull(&buffer),false);
77 for(uint8_t j=0; j<testBurst; j++){
79 for(uint8_t i = 0;i < dataToInsertSize[j]; i++)
81 tested_type value = rand() % SHRT_MAX;
82 queue.push(value);
83 circularBufferPushElement(&buffer, (uint8_t*)&value);
85 for(uint8_t i = 0;i < dataToRemoveSize[j]; i++)
87 tested_type value;
88 circularBufferPopHead(&buffer, (uint8_t*)&value);
89 EXPECT_EQ(queue.front(),value);
90 queue.pop();
92 EXPECT_EQ(circularBufferCountElements(&buffer),queue.size());
95 EXPECT_EQ(circularBufferIsFull(&buffer),true);
97 //Remove all elements
98 while(!circularBufferIsEmpty(&buffer))
100 tested_type value;
101 circularBufferPopHead(&buffer, (uint8_t*)&value);
102 EXPECT_EQ(queue.front(),value);
103 queue.pop();
106 EXPECT_EQ(circularBufferIsFull(&buffer),false);
107 EXPECT_EQ(circularBufferIsEmpty(&buffer),true);
108 EXPECT_EQ(circularBufferIsEmpty(&buffer),queue.empty());
111 TEST(CircularQueue, CheckIsEmptyAndIsFull32){ //check with uint32_t
112 srand (time(NULL));
114 circularBuffer_t buffer;
116 typedef uint32_t tested_type;
118 const size_t bufferSize = 16 * sizeof(tested_type);
119 uint8_t buff[bufferSize];
121 circularBufferInit(&buffer, buff, bufferSize, sizeof(tested_type));
123 const int testBurst = 3;
125 const int dataToInsertSize[testBurst] = {10, 8, 4};
126 const int dataToRemoveSize[testBurst] = {3, 3, 0};
127 //At the end we have 0 elements
129 std::queue <tested_type>queue;
131 EXPECT_EQ(circularBufferIsEmpty(&buffer),true);
132 EXPECT_EQ(circularBufferIsFull(&buffer),false);
134 for(uint8_t j=0; j<testBurst; j++){
136 for(uint8_t i = 0;i < dataToInsertSize[j]; i++)
138 tested_type value = rand() % INT_MAX;
139 queue.push(value);
140 circularBufferPushElement(&buffer, (uint8_t*)&value);
142 for(uint8_t i = 0;i < dataToRemoveSize[j]; i++)
144 tested_type value;
145 circularBufferPopHead(&buffer, (uint8_t*)&value);
146 EXPECT_EQ(queue.front(),value);
147 queue.pop();
149 EXPECT_EQ(circularBufferCountElements(&buffer),queue.size());
152 EXPECT_EQ(circularBufferIsFull(&buffer),true);
154 //Remove all elements
155 while(!circularBufferIsEmpty(&buffer))
157 tested_type value;
158 circularBufferPopHead(&buffer, (uint8_t*)&value);
159 EXPECT_EQ(queue.front(),value);
160 queue.pop();
163 EXPECT_EQ(circularBufferIsFull(&buffer),false);
164 EXPECT_EQ(circularBufferIsEmpty(&buffer),true);
165 EXPECT_EQ(circularBufferIsEmpty(&buffer),queue.empty());