firmware  v0.1.2
Chromation Spectrometer Dev-Kit
test_Queue.c
Go to the documentation of this file.
1 
17 #include "unity.h"
18 #include "test_Queue.h"
19 #include "Queue.h"
20 
21 /* ------------ */
22 /* Queue memory */
23 /* ------------ */
24 // Tests allocate static memory for the queue object.
25 volatile Queue_s * SpiFifo;
26 // Define maximum size (bytes) of the queue's buffer
27 #define max_length_of_queue 5
28 /* --------------------------------------------------- */
29 /* FIFO buffer memory is allocated local to each test. */
30 /* --------------------------------------------------- */
31 // Memory for the buffer (`spi_rx_buffer`) is allocated on the
32 // stack (`spi_rx_buffer` is a local variable in each test).
33 // This eliminates the need for buffer setup/teardown code.
34 
35 void QueueInit_returns_a_pointer_to_a_Queue_struct(void)
36 {
37  /* =====[ Setup ]===== */
38  volatile uint8_t spi_rx_buffer[max_length_of_queue];
39  /* =====[ Operate and Test ]===== */
41  TEST_PASS();
42 }
43 void QueueInit_memory_for_Queue_struct_is_allocated_in_Queue_object_file(void)
44 {
45  /* =====[ Setup ]===== */
46  volatile uint8_t spi_rx_buffer[max_length_of_queue];
47  /* =====[ Operate and Test ]===== */
49  /* printf( */
50  /* "QueueInit accesses memory allocated " */
51  /* "for Queue object in Queue.o\n" */
52  /* ); */
53  TEST_PASS();
54 }
55 void QueueInit_assigns_input_buffer_as_Queue_buffer(void)
56 {
57  TEST_PASS();
58 }
59 void QueueInit_size_input_is_the_maximum_Queue_length(void)
60 {
61  TEST_PASS();
62 }
63 void QueueInit_initializes_Queue_with_length_0(void)
64 {
65  /* =====[ Setup ]===== */
66  volatile uint8_t spi_rx_buffer[max_length_of_queue];
67  /* =====[ Operate ]===== */
69  /* =====[ Test ]===== */
70  TEST_ASSERT_EQUAL_UINT16(0, QueueLength(SpiFifo));
71 }
72 void QueueLength_increments_after_a_push(void)
73 {
74  /* =====[ Setup ]===== */
75  volatile uint8_t spi_rx_buffer[max_length_of_queue];
77  /* =====[ Operate ]===== */
78  QueuePush(SpiFifo, 0xAB);
79  /* =====[ Test ]===== */
80  TEST_ASSERT_EQUAL_UINT16(1, QueueLength(SpiFifo));
81 }
82 void QueueLength_does_not_increase_beyond_max_length(void)
83 {
84  /* =====[ Setup ]===== */
85  volatile uint8_t spi_rx_buffer[max_length_of_queue];
87  // Fill the queue
88  uint8_t bytes_pushed=0;
89  while(bytes_pushed < max_length_of_queue)
90  {
91  QueuePush(SpiFifo, 0xCD);
92  bytes_pushed++;
93  }
94  // Assert the Length is at its max value.
95  TEST_ASSERT_EQUAL_UINT16(max_length_of_queue, QueueLength(SpiFifo));
96  /* =====[ Operate ]===== */
97  // Try another push.
98  QueuePush(SpiFifo, 0xEF);
99  /* =====[ Test ]===== */
100  // Assert the Length is still at its max value.
101  TEST_ASSERT_EQUAL_UINT16(max_length_of_queue, QueueLength(SpiFifo));
102 }
103 void QueueLength_decrements_after_a_pop(void)
104 {
105  /* =====[ Setup ]===== */
106  volatile uint8_t spi_rx_buffer[max_length_of_queue];
108  // Fill the queue
109  QueuePush(SpiFifo, 0xAB);
110  QueuePush(SpiFifo, 0xCD);
111  // Assert queue length is 2
112  TEST_ASSERT_EQUAL_UINT16(2, QueueLength(SpiFifo));
113  /* =====[ Operate ]===== */
114  QueuePop(SpiFifo);
115  // Assert queue length decrements after pop
116  TEST_ASSERT_EQUAL_UINT16(1, QueueLength(SpiFifo));
117 }
118 void QueueLength_does_not_decrease_below_zero(void)
119 {
120  /* =====[ Setup ]===== */
121  volatile uint8_t spi_rx_buffer[max_length_of_queue];
123  // Fill the queue
124  QueuePush(SpiFifo, 0xAB);
125  QueuePush(SpiFifo, 0xCD);
126  // Assert Queue length is 2
127  TEST_ASSERT_EQUAL_UINT16(2, QueueLength(SpiFifo));
128  /* =====[ Operate ]===== */
129  QueuePop(SpiFifo);
130  QueuePop(SpiFifo);
131  // Assert Queue length is 0
132  TEST_ASSERT_EQUAL_UINT16(0, QueueLength(SpiFifo));
133  QueuePop(SpiFifo);
134  /* =====[ Test ]===== */
135  // Assert Queue length is still 0
136  TEST_ASSERT_EQUAL_UINT16(0, QueueLength(SpiFifo));
137 }
138 void QueuePush_writes_byte_to_Queue_buffer(void)
139 {
140  /* =====[ Setup ]===== */
141  volatile uint8_t spi_rx_buffer[max_length_of_queue];
143  /* =====[ Operate ]===== */
144  QueuePush(SpiFifo, 0xAB);
145  /* =====[ Test ]===== */
146  TEST_ASSERT_EQUAL_UINT8(0xAB, spi_rx_buffer[0]);
147 }
148 void QueuePush_writes_next_byte_to_address_after_previous_write(void)
149 {
150  /* =====[ Setup ]===== */
151  volatile uint8_t spi_rx_buffer[max_length_of_queue];
153  /* =====[ Operate ]===== */
154  QueuePush(SpiFifo, 0xAB);
155  QueuePush(SpiFifo, 0xCD);
156  TEST_ASSERT_EQUAL_UINT8(0xAB, spi_rx_buffer[0]);
157  TEST_ASSERT_EQUAL_UINT8(0xCD, spi_rx_buffer[1]);
158 }
159 void QueuePush_does_not_write_byte_if_Queue_is_full(void)
160 {
161  /* =====[ Setup ]===== */
162  volatile uint8_t spi_rx_buffer[max_length_of_queue];
164  // Fill the queue
165  uint8_t bytes_pushed=0;
166  while(bytes_pushed < max_length_of_queue)
167  {
168  QueuePush(SpiFifo, 0xCD);
169  bytes_pushed++;
170  }
171  // Assert the Length is at its max value.
172  TEST_ASSERT_EQUAL_UINT16(max_length_of_queue, QueueLength(SpiFifo));
173  /* =====[ Operate ]===== */
174  // Try another push.
175  QueuePush(SpiFifo, 0xEF);
176  /* =====[ Test ]===== */
177  // Assert no values were overwritten.
178  bytes_pushed=0;
179  while(bytes_pushed < max_length_of_queue)
180  {
181  TEST_ASSERT_EQUAL_UINT8(0xCD, spi_rx_buffer[0]);
182  bytes_pushed++;
183  }
184 }
185 void QueuePush_hits_end_of_buffer_and_wraps_around_if_Queue_is_not_full(void)
186 {
187  /* =====[ Setup ]===== */
188  volatile uint8_t spi_rx_buffer[max_length_of_queue];
190  // Fill the queue
191  uint8_t bytes_pushed=0;
192  while(bytes_pushed < max_length_of_queue)
193  {
194  QueuePush(SpiFifo, 0xCD);
195  bytes_pushed++;
196  }
197  // Assert the Queue is full
198  TEST_ASSERT_TRUE(QueueIsFull(SpiFifo));
199  // Assert the head is pointing at the last byte index in the Queue.
200  TEST_ASSERT_EQUAL_UINT16(max_length_of_queue, QueueLength(SpiFifo));
201  // Empty the queue
202  uint8_t bytes_popped=0;
203  while(bytes_popped < max_length_of_queue)
204  {
205  QueuePop(SpiFifo);
206  bytes_popped++;
207  }
208  // Assert the Queue is empty.
209  TEST_ASSERT_TRUE(QueueIsEmpty(SpiFifo));
210  /* =====[ Operate ]===== */
211  // Put more data on the Queue. `head` should wrap around.
212  QueuePush(SpiFifo, 33);
213  /* =====[ Test ]===== */
214  TEST_ASSERT_EQUAL_MESSAGE(
215  33, spi_rx_buffer[0],
216  "Failed to overwrite byte 0! "
217  "Head should wrap around when it reaches the end of the array."
218  );
219 }
220 void QueuePop_removes_oldest_byte_from_Queue(void)
221 {
222  /* =====[ Setup ]===== */
223  volatile uint8_t spi_rx_buffer[max_length_of_queue];
225  // Fill the queue
226  QueuePush(SpiFifo, 0xAB); // oldest byte in Queue
227  QueuePush(SpiFifo, 0xCD); // next-oldest byte in Queue
228  /* =====[ Operate ]===== */
229  uint8_t byte_0 = QueuePop(SpiFifo);
230  /* =====[ Test ]===== */
231  // Assert the oldest byte is read.
232  TEST_ASSERT_EQUAL_UINT8(0xAB, byte_0);
233  /* =====[ Operate ]===== */
234  uint8_t byte_1 = QueuePop(SpiFifo);
235  /* =====[ Test ]===== */
236  // Assert the next-oldest byte is read.
237  // This implies the oldest byte was removed on the previous pop.
238  TEST_ASSERT_EQUAL_UINT8(0xCD, byte_1);
239 }
240 void QueuePop_returns_oldest_byte(void)
241 {
242  /* =====[ Setup ]===== */
243  volatile uint8_t spi_rx_buffer[max_length_of_queue];
245  // Fill the queue
246  QueuePush(SpiFifo, 0xAB);
247  QueuePush(SpiFifo, 0xCD);
248  /* =====[ Operate ]===== */
249  uint8_t byte_0 = QueuePop(SpiFifo);
250  /* =====[ Test ]===== */
251  TEST_ASSERT_EQUAL_UINT8(0xAB, byte_0);
252 }
253 void QueuePop_does_not_remove_any_bytes_if_Queue_is_empty(void)
254 {
255  /* =====[ Setup ]===== */
256  volatile uint8_t spi_rx_buffer[max_length_of_queue];
258  // Put fake old garbage non-zero values in the fake buffer
259  spi_rx_buffer[0] = 0xAB;
260  spi_rx_buffer[1] = 0xCD;
261  TEST_ASSERT_EQUAL_UINT8(0, QueuePop(SpiFifo));
262  // Removing a byte means advancing tail.
263  // Test is to check that this pop did not move tail.
264  // Push one byte into the Queue.
265  QueuePush(SpiFifo, 0x01);
266  // Head is at byte 1.
267  // But tail should still be at byte 0.
268  /* =====[ Operate and Test ]===== */
269  // If the value popped is the one just pushed, tail did not move.
270  TEST_ASSERT_EQUAL_UINT8(0x01, QueuePop(SpiFifo));
271  // Now tail should be at byte 1, but Queue is empty, so Pop should return 0.
272  TEST_ASSERT_EQUAL_UINT8(0, QueuePop(SpiFifo));
273 }
274 void QueuePop_returns_0_if_Queue_is_empty(void)
275 {
276  /* =====[ Setup ]===== */
277  volatile uint8_t spi_rx_buffer[max_length_of_queue];
279  // Put fake old garbage non-zero values in the fake buffer
280  spi_rx_buffer[0] = 0xAB;
281  spi_rx_buffer[1] = 0xCD;
282  TEST_ASSERT_EQUAL_UINT8(0xAB, spi_rx_buffer[0]);
283  /* =====[ Operate and Test ]===== */
284  TEST_ASSERT_EQUAL_UINT8(0, QueuePop(SpiFifo));
285 }
286 void QueuePop_hits_end_of_buffer_and_wraps_around_if_Queue_is_not_empty(void)
287 {
288  /* =====[ Setup ]===== */
289  // Allocate buffer with one extra byte.
290  volatile uint8_t spi_rx_buffer[max_length_of_queue+1];
291  // Extra byte is to place a value that Pop should never see.
293  uint8_t bytes_pushed=0;
294  while(bytes_pushed < max_length_of_queue)
295  {
296  QueuePush(SpiFifo, 0x11);
297  bytes_pushed++;
298  }
299  // Assert the Queue is full
300  TEST_ASSERT_TRUE(QueueIsFull(SpiFifo));
301  // Load next byte of buffer with value Pop should never see
303  // Empty the queue
304  uint8_t bytes_popped=0;
305  while(bytes_popped < max_length_of_queue)
306  {
307  QueuePop(SpiFifo);
308  bytes_popped++;
309  }
310  // Assert the Queue is empty.
311  TEST_ASSERT_TRUE(QueueIsEmpty(SpiFifo));
312  // Put more data on the Queue. `head` should wrap around.
313  QueuePush(SpiFifo, 33);
314  // Check test is setup: assert byte 0 is 33.
315  TEST_ASSERT_EQUAL_MESSAGE(
316  33, spi_rx_buffer[0],
317  "Failed to overwrite byte 0! "
318  "Head should wrap around when it reaches the end of the array."
319  );
320  /* =====[ Operate and Test ]===== */
321  // Pop data from the Queue.
322  // `tail` should wrap around to read the value just pushed.
323  // If `tail` does not wrap, it will point to the value 99.
324  TEST_ASSERT_EQUAL_UINT8(33, QueuePop(SpiFifo));
325 }
326 void QueueIsFull_returns_true_if_Queue_is_full(void)
327 {
328  /* =====[ Setup ]===== */
329  volatile uint8_t spi_rx_buffer[max_length_of_queue];
331  // Fill the Queue
332  uint16_t bytes_pushed = 0;
333  while(bytes_pushed < max_length_of_queue)
334  {
335  QueuePush(SpiFifo, 0);
336  bytes_pushed++;
337  }
338  // Assert the Length is at its max value.
339  TEST_ASSERT_EQUAL_UINT16(max_length_of_queue, QueueLength(SpiFifo));
340  /* =====[ Operate and Test ]===== */
341  TEST_ASSERT_TRUE(QueueIsFull(SpiFifo));
342 }
343 void QueueIsFull_returns_false_if_Queue_is_not_full(void)
344 {
345  /* =====[ Setup ]===== */
346  volatile uint8_t spi_rx_buffer[max_length_of_queue];
348  uint16_t const underfilled = max_length_of_queue-1;
349  uint8_t bytes_pushed=0;
350  while(bytes_pushed < underfilled)
351  {
352  QueuePush(SpiFifo, 0xAB);
353  bytes_pushed++;
354  }
355  // Assert the Length is less than the max value.
356  TEST_ASSERT_EQUAL_UINT16(underfilled, QueueLength(SpiFifo));
357  /* =====[ Operate and Test ]===== */
358  TEST_ASSERT_FALSE(QueueIsFull(SpiFifo));
359 }
360 void QueueIsEmpty_returns_true_if_Queue_is_empty(void)
361 {
362  /* =====[ Setup ]===== */
363  volatile uint8_t spi_rx_buffer[max_length_of_queue];
365  // Assert the Queue Length is 0 (nothing pushed onto Queue yet)
366  TEST_ASSERT_EQUAL_UINT16(0, QueueLength(SpiFifo));
367  /* =====[ Operate and Test ]===== */
368  TEST_ASSERT_TRUE(QueueIsEmpty(SpiFifo));
369 }
370 void QueueIsEmpty_returns_false_if_Queue_is_not_empty(void)
371 {
372  /* =====[ Setup ]===== */
373  volatile uint8_t spi_rx_buffer[max_length_of_queue];
375  QueuePush(SpiFifo, 0xAB);
376  // Assert the Queue Length is 1
377  TEST_ASSERT_EQUAL_UINT16(1, QueueLength(SpiFifo));
378  /* =====[ Operate and Test ]===== */
379  TEST_ASSERT_FALSE(QueueIsEmpty(SpiFifo));
380 }
381 
uint8_t QueuePop(volatile Queue_s *SpiFifo)
Definition: Queue.h:139
uint16_t QueueLength(volatile Queue_s *pq)
Definition: Queue.h:76
bool QueueIsFull(volatile Queue_s *SpiFifo)
Definition: Queue.h:86
void QueuePush(volatile Queue_s *SpiFifo, uint8_t data_to_push)
Definition: Queue.h:122
volatile Queue_s * QueueInit(volatile uint8_t *buffer, uint16_t const buffer_size_in_bytes)
Definition: Queue.h:51
bool QueueIsEmpty(volatile Queue_s *SpiFifo)
Definition: Queue.h:95
Queue uses a byte array as a circular buffer.
Definition: Queue.h:27
#define max_length_of_queue
Maximum size of the Queue's FIFO buffer is 24 bytes.
Definition: vis-spi-out.c:29
volatile uint8_t spi_rx_buffer[max_length_of_queue]
Allocate static memory for the Queue's FIFO buffer.
Definition: vis-spi-out.c:31