1*a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*a9de470cSBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3*a9de470cSBruce Richardson */
4*a9de470cSBruce Richardson
5*a9de470cSBruce Richardson #include <stdio.h>
6*a9de470cSBruce Richardson #include <stdlib.h>
7*a9de470cSBruce Richardson #include <string.h>
8*a9de470cSBruce Richardson
9*a9de470cSBruce Richardson #include <rte_string_fns.h>
10*a9de470cSBruce Richardson
11*a9de470cSBruce Richardson #include <cmdline_cirbuf.h>
12*a9de470cSBruce Richardson
13*a9de470cSBruce Richardson #include "test_cmdline.h"
14*a9de470cSBruce Richardson
15*a9de470cSBruce Richardson /* different length strings */
16*a9de470cSBruce Richardson #define CIRBUF_STR_HEAD " HEAD"
17*a9de470cSBruce Richardson #define CIRBUF_STR_TAIL "TAIL"
18*a9de470cSBruce Richardson
19*a9de470cSBruce Richardson /* miscellaneous tests - they make bullseye happy */
20*a9de470cSBruce Richardson static int
test_cirbuf_string_misc(void)21*a9de470cSBruce Richardson test_cirbuf_string_misc(void)
22*a9de470cSBruce Richardson {
23*a9de470cSBruce Richardson struct cirbuf cb;
24*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
25*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
26*a9de470cSBruce Richardson
27*a9de470cSBruce Richardson /* initialize buffers */
28*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
29*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
30*a9de470cSBruce Richardson
31*a9de470cSBruce Richardson /*
32*a9de470cSBruce Richardson * initialize circular buffer
33*a9de470cSBruce Richardson */
34*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
35*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
36*a9de470cSBruce Richardson return -1;
37*a9de470cSBruce Richardson }
38*a9de470cSBruce Richardson
39*a9de470cSBruce Richardson /*
40*a9de470cSBruce Richardson * add strings to head and tail, but read only tail
41*a9de470cSBruce Richardson * this results in read operation that does not transcend
42*a9de470cSBruce Richardson * from buffer end to buffer beginning (in other words,
43*a9de470cSBruce Richardson * strlen <= cb->maxlen - cb->end)
44*a9de470cSBruce Richardson */
45*a9de470cSBruce Richardson
46*a9de470cSBruce Richardson /* add string to head */
47*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
48*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
49*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
50*a9de470cSBruce Richardson return -1;
51*a9de470cSBruce Richardson }
52*a9de470cSBruce Richardson /* add string to tail */
53*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
54*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
55*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
56*a9de470cSBruce Richardson return -1;
57*a9de470cSBruce Richardson }
58*a9de470cSBruce Richardson /* read string from tail */
59*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
60*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
61*a9de470cSBruce Richardson printf("Error: failed to get string from tail!\n");
62*a9de470cSBruce Richardson return -1;
63*a9de470cSBruce Richardson }
64*a9de470cSBruce Richardson /* verify string */
65*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
66*a9de470cSBruce Richardson printf("Error: tail strings do not match!\n");
67*a9de470cSBruce Richardson return -1;
68*a9de470cSBruce Richardson }
69*a9de470cSBruce Richardson /* clear buffers */
70*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
71*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
72*a9de470cSBruce Richardson
73*a9de470cSBruce Richardson
74*a9de470cSBruce Richardson
75*a9de470cSBruce Richardson /*
76*a9de470cSBruce Richardson * add a string to buffer when start/end is at end of buffer
77*a9de470cSBruce Richardson */
78*a9de470cSBruce Richardson
79*a9de470cSBruce Richardson /*
80*a9de470cSBruce Richardson * reinitialize circular buffer with start at the end of cirbuf
81*a9de470cSBruce Richardson */
82*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) {
83*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
84*a9de470cSBruce Richardson return -1;
85*a9de470cSBruce Richardson }
86*a9de470cSBruce Richardson
87*a9de470cSBruce Richardson
88*a9de470cSBruce Richardson /* add string to tail */
89*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
90*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
91*a9de470cSBruce Richardson printf("Error: failed to add string to tail!\n");
92*a9de470cSBruce Richardson return -1;
93*a9de470cSBruce Richardson }
94*a9de470cSBruce Richardson /* read string from tail */
95*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
96*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
97*a9de470cSBruce Richardson printf("Error: failed to get string from tail!\n");
98*a9de470cSBruce Richardson return -1;
99*a9de470cSBruce Richardson }
100*a9de470cSBruce Richardson /* verify string */
101*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
102*a9de470cSBruce Richardson printf("Error: tail strings do not match!\n");
103*a9de470cSBruce Richardson return -1;
104*a9de470cSBruce Richardson }
105*a9de470cSBruce Richardson /* clear tmp buffer */
106*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
107*a9de470cSBruce Richardson
108*a9de470cSBruce Richardson
109*a9de470cSBruce Richardson /* add string to head */
110*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
111*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
112*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
113*a9de470cSBruce Richardson return -1;
114*a9de470cSBruce Richardson }
115*a9de470cSBruce Richardson /* read string from tail */
116*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
117*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
118*a9de470cSBruce Richardson printf("Error: failed to get string from head!\n");
119*a9de470cSBruce Richardson return -1;
120*a9de470cSBruce Richardson }
121*a9de470cSBruce Richardson /* verify string */
122*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
123*a9de470cSBruce Richardson printf("Error: headstrings do not match!\n");
124*a9de470cSBruce Richardson return -1;
125*a9de470cSBruce Richardson }
126*a9de470cSBruce Richardson
127*a9de470cSBruce Richardson return 0;
128*a9de470cSBruce Richardson }
129*a9de470cSBruce Richardson
130*a9de470cSBruce Richardson /* test adding and deleting strings */
131*a9de470cSBruce Richardson static int
test_cirbuf_string_add_del(void)132*a9de470cSBruce Richardson test_cirbuf_string_add_del(void)
133*a9de470cSBruce Richardson {
134*a9de470cSBruce Richardson struct cirbuf cb;
135*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
136*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
137*a9de470cSBruce Richardson
138*a9de470cSBruce Richardson /* initialize buffers */
139*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
140*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
141*a9de470cSBruce Richardson
142*a9de470cSBruce Richardson /*
143*a9de470cSBruce Richardson * initialize circular buffer
144*a9de470cSBruce Richardson */
145*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
146*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
147*a9de470cSBruce Richardson return -1;
148*a9de470cSBruce Richardson }
149*a9de470cSBruce Richardson
150*a9de470cSBruce Richardson /* add string to head */
151*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
152*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
153*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
154*a9de470cSBruce Richardson return -1;
155*a9de470cSBruce Richardson }
156*a9de470cSBruce Richardson /* read string from head */
157*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
158*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
159*a9de470cSBruce Richardson printf("Error: failed to get string from head!\n");
160*a9de470cSBruce Richardson return -1;
161*a9de470cSBruce Richardson }
162*a9de470cSBruce Richardson /* verify string */
163*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
164*a9de470cSBruce Richardson printf("Error: head strings do not match!\n");
165*a9de470cSBruce Richardson return -1;
166*a9de470cSBruce Richardson }
167*a9de470cSBruce Richardson /* clear tmp buffer */
168*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
169*a9de470cSBruce Richardson /* read string from tail */
170*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD))
171*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
172*a9de470cSBruce Richardson printf("Error: failed to get string from head!\n");
173*a9de470cSBruce Richardson return -1;
174*a9de470cSBruce Richardson }
175*a9de470cSBruce Richardson /* verify string */
176*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) {
177*a9de470cSBruce Richardson printf("Error: head strings do not match!\n");
178*a9de470cSBruce Richardson return -1;
179*a9de470cSBruce Richardson }
180*a9de470cSBruce Richardson /* delete string from head*/
181*a9de470cSBruce Richardson if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
182*a9de470cSBruce Richardson printf("Error: failed to delete string from head!\n");
183*a9de470cSBruce Richardson return -1;
184*a9de470cSBruce Richardson }
185*a9de470cSBruce Richardson /* verify string was deleted */
186*a9de470cSBruce Richardson if (cirbuf_del_head_safe(&cb) == 0) {
187*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
188*a9de470cSBruce Richardson return -1;
189*a9de470cSBruce Richardson }
190*a9de470cSBruce Richardson /* clear tmp buffer */
191*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
192*a9de470cSBruce Richardson
193*a9de470cSBruce Richardson
194*a9de470cSBruce Richardson
195*a9de470cSBruce Richardson /*
196*a9de470cSBruce Richardson * reinitialize circular buffer
197*a9de470cSBruce Richardson */
198*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
199*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
200*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
201*a9de470cSBruce Richardson return -1;
202*a9de470cSBruce Richardson }
203*a9de470cSBruce Richardson
204*a9de470cSBruce Richardson /* add string to tail */
205*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
206*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
207*a9de470cSBruce Richardson printf("Error: failed to add string to tail!\n");
208*a9de470cSBruce Richardson return -1;
209*a9de470cSBruce Richardson }
210*a9de470cSBruce Richardson /* get string from tail */
211*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
212*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
213*a9de470cSBruce Richardson printf("Error: failed to get string from tail!\n");
214*a9de470cSBruce Richardson return -1;
215*a9de470cSBruce Richardson }
216*a9de470cSBruce Richardson /* verify string */
217*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
218*a9de470cSBruce Richardson printf("Error: tail strings do not match!\n");
219*a9de470cSBruce Richardson return -1;
220*a9de470cSBruce Richardson }
221*a9de470cSBruce Richardson /* clear tmp buffer */
222*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
223*a9de470cSBruce Richardson /* get string from head */
224*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL))
225*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
226*a9de470cSBruce Richardson printf("Error: failed to get string from tail!\n");
227*a9de470cSBruce Richardson return -1;
228*a9de470cSBruce Richardson }
229*a9de470cSBruce Richardson /* verify string */
230*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) {
231*a9de470cSBruce Richardson printf("Error: tail strings do not match!\n");
232*a9de470cSBruce Richardson return -1;
233*a9de470cSBruce Richardson }
234*a9de470cSBruce Richardson /* delete string from tail */
235*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
236*a9de470cSBruce Richardson printf("Error: failed to delete string from tail!\n");
237*a9de470cSBruce Richardson return -1;
238*a9de470cSBruce Richardson }
239*a9de470cSBruce Richardson /* verify string was deleted */
240*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(&cb) == 0) {
241*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
242*a9de470cSBruce Richardson return -1;
243*a9de470cSBruce Richardson }
244*a9de470cSBruce Richardson
245*a9de470cSBruce Richardson return 0;
246*a9de470cSBruce Richardson }
247*a9de470cSBruce Richardson
248*a9de470cSBruce Richardson /* test adding from head and deleting from tail, and vice versa */
249*a9de470cSBruce Richardson static int
test_cirbuf_string_add_del_reverse(void)250*a9de470cSBruce Richardson test_cirbuf_string_add_del_reverse(void)
251*a9de470cSBruce Richardson {
252*a9de470cSBruce Richardson struct cirbuf cb;
253*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
254*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
255*a9de470cSBruce Richardson
256*a9de470cSBruce Richardson /* initialize buffers */
257*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
258*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
259*a9de470cSBruce Richardson
260*a9de470cSBruce Richardson /*
261*a9de470cSBruce Richardson * initialize circular buffer
262*a9de470cSBruce Richardson */
263*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
264*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
265*a9de470cSBruce Richardson return -1;
266*a9de470cSBruce Richardson }
267*a9de470cSBruce Richardson
268*a9de470cSBruce Richardson /* add string to head */
269*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
270*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
271*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
272*a9de470cSBruce Richardson return -1;
273*a9de470cSBruce Richardson }
274*a9de470cSBruce Richardson /* delete string from tail */
275*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) {
276*a9de470cSBruce Richardson printf("Error: failed to delete string from tail!\n");
277*a9de470cSBruce Richardson return -1;
278*a9de470cSBruce Richardson }
279*a9de470cSBruce Richardson /* verify string was deleted */
280*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(&cb) == 0) {
281*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
282*a9de470cSBruce Richardson return -1;
283*a9de470cSBruce Richardson }
284*a9de470cSBruce Richardson /* clear tmp buffer */
285*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
286*a9de470cSBruce Richardson
287*a9de470cSBruce Richardson /*
288*a9de470cSBruce Richardson * reinitialize circular buffer
289*a9de470cSBruce Richardson */
290*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
291*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
292*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
293*a9de470cSBruce Richardson return -1;
294*a9de470cSBruce Richardson }
295*a9de470cSBruce Richardson
296*a9de470cSBruce Richardson /* add string to tail */
297*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
298*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
299*a9de470cSBruce Richardson printf("Error: failed to add string to tail!\n");
300*a9de470cSBruce Richardson return -1;
301*a9de470cSBruce Richardson }
302*a9de470cSBruce Richardson /* delete string from head */
303*a9de470cSBruce Richardson if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) {
304*a9de470cSBruce Richardson printf("Error: failed to delete string from head!\n");
305*a9de470cSBruce Richardson return -1;
306*a9de470cSBruce Richardson }
307*a9de470cSBruce Richardson /* verify string was deleted */
308*a9de470cSBruce Richardson if (cirbuf_del_head_safe(&cb) == 0) {
309*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
310*a9de470cSBruce Richardson return -1;
311*a9de470cSBruce Richardson }
312*a9de470cSBruce Richardson
313*a9de470cSBruce Richardson return 0;
314*a9de470cSBruce Richardson }
315*a9de470cSBruce Richardson
316*a9de470cSBruce Richardson /* try to write more than available */
317*a9de470cSBruce Richardson static int
test_cirbuf_string_add_boundaries(void)318*a9de470cSBruce Richardson test_cirbuf_string_add_boundaries(void)
319*a9de470cSBruce Richardson {
320*a9de470cSBruce Richardson struct cirbuf cb;
321*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
322*a9de470cSBruce Richardson unsigned i;
323*a9de470cSBruce Richardson
324*a9de470cSBruce Richardson /* initialize buffers */
325*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
326*a9de470cSBruce Richardson
327*a9de470cSBruce Richardson /*
328*a9de470cSBruce Richardson * initialize circular buffer
329*a9de470cSBruce Richardson */
330*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
331*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
332*a9de470cSBruce Richardson return -1;
333*a9de470cSBruce Richardson }
334*a9de470cSBruce Richardson
335*a9de470cSBruce Richardson /* fill the buffer from tail */
336*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++)
337*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
338*a9de470cSBruce Richardson
339*a9de470cSBruce Richardson /* try adding a string to tail */
340*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
341*a9de470cSBruce Richardson > 0) {
342*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
343*a9de470cSBruce Richardson return -1;
344*a9de470cSBruce Richardson }
345*a9de470cSBruce Richardson /* try adding a string to head */
346*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
347*a9de470cSBruce Richardson > 0) {
348*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
349*a9de470cSBruce Richardson return -1;
350*a9de470cSBruce Richardson }
351*a9de470cSBruce Richardson
352*a9de470cSBruce Richardson /*
353*a9de470cSBruce Richardson * reinitialize circular buffer
354*a9de470cSBruce Richardson */
355*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
356*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
357*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
358*a9de470cSBruce Richardson return -1;
359*a9de470cSBruce Richardson }
360*a9de470cSBruce Richardson
361*a9de470cSBruce Richardson /* fill the buffer from head */
362*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++)
363*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
364*a9de470cSBruce Richardson
365*a9de470cSBruce Richardson /* try adding a string to head */
366*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
367*a9de470cSBruce Richardson > 0) {
368*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
369*a9de470cSBruce Richardson return -1;
370*a9de470cSBruce Richardson }
371*a9de470cSBruce Richardson /* try adding a string to tail */
372*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
373*a9de470cSBruce Richardson > 0) {
374*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
375*a9de470cSBruce Richardson return -1;
376*a9de470cSBruce Richardson }
377*a9de470cSBruce Richardson
378*a9de470cSBruce Richardson return 0;
379*a9de470cSBruce Richardson }
380*a9de470cSBruce Richardson
381*a9de470cSBruce Richardson /* try to read/delete more than written */
382*a9de470cSBruce Richardson static int
test_cirbuf_string_get_del_boundaries(void)383*a9de470cSBruce Richardson test_cirbuf_string_get_del_boundaries(void)
384*a9de470cSBruce Richardson {
385*a9de470cSBruce Richardson struct cirbuf cb;
386*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
387*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
388*a9de470cSBruce Richardson
389*a9de470cSBruce Richardson /* initialize buffers */
390*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
391*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
392*a9de470cSBruce Richardson
393*a9de470cSBruce Richardson /*
394*a9de470cSBruce Richardson * initialize circular buffer
395*a9de470cSBruce Richardson */
396*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
397*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
398*a9de470cSBruce Richardson return -1;
399*a9de470cSBruce Richardson }
400*a9de470cSBruce Richardson
401*a9de470cSBruce Richardson
402*a9de470cSBruce Richardson /* add string to head */
403*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
404*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
405*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
406*a9de470cSBruce Richardson return -1;
407*a9de470cSBruce Richardson }
408*a9de470cSBruce Richardson /* read more than written (head) */
409*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
410*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD)) {
411*a9de470cSBruce Richardson printf("Error: unexpected result when reading too much data!\n");
412*a9de470cSBruce Richardson return -1;
413*a9de470cSBruce Richardson }
414*a9de470cSBruce Richardson /* read more than written (tail) */
415*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1)
416*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD)) {
417*a9de470cSBruce Richardson printf("Error: unexpected result when reading too much data!\n");
418*a9de470cSBruce Richardson return -1;
419*a9de470cSBruce Richardson }
420*a9de470cSBruce Richardson /* delete more than written (head) */
421*a9de470cSBruce Richardson if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
422*a9de470cSBruce Richardson printf("Error: unexpected result when deleting too much data!\n");
423*a9de470cSBruce Richardson return -1;
424*a9de470cSBruce Richardson }
425*a9de470cSBruce Richardson /* delete more than written (tail) */
426*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) {
427*a9de470cSBruce Richardson printf("Error: unexpected result when deleting too much data!\n");
428*a9de470cSBruce Richardson return -1;
429*a9de470cSBruce Richardson }
430*a9de470cSBruce Richardson
431*a9de470cSBruce Richardson /*
432*a9de470cSBruce Richardson * reinitialize circular buffer
433*a9de470cSBruce Richardson */
434*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
435*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
436*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
437*a9de470cSBruce Richardson return -1;
438*a9de470cSBruce Richardson }
439*a9de470cSBruce Richardson
440*a9de470cSBruce Richardson /* add string to tail */
441*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL))
442*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_TAIL))) {
443*a9de470cSBruce Richardson printf("Error: failed to add string to tail!\n");
444*a9de470cSBruce Richardson return -1;
445*a9de470cSBruce Richardson }
446*a9de470cSBruce Richardson /* read more than written (tail) */
447*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
448*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_TAIL)) {
449*a9de470cSBruce Richardson printf("Error: unexpected result when reading too much data!\n");
450*a9de470cSBruce Richardson return -1;
451*a9de470cSBruce Richardson }
452*a9de470cSBruce Richardson /* read more than written (head) */
453*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1)
454*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_TAIL)) {
455*a9de470cSBruce Richardson printf("Error: unexpected result when reading too much data!\n");
456*a9de470cSBruce Richardson return -1;
457*a9de470cSBruce Richardson }
458*a9de470cSBruce Richardson /* delete more than written (tail) */
459*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
460*a9de470cSBruce Richardson printf("Error: unexpected result when deleting too much data!\n");
461*a9de470cSBruce Richardson return -1;
462*a9de470cSBruce Richardson }
463*a9de470cSBruce Richardson /* delete more than written (head) */
464*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) {
465*a9de470cSBruce Richardson printf("Error: unexpected result when deleting too much data!\n");
466*a9de470cSBruce Richardson return -1;
467*a9de470cSBruce Richardson }
468*a9de470cSBruce Richardson
469*a9de470cSBruce Richardson return 0;
470*a9de470cSBruce Richardson }
471*a9de470cSBruce Richardson
472*a9de470cSBruce Richardson /* try to read/delete less than written */
473*a9de470cSBruce Richardson static int
test_cirbuf_string_get_del_partial(void)474*a9de470cSBruce Richardson test_cirbuf_string_get_del_partial(void)
475*a9de470cSBruce Richardson {
476*a9de470cSBruce Richardson struct cirbuf cb;
477*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
478*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
479*a9de470cSBruce Richardson char tmp2[CMDLINE_TEST_BUFSIZE];
480*a9de470cSBruce Richardson
481*a9de470cSBruce Richardson /* initialize buffers */
482*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
483*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
484*a9de470cSBruce Richardson memset(tmp2, 0, sizeof(tmp));
485*a9de470cSBruce Richardson
486*a9de470cSBruce Richardson strlcpy(tmp2, CIRBUF_STR_HEAD, sizeof(tmp2));
487*a9de470cSBruce Richardson
488*a9de470cSBruce Richardson /*
489*a9de470cSBruce Richardson * initialize circular buffer
490*a9de470cSBruce Richardson */
491*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
492*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
493*a9de470cSBruce Richardson return -1;
494*a9de470cSBruce Richardson }
495*a9de470cSBruce Richardson
496*a9de470cSBruce Richardson /* add string to head */
497*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD))
498*a9de470cSBruce Richardson != (sizeof(CIRBUF_STR_HEAD))) {
499*a9de470cSBruce Richardson printf("Error: failed to add string to head!\n");
500*a9de470cSBruce Richardson return -1;
501*a9de470cSBruce Richardson }
502*a9de470cSBruce Richardson /* read less than written (head) */
503*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
504*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD) - 1) {
505*a9de470cSBruce Richardson printf("Error: unexpected result when reading from head!\n");
506*a9de470cSBruce Richardson return -1;
507*a9de470cSBruce Richardson }
508*a9de470cSBruce Richardson /* verify string */
509*a9de470cSBruce Richardson if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
510*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
511*a9de470cSBruce Richardson return -1;
512*a9de470cSBruce Richardson }
513*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
514*a9de470cSBruce Richardson /* read less than written (tail) */
515*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
516*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD) - 1) {
517*a9de470cSBruce Richardson printf("Error: unexpected result when reading from tail!\n");
518*a9de470cSBruce Richardson return -1;
519*a9de470cSBruce Richardson }
520*a9de470cSBruce Richardson /* verify string */
521*a9de470cSBruce Richardson if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
522*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
523*a9de470cSBruce Richardson return -1;
524*a9de470cSBruce Richardson }
525*a9de470cSBruce Richardson
526*a9de470cSBruce Richardson /*
527*a9de470cSBruce Richardson * verify correct deletion
528*a9de470cSBruce Richardson */
529*a9de470cSBruce Richardson
530*a9de470cSBruce Richardson /* clear buffer */
531*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
532*a9de470cSBruce Richardson
533*a9de470cSBruce Richardson /* delete less than written (head) */
534*a9de470cSBruce Richardson if (cirbuf_del_buf_head(&cb, 1) != 0) {
535*a9de470cSBruce Richardson printf("Error: delete from head failed!\n");
536*a9de470cSBruce Richardson return -1;
537*a9de470cSBruce Richardson }
538*a9de470cSBruce Richardson /* read from head */
539*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1)
540*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD) - 1) {
541*a9de470cSBruce Richardson printf("Error: unexpected result when reading from head!\n");
542*a9de470cSBruce Richardson return -1;
543*a9de470cSBruce Richardson }
544*a9de470cSBruce Richardson /* since we deleted from head, first char should be deleted */
545*a9de470cSBruce Richardson if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) {
546*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
547*a9de470cSBruce Richardson return -1;
548*a9de470cSBruce Richardson }
549*a9de470cSBruce Richardson /* clear buffer */
550*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
551*a9de470cSBruce Richardson
552*a9de470cSBruce Richardson /* delete less than written (tail) */
553*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, 1) != 0) {
554*a9de470cSBruce Richardson printf("Error: delete from tail failed!\n");
555*a9de470cSBruce Richardson return -1;
556*a9de470cSBruce Richardson }
557*a9de470cSBruce Richardson /* read from tail */
558*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2)
559*a9de470cSBruce Richardson != sizeof(CIRBUF_STR_HEAD) - 2) {
560*a9de470cSBruce Richardson printf("Error: unexpected result when reading from head!\n");
561*a9de470cSBruce Richardson return -1;
562*a9de470cSBruce Richardson }
563*a9de470cSBruce Richardson /* since we deleted from tail, last char should be deleted */
564*a9de470cSBruce Richardson if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) {
565*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
566*a9de470cSBruce Richardson return -1;
567*a9de470cSBruce Richardson }
568*a9de470cSBruce Richardson
569*a9de470cSBruce Richardson return 0;
570*a9de470cSBruce Richardson }
571*a9de470cSBruce Richardson
572*a9de470cSBruce Richardson /* test cmdline_cirbuf char add/del functions */
573*a9de470cSBruce Richardson static int
test_cirbuf_char_add_del(void)574*a9de470cSBruce Richardson test_cirbuf_char_add_del(void)
575*a9de470cSBruce Richardson {
576*a9de470cSBruce Richardson struct cirbuf cb;
577*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
578*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
579*a9de470cSBruce Richardson
580*a9de470cSBruce Richardson /* clear buffer */
581*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
582*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
583*a9de470cSBruce Richardson
584*a9de470cSBruce Richardson /*
585*a9de470cSBruce Richardson * initialize circular buffer
586*a9de470cSBruce Richardson */
587*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
588*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
589*a9de470cSBruce Richardson return -1;
590*a9de470cSBruce Richardson }
591*a9de470cSBruce Richardson
592*a9de470cSBruce Richardson /*
593*a9de470cSBruce Richardson * try to delete something from cirbuf. since it's empty,
594*a9de470cSBruce Richardson * these should fail.
595*a9de470cSBruce Richardson */
596*a9de470cSBruce Richardson if (cirbuf_del_head_safe(&cb) == 0) {
597*a9de470cSBruce Richardson printf("Error: deleting from empty cirbuf head succeeded!\n");
598*a9de470cSBruce Richardson return -1;
599*a9de470cSBruce Richardson }
600*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(&cb) == 0) {
601*a9de470cSBruce Richardson printf("Error: deleting from empty cirbuf tail succeeded!\n");
602*a9de470cSBruce Richardson return -1;
603*a9de470cSBruce Richardson }
604*a9de470cSBruce Richardson
605*a9de470cSBruce Richardson /*
606*a9de470cSBruce Richardson * add, verify and delete. these should pass.
607*a9de470cSBruce Richardson */
608*a9de470cSBruce Richardson if (cirbuf_add_head_safe(&cb,'h') < 0) {
609*a9de470cSBruce Richardson printf("Error: adding to cirbuf head failed!\n");
610*a9de470cSBruce Richardson return -1;
611*a9de470cSBruce Richardson }
612*a9de470cSBruce Richardson if (cirbuf_get_head(&cb) != 'h') {
613*a9de470cSBruce Richardson printf("Error: wrong head content!\n");
614*a9de470cSBruce Richardson return -1;
615*a9de470cSBruce Richardson }
616*a9de470cSBruce Richardson if (cirbuf_del_head_safe(&cb) < 0) {
617*a9de470cSBruce Richardson printf("Error: deleting from cirbuf head failed!\n");
618*a9de470cSBruce Richardson return -1;
619*a9de470cSBruce Richardson }
620*a9de470cSBruce Richardson if (cirbuf_add_tail_safe(&cb,'t') < 0) {
621*a9de470cSBruce Richardson printf("Error: adding to cirbuf tail failed!\n");
622*a9de470cSBruce Richardson return -1;
623*a9de470cSBruce Richardson }
624*a9de470cSBruce Richardson if (cirbuf_get_tail(&cb) != 't') {
625*a9de470cSBruce Richardson printf("Error: wrong tail content!\n");
626*a9de470cSBruce Richardson return -1;
627*a9de470cSBruce Richardson }
628*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(&cb) < 0) {
629*a9de470cSBruce Richardson printf("Error: deleting from cirbuf tail failed!\n");
630*a9de470cSBruce Richardson return -1;
631*a9de470cSBruce Richardson }
632*a9de470cSBruce Richardson /* do the same for unsafe versions. those are void. */
633*a9de470cSBruce Richardson cirbuf_add_head(&cb,'h');
634*a9de470cSBruce Richardson if (cirbuf_get_head(&cb) != 'h') {
635*a9de470cSBruce Richardson printf("Error: wrong head content!\n");
636*a9de470cSBruce Richardson return -1;
637*a9de470cSBruce Richardson }
638*a9de470cSBruce Richardson cirbuf_del_head(&cb);
639*a9de470cSBruce Richardson
640*a9de470cSBruce Richardson /* test if char has been deleted. we can't call cirbuf_get_head
641*a9de470cSBruce Richardson * because it's unsafe, but we can call cirbuf_get_buf_head.
642*a9de470cSBruce Richardson */
643*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp, 1) > 0) {
644*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
645*a9de470cSBruce Richardson return -1;
646*a9de470cSBruce Richardson }
647*a9de470cSBruce Richardson
648*a9de470cSBruce Richardson cirbuf_add_tail(&cb,'t');
649*a9de470cSBruce Richardson if (cirbuf_get_tail(&cb) != 't') {
650*a9de470cSBruce Richardson printf("Error: wrong tail content!\n");
651*a9de470cSBruce Richardson return -1;
652*a9de470cSBruce Richardson }
653*a9de470cSBruce Richardson cirbuf_del_tail(&cb);
654*a9de470cSBruce Richardson
655*a9de470cSBruce Richardson /* test if char has been deleted. we can't call cirbuf_get_tail
656*a9de470cSBruce Richardson * because it's unsafe, but we can call cirbuf_get_buf_tail.
657*a9de470cSBruce Richardson */
658*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp, 1) > 0) {
659*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
660*a9de470cSBruce Richardson return -1;
661*a9de470cSBruce Richardson }
662*a9de470cSBruce Richardson
663*a9de470cSBruce Richardson return 0;
664*a9de470cSBruce Richardson }
665*a9de470cSBruce Richardson
666*a9de470cSBruce Richardson /* test filling up buffer with chars */
667*a9de470cSBruce Richardson static int
test_cirbuf_char_fill(void)668*a9de470cSBruce Richardson test_cirbuf_char_fill(void)
669*a9de470cSBruce Richardson {
670*a9de470cSBruce Richardson struct cirbuf cb;
671*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
672*a9de470cSBruce Richardson unsigned i;
673*a9de470cSBruce Richardson
674*a9de470cSBruce Richardson /* clear buffer */
675*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
676*a9de470cSBruce Richardson
677*a9de470cSBruce Richardson /*
678*a9de470cSBruce Richardson * initialize circular buffer
679*a9de470cSBruce Richardson */
680*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
681*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
682*a9de470cSBruce Richardson return -1;
683*a9de470cSBruce Richardson }
684*a9de470cSBruce Richardson
685*a9de470cSBruce Richardson /*
686*a9de470cSBruce Richardson * fill the buffer from head or tail, verify contents, test boundaries
687*a9de470cSBruce Richardson * and clear the buffer
688*a9de470cSBruce Richardson */
689*a9de470cSBruce Richardson
690*a9de470cSBruce Richardson /* fill the buffer from tail */
691*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
692*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
693*a9de470cSBruce Richardson /* verify that contents of the buffer are what they are supposed to be */
694*a9de470cSBruce Richardson for (i = 0; i < sizeof(buf); i++) {
695*a9de470cSBruce Richardson if (buf[i] != 't') {
696*a9de470cSBruce Richardson printf("Error: wrong content in buffer!\n");
697*a9de470cSBruce Richardson return -1;
698*a9de470cSBruce Richardson }
699*a9de470cSBruce Richardson }
700*a9de470cSBruce Richardson /* try to add to a full buffer from tail */
701*a9de470cSBruce Richardson if (cirbuf_add_tail_safe(&cb, 't') == 0) {
702*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
703*a9de470cSBruce Richardson return -1;
704*a9de470cSBruce Richardson }
705*a9de470cSBruce Richardson /* try to add to a full buffer from head */
706*a9de470cSBruce Richardson if (cirbuf_add_head_safe(&cb, 'h') == 0) {
707*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
708*a9de470cSBruce Richardson return -1;
709*a9de470cSBruce Richardson }
710*a9de470cSBruce Richardson /* delete buffer from tail */
711*a9de470cSBruce Richardson for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
712*a9de470cSBruce Richardson cirbuf_del_tail_safe(&cb);
713*a9de470cSBruce Richardson /* try to delete from an empty buffer */
714*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(&cb) >= 0) {
715*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
716*a9de470cSBruce Richardson return -1;
717*a9de470cSBruce Richardson }
718*a9de470cSBruce Richardson
719*a9de470cSBruce Richardson /* fill the buffer from head */
720*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
721*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
722*a9de470cSBruce Richardson /* verify that contents of the buffer are what they are supposed to be */
723*a9de470cSBruce Richardson for (i = 0; i < sizeof(buf); i++) {
724*a9de470cSBruce Richardson if (buf[i] != 'h') {
725*a9de470cSBruce Richardson printf("Error: wrong content in buffer!\n");
726*a9de470cSBruce Richardson return -1;
727*a9de470cSBruce Richardson }
728*a9de470cSBruce Richardson }
729*a9de470cSBruce Richardson /* try to add to a full buffer from head */
730*a9de470cSBruce Richardson if (cirbuf_add_head_safe(&cb,'h') >= 0) {
731*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
732*a9de470cSBruce Richardson return -1;
733*a9de470cSBruce Richardson }
734*a9de470cSBruce Richardson /* try to add to a full buffer from tail */
735*a9de470cSBruce Richardson if (cirbuf_add_tail_safe(&cb, 't') == 0) {
736*a9de470cSBruce Richardson printf("Error: buffer should have been full!\n");
737*a9de470cSBruce Richardson return -1;
738*a9de470cSBruce Richardson }
739*a9de470cSBruce Richardson /* delete buffer from head */
740*a9de470cSBruce Richardson for(i = 0; i < CMDLINE_TEST_BUFSIZE; i++)
741*a9de470cSBruce Richardson cirbuf_del_head_safe(&cb);
742*a9de470cSBruce Richardson /* try to delete from an empty buffer */
743*a9de470cSBruce Richardson if (cirbuf_del_head_safe(&cb) >= 0) {
744*a9de470cSBruce Richardson printf("Error: buffer should have been empty!\n");
745*a9de470cSBruce Richardson return -1;
746*a9de470cSBruce Richardson }
747*a9de470cSBruce Richardson
748*a9de470cSBruce Richardson /*
749*a9de470cSBruce Richardson * fill the buffer from both head and tail, with alternating characters,
750*a9de470cSBruce Richardson * verify contents and clear the buffer
751*a9de470cSBruce Richardson */
752*a9de470cSBruce Richardson
753*a9de470cSBruce Richardson /* fill half of buffer from tail */
754*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
755*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, (char) (i % 2 ? 't' : 'T'));
756*a9de470cSBruce Richardson /* fill other half of the buffer from head */
757*a9de470cSBruce Richardson for (i = 0; i < CMDLINE_TEST_BUFSIZE / 2; i++)
758*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, (char) (i % 2 ? 'H' : 'h')); /* added in reverse */
759*a9de470cSBruce Richardson
760*a9de470cSBruce Richardson /* verify that contents of the buffer are what they are supposed to be */
761*a9de470cSBruce Richardson for (i = 0; i < sizeof(buf) / 2; i++) {
762*a9de470cSBruce Richardson if (buf[i] != (char) (i % 2 ? 't' : 'T')) {
763*a9de470cSBruce Richardson printf("Error: wrong content in buffer at %u!\n", i);
764*a9de470cSBruce Richardson return -1;
765*a9de470cSBruce Richardson }
766*a9de470cSBruce Richardson }
767*a9de470cSBruce Richardson for (i = sizeof(buf) / 2; i < sizeof(buf); i++) {
768*a9de470cSBruce Richardson if (buf[i] != (char) (i % 2 ? 'h' : 'H')) {
769*a9de470cSBruce Richardson printf("Error: wrong content in buffer %u!\n", i);
770*a9de470cSBruce Richardson return -1;
771*a9de470cSBruce Richardson }
772*a9de470cSBruce Richardson }
773*a9de470cSBruce Richardson
774*a9de470cSBruce Richardson return 0;
775*a9de470cSBruce Richardson }
776*a9de470cSBruce Richardson
777*a9de470cSBruce Richardson /* test left alignment */
778*a9de470cSBruce Richardson static int
test_cirbuf_align_left(void)779*a9de470cSBruce Richardson test_cirbuf_align_left(void)
780*a9de470cSBruce Richardson {
781*a9de470cSBruce Richardson #define HALF_OFFSET CMDLINE_TEST_BUFSIZE / 2
782*a9de470cSBruce Richardson #define SMALL_OFFSET HALF_OFFSET / 2
783*a9de470cSBruce Richardson /* resulting buffer lengths for each of the test cases */
784*a9de470cSBruce Richardson #define LEN1 HALF_OFFSET - SMALL_OFFSET - 1
785*a9de470cSBruce Richardson #define LEN2 HALF_OFFSET + SMALL_OFFSET + 2
786*a9de470cSBruce Richardson #define LEN3 HALF_OFFSET - SMALL_OFFSET
787*a9de470cSBruce Richardson #define LEN4 HALF_OFFSET + SMALL_OFFSET - 1
788*a9de470cSBruce Richardson
789*a9de470cSBruce Richardson struct cirbuf cb;
790*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
791*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
792*a9de470cSBruce Richardson unsigned i;
793*a9de470cSBruce Richardson
794*a9de470cSBruce Richardson /*
795*a9de470cSBruce Richardson * align left when start < end and start in left half
796*a9de470cSBruce Richardson */
797*a9de470cSBruce Richardson
798*a9de470cSBruce Richardson /*
799*a9de470cSBruce Richardson * initialize circular buffer
800*a9de470cSBruce Richardson */
801*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
802*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
803*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
804*a9de470cSBruce Richardson return -1;
805*a9de470cSBruce Richardson }
806*a9de470cSBruce Richardson
807*a9de470cSBruce Richardson /* push end into left half */
808*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET - 1; i++)
809*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
810*a9de470cSBruce Richardson
811*a9de470cSBruce Richardson /* push start into left half < end */
812*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
813*a9de470cSBruce Richardson cirbuf_del_head_safe(&cb);
814*a9de470cSBruce Richardson
815*a9de470cSBruce Richardson /* align */
816*a9de470cSBruce Richardson if (cirbuf_align_left(&cb) < 0) {
817*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
818*a9de470cSBruce Richardson return -1;
819*a9de470cSBruce Richardson }
820*a9de470cSBruce Richardson
821*a9de470cSBruce Richardson /* verify result */
822*a9de470cSBruce Richardson if (cb.start != 0 || cb.len != LEN1 || cb.end != cb.len - 1) {
823*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!\n");
824*a9de470cSBruce Richardson return -1;
825*a9de470cSBruce Richardson }
826*a9de470cSBruce Richardson
827*a9de470cSBruce Richardson /*
828*a9de470cSBruce Richardson * align left when start > end and start in left half
829*a9de470cSBruce Richardson */
830*a9de470cSBruce Richardson
831*a9de470cSBruce Richardson /*
832*a9de470cSBruce Richardson * reinitialize circular buffer
833*a9de470cSBruce Richardson */
834*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
835*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
836*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
837*a9de470cSBruce Richardson return -1;
838*a9de470cSBruce Richardson }
839*a9de470cSBruce Richardson
840*a9de470cSBruce Richardson /* push start into left half */
841*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET + 2; i++)
842*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
843*a9de470cSBruce Richardson
844*a9de470cSBruce Richardson /* push end into left half > start */
845*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
846*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
847*a9de470cSBruce Richardson
848*a9de470cSBruce Richardson /* align */
849*a9de470cSBruce Richardson if (cirbuf_align_left(&cb) < 0) {
850*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
851*a9de470cSBruce Richardson return -1;
852*a9de470cSBruce Richardson }
853*a9de470cSBruce Richardson
854*a9de470cSBruce Richardson /* verify result */
855*a9de470cSBruce Richardson if (cb.start != 0 || cb.len != LEN2 || cb.end != cb.len - 1) {
856*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
857*a9de470cSBruce Richardson return -1;
858*a9de470cSBruce Richardson }
859*a9de470cSBruce Richardson
860*a9de470cSBruce Richardson /*
861*a9de470cSBruce Richardson * align left when start < end and start in right half
862*a9de470cSBruce Richardson */
863*a9de470cSBruce Richardson
864*a9de470cSBruce Richardson /*
865*a9de470cSBruce Richardson * reinitialize circular buffer
866*a9de470cSBruce Richardson */
867*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
868*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
869*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
870*a9de470cSBruce Richardson return -1;
871*a9de470cSBruce Richardson }
872*a9de470cSBruce Richardson
873*a9de470cSBruce Richardson /* push start into the right half */
874*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET; i++)
875*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
876*a9de470cSBruce Richardson
877*a9de470cSBruce Richardson /* push end into left half > start */
878*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
879*a9de470cSBruce Richardson cirbuf_del_tail_safe(&cb);
880*a9de470cSBruce Richardson
881*a9de470cSBruce Richardson /* align */
882*a9de470cSBruce Richardson if (cirbuf_align_left(&cb) < 0) {
883*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
884*a9de470cSBruce Richardson return -1;
885*a9de470cSBruce Richardson }
886*a9de470cSBruce Richardson
887*a9de470cSBruce Richardson /* verify result */
888*a9de470cSBruce Richardson if (cb.start != 0 || cb.len != LEN3 || cb.end != cb.len - 1) {
889*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
890*a9de470cSBruce Richardson return -1;
891*a9de470cSBruce Richardson }
892*a9de470cSBruce Richardson
893*a9de470cSBruce Richardson /*
894*a9de470cSBruce Richardson * align left when start > end and start in right half
895*a9de470cSBruce Richardson */
896*a9de470cSBruce Richardson
897*a9de470cSBruce Richardson /*
898*a9de470cSBruce Richardson * reinitialize circular buffer
899*a9de470cSBruce Richardson */
900*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
901*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
902*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
903*a9de470cSBruce Richardson return -1;
904*a9de470cSBruce Richardson }
905*a9de470cSBruce Richardson
906*a9de470cSBruce Richardson /* push start into the right half */
907*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET - 1; i++)
908*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
909*a9de470cSBruce Richardson
910*a9de470cSBruce Richardson /* push end into left half < start */
911*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
912*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
913*a9de470cSBruce Richardson
914*a9de470cSBruce Richardson /* align */
915*a9de470cSBruce Richardson if (cirbuf_align_left(&cb) < 0) {
916*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
917*a9de470cSBruce Richardson return -1;
918*a9de470cSBruce Richardson }
919*a9de470cSBruce Richardson
920*a9de470cSBruce Richardson /* verify result */
921*a9de470cSBruce Richardson if (cb.start != 0 || cb.len != LEN4 ||
922*a9de470cSBruce Richardson cb.end != cb.len - 1) {
923*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
924*a9de470cSBruce Richardson return -1;
925*a9de470cSBruce Richardson }
926*a9de470cSBruce Richardson
927*a9de470cSBruce Richardson /*
928*a9de470cSBruce Richardson * Verify that alignment doesn't corrupt data
929*a9de470cSBruce Richardson */
930*a9de470cSBruce Richardson
931*a9de470cSBruce Richardson /*
932*a9de470cSBruce Richardson * reinitialize circular buffer
933*a9de470cSBruce Richardson */
934*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
935*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
936*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
937*a9de470cSBruce Richardson return -1;
938*a9de470cSBruce Richardson }
939*a9de470cSBruce Richardson
940*a9de470cSBruce Richardson /* add string to tail and head */
941*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD,
942*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD)) < 0 || cirbuf_add_buf_tail(&cb,
943*a9de470cSBruce Richardson CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0) {
944*a9de470cSBruce Richardson printf("Error: failed to add strings!\n");
945*a9de470cSBruce Richardson return -1;
946*a9de470cSBruce Richardson }
947*a9de470cSBruce Richardson
948*a9de470cSBruce Richardson /* align */
949*a9de470cSBruce Richardson if (cirbuf_align_left(&cb) < 0) {
950*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
951*a9de470cSBruce Richardson return -1;
952*a9de470cSBruce Richardson }
953*a9de470cSBruce Richardson
954*a9de470cSBruce Richardson /* get string from head */
955*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp,
956*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
957*a9de470cSBruce Richardson printf("Error: failed to read string from head!\n");
958*a9de470cSBruce Richardson return -1;
959*a9de470cSBruce Richardson }
960*a9de470cSBruce Richardson
961*a9de470cSBruce Richardson /* verify string */
962*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
963*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
964*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
965*a9de470cSBruce Richardson return -1;
966*a9de470cSBruce Richardson }
967*a9de470cSBruce Richardson
968*a9de470cSBruce Richardson /* reset tmp buffer */
969*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
970*a9de470cSBruce Richardson
971*a9de470cSBruce Richardson /* get string from tail */
972*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp,
973*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
974*a9de470cSBruce Richardson printf("Error: failed to read string from head!\n");
975*a9de470cSBruce Richardson return -1;
976*a9de470cSBruce Richardson }
977*a9de470cSBruce Richardson
978*a9de470cSBruce Richardson /* verify string */
979*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
980*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
981*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
982*a9de470cSBruce Richardson return -1;
983*a9de470cSBruce Richardson }
984*a9de470cSBruce Richardson
985*a9de470cSBruce Richardson return 0;
986*a9de470cSBruce Richardson }
987*a9de470cSBruce Richardson
988*a9de470cSBruce Richardson /* test right alignment */
989*a9de470cSBruce Richardson static int
test_cirbuf_align_right(void)990*a9de470cSBruce Richardson test_cirbuf_align_right(void)
991*a9de470cSBruce Richardson {
992*a9de470cSBruce Richardson #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1
993*a9de470cSBruce Richardson struct cirbuf cb;
994*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
995*a9de470cSBruce Richardson char tmp[CMDLINE_TEST_BUFSIZE];
996*a9de470cSBruce Richardson unsigned i;
997*a9de470cSBruce Richardson
998*a9de470cSBruce Richardson
999*a9de470cSBruce Richardson /*
1000*a9de470cSBruce Richardson * align right when start < end and start in left half
1001*a9de470cSBruce Richardson */
1002*a9de470cSBruce Richardson
1003*a9de470cSBruce Richardson /*
1004*a9de470cSBruce Richardson * initialize circular buffer
1005*a9de470cSBruce Richardson */
1006*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
1007*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1008*a9de470cSBruce Richardson printf("Error: failed to initialize circular buffer!\n");
1009*a9de470cSBruce Richardson return -1;
1010*a9de470cSBruce Richardson }
1011*a9de470cSBruce Richardson
1012*a9de470cSBruce Richardson /* push end into left half */
1013*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET - 1; i++)
1014*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
1015*a9de470cSBruce Richardson
1016*a9de470cSBruce Richardson /* push start into left half < end */
1017*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
1018*a9de470cSBruce Richardson cirbuf_del_head_safe(&cb);
1019*a9de470cSBruce Richardson
1020*a9de470cSBruce Richardson /* align */
1021*a9de470cSBruce Richardson cirbuf_align_right(&cb);
1022*a9de470cSBruce Richardson
1023*a9de470cSBruce Richardson /* verify result */
1024*a9de470cSBruce Richardson if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) {
1025*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!\n");
1026*a9de470cSBruce Richardson return -1;
1027*a9de470cSBruce Richardson }
1028*a9de470cSBruce Richardson
1029*a9de470cSBruce Richardson /*
1030*a9de470cSBruce Richardson * align right when start > end and start in left half
1031*a9de470cSBruce Richardson */
1032*a9de470cSBruce Richardson
1033*a9de470cSBruce Richardson /*
1034*a9de470cSBruce Richardson * reinitialize circular buffer
1035*a9de470cSBruce Richardson */
1036*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
1037*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1038*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
1039*a9de470cSBruce Richardson return -1;
1040*a9de470cSBruce Richardson }
1041*a9de470cSBruce Richardson
1042*a9de470cSBruce Richardson /* push start into left half */
1043*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET + 2; i++)
1044*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
1045*a9de470cSBruce Richardson
1046*a9de470cSBruce Richardson /* push end into left half > start */
1047*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
1048*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
1049*a9de470cSBruce Richardson
1050*a9de470cSBruce Richardson /* align */
1051*a9de470cSBruce Richardson cirbuf_align_right(&cb);
1052*a9de470cSBruce Richardson
1053*a9de470cSBruce Richardson /* verify result */
1054*a9de470cSBruce Richardson if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) {
1055*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
1056*a9de470cSBruce Richardson return -1;
1057*a9de470cSBruce Richardson }
1058*a9de470cSBruce Richardson
1059*a9de470cSBruce Richardson /*
1060*a9de470cSBruce Richardson * align right when start < end and start in right half
1061*a9de470cSBruce Richardson */
1062*a9de470cSBruce Richardson
1063*a9de470cSBruce Richardson /*
1064*a9de470cSBruce Richardson * reinitialize circular buffer
1065*a9de470cSBruce Richardson */
1066*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
1067*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1068*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
1069*a9de470cSBruce Richardson return -1;
1070*a9de470cSBruce Richardson }
1071*a9de470cSBruce Richardson
1072*a9de470cSBruce Richardson /* push start into the right half */
1073*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET; i++)
1074*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
1075*a9de470cSBruce Richardson
1076*a9de470cSBruce Richardson /* push end into left half > start */
1077*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
1078*a9de470cSBruce Richardson cirbuf_del_tail_safe(&cb);
1079*a9de470cSBruce Richardson
1080*a9de470cSBruce Richardson /* align */
1081*a9de470cSBruce Richardson cirbuf_align_right(&cb);
1082*a9de470cSBruce Richardson
1083*a9de470cSBruce Richardson /* verify result */
1084*a9de470cSBruce Richardson if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) {
1085*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
1086*a9de470cSBruce Richardson return -1;
1087*a9de470cSBruce Richardson }
1088*a9de470cSBruce Richardson
1089*a9de470cSBruce Richardson /*
1090*a9de470cSBruce Richardson * align right when start > end and start in right half
1091*a9de470cSBruce Richardson */
1092*a9de470cSBruce Richardson
1093*a9de470cSBruce Richardson /*
1094*a9de470cSBruce Richardson * reinitialize circular buffer
1095*a9de470cSBruce Richardson */
1096*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
1097*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1098*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
1099*a9de470cSBruce Richardson return -1;
1100*a9de470cSBruce Richardson }
1101*a9de470cSBruce Richardson
1102*a9de470cSBruce Richardson /* push start into the right half */
1103*a9de470cSBruce Richardson for (i = 0; i < HALF_OFFSET - 1; i++)
1104*a9de470cSBruce Richardson cirbuf_add_head_safe(&cb, 'h');
1105*a9de470cSBruce Richardson
1106*a9de470cSBruce Richardson /* push end into left half < start */
1107*a9de470cSBruce Richardson for (i = 0; i < SMALL_OFFSET; i++)
1108*a9de470cSBruce Richardson cirbuf_add_tail_safe(&cb, 't');
1109*a9de470cSBruce Richardson
1110*a9de470cSBruce Richardson /* align */
1111*a9de470cSBruce Richardson cirbuf_align_right(&cb);
1112*a9de470cSBruce Richardson
1113*a9de470cSBruce Richardson /* verify result */
1114*a9de470cSBruce Richardson if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) {
1115*a9de470cSBruce Richardson printf("Error: buffer alignment is wrong!");
1116*a9de470cSBruce Richardson return -1;
1117*a9de470cSBruce Richardson }
1118*a9de470cSBruce Richardson
1119*a9de470cSBruce Richardson /*
1120*a9de470cSBruce Richardson * Verify that alignment doesn't corrupt data
1121*a9de470cSBruce Richardson */
1122*a9de470cSBruce Richardson
1123*a9de470cSBruce Richardson /*
1124*a9de470cSBruce Richardson * reinitialize circular buffer
1125*a9de470cSBruce Richardson */
1126*a9de470cSBruce Richardson memset(buf, 0, sizeof(buf));
1127*a9de470cSBruce Richardson if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) {
1128*a9de470cSBruce Richardson printf("Error: failed to reinitialize circular buffer!\n");
1129*a9de470cSBruce Richardson return -1;
1130*a9de470cSBruce Richardson }
1131*a9de470cSBruce Richardson
1132*a9de470cSBruce Richardson /* add string to tail and head */
1133*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL,
1134*a9de470cSBruce Richardson sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb,
1135*a9de470cSBruce Richardson CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) {
1136*a9de470cSBruce Richardson printf("Error: failed to add strings!\n");
1137*a9de470cSBruce Richardson return -1;
1138*a9de470cSBruce Richardson }
1139*a9de470cSBruce Richardson
1140*a9de470cSBruce Richardson /* align */
1141*a9de470cSBruce Richardson if (cirbuf_align_right(&cb) < 0) {
1142*a9de470cSBruce Richardson printf("Error: alignment failed!\n");
1143*a9de470cSBruce Richardson return -1;
1144*a9de470cSBruce Richardson }
1145*a9de470cSBruce Richardson
1146*a9de470cSBruce Richardson /* get string from head */
1147*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, tmp,
1148*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1149*a9de470cSBruce Richardson printf("Error: failed to read string from head!\n");
1150*a9de470cSBruce Richardson return -1;
1151*a9de470cSBruce Richardson }
1152*a9de470cSBruce Richardson
1153*a9de470cSBruce Richardson /* verify string */
1154*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1155*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1156*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
1157*a9de470cSBruce Richardson return -1;
1158*a9de470cSBruce Richardson }
1159*a9de470cSBruce Richardson
1160*a9de470cSBruce Richardson /* reset tmp buffer */
1161*a9de470cSBruce Richardson memset(tmp, 0, sizeof(tmp));
1162*a9de470cSBruce Richardson
1163*a9de470cSBruce Richardson /* get string from tail */
1164*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, tmp,
1165*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) {
1166*a9de470cSBruce Richardson printf("Error: failed to read string from head!\n");
1167*a9de470cSBruce Richardson return -1;
1168*a9de470cSBruce Richardson }
1169*a9de470cSBruce Richardson /* verify string */
1170*a9de470cSBruce Richardson if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL,
1171*a9de470cSBruce Richardson sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) {
1172*a9de470cSBruce Richardson printf("Error: strings mismatch!\n");
1173*a9de470cSBruce Richardson return -1;
1174*a9de470cSBruce Richardson }
1175*a9de470cSBruce Richardson
1176*a9de470cSBruce Richardson return 0;
1177*a9de470cSBruce Richardson }
1178*a9de470cSBruce Richardson
1179*a9de470cSBruce Richardson /* call functions with invalid parameters */
1180*a9de470cSBruce Richardson int
test_cirbuf_invalid_param(void)1181*a9de470cSBruce Richardson test_cirbuf_invalid_param(void)
1182*a9de470cSBruce Richardson {
1183*a9de470cSBruce Richardson struct cirbuf cb;
1184*a9de470cSBruce Richardson char buf[CMDLINE_TEST_BUFSIZE];
1185*a9de470cSBruce Richardson
1186*a9de470cSBruce Richardson /* null cirbuf */
1187*a9de470cSBruce Richardson if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0)
1188*a9de470cSBruce Richardson return -1;
1189*a9de470cSBruce Richardson /* null buffer */
1190*a9de470cSBruce Richardson if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0)
1191*a9de470cSBruce Richardson return -1;
1192*a9de470cSBruce Richardson /* null cirbuf */
1193*a9de470cSBruce Richardson if (cirbuf_add_head_safe(0, 'h') == 0)
1194*a9de470cSBruce Richardson return -1;
1195*a9de470cSBruce Richardson if (cirbuf_add_tail_safe(0, 't') == 0)
1196*a9de470cSBruce Richardson return -1;
1197*a9de470cSBruce Richardson if (cirbuf_del_head_safe(0) == 0)
1198*a9de470cSBruce Richardson return -1;
1199*a9de470cSBruce Richardson if (cirbuf_del_tail_safe(0) == 0)
1200*a9de470cSBruce Richardson return -1;
1201*a9de470cSBruce Richardson /* null buffer */
1202*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, 0, 0) == 0)
1203*a9de470cSBruce Richardson return -1;
1204*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, 0, 0) == 0)
1205*a9de470cSBruce Richardson return -1;
1206*a9de470cSBruce Richardson /* null cirbuf */
1207*a9de470cSBruce Richardson if (cirbuf_add_buf_head(0, buf, 0) == 0)
1208*a9de470cSBruce Richardson return -1;
1209*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(0, buf, 0) == 0)
1210*a9de470cSBruce Richardson return -1;
1211*a9de470cSBruce Richardson /* null size */
1212*a9de470cSBruce Richardson if (cirbuf_add_buf_head(&cb, buf, 0) == 0)
1213*a9de470cSBruce Richardson return -1;
1214*a9de470cSBruce Richardson if (cirbuf_add_buf_tail(&cb, buf, 0) == 0)
1215*a9de470cSBruce Richardson return -1;
1216*a9de470cSBruce Richardson /* null cirbuf */
1217*a9de470cSBruce Richardson if (cirbuf_del_buf_head(0, 0) == 0)
1218*a9de470cSBruce Richardson return -1;
1219*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(0, 0) == 0)
1220*a9de470cSBruce Richardson return -1;
1221*a9de470cSBruce Richardson /* null size */
1222*a9de470cSBruce Richardson if (cirbuf_del_buf_head(&cb, 0) == 0)
1223*a9de470cSBruce Richardson return -1;
1224*a9de470cSBruce Richardson if (cirbuf_del_buf_tail(&cb, 0) == 0)
1225*a9de470cSBruce Richardson return -1;
1226*a9de470cSBruce Richardson /* null cirbuf */
1227*a9de470cSBruce Richardson if (cirbuf_get_buf_head(0, 0, 0) == 0)
1228*a9de470cSBruce Richardson return -1;
1229*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(0, 0, 0) == 0)
1230*a9de470cSBruce Richardson return -1;
1231*a9de470cSBruce Richardson /* null buffer */
1232*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, 0, 0) == 0)
1233*a9de470cSBruce Richardson return -1;
1234*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, 0, 0) == 0)
1235*a9de470cSBruce Richardson return -1;
1236*a9de470cSBruce Richardson /* null size, this is valid but should return 0 */
1237*a9de470cSBruce Richardson if (cirbuf_get_buf_head(&cb, buf, 0) != 0)
1238*a9de470cSBruce Richardson return -1;
1239*a9de470cSBruce Richardson if (cirbuf_get_buf_tail(&cb, buf, 0) != 0)
1240*a9de470cSBruce Richardson return -1;
1241*a9de470cSBruce Richardson /* null cirbuf */
1242*a9de470cSBruce Richardson if (cirbuf_align_left(0) == 0)
1243*a9de470cSBruce Richardson return -1;
1244*a9de470cSBruce Richardson if (cirbuf_align_right(0) == 0)
1245*a9de470cSBruce Richardson return -1;
1246*a9de470cSBruce Richardson
1247*a9de470cSBruce Richardson return 0;
1248*a9de470cSBruce Richardson }
1249*a9de470cSBruce Richardson
1250*a9de470cSBruce Richardson /* test cmdline_cirbuf char functions */
1251*a9de470cSBruce Richardson int
test_cirbuf_char(void)1252*a9de470cSBruce Richardson test_cirbuf_char(void)
1253*a9de470cSBruce Richardson {
1254*a9de470cSBruce Richardson int ret;
1255*a9de470cSBruce Richardson
1256*a9de470cSBruce Richardson ret = test_cirbuf_char_add_del();
1257*a9de470cSBruce Richardson if (ret < 0)
1258*a9de470cSBruce Richardson return -1;
1259*a9de470cSBruce Richardson
1260*a9de470cSBruce Richardson ret = test_cirbuf_char_fill();
1261*a9de470cSBruce Richardson if (ret < 0)
1262*a9de470cSBruce Richardson return -1;
1263*a9de470cSBruce Richardson
1264*a9de470cSBruce Richardson return 0;
1265*a9de470cSBruce Richardson }
1266*a9de470cSBruce Richardson
1267*a9de470cSBruce Richardson /* test cmdline_cirbuf string functions */
1268*a9de470cSBruce Richardson int
test_cirbuf_string(void)1269*a9de470cSBruce Richardson test_cirbuf_string(void)
1270*a9de470cSBruce Richardson {
1271*a9de470cSBruce Richardson if (test_cirbuf_string_add_del() < 0)
1272*a9de470cSBruce Richardson return -1;
1273*a9de470cSBruce Richardson
1274*a9de470cSBruce Richardson if (test_cirbuf_string_add_del_reverse() < 0)
1275*a9de470cSBruce Richardson return -1;
1276*a9de470cSBruce Richardson
1277*a9de470cSBruce Richardson if (test_cirbuf_string_add_boundaries() < 0)
1278*a9de470cSBruce Richardson return -1;
1279*a9de470cSBruce Richardson
1280*a9de470cSBruce Richardson if (test_cirbuf_string_get_del_boundaries() < 0)
1281*a9de470cSBruce Richardson return -1;
1282*a9de470cSBruce Richardson
1283*a9de470cSBruce Richardson if (test_cirbuf_string_get_del_partial() < 0)
1284*a9de470cSBruce Richardson return -1;
1285*a9de470cSBruce Richardson
1286*a9de470cSBruce Richardson if (test_cirbuf_string_misc() < 0)
1287*a9de470cSBruce Richardson return -1;
1288*a9de470cSBruce Richardson
1289*a9de470cSBruce Richardson return 0;
1290*a9de470cSBruce Richardson }
1291*a9de470cSBruce Richardson
1292*a9de470cSBruce Richardson /* test cmdline_cirbuf align functions */
1293*a9de470cSBruce Richardson int
test_cirbuf_align(void)1294*a9de470cSBruce Richardson test_cirbuf_align(void)
1295*a9de470cSBruce Richardson {
1296*a9de470cSBruce Richardson if (test_cirbuf_align_left() < 0)
1297*a9de470cSBruce Richardson return -1;
1298*a9de470cSBruce Richardson if (test_cirbuf_align_right() < 0)
1299*a9de470cSBruce Richardson return -1;
1300*a9de470cSBruce Richardson return 0;
1301*a9de470cSBruce Richardson }
1302