mirror of https://github.com/flysand7/ciabatta.git
assert.h documentation
This commit is contained in:
parent
320bb829ea
commit
c12676e3de
|
@ -0,0 +1,84 @@
|
||||||
|
|
||||||
|
# assert.h - debug assertions
|
||||||
|
|
||||||
|
Macro definitions:
|
||||||
|
|
||||||
|
- [`assert(expr)`](#assert)
|
||||||
|
- [`static_assert`](#static-assert)
|
||||||
|
|
||||||
|
## [assert](#assert)
|
||||||
|
|
||||||
|
Assert a given condition is true, otherwise abort execution of a program.
|
||||||
|
|
||||||
|
<details><summary>Description</summary>
|
||||||
|
|
||||||
|
The macro checks whether `expr` is true, and if not, prints the
|
||||||
|
information about where the error occurs and then aborts execution in a way
|
||||||
|
equivalent to calling abort() function (See SIGABRT).
|
||||||
|
|
||||||
|
You can disable the assertion checks by defining NDEBUG macro.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details><summary>Declaration</summary>
|
||||||
|
|
||||||
|
```c
|
||||||
|
#if defined(NDEBUG)
|
||||||
|
#define assert(expr) ((void)0)
|
||||||
|
#else
|
||||||
|
#define assert(expr) /* see description */
|
||||||
|
#endif
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Example</summary>
|
||||||
|
|
||||||
|
```c
|
||||||
|
// Uncomment to disable assert
|
||||||
|
//#define NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int factorial(int n) {
|
||||||
|
assert(n >= 0);
|
||||||
|
if(n == 0) return 1;
|
||||||
|
return n*factorial(n-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Factorial of %d is %d\n", 10, factorial(10));
|
||||||
|
printf("Factorial of %d is %d\n", -1, factorial(-1));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The first function would print 3628800, the second would trigger an assert.
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```
|
||||||
|
Factorial of 10 is 3628800
|
||||||
|
Assertion failed: n >= 0
|
||||||
|
Function: factorial
|
||||||
|
File: test\test_assert.c
|
||||||
|
Line: 4
|
||||||
|
Trace:
|
||||||
|
4: factorial
|
||||||
|
3: main
|
||||||
|
2: mainCRTStartup
|
||||||
|
1: BaseThreadInitThunk
|
||||||
|
0: RtlUserThreadStart
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## [static_assert](#static-assert)
|
||||||
|
|
||||||
|
Keyword macro that expands to C11 keyword `_Static_assert`.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Definition</summary>
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define static_assert _Static_assert
|
||||||
|
```
|
||||||
|
</details>
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
# Ciabatta
|
||||||
|
|
||||||
|
Ciabatta is a C standard library implementation. The goal is to provide a
|
||||||
|
cross-platform implementation that provides it's own documentation, clarifying
|
||||||
|
implementation-defined details, as well as provides additional functionality,
|
||||||
|
like sockets, or unicode processing, the kind of functionality that is used
|
||||||
|
in different kinds of programs.
|
||||||
|
|
||||||
|
## Headers
|
||||||
|
|
||||||
|
The implementation doesn't provide all headers. The headers that aren't
|
||||||
|
provided are supposed to be provided by compiler. The list of such headers:
|
||||||
|
|
||||||
|
- float.h
|
||||||
|
- iso646.h
|
||||||
|
- limits.h
|
||||||
|
- stdalign.h
|
||||||
|
- stdarg.h
|
||||||
|
- stdbool.h
|
||||||
|
- stddef.h
|
||||||
|
- stdint.h
|
||||||
|
- stdnoreturn.h
|
||||||
|
|
||||||
|
The headers that are provided have documentation included:
|
||||||
|
|
||||||
|
- [assert.h](assert.md)
|
||||||
|
- [complex.h](complex.md)
|
||||||
|
- [ctype.h](ctype.md)
|
||||||
|
- [errno.h](errno.md)
|
||||||
|
- [fenv.h](fenv.md)
|
||||||
|
- [inttypes.h](inttypes.md)
|
||||||
|
- [locale.h](locale.md)
|
||||||
|
- [math.h](math.md)
|
||||||
|
- [signal.h](signal.md)
|
||||||
|
- [stdio.h](stdio.md)
|
||||||
|
- [stdlib.h](stdlib.md)
|
||||||
|
- [string.h](string.md)
|
||||||
|
- [tgmath.h](tgmath.md)
|
||||||
|
- [threads.h](threads.md)
|
||||||
|
- [time.h](time.md)
|
||||||
|
- [uchar.h](uchar.md)
|
||||||
|
- [wchar.h](wchar.md)
|
||||||
|
- [wctype.h](wctype.md)
|
|
@ -1,17 +1,13 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stddef.h>
|
|
||||||
#include <errno.h>
|
|
||||||
void do_more_stuff(char *ptr) {
|
|
||||||
assert(ptr != NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void do_stuff() {
|
int factorial(int n) {
|
||||||
do_more_stuff(NULL);
|
assert(n >= 0);
|
||||||
|
if(n == 0) return 1;
|
||||||
|
return n*factorial(n-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
errno = 0;
|
printf("Factorial of %d is %d\n", 10, factorial(10));
|
||||||
assert(2+2 == 4);
|
printf("Factorial of %d is %d\n", -1, factorial(-1));
|
||||||
do_stuff();
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue