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