断言

来源:互联网 发布:淘宝 618 大促 销售额 编辑:程序博客网 时间:2024/06/11 06:23

1.什么是断言?
断言是编程术语,表示为一些布尔表达式,程序员相信在程序中的某个特定点该表达式值为真,可以在任何时候启用和禁用断言验证,因此可以在测试时启用断言而在部署时禁用断言。同样,程序投入运行后,最终用户在遇到问题时可以重新启用断言。—来自百度。

2.举例子;
/* Exported macro ————————————————————*/

ifdef USE_FULL_ASSERT

/**
* @brief The assert_param macro is used for function’s parameters check.
* @param expr: If expr is false, it calls assert_failed function which reports
* the name of the source file and the source line number of the call
* that failed. If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)FILE, LINE))
/* Exported functions ——————————————————- */
void assert_failed(uint8_t* file, uint32_t line);

else

#define assert_param(expr) ((void)0)

endif /* USE_FULL_ASSERT */

endif /* __STM32F10x_CONF_H */

这是stm32里面的断言宏定义:
很简单 。意思是说:打开USE_FULL_ASSERT 时,使用assert_param ,不打开始时,不用断言,即用((void)0)代替,这个可以认为没有什么意义,但是不影响程序的运行。
举例子:
/* Check the parameters */
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
typedef enum//枚举
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;

define IS_GPIO_SPEED(SPEED) (((SPEED) == GPIO_Speed_10MHz) || ((SPEED) == GPIO_Speed_2MHz) || `((SPEED) == GPIO_Speed_50MHz)) 不是真就是假

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,类型需要匹配
GPIO_InitTypeDef GPIO_InitStructure;

/**
* @brief GPIO Init structure definition
*/

typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */

GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */

GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
明白了吧
当然这只是讲的在32中的一点应用,在内核及其他的工程调试中也和这样的原理差不多 。。。

原创粉丝点击