Add KFile interface over a fifo buffer.
[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  * \version $Id: kfilefifo_test.c 1962 2008-12-02 15:37:17Z batt $
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39
40 #include <struct/fifobuf.h>
41 #include <struct/kfile_fifo.h>
42
43 #include <cfg/compiler.h>
44 #include <cfg/test.h>
45 #include <cfg/debug.h>
46
47
48 int kfilefifo_testSetup(void)
49 {
50         kdbg_init();
51         return 0;
52 }
53
54 int kfilefifo_testRun(void)
55 {
56         #define FIFOBUF_LEN 256
57
58         uint8_t buf[FIFOBUF_LEN];
59
60         FIFOBuffer fifo;
61         fifo_init(&fifo, buf, sizeof(buf));
62
63         ASSERT(fifo_isempty(&fifo));
64         ASSERT(!fifo_isfull(&fifo));
65
66         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
67         {
68                 ASSERT(!fifo_isfull(&fifo));
69                 fifo_push(&fifo, i);
70         }
71
72         ASSERT(fifo_isfull(&fifo));
73
74         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
75         {
76                 ASSERT(!fifo_isempty(&fifo));
77                 ASSERT(fifo_pop(&fifo) == i);
78         }
79
80         ASSERT(fifo_isempty(&fifo));
81
82         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
83         {
84                 ASSERT(!fifo_isfull(&fifo));
85                 fifo_push(&fifo, i);
86         }
87         ASSERT(fifo_isfull(&fifo));
88         fifo_flush(&fifo);
89         ASSERT(!fifo_isfull(&fifo));
90         ASSERT(fifo_isempty(&fifo));
91
92         KFileFifo kfifo;
93         kfilefifo_init(&kfifo, &fifo);
94
95         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
96         {
97                 ASSERT(!fifo_isfull(&fifo));
98                 fifo_push(&fifo, i);
99         }
100
101         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
102                 ASSERT(kfile_getc(&kfifo.fd) == i);
103
104         ASSERT(kfile_getc(&kfifo.fd) == EOF);
105         ASSERT(fifo_isempty(&fifo));
106
107         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
108                 ASSERT(kfile_putc(i, &kfifo.fd) == i);
109
110         ASSERT(fifo_isfull(&fifo));
111
112         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
113         {
114                 ASSERT(!fifo_isempty(&fifo));
115                 ASSERT(fifo_pop(&fifo) == i);
116         }
117
118         ASSERT(fifo_isempty(&fifo));
119
120         for (int i = 0; i < FIFOBUF_LEN - 1; i++)
121                 ASSERT(kfile_putc(i, &kfifo.fd) == i);
122
123         ASSERT(fifo_isfull(&fifo));
124         ASSERT(kfile_putc('a', &kfifo.fd) == EOF);
125
126         fifo_flush(&fifo);
127         ASSERT(!fifo_isfull(&fifo));
128         ASSERT(fifo_isempty(&fifo));
129         ASSERT(kfile_getc(&kfifo.fd) == EOF);
130
131         ASSERT(kfile_write(&kfifo.fd, "hello world", 11) == 11);
132         ASSERT(kfile_write(&kfifo.fd, "hello world", FIFOBUF_LEN) == FIFOBUF_LEN - 1 - 11);
133
134         uint8_t test_buf[FIFOBUF_LEN];
135         ASSERT(kfile_read(&kfifo.fd, test_buf, FIFOBUF_LEN + 20) == FIFOBUF_LEN - 1);
136
137         ASSERT(!fifo_isfull(&fifo));
138         ASSERT(fifo_isempty(&fifo));
139         ASSERT(kfile_getc(&kfifo.fd) == EOF);
140         return 0;
141 }
142
143 int kfilefifo_testTearDown(void)
144 {
145         return 0;
146 }
147
148 TEST_MAIN(kfilefifo);