ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_timer_test.c
Go to the documentation of this file.
4 #if RUUVI_RUN_TESTS
5 #include "ruuvi_driver_error.h"
7 #include <stddef.h>
8 #include <string.h>
23 static char test_output[20];
24 static uint8_t executions;
25 
26 static void test_handler (void * context)
27 {
28  test_output[strlen (test_output)] = ( (char *) context) [0];
29  executions++;
30 }
31 
41 static bool ri_timer_init_test (const rd_test_print_fp printfp)
42 {
43  printfp ("\"init\":");
44  bool status = false;
45  rd_status_t err_code = RD_SUCCESS;
46  err_code = ri_timer_init();
47 
48  if (RD_SUCCESS != err_code)
49  {
50  status = true;
51  }
52  else
53  {
54  // Dual-init.
55  err_code = ri_timer_init();
56 
57  if (RD_ERROR_INVALID_STATE != err_code)
58  {
59  status = true;
60  }
61 
62  err_code = ri_timer_uninit();
63  err_code |= ri_timer_init();
64 
65  if (RD_SUCCESS != err_code)
66  {
67  status = true;
68  }
69  }
70 
71  if (status)
72  {
73  printfp ("\"fail\",\r\n");
74  }
75  else
76  {
77  printfp ("\"pass\",\r\n");
78  }
79 
81  return status;
82 }
83 
94 static bool ri_timer_create_test (const rd_test_print_fp printfp)
95 {
96  bool status = false;
97  rd_status_t err_code = RD_SUCCESS;
98  printfp ("\"create\":");
99  err_code |= ri_timer_init();
100  ri_timer_id_t tids[RI_TIMER_MAX_INSTANCES];
101  err_code |= ri_timer_create (&tids[0], RI_TIMER_MODE_SINGLE_SHOT, &test_handler);
102 
103  if (RD_SUCCESS != err_code)
104  {
105  status = true;
106  }
107  else
108  {
109  // Allocation should fail at max instances as one instance is allocated and not
110  // released already.
111  for (size_t ii = 0; ii < RI_TIMER_MAX_INSTANCES; ii++)
112  {
113  err_code |= ri_timer_create (&tids[ii], RI_TIMER_MODE_SINGLE_SHOT, &test_handler);
114  }
115 
116  if (RD_ERROR_RESOURCES != err_code)
117  {
118  status |= true;
119  }
120 
121  err_code = ri_timer_uninit();
122  err_code |= ri_timer_create (&tids[0], RI_TIMER_MODE_SINGLE_SHOT, &test_handler);
123 
124  if (RD_ERROR_INVALID_STATE != err_code)
125  {
126  status |= true;
127  }
128 
129  err_code = ri_timer_init();
130 
131  for (size_t ii = 0; ii < RI_TIMER_MAX_INSTANCES; ii++)
132  {
133  err_code |= ri_timer_create (&tids[ii], RI_TIMER_MODE_SINGLE_SHOT, &test_handler);
134  }
135 
136  if (RD_SUCCESS != err_code)
137  {
138  status |= true;
139  }
140  }
141 
142  if (status)
143  {
144  printfp ("\"fail\",\r\n");
145  }
146  else
147  {
148  printfp ("\"pass\",\r\n");
149  }
150 
151  ri_timer_uninit();
152  return status;
153 }
154 
163 static bool ri_timer_single_shot_test (const rd_test_print_fp printfp)
164 {
165  bool status = false;
166  rd_status_t err_code = RD_SUCCESS;
167  executions = 0;
168  char data[] = "0123456789ABCDEF";
169  printfp ("\"single_shot\":");
170  char expected_output[RI_TIMER_MAX_INSTANCES + 1] = {0};
171  memcpy (expected_output, data, RI_TIMER_MAX_INSTANCES);
172  ri_timer_id_t tids[RI_TIMER_MAX_INSTANCES];
173  err_code |= ri_timer_init();
174 
175  for (size_t ii = 0; ii < RI_TIMER_MAX_INSTANCES; ii++)
176  {
177  err_code |= ri_timer_create (&tids[ii], RI_TIMER_MODE_SINGLE_SHOT, &test_handler);
178  err_code |= ri_timer_start (tids[ii], (10U * ii) + 10U, &data[ii]);
179  }
180 
181  if (RD_SUCCESS != err_code)
182  {
183  status |= true;
184  }
185 
186  ri_delay_ms (10 * (RI_TIMER_MAX_INSTANCES + 1));
187 
188  if (strcmp (expected_output, test_output))
189  {
190  status = true;
191  }
192 
193  if (status)
194  {
195  printfp ("\"fail\",\r\n");
196  }
197  else
198  {
199  printfp ("\"pass\",\r\n");
200  }
201 
202  err_code |= ri_timer_uninit();
203  return status;
204 }
205 
214 static bool ri_timer_repeat_test (const rd_test_print_fp printfp)
215 {
216  bool status = false;
217  rd_status_t err_code = RD_SUCCESS;
218  executions = 0;
219  char data[] = "ABC";
220  printfp ("\"repeat\":");
221  char expected_output[] = "ABCABACBACBA";
222  memset (test_output, 0, sizeof (test_output));
223  ri_timer_id_t tids[RI_TIMER_MAX_INSTANCES];
224  err_code |= ri_timer_init();
225 
226  for (size_t ii = 0; ii < 3; ii++)
227  {
228  err_code |= ri_timer_create (&tids[ii], RI_TIMER_MODE_REPEATED, &test_handler);
229  }
230 
231  err_code |= ri_timer_start (tids[0], 11U, &data[0]);
232  err_code |= ri_timer_start (tids[1], 13U, &data[1]);
233  err_code |= ri_timer_start (tids[2], 17U, &data[2]);
234 
235  if (RD_SUCCESS != err_code)
236  {
237  status |= true;
238  }
239 
240  // 5 times A, 4 times B, 3 times C.
241  ri_delay_ms (56U);
242  err_code |= ri_timer_stop (tids[0]);
243  err_code |= ri_timer_stop (tids[1]);
244  err_code |= ri_timer_stop (tids[2]);
245 
246  if (strcmp (expected_output, test_output))
247  {
248  status = true;
249  }
250 
251  if (status)
252  {
253  printfp ("\"fail\"\r\n");
254  }
255  else
256  {
257  printfp ("\"pass\"\r\n");
258  }
259 
260  ri_timer_uninit();
261  return status;
262 }
263 
265 {
266  printfp ("\"timer\":{\r\n");
267  bool status = false;
268  status |= ri_timer_init_test (printfp);
269  status |= ri_timer_create_test (printfp);
270  status |= ri_timer_single_shot_test (printfp);
271  status |= ri_timer_repeat_test (printfp);
272  printfp ("},\r\n");
273  return status;
274 }
275 #endif
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_RESOURCES
Not enough resources for operation.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
void(* rd_test_print_fp)(const char *const msg)
function pointer to print test information
rd_status_t ri_timer_uninit(void)
rd_status_t ri_timer_start(ri_timer_id_t timer_id, uint32_t ms, void *const context)
Start given timer at a mode defined in ri_timer_create.
rd_status_t ri_timer_create(ri_timer_id_t *p_timer_id, ri_timer_mode_t mode, ruuvi_timer_timeout_handler_t timeout_handler)
rd_status_t ri_timer_init(void)
bool ri_timer_integration_test_run(const rd_test_print_fp printfp)
Run all timer integration tests.
rd_status_t ri_timer_stop(ri_timer_id_t timer_id)
void * ri_timer_id_t
Pointer to timer data.
@ RI_TIMER_MODE_REPEATED
@ RI_TIMER_MODE_SINGLE_SHOT
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Interface functions to timer.
rd_status_t ri_delay_ms(uint32_t time)
Delay a given number of milliseconds.