UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
buffer.c
1 /* @file buffer.c
2  *
3  * @author Phil Crump
4  *
5  * @ingroup tests
6  * @{
7  */
8 
9 #include "../../firmware.h"
10 #include "../../test.h"
11 
12 bool test_buffer_add_remove_element(void);
13 bool test_buffer_retrieve_element(void);
14 bool test_buffer_roll(void);
15 bool test_buffer_fill_elements(void);
16 
17 bool buffer_tests(void)
18 {
19  bool test_state = true;
20 
21  assert_run_print(test_buffer_add_remove_element(), "Buffer Add & Remove Element", test_state);
22 
23  assert_run_print(test_buffer_retrieve_element(), "Buffer Retrieve Element", test_state);
24 
25  assert_run_print(test_buffer_roll(), "Buffer Roll", test_state);
26 
27  assert_run_print(test_buffer_fill_elements(), "Buffer Fill & Overflow", test_state);
28 
29  return test_state;
30 }
31 
32 
33 bool test_buffer_add_remove_element(void)
34 {
35  uint8_t test_buffer_packet[BUFFER_SLOT_SIZE] = { 0 };
36 
37  Buffer_init();
38  /* Reset so we can predict data index = 0x01 */
39  Buffer_reset();
40 
41  if(Buffer_count_occupied() != 0)
42  {
43  return false;
44  }
45 
46  Buffer_store_new_data(test_buffer_packet);
47  if(Buffer_count_occupied() != 1)
48  {
49  return false;
50  }
51 
52  /* Remove incorrect index */
53  Buffer_remove_index(0x34);
54  if(Buffer_count_occupied() != 1)
55  {
56  return false;
57  }
58 
59  /* Remove correct index */
60  Buffer_remove_index(0x01);
61  if(Buffer_count_occupied() != 0)
62  {
63  return false;
64  }
65 
66  return true;
67 }
68 
69 bool test_buffer_retrieve_element(void)
70 {
71  uint32_t i;
72  uint8_t reference_buffer_packet[BUFFER_SLOT_SIZE];
73  uint8_t test_buffer_packet[BUFFER_SLOT_SIZE] = {0};
74 
75  /* Populate packet */
76  for(i=0; i<BUFFER_SLOT_SIZE; i++)
77  {
78  reference_buffer_packet[i] = (uint8_t)(Random(255));
79  }
80 
81  Buffer_init();
82  Buffer_reset();
83 
84  /* Check slot #0 is empty */
85  if(Buffer_get_occupancy(0x00))
86  {
87  return false;
88  }
89 
90  /* Attempt to retrieve a non-existent packet */
91  if(Buffer_get_next_data(test_buffer_packet))
92  {
93  return false;
94  }
95 
96  /* Store a single packet */
97  Buffer_store_new_data(reference_buffer_packet);
98 
99  /* Check slot #0 is occupied */
100  if(!Buffer_get_occupancy(0x00))
101  {
102  return false;
103  }
104 
105  /* Retrieve the packet */
106  if(!Buffer_get_next_data(test_buffer_packet))
107  {
108  return false;
109  }
110 
111  if(memcmp(test_buffer_packet, reference_buffer_packet, BUFFER_SLOT_SIZE) != 0)
112  {
113  return false;
114  }
115 
116  return true;
117 }
118 
119 bool test_buffer_roll(void)
120 {
121  uint32_t i;
122  uint8_t reference1_buffer_packet[BUFFER_SLOT_SIZE];
123  uint8_t reference2_buffer_packet[BUFFER_SLOT_SIZE];
124  uint8_t test_buffer_packet[BUFFER_SLOT_SIZE] = {0};
125 
126  /* Populate packet */
127  for(i=0; i<BUFFER_SLOT_SIZE; i++)
128  {
129  reference1_buffer_packet[i] = (uint8_t)(Random(255));
130  reference2_buffer_packet[i] = (uint8_t)(Random(255));
131  }
132 
133  Buffer_init();
134  Buffer_reset();
135 
136  /* Check buffer is empty */
137  if(Buffer_count_occupied() != 0)
138  {
139  if(TEST_VERBOSE)
140  {
141  Debug_print("Buffer count should be empty, is: %d\r\n", Buffer_count_occupied());
142  }
143  return false;
144  }
145 
146  /* Attempt to retrieve a non-existent packet */
147  if(Buffer_get_next_data(test_buffer_packet))
148  {
149  if(TEST_VERBOSE)
150  {
151  Debug_print("Succeeded in retrieving non-existent packet.\r\n");
152  }
153  return false;
154  }
155 
156  /* Store a single packet */
157  Buffer_store_new_data(reference1_buffer_packet);
158 
159  /* Check buffer is empty */
160  if(Buffer_count_occupied() != 1)
161  {
162  if(TEST_VERBOSE)
163  {
164  Debug_print("Buffer count should be 1, is: %d\r\n", Buffer_count_occupied());
165  }
166  return false;
167  }
168 
169  /* Retrieve the packet and verify it */
170  if(!Buffer_get_next_data(test_buffer_packet))
171  {
172  if(TEST_VERBOSE)
173  {
174  Debug_print("Failed to retrieve packet (attempt 1).\r\n");
175  }
176  return false;
177  }
178  if(memcmp(test_buffer_packet, reference1_buffer_packet, BUFFER_SLOT_SIZE) != 0)
179  {
180  if(TEST_VERBOSE)
181  {
182  Debug_print("Failed to match retrieved packet (attempt 1).\r\n");
183  }
184  return false;
185  }
186 
187  /* Retrieve the packet and verify it */
188  if(!Buffer_get_next_data(test_buffer_packet))
189  {
190  if(TEST_VERBOSE)
191  {
192  Debug_print("Failed to retrieve packet (attempt 2).\r\n");
193  }
194  return false;
195  }
196  if(memcmp(test_buffer_packet, reference1_buffer_packet, BUFFER_SLOT_SIZE) != 0)
197  {
198  if(TEST_VERBOSE)
199  {
200  Debug_print("Failed to match retrieved packet (attempt 2).\r\n");
201  }
202  return false;
203  }
204 
205  /* Check packet is still in the buffer */
206  if(Buffer_count_occupied() != 1)
207  {
208  return false;
209  }
210 
211  /* Store next packet */
212  Buffer_store_new_data(reference2_buffer_packet);
213 
214  /* Check new packet is in the buffer */
215  if(Buffer_count_occupied() != 2)
216  {
217  return false;
218  }
219 
220  /* Retrieve the packet and verify it */
221  if(!Buffer_get_next_data(test_buffer_packet))
222  {
223  if(TEST_VERBOSE)
224  {
225  Debug_print("Failed to retrieve packet2 (attempt 1).\r\n");
226  }
227  return false;
228  }
229  if(memcmp(test_buffer_packet, reference2_buffer_packet, BUFFER_SLOT_SIZE) != 0)
230  {
231  if(TEST_VERBOSE)
232  {
233  Debug_print("Failed to match retrieved packet2 (attempt 1).\r\n");
234  }
235  return false;
236  }
237 
238  /* Retrieve the packet and verify it */
239  if(!Buffer_get_next_data(test_buffer_packet))
240  {
241  if(TEST_VERBOSE)
242  {
243  Debug_print("Failed to retrieve packet1 (attempt 3).\r\n");
244  }
245  return false;
246  }
247  if(memcmp(test_buffer_packet, reference1_buffer_packet, BUFFER_SLOT_SIZE) != 0)
248  {
249  if(TEST_VERBOSE)
250  {
251  Debug_print("Failed to match retrieved packet1 (attempt 3).\r\n");
252  }
253  return false;
254  }
255 
256  /* Retrieve the packet and verify it */
257  if(!Buffer_get_next_data(test_buffer_packet))
258  {
259  if(TEST_VERBOSE)
260  {
261  Debug_print("Failed to retrieve packet2 (attempt 2).\r\n");
262  }
263  return false;
264  }
265  if(memcmp(test_buffer_packet, reference2_buffer_packet, BUFFER_SLOT_SIZE) != 0)
266  {
267  if(TEST_VERBOSE)
268  {
269  Debug_print("Failed to match retrieved packet2 (attempt 2).\r\n");
270  }
271  return false;
272  }
273 
274  /* Remove first packet */
275  Buffer_remove_index(0x01);
276  if(Buffer_count_occupied() != 1)
277  {
278  return false;
279  }
280 
281  /* Retrieve the packet and verify it */
282  if(!Buffer_get_next_data(test_buffer_packet))
283  {
284  if(TEST_VERBOSE)
285  {
286  Debug_print("Failed to retrieve packet2 (attempt 3).\r\n");
287  }
288  return false;
289  }
290  if(memcmp(test_buffer_packet, reference2_buffer_packet, BUFFER_SLOT_SIZE) != 0)
291  {
292  if(TEST_VERBOSE)
293  {
294  Debug_print("Failed to match retrieved packet2 (attempt 3).\r\n");
295  }
296  return false;
297  }
298 
299  return true;
300 }
301 
302 bool test_buffer_fill_elements(void)
303 {
304  uint8_t test_buffer_packet[BUFFER_SLOT_SIZE] = { 0 };
305 
306  Buffer_init();
307  Buffer_reset();
308 
309  /* Fill buffer */
310  for(uint16_t i=0; i<BUFFER_SLOTS; i++)
311  {
312  Buffer_store_new_data(test_buffer_packet);
313 
314  if(Buffer_count_occupied() != i+1)
315  {
316  if(TEST_VERBOSE)
317  {
318  Debug_print("i: %d Occupied: %d\r\n", i, Buffer_count_occupied());
319  }
320  return false;
321  }
322  }
323 
324  /* Check it holds capacity */
325  for(uint16_t i=0; i<BUFFER_SLOTS; i++)
326  {
327  Buffer_store_new_data(test_buffer_packet);
328 
329  if(Buffer_count_occupied() != BUFFER_SLOTS)
330  {
331  if(TEST_VERBOSE)
332  {
333  Debug_print("i: %d Occupied: %d\r\n", i, Buffer_count_occupied());
334  }
335  return false;
336  }
337  }
338 
339  return true;
340 }
341 
uint32_t Random(uint32_t max)
Definition: random.c:21
void Debug_print(char *fmt,...)
Definition: debug.c:16