PIC24 Support Libraries
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 #pragma once
40 
41 #include <stdint.h>
42 
43 // Circular includes: pic24_util needs this include, but
44 // this needs reportError. Pre-declare here.
45 void reportError(const char* message);
46 
47 /** Macro to convert a number to a string.
48  * Typical usage: <code>TOSTRING(__LINE__)</code>
49  */
50 #define TOSTRING(x) _TOSTRING(x)
51 // A helper macro used by \ref TOSTRING.
52 #define _TOSTRING(x) #x
53 ///@}
54 
55 /** A macro to add the file name and line at which the
56  * macro was called to the given message.
57  * \param msg Message to add file/line info to.
58  */
59 #define ERROR_FILE_LINE(msg) "Error in " __FILE__ " line " TOSTRING(__LINE__) ": " msg
60 
61 // Uncomment the following line to remove all the ASSERTs
62 // from the program:
63 //#define _NOASSERT
64 #ifdef _NOASSERT
65 # define ASSERT(placeholder) (void)0
66 # define _COMPILE_ASSERT_SYMBOL_INNER(line, msg)
67 # define _COMPILE_ASSERT_SYMBOL(line, msg)
68 # define COMPILE_ASSERT(test, msg)
69 #else
70 /** A useful compile-time assert macro.
71  * USAGE: COMPILE_ASSERT( condition, message_to_print_if_fails)
72  *
73  * Note: these macros use typedef so they can't be use in a
74  * value assignment type macro
75  * Note: message_to_print_if_fails is used as C language variable in this
76  * clever little macro, SO.... your message must adhere to C variable
77  * naming rules, i.e. no spaces or funny characters. Use underscores
78  * or CamelCase to separate words.
79  */
80 # define COMPILE_ASSERT(test, msg) \
81  typedef char _COMPILE_ASSERT_SYMBOL(__LINE__, msg) [ ((test) ? 1 : -1) ]
82 // A helper macro used by \ref COMPILE_ASSERT.
83 # define _COMPILE_ASSERT_SYMBOL_INNER(line, msg) __COMPILE_ASSERT_ ## line ## _____ ## msg
84 // A helper macro used by \ref COMPILE_ASSERT.
85 # define _COMPILE_ASSERT_SYMBOL(line, msg) _COMPILE_ASSERT_SYMBOL_INNER(line, msg)
86 
87 /** Assert that test is true. See \ref picAssert for
88  * details. NOTE: only test results; DO NOT include main-line
89  * code in an ASSERT, since it can be compiled out.
90  * For example, <code>ASSERT(myFunc())</code> is dangerous,
91  * since <code>myFunc()</code> will no longer be called if
92  * ASSERT is disabled by defining _NDEBUG.
93  */
94 # define ASSERT(test) picAssert(test, ERROR_FILE_LINE("ASSERT(" #test ") failed.\n"))
95 
96 /** A function to reset the chip and report a message
97  * if the test is false. Assumes \ref reportError
98  * is called at start-up to report any assertion
99  * violations.
100  *
101  * \param test Boolean results of test run. If false,
102  * reset and print message.
103  * \param message Message to print if assertion fails.
104  */
105 static inline void picAssert(uint8_t test, const char* message) {
106  if (!test)
107  reportError(message);
108 }
109 #endif
static void picAssert(uint8_t test, const char *message)
void reportError(const char *message)
Definition: pic24_util.c:183
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194