Assertions in C++
1 Assertions in C++ Programs
#include <cassert>
C++ /usr/include/c++/7.*/cassert
(Version number may be newer.)
#include <assert.h>
C /usr/include/assert.h
- Defines the
assert()
macro. Its argument must be a valid Boolean
expression.
#define NDEBUG
- In the absence of this, the assert's boolexp gets compiled in.
When executed, if it evaluates to "false", the macro-expanded
compiled-in code witll print an error message including the line
number of the source code, and aborts the process.
- In the presence of this (above the
#include
), the assert macro
becomes empty, and there is no trace of the boolexp in the
executable image.
- Remember: 0 is false, and non-0 is true in C/C++
2 C++ Examples with Assertions
3 References
- "Googletest",
https://code.google.com/p/googletest/wiki/Primer#Assertions 2018
Google's C++ test framework. Recommended Reading
- Stephen H. Edwards, Murali Sitaraman, Bruce W. Weide, and Joseph
Hollingsworth,
Contract-Checking Wrappers for C++ Classes,
http://www.cse.ohio-state.edu/rsrg/documents/2004/04ESWH.pdf
IEEE Transactions on Software Engineering, Vol. 30, No. 11, November 2004, pp 794 - 810
Recommended Reading