Debian Codegraph @

tests/agxbuf-use-implicit-nul.c

raw
1 /// \file
2 /// \brief test that `agxbuf` can use its entire memory as an inline string
3 ///
4 /// This program accesses `agxbuf` internals in a way that is not expected from
5 /// users. This is testing the internal behavior of `agxbuf` itself.
6
7 #ifdef NDEBUG
8 #error "this program is not intended to be compiled with assertions disabled"
9 #endif
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <util/agxbuf.h>
15
16 int main(void) {
17
18 agxbuf xb = {0};
19 assert(agxbuf_is_inline(&xb));
20
21 // construct a string that covers the entirety of an `agxbuf`
22 char content[sizeof(xb)] = {0};
23 for (size_t i = 0; i < sizeof(content) - 1; ++i) {
24 content[i] = 'A' + (char)i;
25 }
26
27 // we should be able to print this to the buffer while remaining inline
28 agxbprint(&xb, "%s", content);
29 assert(agxbuf_is_inline(&xb));
30
31 // we should be able to retrieve and use the content without incurring a heap
32 // allocation
33 printf("content is: %s\n", agxbuse(&xb));
34 assert(agxbuf_is_inline(&xb));
35
36 return EXIT_SUCCESS;
37 }