https://github.com/tekknolagi/dcheck
C++ dynamic assertion library
https://github.com/tekknolagi/dcheck
assert cpp library
Last synced: about 1 year ago
JSON representation
C++ dynamic assertion library
- Host: GitHub
- URL: https://github.com/tekknolagi/dcheck
- Owner: tekknolagi
- License: other
- Created: 2022-05-19T08:43:15.000Z (almost 4 years ago)
- Default Branch: trunk
- Last Pushed: 2022-10-21T18:42:55.000Z (over 3 years ago)
- Last Synced: 2025-03-23T00:41:40.558Z (about 1 year ago)
- Topics: assert, cpp, library
- Language: C
- Homepage:
- Size: 17.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [dcheck](https://github.com/tekknolagi/dcheck)
This very small assert library provides `assert`-like macros for checking
conditions at runtime. It works with C and C++.
Improvements over raw `assert`:
* Semantic checks for indices, bounds, ranges, unimplemented features, and
unreachable states
* Includes `printf`-compatible error message formatting
* `NDEBUG` and always-on variants
* Bounds/index checking built-in
* File/function/line number and symbolized condition code in error message
* Still checks code for compiler errors if DCHECK is turned off
Here is some sample code:
```c
#include "dcheck.h"
int main() {
// ...
CHECK(true, "thank goodness");
int arr[3];
// Array index won't crash
CHECK_INDEX(ind, 3);
// Still can order off the kids menu
CHECK_BOUND(age, 5);
// Month is valid
CHECK_RANGE(month, 1, 12);
if (needs_feature) {
UNIMPLEMENTED("needs Brian's feature");
}
if (val == 4) {
UNREACHABLE("unexpected number %d", val);
}
// ...
}
```
Add `D` before `CHECK`, `CHECK_INDEX`, `CHECK_BOUND`, and `CHECK_RANGE` if you
want the check to go away in `NDEBUG` mode. Like `DCHECK_INDEX` and so forth.
Happy hacking.
## Use in CMake projects
```cmake
include(FetchContent)
FetchContent_Declare(
dcheck
GIT_REPOSITORY https://github.com/tekknolagi/dcheck
GIT_TAG trunk
)
FetchContent_MakeAvailable(dcheck)
```