File: agxbuf-use-implicit-nul.c

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (37 lines) | stat: -rw-r--r-- 1,024 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// \file
/// \brief test that `agxbuf` can use its entire memory as an inline string
///
/// This program accesses `agxbuf` internals in a way that is not expected from
/// users. This is testing the internal behavior of `agxbuf` itself.

#ifdef NDEBUG
#error "this program is not intended to be compiled with assertions disabled"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/agxbuf.h>

int main(void) {

  agxbuf xb = {0};
  assert(agxbuf_is_inline(&xb));

  // construct a string that covers the entirety of an `agxbuf`
  char content[sizeof(xb)] = {0};
  for (size_t i = 0; i < sizeof(content) - 1; ++i) {
    content[i] = 'A' + (char)i;
  }

  // we should be able to print this to the buffer while remaining inline
  agxbprint(&xb, "%s", content);
  assert(agxbuf_is_inline(&xb));

  // we should be able to retrieve and use the content without incurring a heap
  // allocation
  printf("content is: %s\n", agxbuse(&xb));
  assert(agxbuf_is_inline(&xb));

  return EXIT_SUCCESS;
}