Line data Source code
1 : /*
2 : * Copyright (C) 2015 Andrea Mazzoleni
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 3 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, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "stream.h"
21 : #include "support.h"
22 :
23 : volatile int global_interrupt = 0;
24 :
25 : #define STREAM_MAX 8
26 : #define BUFFER_MAX 64
27 : #define STR_MAX 128
28 :
29 16 : void test(void)
30 : {
31 : struct stream* s;
32 : char file[32];
33 : unsigned char buffer[BUFFER_MAX];
34 : char str[STR_MAX];
35 : unsigned i, j;
36 16 : uint32_t u32 = -1L;
37 16 : uint64_t u64 = -1LL;
38 : uint32_t put_crc_stored;
39 : uint32_t put_crc_computed;
40 :
41 16 : crc32c_init();
42 :
43 16 : s = sopen_multi_write(STREAM_MAX);
44 144 : for (i = 0; i < STREAM_MAX; ++i) {
45 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
46 128 : remove(file);
47 128 : if (sopen_multi_file(s, i, file) != 0) {
48 : /* LCOV_EXCL_START */
49 : exit(EXIT_FAILURE);
50 : /* LCOV_EXCL_STOP */
51 : }
52 : }
53 :
54 4112 : for (j = 0; j < 256; ++j) {
55 4096 : if (sputc(j, s) != 0) {
56 : /* LCOV_EXCL_START */
57 : exit(EXIT_FAILURE);
58 : /* LCOV_EXCL_STOP */
59 : }
60 : }
61 :
62 528 : for (j = 0; j < 32; ++j) {
63 512 : if (sputb32(u32 >> j, s) != 0) {
64 : /* LCOV_EXCL_START */
65 : exit(EXIT_FAILURE);
66 : /* LCOV_EXCL_STOP */
67 : }
68 : }
69 :
70 1040 : for (j = 0; j < 64; ++j) {
71 1024 : if (sputb64(u64 >> j, s) != 0) {
72 : /* LCOV_EXCL_START */
73 : exit(EXIT_FAILURE);
74 : /* LCOV_EXCL_STOP */
75 : }
76 : }
77 :
78 1040 : for (j = 0; j < BUFFER_MAX; ++j) {
79 1024 : memset(buffer, j, j);
80 1024 : if (swrite(buffer, j, s) != 0) {
81 : /* LCOV_EXCL_START */
82 : exit(EXIT_FAILURE);
83 : /* LCOV_EXCL_STOP */
84 : }
85 : }
86 :
87 2048 : for (j = 1; j < STR_MAX; ++j) {
88 2032 : memset(str, ' ' + j, j - 1);
89 2032 : str[j - 1] = 0;
90 2032 : if (sputbs(str, s) != 0) {
91 : /* LCOV_EXCL_START */
92 : exit(EXIT_FAILURE);
93 : /* LCOV_EXCL_STOP */
94 : }
95 : }
96 :
97 16 : put_crc_stored = scrc(s);
98 16 : put_crc_computed = scrc_stream(s);
99 :
100 16 : if (put_crc_stored != put_crc_computed) {
101 : /* LCOV_EXCL_START */
102 : exit(EXIT_FAILURE);
103 : /* LCOV_EXCL_STOP */
104 : }
105 :
106 16 : if (sputble32(put_crc_stored, s) != 0) {
107 : /* LCOV_EXCL_START */
108 : exit(EXIT_FAILURE);
109 : /* LCOV_EXCL_STOP */
110 : }
111 :
112 16 : if (sclose(s) != 0) {
113 : /* LCOV_EXCL_START */
114 : exit(EXIT_FAILURE);
115 : /* LCOV_EXCL_STOP */
116 : }
117 :
118 144 : for (i = 0; i < STREAM_MAX; ++i) {
119 : uint32_t get_crc_stored;
120 : uint32_t get_crc_computed;
121 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
122 :
123 128 : s = sopen_read(file);
124 128 : if (s == 0) {
125 : /* LCOV_EXCL_START */
126 : exit(EXIT_FAILURE);
127 : /* LCOV_EXCL_STOP */
128 : }
129 :
130 32896 : for (j = 0; j < 256; ++j) {
131 32768 : int c = sgetc(s);
132 32768 : if (c == EOF || (unsigned char)c != j) {
133 : /* LCOV_EXCL_START */
134 : exit(EXIT_FAILURE);
135 : /* LCOV_EXCL_STOP */
136 : }
137 : }
138 :
139 4224 : for (j = 0; j < 32; ++j) {
140 : uint32_t v32;
141 4096 : if (sgetb32(s, &v32) != 0 || v32 != (u32 >> j)) {
142 : /* LCOV_EXCL_START */
143 : exit(EXIT_FAILURE);
144 : /* LCOV_EXCL_STOP */
145 : }
146 : }
147 :
148 8320 : for (j = 0; j < 64; ++j) {
149 : uint64_t v64;
150 8192 : if (sgetb64(s, &v64) != 0 || v64 != (u64 >> j)) {
151 : /* LCOV_EXCL_START */
152 : exit(EXIT_FAILURE);
153 : /* LCOV_EXCL_STOP */
154 : }
155 : }
156 :
157 8192 : for (j = 1; j < BUFFER_MAX; ++j) {
158 : char copy[BUFFER_MAX];
159 8064 : memset(buffer, j, j);
160 8064 : if (sread(s, copy, j) != 0 || memcmp(copy, buffer, j) != 0) {
161 : /* LCOV_EXCL_START */
162 : exit(EXIT_FAILURE);
163 : /* LCOV_EXCL_STOP */
164 : }
165 : }
166 :
167 16384 : for (j = 1; j < STR_MAX; ++j) {
168 : char copy[STR_MAX];
169 16256 : memset(str, ' ' + j, j - 1);
170 16256 : str[j - 1] = 0;
171 16256 : if (sgetbs(s, copy, sizeof(copy)) != 0 || strcmp(copy, str) != 0) {
172 : /* LCOV_EXCL_START */
173 : exit(EXIT_FAILURE);
174 : /* LCOV_EXCL_STOP */
175 : }
176 : }
177 :
178 : /* get the computed CRC *before* reading the stored one */
179 128 : get_crc_computed = scrc(s);
180 :
181 128 : if (sgetble32(s, &get_crc_stored) != 0) {
182 : /* LCOV_EXCL_START */
183 : exit(EXIT_FAILURE);
184 : /* LCOV_EXCL_STOP */
185 : }
186 :
187 128 : if (get_crc_stored != put_crc_stored) {
188 : /* LCOV_EXCL_START */
189 : exit(EXIT_FAILURE);
190 : /* LCOV_EXCL_STOP */
191 : }
192 :
193 128 : if (get_crc_stored != get_crc_computed) {
194 : /* LCOV_EXCL_START */
195 : exit(EXIT_FAILURE);
196 : /* LCOV_EXCL_STOP */
197 : }
198 :
199 128 : if (sclose(s) != 0) {
200 : /* LCOV_EXCL_START */
201 : exit(EXIT_FAILURE);
202 : /* LCOV_EXCL_STOP */
203 : }
204 : }
205 :
206 144 : for (i = 0; i < STREAM_MAX; ++i) {
207 : uint32_t get_crc_stored;
208 : uint32_t get_crc_computed;
209 : unsigned char buf[4];
210 128 : snprintf(file, sizeof(file), "stream%u.bin", i);
211 :
212 128 : s = sopen_read(file);
213 128 : if (s == 0) {
214 : /* LCOV_EXCL_START */
215 : exit(EXIT_FAILURE);
216 : /* LCOV_EXCL_STOP */
217 : }
218 :
219 128 : if (sdeplete(s, buf) != 0) {
220 : /* LCOV_EXCL_START */
221 : exit(EXIT_FAILURE);
222 : /* LCOV_EXCL_STOP */
223 : }
224 :
225 : /* get the stored crc from the last four bytes */
226 128 : get_crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
227 :
228 128 : if (get_crc_stored != put_crc_stored) {
229 : /* LCOV_EXCL_START */
230 : exit(EXIT_FAILURE);
231 : /* LCOV_EXCL_STOP */
232 : }
233 :
234 : /* get the computed CRC *after* reading the stored one */
235 128 : get_crc_computed = scrc(s);
236 :
237 : /* adjust the stored crc to include itself */
238 128 : get_crc_stored = crc32c(get_crc_stored, buf, 4);
239 :
240 128 : if (get_crc_stored != get_crc_computed) {
241 : /* LCOV_EXCL_START */
242 : exit(EXIT_FAILURE);
243 : /* LCOV_EXCL_STOP */
244 : }
245 :
246 128 : if (sclose(s) != 0) {
247 : /* LCOV_EXCL_START */
248 : exit(EXIT_FAILURE);
249 : /* LCOV_EXCL_STOP */
250 : }
251 : }
252 16 : }
253 :
254 1 : int main(void)
255 : {
256 : unsigned i;
257 :
258 1 : lock_init();
259 :
260 17 : for (i = 1; i <= 16; ++i) {
261 :
262 : /* test with different stream buffer size */
263 16 : STREAM_SIZE = i;
264 :
265 16 : printf("Test stream buffer size %u\n", i);
266 :
267 16 : test();
268 : }
269 :
270 1 : return 0;
271 : }
272 :
|