RKH
test_rkhtrc_stream.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 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.20.04 LeFr v2.4.05 ---
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 "rkhtrc_stream.h"
61 #include "Mock_rkhsm.h"
62 #include "Mock_rkhsma.h"
63 
64 /* ----------------------------- Local macros ------------------------------ */
65 /* ------------------------------- Constants ------------------------------- */
66 /* ---------------------------- Local data types --------------------------- */
67 /* ---------------------------- Global variables --------------------------- */
68 /* ---------------------------- Local variables ---------------------------- */
69 static RKHROM RKH_ROM_T base = {0, 0, "receiver"};
70 static RKH_SMA_T receiver;
71 static RKH_SMA_T sender;
72 static RKH_EVT_T event;
73 static RKH_ST_T state = {{RKH_BASIC, "state"}};
74 static RKH_ST_T pseudoState = {{RKH_CHOICE, "pseudoState"}};
75 static rui8_t block[RKH_CFG_TRC_SIZEOF_STREAM + 2];
76 
77 /* ----------------------- Local function prototypes ----------------------- */
78 /* ---------------------------- Local functions ---------------------------- */
79 /* ---------------------------- Global functions --------------------------- */
80 void
81 setUp(void)
82 {
84 }
85 
86 void
87 tearDown(void)
88 {
89 }
90 
97 void
98 test_FlagAfterInit(void)
99 {
100  rui8_t *output;
101 
102  output = rkh_trc_get();
103  TEST_ASSERT_NOT_NULL(output);
104  TEST_ASSERT_EQUAL(RKH_FLG, *output);
105 }
106 
107 void
108 test_NotFullAfterInit(void)
109 {
110  TRCQTY_T nData;
111 
112  rkh_trc_get_block(&nData);
113  TEST_ASSERT_TRUE(RKH_CFG_TRC_SIZEOF_STREAM != nData);
114 }
115 
116 void
117 test_NotEmptyThenEmpty(void)
118 {
119  TRCQTY_T nData;
120 
121  TEST_ASSERT_NOT_NULL(rkh_trc_get());
122  rkh_trc_get_block(&nData);
123  TEST_ASSERT_EQUAL(0, nData);
124 }
125 
126 void
127 test_GetPutOneValue(void)
128 {
129  rui8_t expected;
130  rui8_t *output;
131 
132  rkh_trc_get(); /* removes the first inserted value RKH_FLG */
133  expected = 128;
134 
135  rkh_trc_put(expected);
136 
137  output = rkh_trc_get();
138  TEST_ASSERT_NOT_NULL(output);
139  TEST_ASSERT_EQUAL(expected, *output);
140 }
141 
142 void
143 test_GetPutAFew(void)
144 {
145  rui8_t *output;
146 
147  rkh_trc_put(1);
148  rkh_trc_put(2);
149  rkh_trc_put(3);
150 
151  output = rkh_trc_get();
152  TEST_ASSERT_NOT_NULL(output);
153  TEST_ASSERT_EQUAL(RKH_FLG, *output);
154  output = rkh_trc_get();
155  TEST_ASSERT_NOT_NULL(output);
156  TEST_ASSERT_EQUAL(1, *output);
157  output = rkh_trc_get();
158  TEST_ASSERT_NOT_NULL(output);
159  TEST_ASSERT_EQUAL(2, *output);
160  output = rkh_trc_get();
161  TEST_ASSERT_NOT_NULL(output);
162  TEST_ASSERT_EQUAL(3, *output);
163 }
164 
165 void
166 test_IsFull(void)
167 {
168  int i;
169  TRCQTY_T nData = RKH_CFG_TRC_SIZEOF_STREAM + 1;
170 
171  for (i = 0; i < (RKH_CFG_TRC_SIZEOF_STREAM - 1); i++)
172  rkh_trc_put((rui8_t)i);
173 
174  rkh_trc_get_block(&nData);
175  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM, nData);
176 }
177 
178 void
179 test_EmptyToFullToEmpty(void)
180 {
181  int i;
182  TRCQTY_T nData = RKH_CFG_TRC_SIZEOF_STREAM + 1;
183 
184  for (i = 0; i < (RKH_CFG_TRC_SIZEOF_STREAM - 1); i++)
185  rkh_trc_put((rui8_t)i);
186 
187  rkh_trc_get_block(&nData);
188  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM, nData);
189 
190  for (i = 0; i < RKH_CFG_TRC_SIZEOF_STREAM; i++)
191  rkh_trc_get();
192 
193  rkh_trc_get_block(&nData);
194  TEST_ASSERT_EQUAL(0, nData);
195 }
196 
197 void
198 test_WrapAround(void)
199 {
200  int i;
201  rui8_t *output;
202 
203  for (i = 0; i < (RKH_CFG_TRC_SIZEOF_STREAM - 1); i++)
204  rkh_trc_put((rui8_t)i);
205 
206  rkh_trc_get();
207  rkh_trc_put(128);
208 
209  for (i = 0; i < RKH_CFG_TRC_SIZEOF_STREAM; i++)
210  {
211  output = rkh_trc_get();
212  TEST_ASSERT_NOT_NULL(output);
213  }
214 
215  TEST_ASSERT_EQUAL(128, *output);
216 }
217 
218 void
219 test_GetFromEmptyReturnsNull(void)
220 {
221  rkh_trc_get();
222  TEST_ASSERT_NULL(rkh_trc_get());
223 }
224 
225 void
226 test_GetContinuousBlock(void)
227 {
228  int i;
229  TRCQTY_T nData;
230  rui8_t *output;
231  int qty = 16;
232 
233  rkh_trc_get();
234  for (i = 0; i < qty; i++)
235  rkh_trc_put((rui8_t)i);
236 
237  nData = qty;
238  output = rkh_trc_get_block(&nData);
239  TEST_ASSERT_EQUAL(qty, nData);
240 
241  for (i = 0; i < nData; i++, output++)
242  TEST_ASSERT_EQUAL((rui8_t)i, *output);
243 }
244 
245 void
246 test_GetManyFromEmptyUsingWholeBlock(void)
247 {
248  TRCQTY_T nGetElem;
249 
250  rkh_trc_get();
251  nGetElem = rkh_trc_getWholeBlock(block, 4);
252  TEST_ASSERT_EQUAL(0, nGetElem);
253 }
254 
255 void
256 test_GetManyElemsLessThanStoredUsingWholeBlock(void)
257 {
258  TRCQTY_T nGetElem;
259 
260  memset(block, 0xaa, 8);
261  rkh_trc_put(1);
262  rkh_trc_put(2);
263  rkh_trc_put(3);
264 
265  nGetElem = rkh_trc_getWholeBlock(block, 3);
266  TEST_ASSERT_EQUAL(3, nGetElem);
267  TEST_ASSERT_EQUAL(RKH_FLG, block[0]);
268  TEST_ASSERT_EQUAL(1, block[1]);
269  TEST_ASSERT_EQUAL(2, block[2]);
270  TEST_ASSERT_EQUAL(0xaa, block[3]);
271 }
272 
273 void
274 test_GetManyElemsEqualThanStoredWholeBlock(void)
275 {
276  TRCQTY_T nGetElem;
277 
278  memset(block, 0xaa, 8);
279  rkh_trc_put(1);
280  rkh_trc_put(2);
281  rkh_trc_put(3);
282 
283  nGetElem = rkh_trc_getWholeBlock(block, 4);
284  TEST_ASSERT_EQUAL(4, nGetElem);
285  TEST_ASSERT_EQUAL(RKH_FLG, block[0]);
286  TEST_ASSERT_EQUAL(1, block[1]);
287  TEST_ASSERT_EQUAL(2, block[2]);
288  TEST_ASSERT_EQUAL(3, block[3]);
289  TEST_ASSERT_EQUAL(0xaa, block[4]);
290 }
291 
292 void
293 test_GetManyElemsMoreThanStoredUsingWholeBlock(void)
294 {
295  TRCQTY_T nGetElem;
296 
297  memset(block, 0xaa, 8);
298  rkh_trc_put(1);
299  rkh_trc_put(2);
300  rkh_trc_put(3);
301 
302  nGetElem = rkh_trc_getWholeBlock(block, 5);
303  TEST_ASSERT_EQUAL(4, nGetElem);
304  TEST_ASSERT_EQUAL(RKH_FLG, block[0]);
305  TEST_ASSERT_EQUAL(1, block[1]);
306  TEST_ASSERT_EQUAL(2, block[2]);
307  TEST_ASSERT_EQUAL(3, block[3]);
308  TEST_ASSERT_EQUAL(0xaa, block[4]);
309 }
310 
311 void
312 test_GetManyElemsEqualThanStoredWrapAroundUsingWholeBlock(void)
313 {
314  TRCQTY_T nGetElem;
315  rui8_t i;
316 
317  memset(block, 0xaa, 8);
318  rkh_trc_get();
319  for (i = 0; i < ((2 * RKH_CFG_TRC_SIZEOF_STREAM) - 2); ++i)
320  {
321  rkh_trc_put(i);
322  }
323 
324  nGetElem = rkh_trc_getWholeBlock(&block[1], 4);
325 
326  TEST_ASSERT_EQUAL(4, nGetElem);
327  TEST_ASSERT_EQUAL(0xaa, block[0]);
328  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 2, block[1]);
329  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 1, block[2]);
330  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM, block[3]);
331  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM + 1, block[4]);
332  TEST_ASSERT_EQUAL(0xaa, block[5]);
333 }
334 
335 void
336 test_GetManyElemsLessThanStoredWrapAroundUsingWholeBlock(void)
337 {
338  TRCQTY_T nGetElem;
339  rui8_t i;
340 
341  memset(block, 0xaa, 8);
342  rkh_trc_get();
343  for (i = 0; i < ((2 * RKH_CFG_TRC_SIZEOF_STREAM) - 2); ++i)
344  {
345  rkh_trc_put(i);
346  }
347 
348  nGetElem = rkh_trc_getWholeBlock(&block[1], 3);
349 
350  TEST_ASSERT_EQUAL(3, nGetElem);
351  TEST_ASSERT_EQUAL(0xaa, block[0]);
352  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 2, block[1]);
353  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 1, block[2]);
354  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM, block[3]);
355  TEST_ASSERT_EQUAL(0xaa, block[4]);
356 }
357 
358 void
359 test_GetManyElemsMoreThanStoredWrapAroundUsingWholeBlock(void)
360 {
361  TRCQTY_T nGetElem;
362  rui8_t i;
363 
364  memset(block, 0xaa, RKH_CFG_TRC_SIZEOF_STREAM + 2);
365  rkh_trc_get();
366  for (i = 0; i < ((2 * RKH_CFG_TRC_SIZEOF_STREAM) - 2); ++i)
367  {
368  rkh_trc_put(i);
369  }
370 
371  nGetElem = rkh_trc_getWholeBlock(&block[1], RKH_CFG_TRC_SIZEOF_STREAM + 1);
372 
373  TEST_ASSERT_EQUAL(0xaa, block[0]);
374  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM, nGetElem);
375  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 2, block[1]);
376  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM - 1, block[2]);
377  for (i = 0; i < (RKH_CFG_TRC_SIZEOF_STREAM - 2); ++i)
378  {
379  TEST_ASSERT_EQUAL(RKH_CFG_TRC_SIZEOF_STREAM + i, block[i + 3]);
380  }
381  TEST_ASSERT_EQUAL(0xaa, block[RKH_CFG_TRC_SIZEOF_STREAM + 1]);
382 }
383 
389 /* ------------------------------ End of file ------------------------------ */
#define RKH_CFG_TRC_SIZEOF_STREAM
Specify the maximum number of trace events in the stream. The smaller this number,...
Definition: rkhcfg.h:1016
#define RKH_FLG
Specifies the trace stream manager (circular buffer).
TRCQTY_T rkh_trc_getWholeBlock(rui8_t *destBlock, TRCQTY_T nElem)
Copies the last nElem bytes of the stream to destBlock. Frequently, this function is used by the ca...
void rkh_trc_put(rui8_t b)
Put a data byte into the trace stream.
rui8_t * rkh_trc_get(void)
Retrieves a pointer to oldest stored byte in the trace stream. Frequently, this function is used by t...
void rkh_trcStream_init(void)
Initializes the RKH's trace stream.
rui8_t * rkh_trc_get_block(TRCQTY_T *nget)
Retrieves a pointer to a contiguous block of data from the trace stream.
Represents events without parameters.
Definition: rkhevt.h:170
Constant parameters of state machine.
Definition: rkhsm.h:1829
Describes the SMA (active object in UML).
Definition: rkhsma.h:772
Describes the common properties of regular states (basic, composite, and submachine).
Definition: rkhsm.h:2065