ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_scheduler_test.c
Go to the documentation of this file.
4 #if RUUVI_RUN_TESTS
5 #include "ruuvi_driver_error.h"
6 #include <stddef.h>
7 #include <string.h>
8 
23 static char test_output[20];
24 static uint8_t executions;
25 
26 static void test_handler (void * p_event_data, uint16_t event_size)
27 {
28  memcpy (test_output, p_event_data, event_size);
29  executions++;
30 }
31 
43 static bool ri_scheduler_init_test (const rd_test_print_fp printfp)
44 {
45  printfp ("\"init\":");
46  bool status = false;
47  rd_status_t err_code = RD_SUCCESS;
48  err_code = ri_scheduler_init ();
49 
50  if (RD_SUCCESS != err_code)
51  {
52  status |= true;
53  }
54  else
55  {
56  // Verify init check after init.
57  if (!ri_scheduler_is_init())
58  {
59  status |= true;
60  }
61 
62  // Verify that events are discarded on uninit
63  ri_scheduler_event_put (NULL, 0, test_handler);
65 
66  // Verify init check after uninit.
68  {
69  status |= true;
70  }
71 
72  err_code = ri_scheduler_init();
74 
75  if (executions)
76  {
77  status = true;
78  }
79 
80  // Dual-init.
81  err_code = ri_scheduler_init();
82 
83  if (RD_ERROR_INVALID_STATE != err_code)
84  {
85  status = true;
86  }
87  }
88 
89  if (status)
90  {
91  printfp ("\"fail\",\r\n");
92  }
93  else
94  {
95  printfp ("\"pass\",\r\n");
96  }
97 
99  return status;
100 }
101 
113 static bool ri_scheduler_execute_test (const rd_test_print_fp printfp)
114 {
115  bool status = false;
116  rd_status_t err_code = RD_SUCCESS;
117  char test_input[] = "Hello scheduler";
118  printfp ("\"execute\":");
119  err_code |= ri_scheduler_init();
120  err_code |= ri_scheduler_event_put (&test_input, sizeof (test_input), test_handler);
121  err_code |= ri_scheduler_event_put (NULL, 0, test_handler);
122  err_code |= ri_scheduler_event_put (NULL, 0, test_handler);
124 
125  if (RD_SUCCESS != err_code || strcmp (test_output, test_input) || (3 != executions))
126  {
127  status = true;
128  printfp ("\"fail\",\r\n");
129  }
130  else
131  {
132  printfp ("\"pass\",\r\n");
133  }
134 
136  return status;
137 }
138 
147 static bool ri_scheduler_event_put_test (const rd_test_print_fp printfp)
148 {
149  printfp ("\"put\":");
150  bool status = false;
151  rd_status_t err_code = RD_SUCCESS;
152  // Verify that scheduler will end up filled.
153  executions = 0;
154  err_code |= ri_scheduler_init();
155 
156  for (size_t ii = 0; ii <= RI_SCHEDULER_LENGTH; ii++)
157  {
158  err_code |= ri_scheduler_event_put (NULL, 0, test_handler);
159  }
160 
162 
163  if (RD_ERROR_NO_MEM != err_code || RI_SCHEDULER_LENGTH != executions)
164  {
165  status = true;
166  }
167 
168  err_code = ri_scheduler_event_put (NULL, RI_SCHEDULER_SIZE + 1, test_handler);
169 
170  if (RD_ERROR_INVALID_LENGTH != err_code)
171  {
172  status = true;
173  }
174 
175  err_code = ri_scheduler_event_put (NULL, 0, NULL);
176 
177  if (RD_ERROR_NULL != err_code)
178  {
179  status = true;
180  }
181 
183  err_code = ri_scheduler_event_put (NULL, 0, test_handler);
184 
185  if (RD_ERROR_INVALID_STATE != err_code)
186  {
187  status = true;
188  }
189 
190  if (status)
191  {
192  printfp ("\"fail\"\r\n");
193  }
194  else
195  {
196  printfp ("\"pass\"\r\n");
197  }
198 
199  return status;
200 }
201 
203 {
204  printfp ("\"scheduler\":{\r\n");
205  bool status = false;
206  status |= ri_scheduler_init_test (printfp);
207  status |= ri_scheduler_execute_test (printfp);
208  status |= ri_scheduler_event_put_test (printfp);
209  printfp ("},\r\n");
210  return status;
211 }
212 #endif
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_INVALID_LENGTH
Invalid Length.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_NO_MEM
No Memory for operation.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
rd_status_t ri_scheduler_event_put(const void *const p_event_data, const uint16_t event_size, const ruuvi_scheduler_event_handler_t handler)
Schedule given task to be executed on next call to ri_scheduler_execute.
bool ri_scheduler_run_integration_test(const rd_test_print_fp printfp)
Run all scheduler integration tests.
rd_status_t ri_scheduler_execute(void)
Executes all scheduled tasks.
bool ri_scheduler_is_init(void)
Check if scheduler is initialized.
rd_status_t ri_scheduler_init(void)
Enable implementation selected by application.
rd_status_t ri_scheduler_uninit(void)
Uninitialize scheduler.
void(* rd_test_print_fp)(const char *const msg)
function pointer to print test information
Header to enable and disable module compilation.
#define RI_SCHEDULER_LENGTH
#define RI_SCHEDULER_SIZE
Ruuvi error codes and error check function.
Interface functions to scheduler.
Test interface functions to scheduler.