C++ 断言(assert)机制
一、assert 宏的基本语法与工作机制断言是 C++ 标准库提供的调试工具,核心通过头文件(兼容 C 的<assert.h>)中的assert宏实现,其本质是条件检查宏,仅在调试阶段生效。 1.1 核心语法assert宏接收一个布尔表达式作为参数,语法如下: 12345678910111213#include <cassert> // 必须包含的头文件int main() { int* ptr = new int(10); // 检查指针是否非空(调试阶段生效) assert(ptr != nullptr); delete ptr; ptr = nullptr; // 此时断言会失败(指针已置空) assert(ptr != nullptr); return 0;} 1.2 工作机制与 NDEBUG 宏assert的行为完全由 **NDEBUG宏 **("No Debug")控制,这是 C++...
Python测试框架:pytest与assert断言
Created|Python
Python提供了多种测试工具,其中pytest是最流行的单元测试框架之一。本文将介绍Python中的assert语句以及pytest框架的基本用法。 一、assert语句1. 基本用法1234567# assert 条件x = 10assert x > 0 # 条件为True,无输出# assert 条件, 错误信息x = -5assert x > 0, "x必须大于0" # 抛出AssertionError 2. 常见用途12345678910def divide(a, b): assert b != 0, "除数不能为零" return a / bdef validate_age(age): assert 0 <= age <= 150, "年龄必须在0到150之间" return ageprint(validate_age(25)) # 正常# validate_age(-5) # 抛出AssertionError 二、pytest框架1....


Recent Posts
Categories
- C++85
- C-Code23
- CMake3
- CS501
- Computer-Networking40
- Computer-Organization1
- Data Structures and Algorithms1
- Data-Structures6
- Essay1
- Essays4
- Foundational Syntax and Core Concepts4
- Git3
- HTTP2
- Interview7
- Lcov2
- Leetcode133
- Linux40
- Linux Internals2
- List1
- Lists1
- MQTT8
- MYSQL3
- MySQL1
- Operating-Systems7
- PKGCONF1
- Python63
- Redis14
- STL3
- UML1
- XML1
- lock1
- practice problems1
- 代码提交1
- 函数模板2
- 前端5
- 工厂模式2
- 文章19
- 类图1
- 编译1
- 设计模式8
- 链表1
Archives
- 2026年05月 6
- 2026年04月 7
- 2026年03月 7
- 2026年02月 3
- 2026年01月 5
- 2025年12月 11
- 2025年11月 6
- 2025年10月 5
- 2025年09月 15
- 2025年08月 5
- 2025年07月 5
- 2025年06月 5
- 2025年05月 5
- 2025年04月 10
- 2025年03月 5
- 2025年02月 8
- 2025年01月 10
- 2024年12月 10
- 2024年11月 8
- 2024年10月 12
- 2024年09月 11
- 2024年08月 11
- 2024年07月 9
- 2024年06月 9
- 2024年05月 28
- 2024年04月 41
- 2024年03月 41
- 2024年02月 37
- 2024年01月 43
- 2023年12月 5
- 2023年11月 6
- 2023年10月 5
- 2023年09月 4
- 2023年08月 4
- 2023年07月 6
- 2023年06月 5
- 2023年05月 7
- 2023年04月 5
- 2023年03月 5
- 2023年02月 5
- 2023年01月 5
- 2022年12月 6
- 2022年11月 11
- 2022年10月 7
- 2022年09月 4
- 2022年08月 3
- 2022年07月 5
- 2022年06月 7
- 2022年05月 4
- 2022年04月 4
- 2022年03月 4
- 2022年02月 5
- 2022年01月 5
Website Info
Article Count :
505
Runtime :
Total Word Count :
844.1k
Last Update :