RKH
test_rkhsma_prio.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  * 2017.05.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 "rkhsma_prio.h"
58 #include "Mock_rkhfwk_bittbl.h"
59 #include "Mock_rkhassert.h"
60 
61 /* ----------------------------- Local macros ------------------------------ */
62 /* ------------------------------- Constants ------------------------------- */
63 static const rui8_t bitMaskTbl[] =
64 {
65  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
66 };
67 
68 static const rui8_t leastBitSetTbl[] =
69 {
70  0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */
71  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */
72  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */
73  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */
74  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */
75  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */
76  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */
77  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */
78  7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */
79  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */
80  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */
81  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */
82  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */
83  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */
84  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */
85  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */
86 };
87 
88 /* ---------------------------- Local data types --------------------------- */
89 /* ---------------------------- Global variables --------------------------- */
90 TEST_GROUP(prio);
91 
92 /* ---------------------------- Local variables ---------------------------- */
93 /* ----------------------- Local function prototypes ----------------------- */
94 /* ---------------------------- Local functions ---------------------------- */
95 static void
96 MockAssertCallback(const char* const file, int line, int cmock_num_calls)
97 {
98  TEST_PASS();
99 }
100 
101 /* ---------------------------- Global functions --------------------------- */
102 TEST_SETUP(prio)
103 {
105 }
106 
107 TEST_TEAR_DOWN(prio)
108 {
109 }
110 
117 TEST(prio, ClearAfterInit)
118 {
119  rbool_t result;
120 
121  result = rkh_smaPrio_isReady();
122  TEST_ASSERT_EQUAL(0, result);
123 }
124 
125 TEST(prio, SetOneActiveObjectReadyToRun)
126 {
127  rbool_t result;
128  rui8_t prio = 1, resultPrio, column, row;
129 
130  column = prio >> 3;
131  row = prio & 7;
132  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
133  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
134 
135  rkh_smaPrio_setReady(prio);
136  result = rkh_smaPrio_isReady();
137  TEST_ASSERT_EQUAL(1, result);
138 
139  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(1, leastBitSetTbl[1]);
140  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(2, leastBitSetTbl[2]);
141  resultPrio = rkh_smaPrio_findHighest();
142  TEST_ASSERT_EQUAL(prio, resultPrio);
143 }
144 
145 TEST(prio, SetMultipleActiveObjectsReadyToRun)
146 {
147  rui8_t prioA = 1, prioC = 0, prioB = 15, resultPrio, column, row;
148 
149  column = prioA >> 3;
150  row = prioA & 7;
151  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
152  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
153  rkh_smaPrio_setReady(prioA);
154 
155  column = prioB >> 3;
156  row = prioB & 7;
157  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
158  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
159  rkh_smaPrio_setReady(prioB);
160 
161  column = prioC >> 3;
162  row = prioC & 7;
163  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
164  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
165  rkh_smaPrio_setReady(prioC);
166 
167  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(3, leastBitSetTbl[3]);
168  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(3, leastBitSetTbl[3]);
169  resultPrio = rkh_smaPrio_findHighest();
170  TEST_ASSERT_EQUAL(prioC, resultPrio);
171 }
172 
173 TEST(prio, SetOneActiveObjectUnready)
174 {
175  rbool_t result;
176  rui8_t prio = 1, resultPrio, column, row;
177 
178  column = prio >> 3;
179  row = prio & 7;
180  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
181  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
182 
183  rkh_smaPrio_setReady(prio);
184 
185  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(1, leastBitSetTbl[1]);
186  rkh_bittbl_getLeastBitSetPos_ExpectAndReturn(2, leastBitSetTbl[2]);
187  resultPrio = rkh_smaPrio_findHighest();
188  TEST_ASSERT_EQUAL(prio, resultPrio);
189 
190  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
191  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
193  result = rkh_smaPrio_isReady();
194  TEST_ASSERT_EQUAL(0, result);
195 }
196 
197 TEST(prio, SetMultipleActiveObjectsUnready)
198 {
199  rbool_t result;
200  rui8_t prioA = 1, prioC = 0, prioB = 15, column, row;
201 
202  column = prioA >> 3;
203  row = prioA & 7;
204  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
205  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
206  rkh_smaPrio_setReady(prioA);
207 
208  column = prioB >> 3;
209  row = prioB & 7;
210  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
211  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
212  rkh_smaPrio_setReady(prioB);
213 
214  column = prioC >> 3;
215  row = prioC & 7;
216  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
217  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
218  rkh_smaPrio_setReady(prioC);
219 
220  column = prioA >> 3;
221  row = prioA & 7;
222  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
223  rkh_smaPrio_setUnready(prioA);
224 
225  column = prioB >> 3;
226  row = prioB & 7;
227  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
228  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
229  rkh_smaPrio_setUnready(prioB);
230 
231  column = prioC >> 3;
232  row = prioC & 7;
233  rkh_bittbl_getBitMask_ExpectAndReturn(row, bitMaskTbl[row]);
234  rkh_bittbl_getBitMask_ExpectAndReturn(column, bitMaskTbl[column]);
235  rkh_smaPrio_setUnready(prioC);
236 
237  result = rkh_smaPrio_isReady();
238  TEST_ASSERT_EQUAL(0, result);
239 }
240 
241 TEST(prio, Fails_InvalidActiveObjectOnSet)
242 {
243  rui8_t prio = RKH_CFG_FWK_MAX_SMA;
244 
245  rkh_assert_Expect("rkhsma_prio", 0);
246  rkh_assert_IgnoreArg_line();
247  rkh_assert_StubWithCallback(MockAssertCallback);
248 
249  rkh_smaPrio_setReady(prio);
250 }
251 
256 /* ------------------------------ End of file ------------------------------ */
void rkh_smaPrio_setUnready(rui8_t prio)
Removing an active object from the ready list.
rui8_t rkh_smaPrio_findHighest(void)
Finding the highest priority active object ready to run.
void rkh_smaPrio_init(void)
Initializes the native priority mechanism for active object scheduling.
Specifies the native priority mechanism for active object scheduling.
#define RKH_CFG_FWK_MAX_SMA
Specify the maximum number of state machine applications (SMA) to be used by the application (can be ...
Definition: rkhcfg.h:86
rbool_t rkh_smaPrio_isReady(void)
Evaluates to true if any active object is ready to run.
void rkh_smaPrio_setReady(rui8_t prio)
Making an active object ready-to-run inserting it into the ready list.