RKH
test_rkhfwk_evtpool.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.09 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 <string.h>
59 #include "unity.h"
60 #include "rkhfwk_evtpool.h"
61 #include "Mock_rkhtrc_record.h"
62 #include "Mock_rkhtrc_filter.h"
63 #include "Mock_rkhmempool.h"
64 #include "Mock_rkhassert.h"
65 
66 /* ----------------------------- Local macros ------------------------------ */
67 /* ------------------------------- Constants ------------------------------- */
68 /* ---------------------------- Local data types --------------------------- */
69 /* ---------------------------- Global variables --------------------------- */
70 /* ---------------------------- Local variables ---------------------------- */
71 static rui8_t *stoStart;
72 static rui16_t stoSize;
73 static RKH_ES_T evtSize;
74 
75 /* ----------------------- Local function prototypes ----------------------- */
76 /* ---------------------------- Local functions ---------------------------- */
77 static void
78 MockAssertCallback(const char* const file, int line, int cmock_num_calls)
79 {
80  TEST_PASS();
81 }
82 
83 static void
84 MockMemPoolInitCallback(RKH_MEMPOOL_T *mp, void* sstart, rui16_t ssize,
85  RKH_MPBS_T bsize, int cmock_num_calls)
86 {
87  mp->nblocks = 1; /* this value is only used just for testing */
88 }
89 
90 /* ---------------------------- Global functions --------------------------- */
91 void
92 setUp(void)
93 {
94  Mock_rkhmempool_Init();
95  Mock_rkhassert_Init();
96  rkh_trc_isoff__IgnoreAndReturn(RKH_FALSE);
98  rkh_assert_StubWithCallback(MockAssertCallback);
99  rkh_memPool_init_StubWithCallback(MockMemPoolInitCallback);
100 
101  stoStart = (void *)0xdeadbeaf;
102  stoSize = 128;
103  evtSize = 32;
104 }
105 
106 void
107 tearDown(void)
108 {
109  Mock_rkhmempool_Verify();
110  Mock_rkhmempool_Destroy();
111  Mock_rkhassert_Verify();
112  Mock_rkhassert_Destroy();
113 }
114 
121 void
122 test_AfterInitAllEvtPoolAvailable(void)
123 {
124  RKHEvtPool *ep;
125  int i;
126  char name[10];
127 
128  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
129  {
130  sprintf(name, "memPool%1d", i);
131  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
132  rkh_trc_obj_IgnoreArg_obj();
133  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
134  rkh_memPool_init_IgnoreArg_mp();
135 
136  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
137  TEST_ASSERT_NOT_NULL(ep);
138  }
139  TEST_ASSERT_EQUAL(RKH_CFG_FWK_MAX_EVT_POOL, i);
140 }
141 
142 void
143 test_GetOneEvtPool(void)
144 {
145  RKHEvtPool *ep;
146  char name[10];
147 
148  strcpy(name, "memPool0");
149  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
150  rkh_trc_obj_IgnoreArg_obj();
151  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
152  rkh_memPool_init_IgnoreArg_mp();
153 
154  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
155  TEST_ASSERT_NOT_NULL(ep);
156 }
157 
158 void
159 test_GetMultipleEvtPool(void)
160 {
161  rInt i;
162  RKHEvtPool *ep[RKH_CFG_FWK_MAX_EVT_POOL];
163  char name[10];
164 
165  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
166  {
167  sprintf(name, "memPool%1d", i);
168  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
169  rkh_trc_obj_IgnoreArg_obj();
170  rkh_memPool_init_Expect(0, stoStart + (stoSize * i),
171  stoSize, (RKH_MPBS_T)evtSize * (2 * (i + 1)));
172  rkh_memPool_init_IgnoreArg_mp();
173  ep[i] = rkh_evtPool_getPool(stoStart + (stoSize * i), stoSize,
174  evtSize * (2 * (i + 1)));
175  TEST_ASSERT_NOT_NULL(ep[i]);
176  }
177 
178  TEST_ASSERT_TRUE((ep[0] != ep[1]) && (ep[0] != ep[2]));
179  TEST_ASSERT_TRUE(ep[1] != ep[2]);
180 }
181 
182 void
183 test_Fails_ExceedsMaxAvailableEvtPool(void)
184 {
185  rInt i;
186  RKHEvtPool *ep;
187  char name[10];
188 
189  for (i = 0; i < RKH_CFG_FWK_MAX_EVT_POOL; ++i)
190  {
191  sprintf(name, "memPool%1d", i);
192  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
193  rkh_trc_obj_IgnoreArg_obj();
194  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
195  rkh_memPool_init_IgnoreArg_mp();
196  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
197  TEST_ASSERT_NOT_NULL(ep);
198  }
199  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
200  TEST_ASSERT_NULL(ep);
201 }
202 
203 void
204 test_GetBlockSize(void)
205 {
206  RKHEvtPool *ep;
207  char name[10];
208 
209  strcpy(name, "memPool0");
210  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
211  rkh_trc_obj_IgnoreArg_obj();
212  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
213  rkh_memPool_init_IgnoreArg_mp();
214 
215  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
217 }
218 
219 void
220 test_Fails_GetBlockSizeInvalidInstance(void)
221 {
222  rkh_assert_Expect("rkhfwk_evtpool", 0);
223  rkh_assert_IgnoreArg_file();
224  rkh_assert_IgnoreArg_line();
225 
226  rkh_evtPool_getBlockSize((RKHEvtPool *)0);
227 }
228 
229 void
230 test_GetBlock(void)
231 {
232  RKHEvtPool *ep;
233  RKH_EVT_T *evt;
234  char name[10];
235 
236  strcpy(name, "memPool0");
237  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
238  rkh_trc_obj_IgnoreArg_obj();
239  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
240  rkh_memPool_init_IgnoreArg_mp();
241  rkh_memPool_get_ExpectAndReturn(0, (void *)0xdead);
242  rkh_memPool_get_IgnoreArg_mp();
243 
244  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
245  evt = rkh_evtPool_get(ep);
246 
247  TEST_ASSERT_EQUAL_PTR((RKH_EVT_T *)0xdead, evt);
248 }
249 
250 void
251 test_Fails_GetBlockInvalidInstance(void)
252 {
253  rkh_assert_Expect("rkhfwk_evtpool", 0);
254  rkh_assert_IgnoreArg_file();
255  rkh_assert_IgnoreArg_line();
256 
257  rkh_evtPool_get((RKHEvtPool *)0);
258 }
259 
260 void
261 test_PutBlock(void)
262 {
263  RKHEvtPool *ep;
264  RKH_EVT_T *evt = (RKH_EVT_T *)0xdead;
265  char name[10];
266 
267  strcpy(name, "memPool0");
268  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
269  rkh_trc_obj_IgnoreArg_obj();
270  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
271  rkh_memPool_init_IgnoreArg_mp();
272  rkh_memPool_put_Expect(0, evt);
273  rkh_memPool_put_IgnoreArg_mp();
274 
275  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
276  rkh_evtPool_put(ep, evt);
277 }
278 
279 void
280 test_Fails_PutBlockInvalidInstance(void)
281 {
282  RKH_EVT_T *evt = (RKH_EVT_T *)0xdead;
283 
284  rkh_assert_Expect("rkhfwk_evtpool", 0);
285  rkh_assert_IgnoreArg_file();
286  rkh_assert_IgnoreArg_line();
287 
288  rkh_evtPool_put((RKHEvtPool *)0, evt);
289 }
290 
291 void
292 test_GetNumUsed(void)
293 {
294  RKHEvtPool *ep;
295  char name[10];
296 
297  strcpy(name, "memPool0");
298  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
299  rkh_trc_obj_IgnoreArg_obj();
300  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
301  rkh_memPool_init_IgnoreArg_mp();
302 
303  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
305 }
306 
307 void
308 test_Fails_GetNumUsedInvalidInstance(void)
309 {
310  rkh_assert_Expect("rkhfwk_evtpool", 0);
311  rkh_assert_IgnoreArg_file();
312  rkh_assert_IgnoreArg_line();
313 
314  rkh_evtPool_getNumUsed((RKHEvtPool *)0);
315 }
316 
317 void
318 test_GetNumMin(void)
319 {
320  RKHEvtPool *ep;
321  char name[10];
322 
323  strcpy(name, "memPool0");
324  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
325  rkh_trc_obj_IgnoreArg_obj();
326  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
327  rkh_memPool_init_IgnoreArg_mp();
328 
329  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
331 }
332 
333 void
334 test_Fails_GetNumMinInvalidInstance(void)
335 {
336  rkh_assert_Expect("rkhfwk_evtpool", 0);
337  rkh_assert_IgnoreArg_file();
338  rkh_assert_IgnoreArg_line();
339 
340  rkh_evtPool_getNumMin((RKHEvtPool *)0);
341 }
342 
343 void
344 test_GetNumBlock(void)
345 {
346  RKHEvtPool *ep;
347  char name[10];
348 
349  strcpy(name, "memPool0");
350  rkh_trc_obj_Expect(RKH_TE_FWK_OBJ, 0, name);
351  rkh_trc_obj_IgnoreArg_obj();
352  rkh_memPool_init_Expect(0, stoStart, stoSize, (RKH_MPBS_T)evtSize);
353  rkh_memPool_init_IgnoreArg_mp();
354 
355  ep = rkh_evtPool_getPool(stoStart, stoSize, evtSize);
357 }
358 
359 void
360 test_Fails_GetNumBlockInvalidInstance(void)
361 {
362  rkh_assert_Expect("rkhfwk_evtpool", 0);
363  rkh_assert_IgnoreArg_file();
364  rkh_assert_IgnoreArg_line();
365 
366  rkh_evtPool_getNumBlock((RKHEvtPool *)0);
367 }
368 
374 /* ------------------------------ End of file ------------------------------ */
#define RKH_FALSE
Standard define.
Definition: rkhdef.h:256
#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_ES_T
Definition: rkhevt.h:116
Specifies the event pool interface.
void rkh_evtPool_init()
Encapsulates the initialization of event pool manager.
rui8_t rkh_evtPool_getBlockSize(RKHEvtPool *const me)
Encapsulates how RKH should obtain the block size of an event pool.
rui8_t rkh_evtPool_getNumMin(RKHEvtPool *const me)
Encapsulates how RKH should return the lowest number of free blocks ever present in the pool me....
RKH_EVT_T * rkh_evtPool_get(RKHEvtPool *const me)
Encapsulates how RKH should obtain an event evt from the event pool me.
rui8_t rkh_evtPool_getNumBlock(RKHEvtPool *const me)
Encapsulates how RKH should return the total number of blocks in the pool me.
rui8_t rkh_evtPool_getNumUsed(RKHEvtPool *const me)
Encapsulates how RKH should return the current number of memory blocks used in the pool me.
RKHEvtPool * rkh_evtPool_getPool(void *stoStart, rui16_t stoSize, RKH_ES_T evtSize)
Encapsulates the creation of an event pool.
void rkh_evtPool_put(RKHEvtPool *const me, RKH_EVT_T *evt)
Encapsulates how RKH should return an event evt to the event pool me.
rui8_t RKH_MPBS_T
Definition: rkhmempool.h:95
#define RKH_TE_FWK_OBJ
Entry symbol table for memory object.
Represents events without parameters.
Definition: rkhevt.h:170
Defines the data structure used to memory block pool facility.
Definition: rkhmempool.h:192
RKH_MPNB_T nblocks
Total number of blocks in bytes.
Definition: rkhmempool.h:218