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 web site: http://sourceforge.net/projects/rkh-reactivesys/
28  * e-mail: francuccilea@gmail.com
29  * ---------------------------------------------------------------------------
30  */
31 
44 /* -------------------------- Development history -------------------------- */
45 /*
46  * 2017.05.04 LeFr v2.4.05 Initial version
47  */
48 
49 /* -------------------------------- Authors -------------------------------- */
50 /*
51  * LeFr Leandro Francucci francuccilea@gmail.com
52  */
53 
54 /* --------------------------------- Notes --------------------------------- */
55 /* ----------------------------- Include files ----------------------------- */
56 #include "unity_fixture.h"
57 #include "rkhfwk_dynevt.h"
58 #include "Mock_rkhassert.h"
59 #include "Mock_rkhfwk_evtpool.h"
60 #include "Mock_rkhtrc_record.h"
61 #include "Mock_rkhtrc_filter.h"
62 #include "Mock_rkhport.h"
63 
64 /* ----------------------------- Local macros ------------------------------ */
65 /* ------------------------------- Constants ------------------------------- */
66 /* ---------------------------- Local data types --------------------------- */
67 /* ---------------------------- Global variables --------------------------- */
68 TEST_GROUP(dynevt);
69 
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 /* =========================== Bittbl test group =========================== */
83 TEST_SETUP(dynevt)
84 {
85  Mock_rkhassert_Init();
86  Mock_rkhfwk_evtpool_Init();
87  rkh_trc_isoff__IgnoreAndReturn(RKH_FALSE);
89 }
90 
91 TEST_TEAR_DOWN(dynevt)
92 {
93  Mock_rkhassert_Verify();
94  Mock_rkhfwk_evtpool_Verify();
95  Mock_rkhassert_Destroy();
96  Mock_rkhfwk_evtpool_Destroy();
97 }
98 
105 TEST(dynevt, RegisterOneEventPool)
106 {
107  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
108  (RKHEvtPool *)1);
109  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
110 }
111 
112 TEST(dynevt, RegisterMultipleEventPool)
113 {
114  int i;
115 
116  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
117  {
118  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
119  (RKHEvtPool *)1);
120  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
121  }
122 }
123 
124 TEST(dynevt, Fails_ExceedsNumberOfAllowedEventPools)
125 {
126  int i;
127 
128  rkh_assert_Expect("rkhfwk_dynevt", 0);
129  rkh_assert_IgnoreArg_line();
130  rkh_assert_StubWithCallback(MockAssertCallback);
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  for (i = 0; i < (RKH_CFG_FWK_MAX_EVT_POOL + 1); ++i)
138  {
139  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
140  }
141 }
142 
143 TEST(dynevt, Fails_UnavailablePool)
144 {
145  rkh_assert_Expect("rkhfwk_dynevt", 0);
146  rkh_assert_IgnoreArg_line();
147  rkh_assert_StubWithCallback(MockAssertCallback);
148 
149  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
150  (RKHEvtPool *)0);
151  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
152 }
153 
154 TEST(dynevt, Fails_TriesAllocateOneEvtWithoutAssignedPool)
155 {
156  RKH_EVT_T *evt;
157 
158  rkh_assert_Expect("rkhfwk_dynevt", 0);
159  rkh_assert_IgnoreArg_line();
160  rkh_assert_StubWithCallback(MockAssertCallback);
161 
162  evt = rkh_fwk_ae(2, 1, (const void *)0xbeaf);
163  TEST_ASSERT_NULL(evt);
164 }
165 
166 TEST(dynevt, AllocateOneEvt)
167 {
168  int i, blockSize;
169  RKH_EVT_T *pEvt, evt, *expectedEvt;
170  int epAddress;
171  RKH_SIG_T sig;
172  rui8_t pool, nRef;
173 
174  sig = 1;
175  pool = 3;
176  nRef = 0;
177  evt.e = 4;
178  evt.pool = 0;
179  evt.nref = 8;
180  expectedEvt = &evt;
181 
182  for (i = 0, epAddress = 0xdead0000, blockSize = 4;
184  ++i, (blockSize *= 2), ++epAddress)
185  {
186  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), blockSize,
187  (RKHEvtPool *)epAddress);
188  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
189  }
190  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0000, 4);
191  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0001, 8);
192  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0002, 16);
193  rkh_evtPool_get_ExpectAndReturn((RKHEvtPool *)0xdead0002, expectedEvt);
194 
195  pEvt = rkh_fwk_ae(9, sig, (const void *)0xbeaf);
196 
197  TEST_ASSERT_EQUAL_PTR(expectedEvt, pEvt);
198  TEST_ASSERT_EQUAL(sig, pEvt->e);
199  TEST_ASSERT_EQUAL(pool, pEvt->pool);
200  TEST_ASSERT_EQUAL(nRef, pEvt->nref);
201 }
202 
203 TEST(dynevt, Fails_ExceededBlockSize)
204 {
205  int i, blockSize;
206  RKH_EVT_T *evt;
207  int epAddress;
208 
209  rkh_assert_Expect("rkhfwk_dynevt", 0);
210  rkh_assert_IgnoreArg_line();
211  rkh_assert_StubWithCallback(MockAssertCallback);
212 
213  for (i = 0, epAddress = 0xdead0000, blockSize = 4;
215  ++i, (blockSize *= 2), ++epAddress)
216  {
217  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), blockSize,
218  (RKHEvtPool *)epAddress);
219  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
220  }
221  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0000, 4);
222  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0001, 8);
223  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0002, 16);
224  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0003, 32);
225 
226  evt = rkh_fwk_ae(33, 1, (const void *)0xbeaf);
227 
228  TEST_ASSERT_NULL(evt);
229 }
230 
231 TEST(dynevt, Fails_UnavailableBlockFromPool)
232 {
233  int i, blockSize;
234  RKH_EVT_T *evt;
235  int epAddress;
236 
237  rkh_assert_Expect("rkhfwk_dynevt", 0);
238  rkh_assert_IgnoreArg_line();
239  rkh_assert_StubWithCallback(MockAssertCallback);
240 
241  for (i = 0, epAddress = 0xdead0000, blockSize = 4;
243  ++i, (blockSize *= 2), ++epAddress)
244  {
245  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), blockSize,
246  (RKHEvtPool *)epAddress);
247  rkh_fwk_registerEvtPool(storage, sizeof(storage), blockSize);
248  }
249  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0000, 4);
250  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0001, 8);
251  rkh_evtPool_getBlockSize_ExpectAndReturn((RKHEvtPool *)0xdead0002, 16);
252  rkh_evtPool_get_ExpectAndReturn((RKHEvtPool *)0xdead0002, 0);
253 
254  evt = rkh_fwk_ae(9, 1, (const void *)0xbeaf);
255 
256  TEST_ASSERT_NULL(evt);
257 }
258 
259 TEST(dynevt, ReservesOneEvt)
260 {
261  RKH_EVT_T evt;
262 
263  evt.e = 8;
264  evt.nref = 16;
265  evt.pool = 32;
266  rkh_enter_critical_Expect();
267  rkh_exit_critical_Expect();
268 
269  rkh_fwk_reserve(&evt);
270 
271  TEST_ASSERT_EQUAL(8, evt.e);
272  TEST_ASSERT_EQUAL(16 + 1, evt.nref);
273  TEST_ASSERT_EQUAL(32, evt.pool);
274 }
275 
276 TEST(dynevt, TriesRecycleNotReferencedEvt)
277 {
278  RKH_EVT_T evt;
279 
280  evt.nref = 0;
281  rkh_fwk_gc(&evt, (const void *)0xdead);
282 }
283 
284 TEST(dynevt, TriesRecycleMultipleReferencedEvt)
285 {
286  RKH_EVT_T evt;
287 
288  evt.nref = 4;
289  rkh_enter_critical_Expect();
290  rkh_exit_critical_Expect();
291 
292  rkh_fwk_gc(&evt, (const void *)0xdead);
293 
294  TEST_ASSERT_EQUAL(3, evt.nref);
295 }
296 
297 TEST(dynevt, RecycleEvt)
298 {
299  RKH_EVT_T evt;
300 
301  evt.nref = 1;
302  evt.pool = 1;
303  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
304  (RKHEvtPool *)1);
305  rkh_evtPool_put_Expect((RKHEvtPool *)1, &evt);
306  rkh_enter_critical_Expect();
307  rkh_exit_critical_Expect();
308 
309  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
310  rkh_fwk_gc(&evt, (const void *)0xdead);
311 }
312 
313 TEST(dynevt, Fails_OnRecycleEvtNullPool)
314 {
315  RKH_EVT_T evt;
316 
317  evt.nref = 1;
318  evt.pool = 0;
319  rkh_enter_critical_Expect();
320  rkh_exit_critical_Expect();
321  rkh_assert_Expect("rkhfwk_dynevt", 0);
322  rkh_assert_IgnoreArg_line();
323  rkh_assert_StubWithCallback(MockAssertCallback);
324 
325  rkh_fwk_gc(&evt, (const void *)0xdead);
326 }
327 
328 TEST(dynevt, Fails_OnRecycleEvtWrongPool)
329 {
330  RKH_EVT_T evt;
331 
332  evt.nref = 1;
333  evt.pool = 2;
334  rkh_evtPool_getPool_ExpectAndReturn(storage, sizeof(storage), 4,
335  (RKHEvtPool *)1);
336  rkh_enter_critical_Expect();
337  rkh_exit_critical_Expect();
338  rkh_assert_Expect("rkhfwk_dynevt", 0);
339  rkh_assert_IgnoreArg_line();
340  rkh_assert_StubWithCallback(MockAssertCallback);
341 
342  rkh_fwk_registerEvtPool(storage, sizeof(storage), 4);
343  rkh_fwk_gc(&evt, (const void *)0xdead);
344 }
345 
351 /* ------------------------------ End of file ------------------------------ */
RKH_SIG_T e
Signal of the event instance.
Definition: rkhevt.h:173
rui8_t pool
Attribute of dynamic events (0 for static event).
Definition: rkhevt.h:185
Represents events without parameters.
Definition: rkhevt.h:167
rui8_t RKH_SIG_T
Definition: rkhevt.h:98
#define RKH_FALSE
Standard define.
Definition: rkhdef.h:254
void rkh_dynEvt_init(void)
Initializes the dynamic event manager.
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.
void rkh_fwk_registerEvtPool(void *sstart, rui32_t ssize, RKH_ES_T esize)
Registers a new event pool into the event pool list.
rui8_t nref
Attribute of dynamic events.
Definition: rkhevt.h:179
void rkh_fwk_reserve(RKH_EVT_T *e)
Reserve the dynamic event to be recycled.
void rkh_fwk_gc(RKH_EVT_T *e, const void *const sender)
Recycle a dynamic event.
#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:112
Specifies the interface of dynamic event support.