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 crc_stored;
36 : uint32_t 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 :
94 16 : crc_stored = scrc(s);
95 16 : crc_computed = scrc_stream(s);
96 :
97 16 : if (crc_stored != crc_computed) {
98 : /* LCOV_EXCL_START */
99 : exit(EXIT_FAILURE);
100 : /* LCOV_EXCL_STOP */
101 : }
102 :
103 16 : if (sputble32(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 crc_stored;
117 : uint32_t 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 128 : crc_computed = scrc(s);
176 :
177 128 : if (sgetble32(s, &crc_stored) != 0) {
178 : /* LCOV_EXCL_START */
179 : exit(EXIT_FAILURE);
180 : /* LCOV_EXCL_STOP */
181 : }
182 :
183 128 : if (crc_stored != crc_computed) {
184 : /* LCOV_EXCL_START */
185 : exit(EXIT_FAILURE);
186 : /* LCOV_EXCL_STOP */
187 : }
188 :
189 128 : if (sclose(s) != 0) {
190 : /* LCOV_EXCL_START */
191 : exit(EXIT_FAILURE);
192 : /* LCOV_EXCL_STOP */
193 : }
194 : }
195 16 : }
196 :
197 1 : int main(void)
198 : {
199 : unsigned i;
200 17 : for (i = 1; i <= 16; ++i) {
201 :
202 : /* test with different stream buffer size */
203 16 : STREAM_SIZE = i;
204 :
205 16 : printf("Test stream buffer size %u\n", i);
206 :
207 16 : test();
208 : }
209 :
210 1 : return 0;
211 : }
212 :
|