1 #include "unittest_macros.h"
2 #include "gtest/gtest.h"
9 #include "common/circular_queue.h"
15 TEST(CircularQueue
, ElementsAreCorrect
){
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;
38 circularBufferPushElement(&buffer
, &value
);
40 for(uint8_t i
= 0;i
< dataToRemoveSize
[j
]; i
++)
43 circularBufferPopHead(&buffer
, &value
);
44 EXPECT_EQ(queue
.front(),value
);
47 EXPECT_EQ(circularBufferCountElements(&buffer
),queue
.size());
50 EXPECT_EQ(circularBufferIsEmpty(&buffer
),queue
.empty());
54 TEST(CircularQueue
, CheckIsEmptyAndIsFull16
){ //check with uint16_t
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
;
83 circularBufferPushElement(&buffer
, (uint8_t*)&value
);
85 for(uint8_t i
= 0;i
< dataToRemoveSize
[j
]; i
++)
88 circularBufferPopHead(&buffer
, (uint8_t*)&value
);
89 EXPECT_EQ(queue
.front(),value
);
92 EXPECT_EQ(circularBufferCountElements(&buffer
),queue
.size());
95 EXPECT_EQ(circularBufferIsFull(&buffer
),true);
98 while(!circularBufferIsEmpty(&buffer
))
101 circularBufferPopHead(&buffer
, (uint8_t*)&value
);
102 EXPECT_EQ(queue
.front(),value
);
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
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
;
140 circularBufferPushElement(&buffer
, (uint8_t*)&value
);
142 for(uint8_t i
= 0;i
< dataToRemoveSize
[j
]; i
++)
145 circularBufferPopHead(&buffer
, (uint8_t*)&value
);
146 EXPECT_EQ(queue
.front(),value
);
149 EXPECT_EQ(circularBufferCountElements(&buffer
),queue
.size());
152 EXPECT_EQ(circularBufferIsFull(&buffer
),true);
154 //Remove all elements
155 while(!circularBufferIsEmpty(&buffer
))
158 circularBufferPopHead(&buffer
, (uint8_t*)&value
);
159 EXPECT_EQ(queue
.front(),value
);
163 EXPECT_EQ(circularBufferIsFull(&buffer
),false);
164 EXPECT_EQ(circularBufferIsEmpty(&buffer
),true);
165 EXPECT_EQ(circularBufferIsEmpty(&buffer
),queue
.empty());