2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: main.c,v 1.1.2.1 2007-08-19 10:33:55 zer0 Exp $
32 #define assert(cond) do { if (!cond) { printf("%s:%d - %s", __FILE__, __LINE__, #cond); while(1); } } while(0)
35 void dump_it(struct cirbuf * cbuf)
40 printf("sta=%2.2d end=%2.2d len=%2.2d/%2.2d { ",
41 cbuf->start, cbuf->end,
43 CIRBUF_GET_MAXLEN(cbuf));
45 for (i=0; i<CIRBUF_GET_MAXLEN(cbuf) ; i++) {
46 if (cbuf->start <= cbuf->end) {
47 if (i >= cbuf->start &&
49 CIRBUF_GET_LEN(cbuf) != 0)
50 printf("%2.2x, ", cbuf->buf[i]&0xFF);
55 if (i < cbuf->start &&
59 printf("%2.2x, ", cbuf->buf[i]&0xFF);
65 CIRBUF_FOREACH(cbuf, i, e) {
66 printf("%2.2x, ", e&0xFF);
70 if(CIRBUF_GET_LEN(cbuf) == 0) {
71 assert(cbuf->start == cbuf->end);
74 assert( ((cbuf->end - cbuf->start + 1) + cbuf->maxlen) % cbuf->maxlen ==
75 CIRBUF_GET_LEN(cbuf) % cbuf->maxlen );
83 struct cirbuf my_fifo;
86 char buf1[] = { 0x10, 0x11, 0x12 };
87 char buf2[] = { 0x20, 0x21, 0x22, 0x23 };
90 char ref_buf[] = { 0x20, 0x21, 0x22, 0x23, 0x01, 0x10, 0x11, 0x12 };
96 cirbuf_init(&my_fifo, fifo_buf, 0, 4);
97 assert(CIRBUF_IS_EMPTY(&my_fifo));
98 assert(!CIRBUF_IS_FULL(&my_fifo));
101 cirbuf_add_tail(&my_fifo, 1);
102 assert(cirbuf_get_head(&my_fifo) == 1);
103 assert(cirbuf_get_tail(&my_fifo) == 1);
107 cirbuf_add_tail(&my_fifo, 2);
108 assert(!CIRBUF_IS_EMPTY(&my_fifo));
109 assert(!CIRBUF_IS_FULL(&my_fifo));
112 cirbuf_add_tail(&my_fifo, 3);
113 assert(cirbuf_get_head(&my_fifo) == 1);
114 assert(cirbuf_get_tail(&my_fifo) == 3);
117 cirbuf_add_tail(&my_fifo, 4);
118 assert(!CIRBUF_IS_EMPTY(&my_fifo));
119 assert(CIRBUF_IS_FULL(&my_fifo));
122 cirbuf_del_tail(&my_fifo);
124 assert(cirbuf_get_tail(&my_fifo) == 3);
125 assert(cirbuf_get_head(&my_fifo) == 1);
127 cirbuf_del_head(&my_fifo);
128 assert(cirbuf_get_tail(&my_fifo) == 3);
129 assert(cirbuf_get_head(&my_fifo) == 2);
132 cirbuf_del_head(&my_fifo);
133 assert(cirbuf_get_tail(&my_fifo) == 3);
134 assert(cirbuf_get_head(&my_fifo) == 3);
137 cirbuf_del_head(&my_fifo);
138 assert(CIRBUF_IS_EMPTY(&my_fifo));
146 cirbuf_init(&my_fifo, fifo_buf, 2, 4);
149 cirbuf_add_head(&my_fifo, 4);
150 assert(cirbuf_get_head(&my_fifo) == 4);
151 assert(cirbuf_get_tail(&my_fifo) == 4);
155 cirbuf_add_head(&my_fifo, 3);
156 assert(!CIRBUF_IS_EMPTY(&my_fifo));
157 assert(!CIRBUF_IS_FULL(&my_fifo));
160 cirbuf_add_head(&my_fifo, 2);
161 assert(cirbuf_get_head(&my_fifo) == 2);
162 assert(cirbuf_get_tail(&my_fifo) == 4);
165 cirbuf_add_head(&my_fifo, 1);
166 assert(!CIRBUF_IS_EMPTY(&my_fifo));
167 assert(CIRBUF_IS_FULL(&my_fifo));
175 for (i=0 ; i<16; i++) {
176 cirbuf_init(&my_fifo, fifo_buf, i, 16);
178 cirbuf_add_buf_head(&my_fifo, buf1, sizeof(buf1));
180 cirbuf_add_head(&my_fifo, 1);
182 cirbuf_add_buf_head(&my_fifo, buf2, sizeof(buf2));
184 cirbuf_get_buf_head(&my_fifo, tmp_buf, sizeof(tmp_buf));
185 assert(memcmp(tmp_buf, ref_buf, sizeof(ref_buf)) == 0);
192 for (i=0 ; i<16; i++) {
193 cirbuf_init(&my_fifo, fifo_buf, i, 16);
195 cirbuf_add_buf_tail(&my_fifo, buf2, sizeof(buf2));
197 cirbuf_add_tail(&my_fifo, 1);
199 cirbuf_add_buf_tail(&my_fifo, buf1, sizeof(buf1));
201 cirbuf_get_buf_tail(&my_fifo, tmp_buf, sizeof(tmp_buf));
202 assert(memcmp(tmp_buf, ref_buf, sizeof(ref_buf)) == 0);
209 cirbuf_init(&my_fifo, fifo_buf, 10, 16);
212 while (cirbuf_add_tail_safe(&my_fifo, i) == 0)
215 cirbuf_del_buf_tail(&my_fifo, 10);
217 assert(CIRBUF_GET_LEN(&my_fifo)==6);
218 assert(cirbuf_del_buf_tail(&my_fifo, 10) != 0);
219 assert(cirbuf_get_tail(&my_fifo) == 5);
220 assert(cirbuf_get_head(&my_fifo) == 0);