summaryrefslogtreecommitdiff
path: root/t/unit-tests/u-strbuf.c
blob: caa5d78aa3428941e159c244bd2428cc808fa08f (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "unit-test.h"
#include "strbuf.h"

/* wrapper that supplies tests with an empty, initialized strbuf */
static void setup(void (*f)(struct strbuf*, const void*),
		  const void *data)
{
	struct strbuf buf = STRBUF_INIT;

	f(&buf, data);
	strbuf_release(&buf);
	cl_assert_equal_i(buf.len, 0);
	cl_assert_equal_i(buf.alloc, 0);
}

/* wrapper that supplies tests with a populated, initialized strbuf */
static void setup_populated(void (*f)(struct strbuf*, const void*),
			    const char *init_str, const void *data)
{
	struct strbuf buf = STRBUF_INIT;

	strbuf_addstr(&buf, init_str);
	cl_assert_equal_i(buf.len, strlen(init_str));
	f(&buf, data);
	strbuf_release(&buf);
	cl_assert_equal_i(buf.len, 0);
	cl_assert_equal_i(buf.alloc, 0);
}

static void assert_sane_strbuf(struct strbuf *buf)
{
	/* Initialized strbufs should always have a non-NULL buffer */
	cl_assert(buf->buf != NULL);
	/* Buffers should always be NUL-terminated */
	cl_assert(buf->buf[buf->len] == '\0');
	/*
         * In case the buffer contains anything, `alloc` must alloc must
         * be at least one byte larger than `len`.
         */
	if (buf->len)
            cl_assert(buf->len < buf->alloc);
}

void test_strbuf__static_init(void)
{
	struct strbuf buf = STRBUF_INIT;

	cl_assert_equal_i(buf.len, 0);
	cl_assert_equal_i(buf.alloc, 0);
	cl_assert(buf.buf[0] == '\0');
}

void test_strbuf__dynamic_init(void)
{
	struct strbuf buf;

	strbuf_init(&buf, 1024);
	assert_sane_strbuf(&buf);
	cl_assert_equal_i(buf.len, 0);
	cl_assert(buf.alloc >= 1024);
	cl_assert(buf.buf[0] == '\0');
	strbuf_release(&buf);
}

static void t_addch(struct strbuf *buf, const void *data)
{
	const char *p_ch = data;
	const char ch = *p_ch;
	size_t orig_alloc = buf->alloc;
	size_t orig_len = buf->len;

	assert_sane_strbuf(buf);
	strbuf_addch(buf, ch);
	assert_sane_strbuf(buf);
	cl_assert_equal_i(buf->len, orig_len + 1);
	cl_assert(buf->alloc >= orig_alloc);
	cl_assert(buf->buf[buf->len] == '\0');
}

static void t_addstr(struct strbuf *buf, const void *data)
{
	const char *text = data;
	size_t len = strlen(text);
	size_t orig_alloc = buf->alloc;
	size_t orig_len = buf->len;

	assert_sane_strbuf(buf);
	strbuf_addstr(buf, text);
	assert_sane_strbuf(buf);
	cl_assert_equal_i(buf->len, orig_len + len);
	cl_assert(buf->alloc >= orig_alloc);
	cl_assert(buf->buf[buf->len] == '\0');
	cl_assert_equal_s(buf->buf + orig_len, text);
}

void test_strbuf__add_single_char(void)
{
	setup(t_addch, "a");
}

void test_strbuf__add_empty_char(void)
{
	setup(t_addch, "");
}

void test_strbuf__add_append_char(void)
{
	setup_populated(t_addch, "initial value", "a");
}

void test_strbuf__add_single_str(void)
{
	setup(t_addstr, "hello there");
}

void test_strbuf__add_append_str(void)
{
	setup_populated(t_addstr, "initial value", "hello there");
}