throttle speed before ejection
[aversive.git] / modules / base / cirbuf / test / main.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
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.
8  *
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.
13  *
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
17  *
18  *  Revision : $Id: main.c,v 1.1.2.1 2007-08-19 10:33:55 zer0 Exp $
19  *
20  */
21
22 #include <aversive.h>
23
24 #include <cirbuf.h>
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #ifdef HOST_VERSION
30 #include <assert.h>
31 #else
32 #define assert(cond) do { if (!cond) { printf("%s:%d - %s", __FILE__, __LINE__, #cond); while(1); } } while(0)
33 #endif
34
35 void dump_it(struct cirbuf * cbuf)
36 {
37         int i;
38         char e;
39         
40         printf("sta=%2.2d end=%2.2d len=%2.2d/%2.2d { ", 
41                cbuf->start, cbuf->end,
42                CIRBUF_GET_LEN(cbuf),
43                CIRBUF_GET_MAXLEN(cbuf));
44
45         for (i=0; i<CIRBUF_GET_MAXLEN(cbuf) ; i++) {
46                 if (cbuf->start <= cbuf->end) {
47                         if (i >= cbuf->start && 
48                             i <= cbuf->end && 
49                             CIRBUF_GET_LEN(cbuf) != 0)
50                                 printf("%2.2x, ", cbuf->buf[i]&0xFF);
51                         else
52                                 printf("XX, ");
53                 }
54                 else {
55                         if (i < cbuf->start && 
56                             i > cbuf->end)
57                                 printf("XX, ");
58                         else
59                                 printf("%2.2x, ", cbuf->buf[i]&0xFF);
60                 }
61         }
62         printf("}  -> ");
63
64         printf("[ ");
65         CIRBUF_FOREACH(cbuf, i, e) {
66                 printf("%2.2x, ", e&0xFF);
67         }
68         printf("]\n");
69
70         if(CIRBUF_GET_LEN(cbuf) == 0) {
71                 assert(cbuf->start == cbuf->end);
72         }
73         else {
74                 assert( ((cbuf->end - cbuf->start + 1) + cbuf->maxlen) % cbuf->maxlen == 
75                         CIRBUF_GET_LEN(cbuf) % cbuf->maxlen );
76         }
77
78 }
79
80 int main(void)
81 {
82         uint8_t i;
83         struct cirbuf my_fifo;
84         char fifo_buf[16];
85         
86         char buf1[] = { 0x10, 0x11, 0x12 };
87         char buf2[] = { 0x20, 0x21, 0x22, 0x23 };
88
89         char tmp_buf[16];
90         char ref_buf[] = { 0x20, 0x21, 0x22, 0x23, 0x01, 0x10, 0x11, 0x12 };
91
92         /* Test 1 */
93
94         printf("Test 1\n");
95         
96         cirbuf_init(&my_fifo, fifo_buf, 0, 4);
97         assert(CIRBUF_IS_EMPTY(&my_fifo));
98         assert(!CIRBUF_IS_FULL(&my_fifo));
99         dump_it(&my_fifo);
100
101         cirbuf_add_tail(&my_fifo, 1);
102         assert(cirbuf_get_head(&my_fifo) == 1);
103         assert(cirbuf_get_tail(&my_fifo) == 1);
104         dump_it(&my_fifo);
105
106
107         cirbuf_add_tail(&my_fifo, 2);
108         assert(!CIRBUF_IS_EMPTY(&my_fifo));
109         assert(!CIRBUF_IS_FULL(&my_fifo));
110         dump_it(&my_fifo);
111
112         cirbuf_add_tail(&my_fifo, 3);
113         assert(cirbuf_get_head(&my_fifo) == 1);
114         assert(cirbuf_get_tail(&my_fifo) == 3);
115         dump_it(&my_fifo);
116
117         cirbuf_add_tail(&my_fifo, 4);
118         assert(!CIRBUF_IS_EMPTY(&my_fifo));
119         assert(CIRBUF_IS_FULL(&my_fifo));
120         dump_it(&my_fifo);
121
122         cirbuf_del_tail(&my_fifo);
123         dump_it(&my_fifo);
124         assert(cirbuf_get_tail(&my_fifo) == 3);
125         assert(cirbuf_get_head(&my_fifo) == 1);
126
127         cirbuf_del_head(&my_fifo);
128         assert(cirbuf_get_tail(&my_fifo) == 3);
129         assert(cirbuf_get_head(&my_fifo) == 2);
130         dump_it(&my_fifo);
131         
132         cirbuf_del_head(&my_fifo);
133         assert(cirbuf_get_tail(&my_fifo) == 3);
134         assert(cirbuf_get_head(&my_fifo) == 3);
135         dump_it(&my_fifo);
136         
137         cirbuf_del_head(&my_fifo);
138         assert(CIRBUF_IS_EMPTY(&my_fifo));
139         dump_it(&my_fifo);
140         
141
142         /* Test 2 */
143         
144         printf("Test 2\n");
145
146         cirbuf_init(&my_fifo, fifo_buf, 2, 4);
147         dump_it(&my_fifo);
148
149         cirbuf_add_head(&my_fifo, 4);
150         assert(cirbuf_get_head(&my_fifo) == 4);
151         assert(cirbuf_get_tail(&my_fifo) == 4);
152         dump_it(&my_fifo);
153
154
155         cirbuf_add_head(&my_fifo, 3);
156         assert(!CIRBUF_IS_EMPTY(&my_fifo));
157         assert(!CIRBUF_IS_FULL(&my_fifo));
158         dump_it(&my_fifo);
159
160         cirbuf_add_head(&my_fifo, 2);
161         assert(cirbuf_get_head(&my_fifo) == 2);
162         assert(cirbuf_get_tail(&my_fifo) == 4);
163         dump_it(&my_fifo);
164
165         cirbuf_add_head(&my_fifo, 1);
166         assert(!CIRBUF_IS_EMPTY(&my_fifo));
167         assert(CIRBUF_IS_FULL(&my_fifo));
168         dump_it(&my_fifo);
169
170
171         /* Test 3 */
172         
173         printf("Test 3\n");
174
175         for (i=0 ; i<16; i++) {
176                 cirbuf_init(&my_fifo, fifo_buf, i, 16);
177                 dump_it(&my_fifo);
178                 cirbuf_add_buf_head(&my_fifo, buf1, sizeof(buf1));
179                 dump_it(&my_fifo);
180                 cirbuf_add_head(&my_fifo, 1);
181                 dump_it(&my_fifo);
182                 cirbuf_add_buf_head(&my_fifo, buf2, sizeof(buf2));
183                 dump_it(&my_fifo);
184                 cirbuf_get_buf_head(&my_fifo, tmp_buf, sizeof(tmp_buf));
185                 assert(memcmp(tmp_buf, ref_buf, sizeof(ref_buf)) == 0);
186         }
187
188         /* Test 4 */
189         
190         printf("Test 4\n");
191
192         for (i=0 ; i<16; i++) {
193                 cirbuf_init(&my_fifo, fifo_buf, i, 16);
194                 dump_it(&my_fifo);
195                 cirbuf_add_buf_tail(&my_fifo, buf2, sizeof(buf2));
196                 dump_it(&my_fifo);
197                 cirbuf_add_tail(&my_fifo, 1);
198                 dump_it(&my_fifo);
199                 cirbuf_add_buf_tail(&my_fifo, buf1, sizeof(buf1));
200                 dump_it(&my_fifo);
201                 cirbuf_get_buf_tail(&my_fifo, tmp_buf, sizeof(tmp_buf));
202                 assert(memcmp(tmp_buf, ref_buf, sizeof(ref_buf)) == 0);
203         }
204
205         /* Test 5 */
206
207         printf("Test 5\n");
208
209         cirbuf_init(&my_fifo, fifo_buf, 10, 16);
210         dump_it(&my_fifo);
211         i=0;
212         while (cirbuf_add_tail_safe(&my_fifo, i) == 0)
213                 i++;
214         dump_it(&my_fifo);
215         cirbuf_del_buf_tail(&my_fifo, 10);
216         dump_it(&my_fifo);
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);
221         
222
223
224         return 0;
225 }