uCUnit Logo
uCUnit-v1.0.h
1 /*****************************************************************************
2  * *
3  * uCUnit - A unit testing framework for microcontrollers *
4  * *
5  * (C) 2007 - 2008 Sven Stefan Krauss *
6  * https://www.ucunit.org *
7  * *
8  * File : uCUnit-v1.0.h *
9  * Description : Macros for Unit-Testing *
10  * Author : Sven Stefan Krauss *
11  * Contact : www.ucunit.org *
12  * *
13  *****************************************************************************/
14 
15 /*
16  * This file is part of ucUnit.
17  *
18  * You can redistribute and/or modify it under the terms of the
19  * Common Public License as published by IBM Corporation; either
20  * version 1.0 of the License, or (at your option) any later version.
21  *
22  * uCUnit is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * Common Public License for more details.
26  *
27  * You should have received a copy of the Common Public License
28  * along with uCUnit.
29  *
30  * It may also be available at the following URL:
31  * http://www.opensource.org/licenses/cpl1.0.txt
32  *
33  * If you cannot obtain a copy of the License, please contact the
34  * author.
35  */
36 
37 #ifndef UCUNIT_0100_H_
38 #define UCUNIT_0100_H_
39 
40 /*****************************************************************************/
41 /****** Customizing area ******/
42 /*****************************************************************************/
43 
60 #define UCUNIT_WriteString(msg) System_WriteString(msg)
61 
78 #define UCUNIT_WriteInt(n) System_WriteInt(n)
79 
99 #define UCUNIT_Safestate() System_Safestate()
100 
118 #define UCUNIT_Recover() System_Reset()
119 
132 #define UCUNIT_Init() System_Init()
133 
144 #define UCUNIT_Shutdown() System_Shutdown()
145 
152 //#define UCUNIT_MODE_NORMAL
153 #define UCUNIT_MODE_VERBOSE
154 
159 #define UCUNIT_MAX_TRACEPOINTS 16
160 
161 /*****************************************************************************/
162 /* **** End of customizing area ***** */
163 /*****************************************************************************/
164 
165 /*****************************************************************************/
166 /* Some useful constants */
167 /*****************************************************************************/
168 
169 #define UCUNIT_VERSION "v1.0" /* Version info */
170 
171 #ifndef NULL
172 #define NULL (void *)0
173 #endif
174 
175 #ifndef TRUE
176 #define TRUE 1
177 #endif
178 
179 #ifndef FALSE
180 #define FALSE 0
181 #endif
182 
183 /* Action to take if check fails */
184 #define UCUNIT_ACTION_WARNING 0 /* Goes through the checks
185  with message depending on level */
186 #define UCUNIT_ACTION_SHUTDOWN 1 /* Stops on the end of the checklist
187  if any check has failed */
188 #define UCUNIT_ACTION_SAFESTATE 2 /* Goes in safe state if check fails */
189 
190 /*****************************************************************************/
191 /* Variables */
192 /*****************************************************************************/
193 
194 /* Variables for simple statistics */
195 static int ucunit_checks_failed = 0; /* Numer of failed checks */
196 static int ucunit_checks_passed = 0; /* Number of passed checks */
197 
198 static int ucunit_testcases_failed = 0; /* Number of failed test cases */
199 static int ucunit_testcases_passed = 0; /* Number of passed test cases */
200 static int ucunit_testcases_failed_checks = 0; /* Number of failed checks in a testcase */
201 static int ucunit_checklist_failed_checks = 0; /* Number of failed checks in a checklist */
202 static int ucunit_action = UCUNIT_ACTION_WARNING; /* Action to take if a check fails */
203 static int ucunit_checkpoints[UCUNIT_MAX_TRACEPOINTS]; /* Max. number of tracepoints */
204 static int ucunit_index = 0; /* Tracepoint index */
205 
206 /*****************************************************************************/
207 /* Internal (private) Macros */
208 /*****************************************************************************/
209 
221 #define UCUNIT_DefineToStringHelper(x) #x
222 
233 #define UCUNIT_DefineToString(x) UCUNIT_DefineToStringHelper(x)
234 
235 #ifdef UCUNIT_MODE_VERBOSE
236 
250 #define UCUNIT_WritePassedMsg(msg, args) \
251  do \
252  { \
253  UCUNIT_WriteString(__FILE__); \
254  UCUNIT_WriteString(":"); \
255  UCUNIT_WriteString(UCUNIT_DefineToString(__LINE__)); \
256  UCUNIT_WriteString(": passed:"); \
257  UCUNIT_WriteString(msg); \
258  UCUNIT_WriteString("("); \
259  UCUNIT_WriteString(args); \
260  UCUNIT_WriteString(")\n"); \
261  } while(0)
262 #else
263 #define UCUNIT_WritePassedMsg(msg, args)
264 #endif
265 
266 #ifdef UCUNIT_MODE_SILENT
267 #define UCUNIT_WriteFailedMsg(msg, args)
268 #else
269 
283 #define UCUNIT_WriteFailedMsg(msg, args) \
284  do \
285  { \
286  UCUNIT_WriteString(__FILE__); \
287  UCUNIT_WriteString(":"); \
288  UCUNIT_WriteString(UCUNIT_DefineToString(__LINE__)); \
289  UCUNIT_WriteString(": failed:"); \
290  UCUNIT_WriteString(msg); \
291  UCUNIT_WriteString("("); \
292  UCUNIT_WriteString(args); \
293  UCUNIT_WriteString(")\n"); \
294  } while(0)
295 #endif
296 
311 #define UCUNIT_FailCheck(msg, args) \
312  do \
313  { \
314  if (UCUNIT_ACTION_SAFESTATE==ucunit_action) \
315  { \
316  UCUNIT_Safestate(); \
317  } \
318  UCUNIT_WriteFailedMsg(msg, args); \
319  ucunit_checks_failed++; \
320  ucunit_checklist_failed_checks++; \
321  } while(0)
322 
337 #define UCUNIT_PassCheck(message, args) \
338  do \
339  { \
340  UCUNIT_WritePassedMsg(message, args); \
341  ucunit_checks_passed++; \
342  } while(0)
343 
344 /*****************************************************************************/
345 /* Checklist Macros */
346 /*****************************************************************************/
347 
365 #define UCUNIT_ChecklistBegin(action) \
366  do \
367  { \
368  ucunit_action = action; \
369  ucunit_checklist_failed_checks = 0; \
370  } while (0)
371 
381 #define UCUNIT_ChecklistEnd() \
382  if (ucunit_checklist_failed_checks!=0) \
383  { \
384  UCUNIT_WriteFailedMsg("Checklist",""); \
385  if (UCUNIT_ACTION_SHUTDOWN==ucunit_action) \
386  { \
387  UCUNIT_Shutdown(); \
388  } \
389  } \
390  else \
391  { \
392  UCUNIT_WritePassedMsg("Checklist",""); \
393  }
394 
395 /*****************************************************************************/
396 /* Check Macros */
397 /*****************************************************************************/
398 
410 #define UCUNIT_Check(condition, msg, args) \
411  if ( (condition) ) { UCUNIT_PassCheck(msg, args); } else { UCUNIT_FailCheck(msg, args); }
412 
424 #define UCUNIT_CheckIsEqual(expected,actual) \
425  UCUNIT_Check( (expected) == (actual), "IsEqual", #expected "," #actual )
426 
437 #define UCUNIT_CheckIsNull(pointer) \
438  UCUNIT_Check( (pointer) == NULL, "IsNull", #pointer)
439 
450 #define UCUNIT_CheckIsNotNull(pointer) \
451  UCUNIT_Check( (pointer) != NULL, "IsNotNull", #pointer)
452 
466 #define UCUNIT_CheckIsInRange(value, lower, upper) \
467  UCUNIT_Check( ( (value>=lower) && (value<=upper) ), "IsInRange", #value "," #lower "," #upper)
468 
479 #define UCUNIT_CheckIs8Bit(value) \
480  UCUNIT_Check( value==(value & 0xFF), "Is8Bit", #value )
481 
492 #define UCUNIT_CheckIs16Bit(value) \
493  UCUNIT_Check( value==(value & 0xFFFF), "Is16Bit", #value )
494 
505 #define UCUNIT_CheckIs32Bit(value) \
506  UCUNIT_Check( value==(value & 0xFFFFFFFF), "Is32Bit", #value )
507 
522 #define UCUNIT_CheckIsBitSet(value, bitno) \
523  UCUNIT_Check( (1==(((value)>>(bitno)) & 0x01) ), "IsBitSet", #value "," #bitno)
524 
536 #define UCUNIT_CheckIsBitClear(value, bitno) \
537  UCUNIT_Check( (0==(((value)>>(bitno)) & 0x01) ), "IsBitClear", #value "," #bitno)
538 
539 /*****************************************************************************/
540 /* Testcases */
541 /*****************************************************************************/
542 
554 #define UCUNIT_TestcaseBegin(name) \
555  do \
556  { \
557  UCUNIT_WriteString("\n======================================\n"); \
558  UCUNIT_WriteString(name); \
559  UCUNIT_WriteString("\n======================================\n"); \
560  ucunit_testcases_failed_checks = ucunit_checks_failed; \
561  } \
562  while(0)
563 
573 #define UCUNIT_TestcaseEnd() \
574  do \
575  { \
576  UCUNIT_WriteString("======================================\n"); \
577  if( 0==(ucunit_testcases_failed_checks - ucunit_checks_failed) ) \
578  { \
579  UCUNIT_WriteString("Testcase passed.\n"); \
580  ucunit_testcases_passed++; \
581  } \
582  else \
583  { \
584  UCUNIT_WriteFailedMsg("EndTestcase",""); \
585  ucunit_testcases_failed++; \
586  } \
587  UCUNIT_WriteString("======================================\n"); \
588  } \
589  while(0)
590 
591 /*****************************************************************************/
592 /* Support for code coverage */
593 /*****************************************************************************/
594 
609 #define UCUNIT_Tracepoint(index) \
610  if(index<UCUNIT_MAX_TRACEPOINTS) \
611  { \
612  ucunit_checkpoints[index] = __LINE__; \
613  } \
614  else \
615  { \
616  UCUNIT_WriteFailedMsg("Tracepoint index", #index); \
617  }
618 
629 #define UCUNIT_ResetTracepointCoverage() \
630  for (ucunit_index=0; ucunit_index<UCUNIT_MAX_TRACEPOINTS; ucunit_index++) \
631  { \
632  ucunit_checkpoints[ucunit_index]=0; \
633  }
634 
645 #define UCUNIT_CheckTracepointCoverage(index) \
646  UCUNIT_Check( (ucunit_checkpoints[index]!=0), "TracepointCoverage", #index);
647 
648 /*****************************************************************************/
649 /* Testsuite Summary */
650 /*****************************************************************************/
651 
661 #define UCUNIT_WriteSummary() \
662 { \
663  UCUNIT_WriteString("\n**************************************"); \
664  UCUNIT_WriteString("\nTestcases: failed: "); \
665  UCUNIT_WriteInt(ucunit_testcases_failed); \
666  UCUNIT_WriteString("\n passed: "); \
667  UCUNIT_WriteInt(ucunit_testcases_passed); \
668  UCUNIT_WriteString("\nChecks: failed: "); \
669  UCUNIT_WriteInt(ucunit_checks_failed); \
670  UCUNIT_WriteString("\n passed: "); \
671  UCUNIT_WriteInt(ucunit_checks_passed); \
672  UCUNIT_WriteString("\n**************************************\n"); \
673 }
674 
675 #endif /*UCUNIT_H_*/


Impressum  |  Datenschutz  |  Terms of use  |  Copyright (C) 2008-2012 Sven Stefan Krauß. Last site update: 10-Sept-2012