test_strbuf.c 972 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "common.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "util/strbuf.h"
  7. static void test_strbuf_simple(void) {
  8. struct sc_strbuf buf;
  9. bool ok = sc_strbuf_init(&buf, 10);
  10. assert(ok);
  11. ok = sc_strbuf_append_staticstr(&buf, "Hello");
  12. assert(ok);
  13. ok = sc_strbuf_append_char(&buf, ' ');
  14. assert(ok);
  15. ok = sc_strbuf_append_staticstr(&buf, "world");
  16. assert(ok);
  17. ok = sc_strbuf_append_staticstr(&buf, "!\n");
  18. assert(ok);
  19. ok = sc_strbuf_append_staticstr(&buf, "This is a test");
  20. assert(ok);
  21. ok = sc_strbuf_append_n(&buf, '.', 3);
  22. assert(ok);
  23. assert(!strcmp(buf.s, "Hello world!\nThis is a test..."));
  24. sc_strbuf_shrink(&buf);
  25. assert(buf.len == buf.cap);
  26. assert(!strcmp(buf.s, "Hello world!\nThis is a test..."));
  27. free(buf.s);
  28. }
  29. int main(int argc, char *argv[]) {
  30. (void) argc;
  31. (void) argv;
  32. test_strbuf_simple();
  33. return 0;
  34. }