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