Refactor to use new protocol module and sipo.
[bertos.git] / bertos / struct / kfile_fifo_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief FIFO and KFileFifo test.
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  */
37
38
39 #include <struct/fifobuf.h>
40 #include <struct/kfile_fifo.h>
41
42 #include <cfg/compiler.h>
43 #include <cfg/test.h>
44 #include <cfg/debug.h>
45
46
47 int kfilefifo_testSetup(void)
48 {
49         kdbg_init();
50         return 0;
51 }
52
53 int kfilefifo_testRun(void)
54 {
55         #define FIFOBUF_LEN 256
56
57         uint8_t buf[FIFOBUF_LEN];
58
59         FIFOBuffer fifo;
60         fifo_init(&fifo, buf, sizeof(buf));
61
62         ASSERT(fifo_isempty(&fifo));
63         ASSERT(!fifo_isfull(&fifo));
64
65         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
66         {
67                 ASSERT(!fifo_isfull(&fifo));
68                 fifo_push(&fifo, i);
69         }
70
71         ASSERT(fifo_isfull(&fifo));
72
73         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
74         {
75                 ASSERT(!fifo_isempty(&fifo));
76                 ASSERT(fifo_pop(&fifo) == i);
77         }
78
79         ASSERT(fifo_isempty(&fifo));
80
81         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
82         {
83                 ASSERT(!fifo_isfull(&fifo));
84                 fifo_push(&fifo, i);
85         }
86         ASSERT(fifo_isfull(&fifo));
87         fifo_flush(&fifo);
88         ASSERT(!fifo_isfull(&fifo));
89         ASSERT(fifo_isempty(&fifo));
90
91         KFileFifo kfifo;
92         kfilefifo_init(&kfifo, &fifo);
93
94         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
95         {
96                 ASSERT(!fifo_isfull(&fifo));
97                 fifo_push(&fifo, i);
98         }
99
100         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
101                 ASSERT(kfile_getc(&kfifo.fd) == i);
102
103         ASSERT(kfile_getc(&kfifo.fd) == EOF);
104         ASSERT(fifo_isempty(&fifo));
105
106         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
107                 ASSERT(kfile_putc(i, &kfifo.fd) == i);
108
109         ASSERT(fifo_isfull(&fifo));
110
111         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
112         {
113                 ASSERT(!fifo_isempty(&fifo));
114                 ASSERT(fifo_pop(&fifo) == i);
115         }
116
117         ASSERT(fifo_isempty(&fifo));
118
119         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
120                 ASSERT(kfile_putc(i, &kfifo.fd) == i);
121
122         ASSERT(fifo_isfull(&fifo));
123         ASSERT(kfile_putc('a', &kfifo.fd) == EOF);
124
125         fifo_flush(&fifo);
126         ASSERT(!fifo_isfull(&fifo));
127         ASSERT(fifo_isempty(&fifo));
128         ASSERT(kfile_getc(&kfifo.fd) == EOF);
129
130         ASSERT(kfile_write(&kfifo.fd, "hello world", 11) == 11);
131         ASSERT(kfile_write(&kfifo.fd, "hello world", FIFOBUF_LEN) == FIFOBUF_LEN - 1 - 11);
132
133         uint8_t test_buf[FIFOBUF_LEN];
134         ASSERT(kfile_read(&kfifo.fd, test_buf, FIFOBUF_LEN + 20) == FIFOBUF_LEN - 1);
135
136         ASSERT(!fifo_isfull(&fifo));
137         ASSERT(fifo_isempty(&fifo));
138         ASSERT(kfile_getc(&kfifo.fd) == EOF);
139         return 0;
140 }
141
142 int kfilefifo_testTearDown(void)
143 {
144         return 0;
145 }
146
147 TEST_MAIN(kfilefifo);