RKH
test_rkhfwk_dynevt.c
Go to the documentation of this file.
1 /*
2  * --------------------------------------------------------------------------
3  *
4  * Framework RKH
5  * -------------
6  *
7  * State-machine framework for reactive embedded systems
8  *
9  * Copyright (C) 2010 Leandro Francucci.
10  * All rights reserved. Protected by international copyright laws.
11  *
12  *
13  * RKH is free software: you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Recycle Software
15  * Foundation, either version 3 of the License, or (at your option) any
16  * later version.
17  *
18  * RKH is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with RKH, see copying.txt file.
25  *
26  * Contact information:
27  * RKH site: http://vortexmakes.com/que-es/
28  * RKH GitHub: https://github.com/vortexmakes/RKH
29  * RKH Sourceforge: https://sourceforge.net/projects/rkh-reactivesys/
30  * e-mail: lf@vortexmakes.com
31  * ---------------------------------------------------------------------------
32  */
33 
46 /* -------------------------- Development history -------------------------- */
47 /*
48  * 2017.05.04 LeFr v2.4.05 Initial version
49  */
50 
51 /* -------------------------------- Authors -------------------------------- */
52 /*
53  * LeFr Leandro Francucci lf@vortexmakes.com
54  */
55 
56 /* --------------------------------- Notes --------------------------------- */
57 /* ----------------------------- Include files ----------------------------- */
58 #include "unity.h"
59 #include "rkhfwk_dynevt.h"
60 #include "Mock_rkhassert.h"
61 #include "Mock_rkhfwk_evtpool.h"
62 #include "Mock_rkhtrc_record.h"
63 #include "Mock_rkhtrc_filter.h"
64 #include "Mock_rkhport.h"
65 
66 /* ----------------------------- Local macros ------------------------------ */
67 /* ------------------------------- Constants ------------------------------- */
68 /* ---------------------------- Local data types --------------------------- */
69 /* ---------------------------- Global variables --------------------------- */
70 /* ---------------------------- Local variables ---------------------------- */
71 static rui8_t *storage;
72 
73 /* ----------------------- Local function prototypes ----------------------- */
74 /* ---------------------------- Local functions ---------------------------- */
75 static void
76 MockAssertCallback(const char* const file, int line, int cmock_num_calls)
77 {
78  TEST_PASS();
79 }
80 
81 /* ---------------------------- Global functions --------------------------- */
82 void
83 setUp(void)
84 {
85  Mock_rkhassert_Init();
86  Mock_rkhfwk_evtpool_Init();
87  rkh_trc_isoff__IgnoreAndReturn(RKH_FALSE);
89 }
90 
91 void
92 tearDown(void)
93 {
94  Mock_rkhassert_Verify();
95  Mock_rkhfwk_evtpool_Verify();
96  Mock_rkhassert_Destroy();
97  Mock_rkhfwk_evtpool_Destroy();
98 }
99 
106 void
107 test_RegisterOneEventPool(void)
108 {
109  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
110  (RKHEvtPool *)1);
111  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
112 }
113 
114 void
115 test_RegisterMultipleEventPool(void)
116 {
117  int i;
118 
119  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
120  {
121  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
122  (RKHEvtPool *)1);
123  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
124  }
125 }
126 
127 void
128 test_Fails_ExceedsNumberOfAllowedEventPools(void)
129 {
130  int i;
131 
132  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
133  {
134  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
135  (RKHEvtPool *)1);
136  }
137  rkh_assert_Expect("rkhfwk_dynevt", 0);
138  rkh_assert_IgnoreArg_file();
139  rkh_assert_IgnoreArg_line();
140  rkh_assert_StubWithCallback(MockAssertCallback);
141 
142  for (i = 0; i < (RKH_CFG_FWK_MAX_EVT_POOL + 1); ++i)
143  {
144  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
145  }
146 }
147 
148 void
149 test_Fails_UnavailablePool(void)
150 {
151  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
152  (RKHEvtPool *)0);
153  rkh_assert_Expect("rkhfwk_dynevt", 0);
154  rkh_assert_IgnoreArg_file();
155  rkh_assert_IgnoreArg_line();
156  rkh_assert_StubWithCallback(MockAssertCallback);
157 
158  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
159 }
160 
161 void
162 test_Fails_TriesAllocateOneEvtWithoutAssignedPool(void)
163 {
164  RKH_EVT_T *evt;
165 
166  rkh_assert_Expect("rkhfwk_dynevt", 0);
167  rkh_assert_IgnoreArg_file();
168  rkh_assert_IgnoreArg_line();
169  rkh_assert_StubWithCallback(MockAssertCallback);
170 
171  evt = rkh_fwk_ae(2, 1, (const void *)0xbeaf);
172  TEST_ASSERT_NULL(evt);
173 }
174 
175 void
176 test_AllocateOneEvt(void)
177 {
178  int i, blockSize;
179  RKH_EVT_T *pEvt, evt, *expectedEvt;
180  int *epAddress;
181  RKH_SIG_T sig;
182  rui8_t pool, nRef;
183 
184  sig = 1;
185  pool = 3;
186  nRef = 0;
187  evt.e = 4;
188  evt.pool = 0;
189  evt.nref = 8;
190  expectedEvt = &evt;
191 
192  for (i = 0, epAddress = (int *)0xdead0000, blockSize = 4;
194  ++i, (blockSize *= 2), ++epAddress)
195  {
196  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage),
197  blockSize,
198  (RKHEvtPool *)epAddress);
199  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
200  }
201  epAddress = (int *)0xdead0000;
202  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress), 4);
203  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 1), 8);
204  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 2),
205  16);
206  rkh_evtPool_get_ExpectAndReturn((RKHEvtPool *)(epAddress + 2),
207  expectedEvt);
208 
209  pEvt = rkh_fwk_ae(9, sig, (const void *)0xbeaf);
210 
211  TEST_ASSERT_EQUAL_PTR(expectedEvt, pEvt);
212  TEST_ASSERT_EQUAL(sig, pEvt->e);
213  TEST_ASSERT_EQUAL(pool, pEvt->pool);
214  TEST_ASSERT_EQUAL(nRef, pEvt->nref);
215 }
216 
217 void
218 test_Fails_ExceededBlockSize(void)
219 {
220  int i, blockSize;
221  RKH_EVT_T *evt;
222  int *epAddress;
223 
224  for (i = 0, epAddress = (int *)0xdead0000, blockSize = 4;
226  ++i, (blockSize *= 2), ++epAddress)
227  {
228  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage),
229  blockSize,
230  (RKHEvtPool *)epAddress);
231  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
232  }
233 
234  epAddress = (int *)0xdead0000;
235  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 0), 4);
236  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 1), 8);
237  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 2), 16);
238  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 3), 32);
239 
240  rkh_assert_Expect("rkhfwk_dynevt", 0);
241  rkh_assert_IgnoreArg_file();
242  rkh_assert_IgnoreArg_line();
243  rkh_assert_StubWithCallback(MockAssertCallback);
244 
245  evt = rkh_fwk_ae(33, 1, (const void *)0xbeaf);
246 
247  TEST_ASSERT_NULL(evt);
248 }
249 
250 void
251 test_Fails_UnavailableBlockFromPool(void)
252 {
253  int i, blockSize;
254  RKH_EVT_T *evt;
255  int *epAddress;
256 
257  for (i = 0, epAddress = (int *)0xdead0000, blockSize = 4;
259  ++i, (blockSize *= 2), ++epAddress)
260  {
261  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage),
262  blockSize,
263  (RKHEvtPool *)epAddress);
264  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
265  }
266  epAddress = (int *)0xdead0000;
267  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 0), 4);
268  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 1), 8);
269  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)(epAddress + 2), 16);
270  rkh_evtPool_get_ExpectAndReturn((RKHEvtPool *)(epAddress + 2), 0);
271 
272  rkh_assert_Expect("rkhfwk_dynevt", 0);
273  rkh_assert_IgnoreArg_file();
274  rkh_assert_IgnoreArg_line();
275  rkh_assert_StubWithCallback(MockAssertCallback);
276 
277  evt = rkh_fwk_ae(9, 1, (const void *)0xbeaf);
278 
279  TEST_ASSERT_NULL(evt);
280 }
281 
282 void
283 test_ReservesOneEvt(void)
284 {
285  RKH_EVT_T evt;
286 
287  evt.e = 8;
288  evt.nref = 16;
289  evt.pool = 32;
290  rkh_enter_critical_Expect();
291  rkh_exit_critical_Expect();
292 
293  rkh_fwk_reserve(&evt);
294 
295  TEST_ASSERT_EQUAL(8, evt.e);
296  TEST_ASSERT_EQUAL(16 + 1, evt.nref);
297  TEST_ASSERT_EQUAL(32, evt.pool);
298 }
299 
300 void
301 test_TriesRecycleNotReferencedEvt(void)
302 {
303  RKH_EVT_T evt;
304 
305  evt.nref = 0;
306  rkh_fwk_gc(&evt, (const void *)0xdead);
307 }
308 
309 void
310 test_TriesRecycleMultipleReferencedEvt(void)
311 {
312  RKH_EVT_T evt;
313 
314  evt.nref = 4;
315  rkh_enter_critical_Expect();
316  rkh_exit_critical_Expect();
317 
318  rkh_fwk_gc(&evt, (const void *)0xdead);
319 
320  TEST_ASSERT_EQUAL(3, evt.nref);
321 }
322 
323 void
324 test_RecycleEvt(void)
325 {
326  RKH_EVT_T evt;
327 
328  evt.nref = 1;
329  evt.pool = 1;
330  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
331  (RKHEvtPool *)1);
332  rkh_enter_critical_Expect();
333  rkh_exit_critical_Expect();
334  rkh_evtPool_put_Expect((RKHEvtPool *)1, &evt);
335 
336  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
337  rkh_fwk_gc(&evt, (const void *)0xdead);
338 }
339 
340 void
341 test_Fails_OnRecycleEvtNullPool(void)
342 {
343  RKH_EVT_T evt;
344 
345  evt.nref = 1;
346  evt.pool = 0;
347  rkh_enter_critical_Expect();
348  rkh_assert_Expect("rkhfwk_dynevt", 0);
349  rkh_assert_IgnoreArg_file();
350  rkh_assert_IgnoreArg_line();
351  rkh_assert_StubWithCallback(MockAssertCallback);
352 
353  rkh_fwk_gc(&evt, (const void *)0xdead);
354 }
355 
356 void
357 test_Fails_OnRecycleEvtWrongPool(void)
358 {
359  RKH_EVT_T evt;
360 
361  evt.nref = 1;
362  evt.pool = 2;
363  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
364  (RKHEvtPool *)1);
365  rkh_enter_critical_Expect();
366  rkh_assert_Expect("rkhfwk_dynevt", 0);
367  rkh_assert_IgnoreArg_file();
368  rkh_assert_IgnoreArg_line();
369  rkh_assert_StubWithCallback(MockAssertCallback);
370 
371  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
372  rkh_fwk_gc(&evt, (const void *)0xdead);
373 }
374 
380 /* ------------------------------ End of file ------------------------------ */
#define RKH_FALSE
Standard define.
Definition: rkhdef.h:256
void rkh_fwk_reserve(RKH_EVT_T *e)
Reserve the dynamic event to be recycled.
void rkh_dynEvt_init(void)
Initializes the dynamic event manager.
void rkh_fwk_gc(RKH_EVT_T *e, const void *const sender)
Recycle a dynamic event.
void rkh_fwk_registerEvtPool(void *sstart, rui32_t ssize, RKH_ES_T esize)
Registers a new event pool into the event pool list.
RKH_EVT_T * rkh_fwk_ae(RKH_ES_T esize, RKH_SIG_T e, const void *const sender)
Allocates an event from the previously created event pool.
#define RKH_CFG_FWK_MAX_EVT_POOL
If the dynamic event support is enabled (see RKH_CFG_FWK_DYN_EVT_EN) then the RKH_CFG_FWK_MAX_EVT_POO...
Definition: rkhcfg.h:114
rui8_t RKH_SIG_T
Definition: rkhevt.h:100
Specifies the interface of dynamic event support.
Represents events without parameters.
Definition: rkhevt.h:170
RKH_SIG_T e
Signal of the event instance.
Definition: rkhevt.h:175
rui8_t nref
Attribute of dynamic events.
Definition: rkhevt.h:181
rui8_t pool
Attribute of dynamic events (0 for static event).
Definition: rkhevt.h:187