PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pic24_unittest.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 
31 // Documentation for this file. If the \file tag isn't present,
32 // this file won't be documented.
33 /**
34  * \file
35  * Extremely simple unit testing framework, inspired by
36  * http://www.jera.com/techinfo/jtns/jtn002.html.
37  */
38 
39 #ifndef _PIC24_UNITTEST_H_
40 #define _PIC24_UNITTEST_H_
41 
42 #include <stdint.h>
43 
44 // Circular includes: pic24_util needs this include, but
45 // this needs reportError. Pre-declare here.
46 void reportError(const char* message);
47 
48 /** Macro to convert a number to a string.
49  * Typical usage: <code>TOSTRING(__LINE__)</code>
50  */
51 #define TOSTRING(x) _TOSTRING(x)
52 // A helper macro used by \ref TOSTRING.
53 #define _TOSTRING(x) #x
54 ///@}
55 
56 /** A macro to add the file name and line at which the
57  * macro was called to the given message.
58  * \param msg Message to add file/line info to.
59  */
60 #define ERROR_FILE_LINE(msg) "Error in " __FILE__ " line " TOSTRING(__LINE__) ": " msg
61 
62 // Uncomment the following line to remove all the ASSERTs
63 // from the program:
64 //#define _NOASSERT
65 #ifdef _NOASSERT
66 #define ASSERT(placeholder) (void)0
67 #define _COMPILE_ASSERT_SYMBOL_INNER(line, msg)
68 #define _COMPILE_ASSERT_SYMBOL(line, msg)
69 #define COMPILE_ASSERT(test, msg)
70 #else
71 /** A useful compile-time assert macro.
72  * USAGE: COMPILE_ASSERT( condition, message_to_print_if_fails)
73  *
74  * Note: these macros use typedef so they can't be use in a
75  * value assignment type macro
76  * Note: message_to_print_if_fails is used as C language variable in this
77  * clever little macro, SO.... your message must adhere to C variable
78  * naming rules, i.e. no spaces or funny characters. Use underscores
79  * or CamelCase to separate words.
80  */
81 #define COMPILE_ASSERT(test, msg) \
82  typedef char _COMPILE_ASSERT_SYMBOL(__LINE__, msg) [ ((test) ? 1 : -1) ]
83 // A helper macro used by \ref COMPILE_ASSERT.
84 #define _COMPILE_ASSERT_SYMBOL_INNER(line, msg) __COMPILE_ASSERT_ ## line ## _____ ## msg
85 // A helper macro used by \ref COMPILE_ASSERT.
86 #define _COMPILE_ASSERT_SYMBOL(line, msg) _COMPILE_ASSERT_SYMBOL_INNER(line, msg)
87 
88 /** Assert that test is true. See \ref picAssert for
89  * details. NOTE: only test results; DO NOT include main-line
90  * code in an ASSERT, since it can be compiled out.
91  * For example, <code>ASSERT(myFunc())</code> is dangerous,
92  * since <code>myFunc()</code> will no longer be called if
93  * ASSERT is disabled by defining _NDEBUG.
94  */
95 #define ASSERT(test) picAssert(test, ERROR_FILE_LINE("ASSERT(" #test ") failed.\n"))
96 
97 /** A function to reset the chip and report a message
98  * if the test is false. Assumes \ref reportError
99  * is called at start-up to report any assertion
100  * violations.
101  *
102  * \param test Boolean results of test run. If false,
103  * reset and print message.
104  * \param message Message to print if assertion fails.
105  */
106 static inline void picAssert(uint8_t test, const char* message) {
107  if (!test)
108  reportError(message);
109 }
110 #endif
111 
112 #endif // #ifndef _PIC24_UNITTEST_H_