RKH
test_smPolymorphism.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 Free 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  * 2016.12.15 LeFr v2.4.05 ---
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 "unitrazer.h"
58 #include "rkhsma.h"
59 #include "smPolymorphism.h"
60 #include "Mock_rkhqueue.h"
61 #include "Mockrkhsm.h"
62 #include "Mockrkhtrc.h"
63 #include "Mock_rkhport.h"
64 
65 /* ----------------------------- Local macros ------------------------------ */
66 /* ------------------------------- Constants ------------------------------- */
67 /* ---------------------------- Local data types --------------------------- */
68 /* ---------------------------- Global variables --------------------------- */
69 TEST_GROUP(polymorphism);
70 
71 /* ---------------------------- Local variables ---------------------------- */
72 static RKH_EVT_T event = {0, 0, 0};
73 
74 /* ----------------------- Local function prototypes ----------------------- */
75 /* ---------------------------- Local functions ---------------------------- */
76 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
77 static void
78 checkVtbl(RKH_SMA_T *me, RKHActivate activate, RKHTask task,
79  RKHPostFifo postFifo, RKHPostLifo postLifo)
80 {
81  TEST_ASSERT_EQUAL_PTR(activate, me->vptr->activate);
82  TEST_ASSERT_EQUAL_PTR(task, me->vptr->task);
83  TEST_ASSERT_EQUAL_PTR(postFifo, me->vptr->post_fifo);
84  TEST_ASSERT_EQUAL_PTR(postLifo, me->vptr->post_lifo);
85 }
86 
87 static void
88 testActivate(RKH_SMA_T *me, const RKH_EVT_T **qSto, RKH_QUENE_T qSize,
89  void *stkSto, rui32_t stkSize)
90 {
91  (void)me;
92  (void)qSto;
93  (void)qSize;
94  (void)stkSto;
95  (void)stkSize;
96 }
97 
98 static void
99 testTask(RKH_SMA_T *me, void *arg)
100 {
101  (void)me;
102  (void)arg;
103 }
104 
105 static void
106 testPostFifo(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
107 {
108  (void)me;
109  (void)e;
110  (void)sender;
111 }
112 
113 static void
114 testPostLifo(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
115 {
116  (void)me;
117  (void)e;
118  (void)sender;
119 }
120 #endif
121 
122 /* ---------------------------- Global functions --------------------------- */
123 TEST_SETUP(polymorphism)
124 {
125 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
126  /* Restore the default virtual table of RKH_SMA_T class */
127  singleton->vptr = &rkhSmaVtbl;
128 #endif
129  memset(rkh_sptbl, 0, sizeof(rkh_sptbl));
130 }
131 
132 TEST_TEAR_DOWN(polymorphism)
133 {
134 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
135 #endif
136 }
137 
145 TEST(polymorphism, defaultVirtualFunctions)
146 {
147 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
148  checkVtbl(singleton,
150 
151  TEST_ASSERT_EQUAL_PTR(&rkhSmaVtbl, singleton->vptr);
152 #endif
153 }
154 
155 TEST(polymorphism, callVirtualFunction)
156 {
157 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
158  const RKH_EVT_T *qs[1];
159 
160  rkh_queue_init_Ignore(); /* for RKH_SMA_ACTIVATE() */
161  rkh_enter_critical_Expect();
162  rkh_trc_isoff__ExpectAndReturn(RKH_TE_SMA_REG, RKH_FALSE);
163  rkh_exit_critical_Expect();
164  rkh_sm_init_Ignore();
165  rkh_trc_isoff__ExpectAndReturn(RKH_TE_SMA_ACT, RKH_FALSE);
166 
167  rkh_enter_critical_Expect();
168  rkh_queue_put_fifo_Ignore();
169  rkh_trc_isoff__ExpectAndReturn(RKH_TE_SMA_FIFO, RKH_FALSE);
170  rkh_exit_critical_Expect();
171 
172  rkh_enter_critical_Expect();
173  rkh_queue_put_lifo_Ignore();
174  rkh_trc_isoff__ExpectAndReturn(RKH_TE_SMA_LIFO, RKH_FALSE);
175  rkh_exit_critical_Expect();
176 
177  RKH_SMA_ACTIVATE(singleton, qs, 1, 0, 0);
178  RKH_SMA_POST_FIFO(singleton, &event, NULL);
179  RKH_SMA_POST_LIFO(singleton, &event, NULL);
180 #endif
181 }
182 
183 TEST(polymorphism, setVirtualTable)
184 {
185 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
186  const RKHSmaVtbl *vptr;
187  static const RKHSmaVtbl vtbl =
188  {
189  testActivate, testTask, testPostFifo, testPostLifo
190  };
191 
192  vptr = singleton->vptr = &vtbl;
193 
194  checkVtbl(singleton,
195  testActivate, testTask, testPostFifo, testPostLifo);
196 #endif
197 }
198 
199 TEST(polymorphism, runtimeSingletonAOCtor)
200 {
201 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
202  rkh_sm_ctor_Expect(&singleton->sm);
203 
204  Singleton_ctor(8);
205  TEST_ASSERT_EQUAL(8, Singleton_getFoo());
206 
207  RKH_SMA_ACTIVATE(singleton, NULL, 0, NULL, 0);
208  TEST_ASSERT_EQUAL(0, Singleton_getFoo());
209 #endif
210 }
211 
212 TEST(polymorphism, runtimeMultipleAOCtorWithVtblForObj)
213 {
214 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
215  rkh_sm_ctor_Ignore();
216  rkh_sm_ctor_Ignore();
217 
218  Multiple_ctor(multA, 2, Multiple_postFifoA);
219  Multiple_ctor(multB, 4, Multiple_postFifoB);
220 
221  checkVtbl((RKH_SMA_T *)multA,
222  rkh_sma_activate, NULL, Multiple_postFifoA, rkh_sma_post_lifo);
223 
224  TEST_ASSERT_EQUAL(2, Multiple_getFoobar(multA));
225  TEST_ASSERT_EQUAL(4, Multiple_getFoobar(multB));
226 
227  RKH_SMA_POST_FIFO((RKH_SMA_T *)multA, NULL, NULL);
228  RKH_SMA_POST_FIFO((RKH_SMA_T *)multB, NULL, NULL);
229 
230  TEST_ASSERT_EQUAL(0, Multiple_getFoobar(multA));
231  TEST_ASSERT_EQUAL(8, Multiple_getFoobar(multB));
232 #endif
233 }
234 
235 TEST(polymorphism, runtimeMultipleAOCtorWithVtblForType)
236 {
237 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
238  Command_ctor(cmdSignal, 128);
239  Command_ctor(cmdRegister, 64);
240 
241  checkVtbl((RKH_SMA_T *)cmdSignal,
242  rkh_sma_activate, Command_task,
243  Command_postFifo, Command_postLifo);
244 
245  checkVtbl((RKH_SMA_T *)cmdRegister,
246  rkh_sma_activate, Command_task,
247  Command_postFifo, Command_postLifo);
248 #endif
249 }
250 
251 TEST(polymorphism, runtimeSubObjectCtorOfSMAAndSM)
252 {
253 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
254  CallControl_ctorA(16);
255 
256  TEST_ASSERT_EQUAL(16, CallControl_getFoo());
257  checkVtbl((RKH_SMA_T *)theCallControl,
258  CallControl_activate, CallControl_task,
260 #endif
261 }
262 
263 TEST(polymorphism, runtimeSubObjectCtorOfSMAAndSMWithDefaultVtbl)
264 {
265 #if RKH_CFG_SMA_VFUNCT_EN == RKH_ENABLED
266  CallControl_ctorB(8);
267 
268  TEST_ASSERT_EQUAL(8, CallControl_getFoo());
269  checkVtbl((RKH_SMA_T *)theCallControl,
270  rkh_sma_activate, NULL,
272 #endif
273 }
274 
279 /* ------------------------------ End of file ------------------------------ */
void rkh_sma_activate(RKH_SMA_T *me, const RKH_EVT_T **qSto, RKH_QUENE_T qSize, void *stkSto, rui32_t stkSize)
Initializes and activates a previously created state machine application (SMA) as known as active obj...
void(* RKHPostFifo)(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
Definition: rkhsma.h:842
Describes the SMA (active object in UML).
Definition: rkhsma.h:748
rui8_t RKH_QUENE_T
This data type defines the maximum number of elements that any queue can contain. ...
Definition: rkhqueue.h:95
Represents events without parameters.
Definition: rkhevt.h:167
RKH_SMA_T * rkh_sptbl[RKH_CFG_FWK_MAX_SMA]
Priority arranged table of registered SMA.
Interface of unit test with Trazer application.
#define RKH_FALSE
Standard define.
Definition: rkhdef.h:254
void(* RKHTask)(RKH_SMA_T *me, void *arg)
Definition: rkhsma.h:838
void rkh_sma_post_lifo(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
Send an event to a state machine application (SMA) as known as active object through a queue using th...
const RKHSmaVtbl rkhSmaVtbl
Virtual table for the RKH_SMA_T structure.
Definition: rkhsma.h:860
void(* RKHPostLifo)(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
Definition: rkhsma.h:850
void rkh_sma_post_fifo(RKH_SMA_T *me, const RKH_EVT_T *e, const void *const sender)
Send an event to a state machine application (SMA) as known as active object through a queue using th...
#define RKH_TE_SMA_REG
Registers a state machine application (SMA) as known as active object into the framework, which implies to store a pointer to the SMA in the priority table.
#define RKH_TE_SMA_LIFO
Send an event to a state machine application (SMA) as known as active object through a queue using th...
#define RKH_SMA_POST_LIFO(me_, e_, sender_)
Invoke the direct event posting facility rkh_sma_post_lifo(). If RKH_CFG_SMA_VFUNCT_EN is set RKH_ENA...
Definition: rkhsma.h:499
#define RKH_TE_SMA_FIFO
Send an event to a state machine application (SMA) as known as active object through a queue using th...
#define RKH_TE_SMA_ACT
Initializes and activates a previously created state machine application (SMA) as known as active obj...
#define RKH_SMA_POST_FIFO(me_, e_, sender_)
Invoke the direct event posting facility rkh_sma_post_fifo(). If RKH_CFG_SMA_VFUNCT_EN is set RKH_ENA...
Definition: rkhsma.h:451
void(* RKHActivate)(RKH_SMA_T *me, const RKH_EVT_T **qSto, RKH_QUENE_T qSize, void *stkSto, rui32_t stkSize)
Definition: rkhsma.h:833
Specifies the interface of the acive object (SMA state machine applications) manager.
#define RKH_SMA_ACTIVATE(me_, qSto_, qStoSize, stkSto_, stkSize_)
Invoke the active object activation function rkh_sma_activate(). If RKH_CFG_SMA_VFUNCT_EN is set RKH_...
Definition: rkhsma.h:248