Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/bitstream.cpp

raw
1 /*
2 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2021 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 4 мар. 2021 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/io/InBitStream.h>
24 #include <lsp-plug.in/io/InMemoryStream.h>
25 #include <lsp-plug.in/io/OutBitStream.h>
26 #include <lsp-plug.in/io/OutMemoryStream.h>
27 #include <lsp-plug.in/stdlib/string.h>
28
29 using namespace lsp;
30
31 namespace
32 {
33 static const uint8_t test_data[] =
34 {
35 0x12, 0xcf, 0xa1, 0xcf, 0x19, 0x12, 0x12, 0x6d,
36 0x28, 0xa4, 0x51, 0x73, 0x15, 0xbf, 0xa7, 0xbd,
37 0xa8, 0xfd, 0xcf, 0xb2, 0xf1, 0xf1, 0xd0, 0x42,
38 0xe5, 0x8d, 0x05, 0x88, 0xf7, 0x32, 0x79, 0xe8
39 };
40 }
41
42 UTEST_BEGIN("runtime.io", bitstream)
43
44 void test_write_bits(io::IOutStream *os)
45 {
46 io::OutBitStream obs;
47 UTEST_ASSERT(obs.wrap(os, WRAP_NONE) == STATUS_OK);
48
49 // Emit '9'
50 UTEST_ASSERT(obs.bwrite(true) == STATUS_OK);
51 UTEST_ASSERT(obs.bwrite(false) == STATUS_OK);
52 UTEST_ASSERT(obs.writev(false) == STATUS_OK);
53 UTEST_ASSERT(obs.writev(true) == STATUS_OK);
54
55 // 1-byte write
56 UTEST_ASSERT(obs.writev(uint8_t(0xce)) == STATUS_OK); // Emit 'ce'
57 UTEST_ASSERT(obs.writev(uint8_t(0xa5), 4) == STATUS_OK); // Emit '5'
58
59 // 2-byte write
60 UTEST_ASSERT(obs.writev(uint16_t(0x1324)) == STATUS_OK); // Emit '1324'
61 UTEST_ASSERT(obs.writev(uint16_t(0xfc4b), 12) == STATUS_OK); // Emit 'c4b'
62
63 // 4-byte write
64 UTEST_ASSERT(obs.writev(uint32_t(0xa7cb329e)) == STATUS_OK); // Emit 'a7cb329e'
65 UTEST_ASSERT(obs.writev(uint32_t(0x5a8c3679), 28) == STATUS_OK); // Emit 'a8c3679'
66
67 // 8-byte write
68 UTEST_ASSERT(obs.writev(uint64_t(0xbf61cd168a7df102ULL)) == STATUS_OK); // Emit 'bf61cd168a7df102'
69 UTEST_ASSERT(obs.writev(uint64_t(0x3e561924d5993bf7ULL), 60) == STATUS_OK); // Emit 'e561924d5993bf7'
70
71 // Arrays
72 UTEST_ASSERT(obs.write(&test_data[0], 9) == 9); // Emit '12cfa1cf1912126d28'
73 UTEST_ASSERT(obs.bwrite(&test_data[9], 180) == 180); // Emit 'a4517315bfa7bda8fdcfb2f1f1d042e58d0588f732798'
74
75 // Emit 'b'
76 UTEST_ASSERT(obs.bwrite(true) == STATUS_OK);
77 UTEST_ASSERT(obs.bwrite(false) == STATUS_OK);
78 UTEST_ASSERT(obs.bwrite(true) == STATUS_OK);
79 UTEST_ASSERT(obs.bwrite(true) == STATUS_OK);
80
81 UTEST_ASSERT(obs.flush() == STATUS_OK); // Emit '0'
82 UTEST_ASSERT(obs.writev(uint32_t(0x5ec9), 12) == STATUS_OK); // Emit 'ec9'
83
84 UTEST_ASSERT(obs.close() == STATUS_OK); // Emit '0'
85 }
86
87 void test_read_bits(io::IInStream *is)
88 {
89 io::InBitStream ibs;
90 UTEST_ASSERT(ibs.wrap(is, WRAP_NONE) == STATUS_OK);
91
92 union
93 {
94 bool b;
95 uint8_t u8;
96 uint16_t u16;
97 uint32_t u32;
98 uint64_t u64;
99 } v;
100
101 uint8_t data[sizeof(test_data)];
102
103 // Read '9'
104 UTEST_ASSERT(ibs.readb(&v.b) == 1);
105 UTEST_ASSERT(v.b == true);
106 UTEST_ASSERT(ibs.readb(&v.b) == 1);
107 UTEST_ASSERT(v.b == false);
108 UTEST_ASSERT(ibs.readv(&v.b) == 1);
109 UTEST_ASSERT(v.b == false);
110 UTEST_ASSERT(ibs.readv(&v.b) == 1);
111 UTEST_ASSERT(v.b == true);
112
113 // 1-byte reads
114 UTEST_ASSERT(ibs.readv(&v.u8) == 8); // Read 'ce'
115 UTEST_ASSERT(v.u8 == 0xce);
116 UTEST_ASSERT(ibs.readv(&v.u8, 4) == 4); // Read '5'
117 UTEST_ASSERT(v.u8 == 0x05);
118
119 // 2-byte reads
120 UTEST_ASSERT(ibs.readv(&v.u16) == 16); // Read '1324'
121 UTEST_ASSERT(v.u16 == 0x1324);
122 UTEST_ASSERT(ibs.readv(&v.u16, 12) == 12); // Read 'c4b'
123 UTEST_ASSERT(v.u16 == 0x0c4b);
124
125 // 4-byte reads
126 UTEST_ASSERT(ibs.readv(&v.u32) == 32); // Read 'a7cb329e'
127 UTEST_ASSERT(v.u32 == 0xa7cb329e);
128 UTEST_ASSERT(ibs.readv(&v.u32, 28) == 28); // Read 'a8c3679'
129 UTEST_ASSERT(v.u32 == 0x0a8c3679);
130
131 // 8-byte reads
132 UTEST_ASSERT(ibs.readv(&v.u64) == 64); // Read 'bf61cd168a7df102'
133 UTEST_ASSERT(v.u64 == uint64_t(0xbf61cd168a7df102ULL));
134 UTEST_ASSERT(ibs.readv(&v.u64, 60) == 60); // Read 'e561924d5993bf7'
135 UTEST_ASSERT(v.u64 == uint64_t(0x0e561924d5993bf7ULL));
136
137 // Arrays
138 UTEST_ASSERT(ibs.read(&data[0], 9) == 9); // Read '12cfa1cf1912126d28'
139 UTEST_ASSERT(memcmp(&data[0], &test_data[0], 9) == 0);
140 UTEST_ASSERT(ibs.bread(&data[9], 180) == 180); // Read 'a4517315bfa7bda8fdcfb2f1f1d042e58d0588f732798'
141 UTEST_ASSERT(memcmp(&data[9], &test_data[9], 22) == 0);
142 UTEST_ASSERT(data[31] == (test_data[31] & 0x0f));
143
144 // Read tail
145 UTEST_ASSERT(ibs.readv(&v.u8) == 8); // Read 'b0'
146 UTEST_ASSERT(v.u8 == 0xb0);
147 UTEST_ASSERT(ibs.readv(&v.u32) == 16); // Read 'ec90'
148 UTEST_ASSERT(v.u32 == 0xec90);
149
150 // Check for EOF
151 UTEST_ASSERT(ibs.readb(&v.b) == -STATUS_EOF);
152 UTEST_ASSERT(ibs.readv(&v.b) == -STATUS_EOF);
153 UTEST_ASSERT(ibs.readv(&v.u8) == -STATUS_EOF);
154 UTEST_ASSERT(ibs.readv(&v.u16) == -STATUS_EOF);
155 UTEST_ASSERT(ibs.readv(&v.u32) == -STATUS_EOF);
156 UTEST_ASSERT(ibs.readv(&v.u64) == -STATUS_EOF);
157
158 UTEST_ASSERT(ibs.close() == STATUS_OK);
159 }
160
161 UTEST_MAIN
162 {
163 io::OutMemoryStream oms;
164
165 // Control sequence:
166 // 9ce51324c4ba7cb329ea8c3679bf61cd168a7df102e561924d5993bf712cfa1cf1912126d28a4517315bfa7bda8fdcfb2f1f1d042e58d0588f732798
167
168 static const uint8_t check[] =
169 {
170 0x9c, 0xe5, 0x13, 0x24,
171 0xc4, 0xba, 0x7c, 0xb3,
172 0x29, 0xea, 0x8c, 0x36,
173 0x79, 0xbf, 0x61, 0xcd,
174 0x16, 0x8a, 0x7d, 0xf1,
175 0x02, 0xe5, 0x61, 0x92,
176 0x4d, 0x59, 0x93, 0xbf,
177 0x71, 0x2c, 0xfa, 0x1c,
178 0xf1, 0x91, 0x21, 0x26,
179 0xd2, 0x8a, 0x45, 0x17,
180 0x31, 0x5b, 0xfa, 0x7b,
181 0xda, 0x8f, 0xdc, 0xfb,
182 0x2f, 0x1f, 0x1d, 0x04,
183 0x2e, 0x58, 0xd0, 0x58,
184 0x8f, 0x73, 0x27, 0x98,
185 0xb0, 0xec, 0x90
186 };
187
188 test_write_bits(&oms);
189
190 const uint8_t *data = oms.data();
191 printf("data : ");
192 for (size_t i=0; i<oms.size(); ++i)
193 printf("%02x ", data[i]);
194 printf("\n");
195
196 UTEST_ASSERT(oms.size() == sizeof(check));
197
198 printf("check: ");
199 for (size_t i=0; i<sizeof(check); ++i)
200 printf("%02x ", check[i]);
201 printf("\n");
202
203 UTEST_ASSERT(memcmp(data, check, sizeof(check)) == 0);
204
205 io::InMemoryStream ims(oms.data(), oms.size());
206 test_read_bits(&ims);
207
208
209 // Drop the array
210 oms.drop();
211 }
212
213 UTEST_END
214
215
216