Line data Source code
1 : /***************************************************************************//**
2 :
3 : @file charbuftest.c
4 :
5 : @author Stephen Brennan
6 :
7 : @date Created Saturday, 23 May 2015
8 :
9 : @brief Tests for the charbuf.
10 :
11 : @copyright Copyright (c) 2015, Stephen Brennan. Released under the Revised
12 : BSD License. See LICENSE.txt for details.
13 :
14 : *******************************************************************************/
15 :
16 : #include <string.h>
17 : #include <wchar.h>
18 :
19 : #include "libstephen/cb.h"
20 : #include "libstephen/ut.h"
21 :
22 : /**
23 : @brief Ensure that simple operation has no memory leaks.
24 : */
25 1 : int test_cbuf_memory(void)
26 : {
27 1 : cbuf *c = cb_create(10);
28 1 : cb_delete(c);
29 1 : return 0;
30 : }
31 :
32 : /**
33 : @brief Ensure that simple operation has no memory leaks.
34 : */
35 1 : int test_wcbuf_memory(void)
36 : {
37 1 : wcbuf *wc = wcb_create(10);
38 1 : wcb_delete(wc);
39 1 : return 0;
40 : }
41 :
42 : /**
43 : @brief Test appending to a cbuf with no allocation.
44 : */
45 1 : int test_cbuf_append_noalloc(void)
46 : {
47 1 : cbuf *c = cb_create(10);
48 1 : cb_append(c, "abc");
49 1 : cb_append(c, "def");
50 1 : TEST_ASSERT(0 == strcmp(c->buf, "abcdef"));
51 1 : TEST_ASSERT(c->capacity == 10);
52 1 : TEST_ASSERT(c->length == 6);
53 1 : cb_delete(c);
54 1 : return 0;
55 : }
56 :
57 : /**
58 : @brief Test appending to a wcbuf with no reallocation.
59 : */
60 1 : int test_wcbuf_append_noalloc(void)
61 : {
62 1 : wcbuf *wc = wcb_create(10);
63 1 : wcb_append(wc, L"abc");
64 1 : wcb_append(wc, L"def");
65 1 : TEST_ASSERT(0 == wcscmp(wc->buf, L"abcdef"));
66 1 : TEST_ASSERT(wc->capacity == 10);
67 1 : TEST_ASSERT(wc->length == 6);
68 1 : wcb_delete(wc);
69 1 : return 0;
70 : }
71 :
72 : /**
73 : @brief Test appending to a cbuf with reallocation.
74 : */
75 1 : int test_cbuf_append_realloc(void)
76 : {
77 1 : cbuf *c = cb_create(4);
78 1 : cb_append(c, "abc");
79 1 : cb_append(c, "def");
80 1 : TEST_ASSERT(0 == strcmp(c->buf, "abcdef"));
81 1 : TEST_ASSERT(c->capacity == 8);
82 1 : TEST_ASSERT(c->length == 6);
83 1 : cb_delete(c);
84 1 : return 0;
85 : }
86 :
87 : /**
88 : @brief Test appending to a wcbuf with reallocation.
89 : */
90 1 : int test_wcbuf_append_realloc(void)
91 : {
92 1 : wcbuf *wc = wcb_create(4);
93 1 : wcb_append(wc, L"abc");
94 1 : wcb_append(wc, L"def");
95 1 : TEST_ASSERT(0 == wcscmp(wc->buf, L"abcdef"));
96 1 : TEST_ASSERT(wc->capacity == 8);
97 1 : TEST_ASSERT(wc->length == 6);
98 1 : wcb_delete(wc);
99 1 : return 0;
100 : }
101 :
102 : /**
103 : @brief Test printf to a cbuf.
104 : */
105 1 : int test_cbuf_printf(void)
106 : {
107 1 : cbuf *cb = cb_create(8);
108 1 : cb_append(cb, "prefix ");
109 1 : cb_printf(cb, "format %ls %s %d suffix", L"wcs", "mbs", 20);
110 1 : TEST_ASSERT(0 == strcmp(cb->buf, "prefix format wcs mbs 20 suffix"));
111 1 : cb_delete(cb);
112 1 : return 0;
113 : }
114 :
115 : /**
116 : @brief Test printf to a wcbuf.
117 : */
118 1 : int test_wcbuf_printf(void)
119 : {
120 1 : wcbuf *wcb = wcb_create(8);
121 1 : wcb_append(wcb, L"prefix ");
122 1 : wcb_printf(wcb, L"format %ls %s %d suffix", L"wcs", "mbs", 20);
123 1 : TEST_ASSERT(0 == wcscmp(wcb->buf, L"prefix format wcs mbs 20 suffix"));
124 1 : wcb_delete(wcb);
125 1 : return 0;
126 : }
127 :
128 1 : void charbuf_test(void)
129 : {
130 1 : smb_ut_group *group = su_create_test_group("charbuf");
131 :
132 1 : smb_ut_test *cbuf_memory = su_create_test("cbuf_memory", test_cbuf_memory);
133 1 : su_add_test(group, cbuf_memory);
134 :
135 1 : smb_ut_test *wcbuf_memory = su_create_test("wcbuf_memory", test_wcbuf_memory);
136 1 : su_add_test(group, wcbuf_memory);
137 :
138 1 : smb_ut_test *cbuf_append_noalloc = su_create_test("cbuf_append_noalloc", test_cbuf_append_noalloc);
139 1 : su_add_test(group, cbuf_append_noalloc);
140 :
141 1 : smb_ut_test *wcbuf_append_noalloc = su_create_test("wcbuf_append_noalloc", test_wcbuf_append_noalloc);
142 1 : su_add_test(group, wcbuf_append_noalloc);
143 :
144 1 : smb_ut_test *cbuf_append_realloc = su_create_test("cbuf_append_realloc", test_cbuf_append_realloc);
145 1 : su_add_test(group, cbuf_append_realloc);
146 :
147 1 : smb_ut_test *wcbuf_append_realloc = su_create_test("wcbuf_append_realloc", test_wcbuf_append_realloc);
148 1 : su_add_test(group, wcbuf_append_realloc);
149 :
150 1 : smb_ut_test *cbuf_printf = su_create_test("cbuf_printf", test_cbuf_printf);
151 1 : su_add_test(group, cbuf_printf);
152 :
153 1 : smb_ut_test *wcbuf_printf = su_create_test("wcbuf_printf", test_wcbuf_printf);
154 1 : su_add_test(group, wcbuf_printf);
155 :
156 1 : su_run_group(group);
157 1 : su_delete_group(group);
158 1 : }
|