test

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers assertion.h Source File

assertion.h

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     assertion.h
00004     @author   hathach (tinyusb.org)
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2013, K. Townsend (microBuilder.eu)
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 /** \file
00038  *  \brief TBD
00039  *
00040  *  \note TBD
00041  */
00042 
00043 /** \ingroup TBD
00044  *  \defgroup TBD
00045  *  \brief TBD
00046  *
00047  *  @{
00048  */
00049 
00050 #ifndef _ASSERTION_H_
00051 #define _ASSERTION_H_
00052 
00053 #include "projectconfig.h"
00054 
00055 #ifdef __cplusplus
00056 extern "C"
00057 {
00058 #endif
00059 
00060 static inline void debugger_breakpoint(void) ATTR_ALWAYS_INLINE;
00061 static inline void debugger_breakpoint(void)
00062 {
00063 #ifndef _TEST_
00064   __asm("BKPT #0\n");
00065 #endif
00066 }
00067 
00068 //--------------------------------------------------------------------+
00069 // Compile-time Assert
00070 //--------------------------------------------------------------------+
00071 #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
00072   #define _ASSERT_COUNTER __COUNTER__
00073 #else
00074   #define _ASSERT_COUNTER __LINE__
00075 #endif
00076 
00077 #define ASSERT_STATIC(const_expr, message) enum { XSTRING_CONCAT_(static_assert_, _ASSERT_COUNTER) = 1/(!!(const_expr)) }
00078 
00079 //--------------------------------------------------------------------+
00080 // Assert Helper
00081 //--------------------------------------------------------------------+
00082 //#ifndef _TEST_
00083 //  #define ASSERT_MESSAGE(format, ...) _PRINTF("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
00084 //#else
00085 //  #define ASSERT_MESSAGE(format, ...) _PRINTF("%d:note: Assert " format "\n", __LINE__, __VA_ARGS__)
00086 //#endif
00087 
00088 #if CFG_DEBUG == 3
00089   #define ASSERT_MESSAGE(format, ...) debugger_breakpoint()
00090 #elif CFG_DEBUG == 2
00091   #define ASSERT_MESSAGE(format, ...) printf("Assert at %s: %s: %d: " format "\n", __BASE_FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__)
00092 #else
00093   #define ASSERT_MESSAGE(format, ...)
00094 #endif
00095 
00096 #define ASSERT_ERROR_HANDLER(x, para)  \
00097     return (x)
00098 
00099 #define ASSERT_DEFINE_WITH_HANDLER(error_handler, handler_para, setup_statement, condition, error, format, ...) \
00100   do{\
00101     setup_statement;\
00102       if (!(condition)) {\
00103         ASSERT_MESSAGE(format, __VA_ARGS__);\
00104         error_handler(error, handler_para);\
00105       }\
00106     }while(0)
00107 
00108 #define ASSERT_DEFINE(...) ASSERT_DEFINE_WITH_HANDLER(ASSERT_ERROR_HANDLER, NULL, __VA_ARGS__)
00109 
00110 //--------------------------------------------------------------------+
00111 // error_t Status Assert TODO use ASSERT_DEFINE
00112 //--------------------------------------------------------------------+
00113 #define ASSERT_STATUS_MESSAGE(sts, message) \
00114     ASSERT_DEFINE(error_t status = (error_t)(sts),\
00115                   ERROR_NONE == status, status, "%s: %s", ErrorStr[status], message)
00116 
00117 #define ASSERT_STATUS(sts) \
00118     ASSERT_DEFINE(error_t status = (error_t)(sts),\
00119                   ERROR_NONE == status, status, "error = %d", status)
00120 
00121 #define ASSERT_STATUS_RET_VOID(sts) \
00122     ASSERT_DEFINE(error_t status = (error_t)(sts),\
00123                   ERROR_NONE == status, (void) 0, "error = %d", status)
00124 
00125 //--------------------------------------------------------------------+
00126 // Logical Assert
00127 //--------------------------------------------------------------------+
00128 #define ASSERT(...)                      ASSERT_TRUE(__VA_ARGS__)
00129 #define ASSERT_TRUE(condition  , error)  ASSERT_DEFINE( , (condition), error, "%s", "evaluated to false")
00130 #define ASSERT_FALSE(condition , error)  ASSERT_DEFINE( ,!(condition), error, "%s", "evaluated to true")
00131 
00132 //--------------------------------------------------------------------+
00133 // Pointer Assert
00134 //--------------------------------------------------------------------+
00135 #define ASSERT_PTR(...)                    ASSERT_PTR_NOT_NULL(__VA_ARGS__)
00136 #define ASSERT_PTR_NOT_NULL(pointer, error) ASSERT_DEFINE( , NULL != (pointer), error, "%s", "pointer is NULL")
00137 #define ASSERT_PTR_NULL(pointer, error)    ASSERT_DEFINE( , NULL == (pointer), error, "%s", "pointer is not NULL")
00138 
00139 //--------------------------------------------------------------------+
00140 // Integral Assert
00141 //--------------------------------------------------------------------+
00142 #define ASSERT_XXX_EQUAL(type_format, expected, actual, error) \
00143     ASSERT_DEFINE(\
00144                   uint32_t exp = (expected); uint32_t act = (actual),\
00145                   exp==act,\
00146                   error,\
00147                   "expected " type_format ", actual " type_format, exp, act)
00148 
00149 #define ASSERT_XXX_WITHIN(type_format, lower, upper, actual, error) \
00150     ASSERT_DEFINE(\
00151                   uint32_t low = (lower); uint32_t up = (upper); uint32_t act = (actual),\
00152                   (low <= act) && (act <= up),\
00153                   error,\
00154                   "expected within " type_format " - " type_format ", actual " type_format, low, up, act)
00155 
00156 //--------------------------------------------------------------------+
00157 // Integer Assert
00158 //--------------------------------------------------------------------+
00159 #define ASSERT_INT(...)        ASSERT_INT_EQUAL(__VA_ARGS__)
00160 #define ASSERT_INT_EQUAL(...)  ASSERT_XXX_EQUAL("%d", __VA_ARGS__)
00161 #define ASSERT_INT_WITHIN(...) ASSERT_XXX_WITHIN("%d", __VA_ARGS__)
00162 
00163 //--------------------------------------------------------------------+
00164 // Hex Assert
00165 //--------------------------------------------------------------------+
00166 #define ASSERT_HEX(...)        ASSERT_HEX_EQUAL(__VA_ARGS__)
00167 #define ASSERT_HEX_EQUAL(...)  ASSERT_XXX_EQUAL("0x%x", __VA_ARGS__)
00168 #define ASSERT_HEX_WITHIN(...) ASSERT_XXX_WITHIN("0x%x", __VA_ARGS__)
00169 
00170 //--------------------------------------------------------------------+
00171 // Bin Assert
00172 //--------------------------------------------------------------------+
00173 #define BIN8_PRINTF_PATTERN "%d%d%d%d%d%d%d%d"
00174 #define BIN8_PRINTF_CONVERT(byte)  \
00175   ((byte) & 0x80 ? 1 : 0), \
00176   ((byte) & 0x40 ? 1 : 0), \
00177   ((byte) & 0x20 ? 1 : 0), \
00178   ((byte) & 0x10 ? 1 : 0), \
00179   ((byte) & 0x08 ? 1 : 0), \
00180   ((byte) & 0x04 ? 1 : 0), \
00181   ((byte) & 0x02 ? 1 : 0), \
00182   ((byte) & 0x01 ? 1 : 0)
00183 
00184 #define ASSERT_BIN8(...)        ASSERT_BIN8_EQUAL(__VA_ARGS__)
00185 #define ASSERT_BIN8_EQUAL(expected, actual, error)\
00186     ASSERT_DEFINE(\
00187                   uint8_t exp = (expected); uint8_t act = (actual),\
00188                   exp==act,\
00189                   error,\
00190                   "expected " BIN8_PRINTF_PATTERN ", actual " BIN8_PRINTF_PATTERN, BIN8_PRINTF_CONVERT(exp), BIN8_PRINTF_CONVERT(act) )
00191 
00192 #ifdef __cplusplus
00193 }
00194 #endif
00195 
00196 #endif /* _ASSERTION_H_ */
00197 
00198 /** @} */