| 1 | #include "unit-test.h" |
| 2 | #include "strbuf.h" |
| 3 | |
| 4 | /* wrapper that supplies tests with an empty, initialized strbuf */ |
| 5 | static void setup(void (*f)(struct strbuf*, const void*), |
| 6 | const void *data) |
| 7 | { |
| 8 | struct strbuf buf = STRBUF_INIT; |
| 9 | |
| 10 | f(&buf, data); |
| 11 | strbuf_release(&buf); |
| 12 | cl_assert_equal_i(buf.len, 0); |
| 13 | cl_assert_equal_i(buf.alloc, 0); |
| 14 | } |
| 15 | |
| 16 | /* wrapper that supplies tests with a populated, initialized strbuf */ |
| 17 | static void setup_populated(void (*f)(struct strbuf*, const void*), |
| 18 | const char *init_str, const void *data) |
| 19 | { |
| 20 | struct strbuf buf = STRBUF_INIT; |
| 21 | |
| 22 | strbuf_addstr(&buf, init_str); |
| 23 | cl_assert_equal_i(buf.len, strlen(init_str)); |
| 24 | f(&buf, data); |
| 25 | strbuf_release(&buf); |
| 26 | cl_assert_equal_i(buf.len, 0); |
| 27 | cl_assert_equal_i(buf.alloc, 0); |
| 28 | } |
| 29 | |
| 30 | static void assert_sane_strbuf(struct strbuf *buf) |
| 31 | { |
| 32 | /* Initialized strbufs should always have a non-NULL buffer */ |
| 33 | cl_assert(buf->buf != NULL); |
| 34 | /* Buffers should always be NUL-terminated */ |
| 35 | cl_assert(buf->buf[buf->len] == '\0'); |
| 36 | /* |
| 37 | * In case the buffer contains anything, `alloc` must alloc must |
| 38 | * be at least one byte larger than `len`. |
| 39 | */ |
| 40 | if (buf->len) |
| 41 | cl_assert(buf->len < buf->alloc); |
| 42 | } |
| 43 | |
| 44 | void test_strbuf__static_init(void) |
| 45 | { |
| 46 | struct strbuf buf = STRBUF_INIT; |
| 47 | |
| 48 | cl_assert_equal_i(buf.len, 0); |
| 49 | cl_assert_equal_i(buf.alloc, 0); |
| 50 | cl_assert(buf.buf[0] == '\0'); |
| 51 | } |
| 52 | |
| 53 | void test_strbuf__dynamic_init(void) |
| 54 | { |
| 55 | struct strbuf buf; |
| 56 | |
| 57 | strbuf_init(&buf, 1024); |
| 58 | assert_sane_strbuf(&buf); |
| 59 | cl_assert_equal_i(buf.len, 0); |
| 60 | cl_assert(buf.alloc >= 1024); |
| 61 | cl_assert(buf.buf[0] == '\0'); |
| 62 | strbuf_release(&buf); |
| 63 | } |
| 64 | |
| 65 | static void t_addch(struct strbuf *buf, const void *data) |
| 66 | { |
| 67 | const char *p_ch = data; |
| 68 | const char ch = *p_ch; |
| 69 | size_t orig_alloc = buf->alloc; |
| 70 | size_t orig_len = buf->len; |
| 71 | |
| 72 | assert_sane_strbuf(buf); |
| 73 | strbuf_addch(buf, ch); |
| 74 | assert_sane_strbuf(buf); |
| 75 | cl_assert_equal_i(buf->len, orig_len + 1); |
| 76 | cl_assert(buf->alloc >= orig_alloc); |
| 77 | cl_assert(buf->buf[buf->len] == '\0'); |
| 78 | } |
| 79 | |
| 80 | static void t_addstr(struct strbuf *buf, const void *data) |
| 81 | { |
| 82 | const char *text = data; |
| 83 | size_t len = strlen(text); |
| 84 | size_t orig_alloc = buf->alloc; |
| 85 | size_t orig_len = buf->len; |
| 86 | |
| 87 | assert_sane_strbuf(buf); |
| 88 | strbuf_addstr(buf, text); |
| 89 | assert_sane_strbuf(buf); |
| 90 | cl_assert_equal_i(buf->len, orig_len + len); |
| 91 | cl_assert(buf->alloc >= orig_alloc); |
| 92 | cl_assert(buf->buf[buf->len] == '\0'); |
| 93 | cl_assert_equal_s(buf->buf + orig_len, text); |
| 94 | } |
| 95 | |
| 96 | void test_strbuf__add_single_char(void) |
| 97 | { |
| 98 | setup(t_addch, "a"); |
| 99 | } |
| 100 | |
| 101 | void test_strbuf__add_empty_char(void) |
| 102 | { |
| 103 | setup(t_addch, ""); |
| 104 | } |
| 105 | |
| 106 | void test_strbuf__add_append_char(void) |
| 107 | { |
| 108 | setup_populated(t_addch, "initial value", "a"); |
| 109 | } |
| 110 | |
| 111 | void test_strbuf__add_single_str(void) |
| 112 | { |
| 113 | setup(t_addstr, "hello there"); |
| 114 | } |
| 115 | |
| 116 | void test_strbuf__add_append_str(void) |
| 117 | { |
| 118 | setup_populated(t_addstr, "initial value", "hello there"); |
| 119 | } |