| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 17 янв. 2026 г. |
| 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/io/InBufStream.h> |
| 23 |
#include <lsp-plug.in/io/InMemoryStream.h> |
| 24 |
#include <lsp-plug.in/test-fw/utest.h> |
| 25 |
|
| 26 |
UTEST_BEGIN("runtime.io", inbufstream) |
| 27 |
|
| 28 |
void test_unbuffered_reads(io::InMemoryStream & ims) |
| 29 |
{ |
| 30 |
printf("Testing unbuffered reads\n"); |
| 31 |
|
| 32 |
const uint8_t * const src = ims.data(); |
| 33 |
uint8_t buf[16]; |
| 34 |
io::InBufStream is(8); |
| 35 |
is.set_buffering(false); |
| 36 |
is.set_seek_buffering(false); |
| 37 |
|
| 38 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16) { |
| 39 |
printf(" block_size=%d ...\n", int(size)); |
| 40 |
|
| 41 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 42 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 43 |
|
| 44 |
size_t offset = 0; |
| 45 |
while (true) |
| 46 |
{ |
| 47 |
ssize_t n_read = is.read(buf, size); |
| 48 |
if (n_read < 0) |
| 49 |
{ |
| 50 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 51 |
break; |
| 52 |
} |
| 53 |
|
| 54 |
UTEST_ASSERT_MSG( |
| 55 |
memcmp(buf, &src[offset], n_read) == 0, |
| 56 |
"Failed block size=%d, offset=%d", |
| 57 |
int(size), int(offset)); |
| 58 |
offset += n_read; |
| 59 |
|
| 60 |
UTEST_ASSERT_MSG( |
| 61 |
is.position() == wssize_t(offset), |
| 62 |
"Invalid position returned %d, expected %d", |
| 63 |
int(is.position()), int(offset)); |
| 64 |
} |
| 65 |
|
| 66 |
UTEST_ASSERT_MSG( |
| 67 |
is.position() == wssize_t(offset), |
| 68 |
"Invalid position returned %d, expected %d", |
| 69 |
int(is.position()), int(offset)); |
| 70 |
UTEST_ASSERT(offset == ims.size()); |
| 71 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void test_unbuffered_byte_reads(io::InMemoryStream & ims) |
| 76 |
{ |
| 77 |
printf("Testing unbuffered byte reads\n"); |
| 78 |
|
| 79 |
const uint8_t * const src = ims.data(); |
| 80 |
io::InBufStream is(8); |
| 81 |
is.set_buffering(false); |
| 82 |
is.set_seek_buffering(false); |
| 83 |
|
| 84 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 85 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 86 |
|
| 87 |
size_t offset = 0; |
| 88 |
while (true) |
| 89 |
{ |
| 90 |
ssize_t n_read = is.read_byte(); |
| 91 |
if (n_read < 0) |
| 92 |
{ |
| 93 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
UTEST_ASSERT_MSG( |
| 98 |
src[offset] == n_read, |
| 99 |
"Failed read byte at offset=%d", |
| 100 |
int(offset)); |
| 101 |
++offset; |
| 102 |
|
| 103 |
UTEST_ASSERT_MSG( |
| 104 |
is.position() == wssize_t(offset), |
| 105 |
"Invalid position returned %d, expected %d", |
| 106 |
int(is.position()), int(offset)); |
| 107 |
} |
| 108 |
|
| 109 |
UTEST_ASSERT_MSG( |
| 110 |
is.position() == wssize_t(offset), |
| 111 |
"Invalid position returned %d, expected %d", |
| 112 |
int(is.position()), int(offset)); |
| 113 |
UTEST_ASSERT(offset == ims.size()); |
| 114 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 115 |
} |
| 116 |
|
| 117 |
void test_buffered_reads(io::InMemoryStream & ims) |
| 118 |
{ |
| 119 |
printf("Testing buffered reads\n"); |
| 120 |
|
| 121 |
const uint8_t * const src = ims.data(); |
| 122 |
uint8_t buf[16]; |
| 123 |
io::InBufStream is(8); |
| 124 |
is.set_buffering(false); |
| 125 |
is.set_seek_buffering(false); |
| 126 |
|
| 127 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16) { |
| 128 |
printf(" block_size=%d ...\n", int(size)); |
| 129 |
|
| 130 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 131 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 132 |
|
| 133 |
size_t offset = 0; |
| 134 |
while (true) |
| 135 |
{ |
| 136 |
ssize_t n_read = is.read(buf, size); |
| 137 |
if (n_read < 0) |
| 138 |
{ |
| 139 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
UTEST_ASSERT_MSG( |
| 144 |
memcmp(buf, &src[offset], n_read) == 0, |
| 145 |
"Failed block size=%d, offset=%d", |
| 146 |
int(size), int(offset)); |
| 147 |
offset += n_read; |
| 148 |
|
| 149 |
UTEST_ASSERT_MSG( |
| 150 |
is.position() == wssize_t(offset), |
| 151 |
"Invalid position returned %d, expected %d", |
| 152 |
int(is.position()), int(offset)); |
| 153 |
} |
| 154 |
|
| 155 |
UTEST_ASSERT_MSG( |
| 156 |
is.position() == wssize_t(offset), |
| 157 |
"Invalid position returned %d, expected %d", |
| 158 |
int(is.position()), int(offset)); |
| 159 |
UTEST_ASSERT(offset == ims.size()); |
| 160 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void test_buffered_byte_reads(io::InMemoryStream & ims) |
| 165 |
{ |
| 166 |
printf("Testing buffered byte reads\n"); |
| 167 |
|
| 168 |
const uint8_t * const src = ims.data(); |
| 169 |
io::InBufStream is(8); |
| 170 |
is.set_buffering(true); |
| 171 |
is.set_seek_buffering(false); |
| 172 |
|
| 173 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 174 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 175 |
|
| 176 |
size_t offset = 0; |
| 177 |
while (true) |
| 178 |
{ |
| 179 |
ssize_t n_read = is.read_byte(); |
| 180 |
if (n_read < 0) |
| 181 |
{ |
| 182 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
UTEST_ASSERT_MSG( |
| 187 |
src[offset] == n_read, |
| 188 |
"Failed read byte at offset=%d", |
| 189 |
int(offset)); |
| 190 |
++offset; |
| 191 |
|
| 192 |
UTEST_ASSERT_MSG( |
| 193 |
is.position() == wssize_t(offset), |
| 194 |
"Invalid position returned %d, expected %d", |
| 195 |
int(is.position()), int(offset)); |
| 196 |
} |
| 197 |
|
| 198 |
UTEST_ASSERT_MSG( |
| 199 |
is.position() == wssize_t(offset), |
| 200 |
"Invalid position returned %d, expected %d", |
| 201 |
int(is.position()), int(offset)); |
| 202 |
UTEST_ASSERT(offset == ims.size()); |
| 203 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 204 |
} |
| 205 |
|
| 206 |
void test_buffered_rereads_non_buffered_seeks(io::InMemoryStream & ims) |
| 207 |
{ |
| 208 |
printf("Testing buffered re-reads with non-buffered seeks\n"); |
| 209 |
|
| 210 |
const uint8_t * const src = ims.data(); |
| 211 |
uint8_t buf[24]; |
| 212 |
io::InBufStream is(8); |
| 213 |
is.set_buffering(true); |
| 214 |
is.set_seek_buffering(false); |
| 215 |
|
| 216 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 217 |
printf(" block_size=%d ...\n", int(size)); |
| 218 |
|
| 219 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 220 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 221 |
|
| 222 |
size_t offset = 0; |
| 223 |
const size_t half = lsp_max(size / 2, size_t(1)); |
| 224 |
while (true) |
| 225 |
{ |
| 226 |
// Perform first read |
| 227 |
ssize_t n_read = is.read(buf, size); |
| 228 |
if (n_read < 0) |
| 229 |
{ |
| 230 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
UTEST_ASSERT_MSG( |
| 235 |
memcmp(buf, &src[offset], n_read) == 0, |
| 236 |
"Failed block size=%d, offset=%d", |
| 237 |
int(size), int(offset)); |
| 238 |
|
| 239 |
UTEST_ASSERT_MSG( |
| 240 |
is.position() == wssize_t(offset + n_read), |
| 241 |
"Invalid position returned %d, expected %d", |
| 242 |
int(is.position()), int(offset + n_read)); |
| 243 |
|
| 244 |
// Perform second read |
| 245 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 246 |
UTEST_ASSERT_MSG( |
| 247 |
is.position() == wssize_t(offset), |
| 248 |
"Invalid position returned %d, expected %d", |
| 249 |
int(is.position()), int(offset)); |
| 250 |
|
| 251 |
n_read = is.read(buf, half); |
| 252 |
if (n_read < 0) |
| 253 |
{ |
| 254 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
UTEST_ASSERT_MSG( |
| 259 |
memcmp(buf, &src[offset], n_read) == 0, |
| 260 |
"Failed block size=%d, offset=%d", |
| 261 |
int(size), int(offset)); |
| 262 |
offset += n_read; |
| 263 |
|
| 264 |
UTEST_ASSERT_MSG( |
| 265 |
is.position() == wssize_t(offset), |
| 266 |
"Invalid position returned %d, expected %d", |
| 267 |
int(is.position()), int(offset)); |
| 268 |
} |
| 269 |
|
| 270 |
UTEST_ASSERT_MSG( |
| 271 |
is.position() == wssize_t(offset), |
| 272 |
"Invalid position returned %d, expected %d", |
| 273 |
int(is.position()), int(offset)); |
| 274 |
UTEST_ASSERT(offset == ims.size()); |
| 275 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
void test_buffered_byte_rereads_non_buffered_seeks(io::InMemoryStream & ims) |
| 280 |
{ |
| 281 |
printf("Testing buffered byte re-reads with non-buffered seeks\n"); |
| 282 |
|
| 283 |
const uint8_t * const src = ims.data(); |
| 284 |
uint8_t buf[24]; |
| 285 |
io::InBufStream is(8); |
| 286 |
is.set_buffering(true); |
| 287 |
is.set_seek_buffering(false); |
| 288 |
|
| 289 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 290 |
printf(" block_size=%d ...\n", int(size)); |
| 291 |
|
| 292 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 293 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 294 |
|
| 295 |
size_t offset = 0; |
| 296 |
while (true) |
| 297 |
{ |
| 298 |
// Perform first read |
| 299 |
ssize_t n_read = is.read(buf, size); |
| 300 |
if (n_read < 0) |
| 301 |
{ |
| 302 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 303 |
break; |
| 304 |
} |
| 305 |
|
| 306 |
UTEST_ASSERT_MSG( |
| 307 |
memcmp(buf, &src[offset], n_read) == 0, |
| 308 |
"Failed block size=%d, offset=%d", |
| 309 |
int(size), int(offset)); |
| 310 |
|
| 311 |
UTEST_ASSERT_MSG( |
| 312 |
is.position() == wssize_t(offset + n_read), |
| 313 |
"Invalid position returned %d, expected %d", |
| 314 |
int(is.position()), int(offset + n_read)); |
| 315 |
|
| 316 |
// Perform second read |
| 317 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 318 |
UTEST_ASSERT_MSG( |
| 319 |
is.position() == wssize_t(offset), |
| 320 |
"Invalid position returned %d, expected %d", |
| 321 |
int(is.position()), int(offset)); |
| 322 |
|
| 323 |
n_read = is.read_byte(); |
| 324 |
if (n_read < 0) |
| 325 |
{ |
| 326 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
UTEST_ASSERT_MSG( |
| 331 |
n_read == src[offset], |
| 332 |
"Failed read_byte at offset=%d", |
| 333 |
int(offset)); |
| 334 |
++offset; |
| 335 |
|
| 336 |
UTEST_ASSERT_MSG( |
| 337 |
is.position() == wssize_t(offset), |
| 338 |
"Invalid position returned %d, expected %d", |
| 339 |
int(is.position()), int(offset)); |
| 340 |
} |
| 341 |
|
| 342 |
UTEST_ASSERT_MSG( |
| 343 |
is.position() == wssize_t(offset), |
| 344 |
"Invalid position returned %d, expected %d", |
| 345 |
int(is.position()), int(offset)); |
| 346 |
UTEST_ASSERT(offset == ims.size()); |
| 347 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
void test_buffered_rereads_with_buffered_seeks(io::InMemoryStream & ims) |
| 352 |
{ |
| 353 |
printf("Testing buffered re-reads with buffered seeks\n"); |
| 354 |
|
| 355 |
const uint8_t * const src = ims.data(); |
| 356 |
uint8_t buf[24]; |
| 357 |
io::InBufStream is(8); |
| 358 |
is.set_buffering(true); |
| 359 |
is.set_seek_buffering(true); |
| 360 |
|
| 361 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 362 |
printf(" block_size=%d ...\n", int(size)); |
| 363 |
|
| 364 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 365 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 366 |
|
| 367 |
size_t offset = 0; |
| 368 |
const size_t half = lsp_max(size / 2, size_t(1)); |
| 369 |
while (true) |
| 370 |
{ |
| 371 |
// Perform first read |
| 372 |
ssize_t n_read = is.read(buf, size); |
| 373 |
// printf(" n_read=%d\n", int(n_read)); |
| 374 |
if (n_read < 0) |
| 375 |
{ |
| 376 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 377 |
break; |
| 378 |
} |
| 379 |
|
| 380 |
UTEST_ASSERT_MSG( |
| 381 |
memcmp(buf, &src[offset], n_read) == 0, |
| 382 |
"Failed block size=%d, offset=%d", |
| 383 |
int(size), int(offset)); |
| 384 |
|
| 385 |
UTEST_ASSERT_MSG( |
| 386 |
is.position() == wssize_t(offset + n_read), |
| 387 |
"Invalid position returned %d, expected %d", |
| 388 |
int(is.position()), int(offset + n_read)); |
| 389 |
|
| 390 |
// Perform second read |
| 391 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 392 |
UTEST_ASSERT_MSG( |
| 393 |
is.position() == wssize_t(offset), |
| 394 |
"Invalid position returned %d, expected %d", |
| 395 |
int(is.position()), int(offset)); |
| 396 |
|
| 397 |
n_read = is.read(buf, half); |
| 398 |
// printf(" n_read=%d\n", int(n_read)); |
| 399 |
if (n_read < 0) |
| 400 |
{ |
| 401 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 402 |
break; |
| 403 |
} |
| 404 |
|
| 405 |
UTEST_ASSERT_MSG( |
| 406 |
memcmp(buf, &src[offset], n_read) == 0, |
| 407 |
"Failed block size=%d, offset=%d", |
| 408 |
int(size), int(offset)); |
| 409 |
offset += n_read; |
| 410 |
|
| 411 |
UTEST_ASSERT_MSG( |
| 412 |
is.position() == wssize_t(offset), |
| 413 |
"Invalid position returned %d, expected %d", |
| 414 |
int(is.position()), int(offset)); |
| 415 |
} |
| 416 |
|
| 417 |
UTEST_ASSERT_MSG( |
| 418 |
is.position() == wssize_t(offset), |
| 419 |
"Invalid position returned %d, expected %d", |
| 420 |
int(is.position()), int(offset)); |
| 421 |
UTEST_ASSERT(offset == ims.size()); |
| 422 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
void test_buffered_byte_rereads_with_buffered_seeks(io::InMemoryStream & ims) |
| 427 |
{ |
| 428 |
printf("Testing buffered byte re-reads with buffered seeks\n"); |
| 429 |
|
| 430 |
const uint8_t * const src = ims.data(); |
| 431 |
uint8_t buf[24]; |
| 432 |
io::InBufStream is(8); |
| 433 |
is.set_buffering(true); |
| 434 |
is.set_seek_buffering(true); |
| 435 |
|
| 436 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 437 |
printf(" block_size=%d ...\n", int(size)); |
| 438 |
|
| 439 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 440 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 441 |
|
| 442 |
size_t offset = 0; |
| 443 |
while (true) |
| 444 |
{ |
| 445 |
// Perform first read |
| 446 |
ssize_t n_read = is.read(buf, size); |
| 447 |
// printf(" n_read=%d\n", int(n_read)); |
| 448 |
if (n_read < 0) |
| 449 |
{ |
| 450 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 451 |
break; |
| 452 |
} |
| 453 |
|
| 454 |
UTEST_ASSERT_MSG( |
| 455 |
memcmp(buf, &src[offset], n_read) == 0, |
| 456 |
"Failed block size=%d, offset=%d", |
| 457 |
int(size), int(offset)); |
| 458 |
|
| 459 |
UTEST_ASSERT_MSG( |
| 460 |
is.position() == wssize_t(offset + n_read), |
| 461 |
"Invalid position returned %d, expected %d", |
| 462 |
int(is.position()), int(offset + n_read)); |
| 463 |
|
| 464 |
// Perform second read |
| 465 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 466 |
UTEST_ASSERT_MSG( |
| 467 |
is.position() == wssize_t(offset), |
| 468 |
"Invalid position returned %d, expected %d", |
| 469 |
int(is.position()), int(offset)); |
| 470 |
|
| 471 |
n_read = is.read_byte(); |
| 472 |
// printf(" n_read=%d\n", int(n_read)); |
| 473 |
if (n_read < 0) |
| 474 |
{ |
| 475 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 476 |
break; |
| 477 |
} |
| 478 |
|
| 479 |
UTEST_ASSERT_MSG( |
| 480 |
n_read == src[offset], |
| 481 |
"Failed read_byte at offset=%d", |
| 482 |
int(offset)); |
| 483 |
++offset; |
| 484 |
|
| 485 |
UTEST_ASSERT_MSG( |
| 486 |
is.position() == wssize_t(offset), |
| 487 |
"Invalid position returned %d, expected %d", |
| 488 |
int(is.position()), int(offset)); |
| 489 |
} |
| 490 |
|
| 491 |
UTEST_ASSERT_MSG( |
| 492 |
is.position() == wssize_t(offset), |
| 493 |
"Invalid position returned %d, expected %d", |
| 494 |
int(is.position()), int(offset)); |
| 495 |
UTEST_ASSERT(offset == ims.size()); |
| 496 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
void test_unbuffered_skip_unbuffered_rereads(io::InMemoryStream & ims) |
| 501 |
{ |
| 502 |
printf("Testing unbuffered skip + unbuffered re-reads\n"); |
| 503 |
|
| 504 |
const uint8_t * const src = ims.data(); |
| 505 |
uint8_t buf[24]; |
| 506 |
io::InBufStream is(8); |
| 507 |
is.set_buffering(false); |
| 508 |
is.set_seek_buffering(false); |
| 509 |
|
| 510 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 511 |
printf(" block_size=%d ...\n", int(size)); |
| 512 |
|
| 513 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 514 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 515 |
|
| 516 |
size_t offset = 0; |
| 517 |
const size_t half = lsp_max(size / 2, size_t(1)); |
| 518 |
while (true) |
| 519 |
{ |
| 520 |
// Perform skip |
| 521 |
ssize_t n_read = is.skip(size); |
| 522 |
// printf(" n_skip=%d\n", int(n_read)); |
| 523 |
UTEST_ASSERT(n_read >= 0); |
| 524 |
if (n_read == 0) |
| 525 |
break; |
| 526 |
|
| 527 |
offset += n_read; |
| 528 |
UTEST_ASSERT_MSG( |
| 529 |
is.position() == wssize_t(offset), |
| 530 |
"Invalid position returned %d, expected %d", |
| 531 |
int(is.position()), int(offset)); |
| 532 |
|
| 533 |
n_read = is.read(buf, half); |
| 534 |
// printf(" n_read=%d\n", int(n_read)); |
| 535 |
if (n_read < 0) |
| 536 |
{ |
| 537 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 538 |
break; |
| 539 |
} |
| 540 |
UTEST_ASSERT_MSG( |
| 541 |
memcmp(buf, &src[offset], n_read) == 0, |
| 542 |
"Failed block size=%d, offset=%d", |
| 543 |
int(size), int(offset)); |
| 544 |
offset += n_read; |
| 545 |
UTEST_ASSERT_MSG( |
| 546 |
is.position() == wssize_t(offset), |
| 547 |
"Invalid position returned %d, expected %d", |
| 548 |
int(is.position()), int(offset)); |
| 549 |
|
| 550 |
// Perform backward seek |
| 551 |
offset -= size; |
| 552 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 553 |
UTEST_ASSERT_MSG( |
| 554 |
is.position() == wssize_t(offset), |
| 555 |
"Invalid position returned %d, expected %d", |
| 556 |
int(is.position()), int(offset)); |
| 557 |
|
| 558 |
n_read = is.read(buf, half); |
| 559 |
// printf(" n_read=%d\n", int(n_read)); |
| 560 |
if (n_read < 0) |
| 561 |
{ |
| 562 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 563 |
break; |
| 564 |
} |
| 565 |
|
| 566 |
UTEST_ASSERT_MSG( |
| 567 |
memcmp(buf, &src[offset], n_read) == 0, |
| 568 |
"Failed block size=%d, offset=%d", |
| 569 |
int(size), int(offset)); |
| 570 |
offset += n_read; |
| 571 |
|
| 572 |
UTEST_ASSERT_MSG( |
| 573 |
is.position() == wssize_t(offset), |
| 574 |
"Invalid position returned %d, expected %d", |
| 575 |
int(is.position()), int(offset)); |
| 576 |
} |
| 577 |
|
| 578 |
UTEST_ASSERT_MSG( |
| 579 |
is.position() == wssize_t(offset), |
| 580 |
"Invalid position returned %d, expected %d", |
| 581 |
int(is.position()), int(offset)); |
| 582 |
UTEST_ASSERT(offset == ims.size()); |
| 583 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
void test_unbuffered_skip_buffered_rereads(io::InMemoryStream & ims) |
| 588 |
{ |
| 589 |
printf("Testing unbuffered skip + buffered re-reads\n"); |
| 590 |
|
| 591 |
const uint8_t * const src = ims.data(); |
| 592 |
uint8_t buf[24]; |
| 593 |
io::InBufStream is(8); |
| 594 |
is.set_buffering(true); |
| 595 |
is.set_seek_buffering(false); |
| 596 |
|
| 597 |
UTEST_FOREACH(size, 1, 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 598 |
printf(" block_size=%d ...\n", int(size)); |
| 599 |
|
| 600 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 601 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 602 |
|
| 603 |
size_t offset = 0; |
| 604 |
const size_t half = lsp_max(size / 2, size_t(1)); |
| 605 |
while (true) |
| 606 |
{ |
| 607 |
// Perform skip |
| 608 |
ssize_t n_read = is.skip(size); |
| 609 |
// printf(" n_skip=%d\n", int(n_read)); |
| 610 |
UTEST_ASSERT(n_read >= 0); |
| 611 |
if (n_read == 0) |
| 612 |
break; |
| 613 |
|
| 614 |
offset += n_read; |
| 615 |
UTEST_ASSERT_MSG( |
| 616 |
is.position() == wssize_t(offset), |
| 617 |
"Invalid position returned %d, expected %d", |
| 618 |
int(is.position()), int(offset)); |
| 619 |
|
| 620 |
n_read = is.read(buf, half); |
| 621 |
// printf(" n_read=%d\n", int(n_read)); |
| 622 |
if (n_read < 0) |
| 623 |
{ |
| 624 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 625 |
break; |
| 626 |
} |
| 627 |
UTEST_ASSERT_MSG( |
| 628 |
memcmp(buf, &src[offset], n_read) == 0, |
| 629 |
"Failed block size=%d, offset=%d", |
| 630 |
int(size), int(offset)); |
| 631 |
offset += n_read; |
| 632 |
UTEST_ASSERT_MSG( |
| 633 |
is.position() == wssize_t(offset), |
| 634 |
"Invalid position returned %d, expected %d", |
| 635 |
int(is.position()), int(offset)); |
| 636 |
|
| 637 |
// Perform backward seek |
| 638 |
offset -= size; |
| 639 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 640 |
UTEST_ASSERT_MSG( |
| 641 |
is.position() == wssize_t(offset), |
| 642 |
"Invalid position returned %d, expected %d", |
| 643 |
int(is.position()), int(offset)); |
| 644 |
|
| 645 |
n_read = is.read(buf, half); |
| 646 |
// printf(" n_read=%d\n", int(n_read)); |
| 647 |
if (n_read < 0) |
| 648 |
{ |
| 649 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 650 |
break; |
| 651 |
} |
| 652 |
|
| 653 |
UTEST_ASSERT_MSG( |
| 654 |
memcmp(buf, &src[offset], n_read) == 0, |
| 655 |
"Failed block size=%d, offset=%d", |
| 656 |
int(size), int(offset)); |
| 657 |
offset += n_read; |
| 658 |
|
| 659 |
UTEST_ASSERT_MSG( |
| 660 |
is.position() == wssize_t(offset), |
| 661 |
"Invalid position returned %d, expected %d", |
| 662 |
int(is.position()), int(offset)); |
| 663 |
} |
| 664 |
|
| 665 |
UTEST_ASSERT_MSG( |
| 666 |
is.position() == wssize_t(offset), |
| 667 |
"Invalid position returned %d, expected %d", |
| 668 |
int(is.position()), int(offset)); |
| 669 |
UTEST_ASSERT(offset == ims.size()); |
| 670 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
void test_buffered_skip_buffered_rereads(io::InMemoryStream & ims) |
| 675 |
{ |
| 676 |
printf("Testing buffered skip + buffered re-reads\n"); |
| 677 |
|
| 678 |
const uint8_t * const src = ims.data(); |
| 679 |
uint8_t buf[24]; |
| 680 |
io::InBufStream is(8); |
| 681 |
is.set_buffering(true); |
| 682 |
is.set_seek_buffering(true); |
| 683 |
|
| 684 |
UTEST_FOREACH(size, /*1,*/ 2, 3, 5, 7, 8, 11, 13, 16, 23) { |
| 685 |
printf(" block_size=%d ...\n", int(size)); |
| 686 |
|
| 687 |
UTEST_ASSERT(ims.seek(0) == 0); |
| 688 |
UTEST_ASSERT(is.wrap(&ims) == STATUS_OK); |
| 689 |
|
| 690 |
size_t offset = 0; |
| 691 |
const size_t half = lsp_max(size / 2, size_t(1)); |
| 692 |
while (true) |
| 693 |
{ |
| 694 |
// Perform skip |
| 695 |
ssize_t n_read = is.skip(size); |
| 696 |
// printf(" n_skip=%d\n", int(n_read)); |
| 697 |
UTEST_ASSERT(n_read >= 0); |
| 698 |
if (n_read == 0) |
| 699 |
break; |
| 700 |
|
| 701 |
offset += n_read; |
| 702 |
UTEST_ASSERT_MSG( |
| 703 |
is.position() == wssize_t(offset), |
| 704 |
"Invalid position returned %d, expected %d", |
| 705 |
int(is.position()), int(offset)); |
| 706 |
|
| 707 |
n_read = is.read(buf, half); |
| 708 |
// printf(" n_read=%d\n", int(n_read)); |
| 709 |
if (n_read < 0) |
| 710 |
{ |
| 711 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 712 |
break; |
| 713 |
} |
| 714 |
UTEST_ASSERT_MSG( |
| 715 |
memcmp(buf, &src[offset], n_read) == 0, |
| 716 |
"Failed block size=%d, offset=%d", |
| 717 |
int(size), int(offset)); |
| 718 |
offset += n_read; |
| 719 |
UTEST_ASSERT_MSG( |
| 720 |
is.position() == wssize_t(offset), |
| 721 |
"Invalid position returned %d, expected %d", |
| 722 |
int(is.position()), int(offset)); |
| 723 |
|
| 724 |
// Perform backward seek |
| 725 |
offset -= size; |
| 726 |
UTEST_ASSERT(is.seek(offset) == wssize_t(offset)); |
| 727 |
UTEST_ASSERT_MSG( |
| 728 |
is.position() == wssize_t(offset), |
| 729 |
"Invalid position returned %d, expected %d", |
| 730 |
int(is.position()), int(offset)); |
| 731 |
|
| 732 |
n_read = is.read(buf, half); |
| 733 |
// printf(" n_read=%d\n", int(n_read)); |
| 734 |
if (n_read < 0) |
| 735 |
{ |
| 736 |
UTEST_ASSERT(n_read == -STATUS_EOF); |
| 737 |
break; |
| 738 |
} |
| 739 |
|
| 740 |
UTEST_ASSERT_MSG( |
| 741 |
memcmp(buf, &src[offset], n_read) == 0, |
| 742 |
"Failed block size=%d, offset=%d", |
| 743 |
int(size), int(offset)); |
| 744 |
offset += n_read; |
| 745 |
|
| 746 |
UTEST_ASSERT_MSG( |
| 747 |
is.position() == wssize_t(offset), |
| 748 |
"Invalid position returned %d, expected %d", |
| 749 |
int(is.position()), int(offset)); |
| 750 |
} |
| 751 |
|
| 752 |
UTEST_ASSERT_MSG( |
| 753 |
is.position() == wssize_t(offset), |
| 754 |
"Invalid position returned %d, expected %d", |
| 755 |
int(is.position()), int(offset)); |
| 756 |
UTEST_ASSERT(offset == ims.size()); |
| 757 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
UTEST_MAIN |
| 762 |
{ |
| 763 |
uint8_t *data = new uint8_t[0x80]; |
| 764 |
UTEST_ASSERT(data != NULL); |
| 765 |
for (size_t i=0; i<0x80; ++i) |
| 766 |
data[i] = uint8_t(i + 1); |
| 767 |
|
| 768 |
io::InMemoryStream ims; |
| 769 |
ims.wrap(data, 0x80, MEMDROP_ARR_DELETE); |
| 770 |
|
| 771 |
test_unbuffered_reads(ims); |
| 772 |
test_unbuffered_byte_reads(ims); |
| 773 |
test_buffered_reads(ims); |
| 774 |
test_buffered_byte_reads(ims); |
| 775 |
test_buffered_rereads_non_buffered_seeks(ims); |
| 776 |
test_buffered_byte_rereads_non_buffered_seeks(ims); |
| 777 |
test_buffered_rereads_with_buffered_seeks(ims); |
| 778 |
test_buffered_byte_rereads_with_buffered_seeks(ims); |
| 779 |
test_unbuffered_skip_unbuffered_rereads(ims); |
| 780 |
test_unbuffered_skip_buffered_rereads(ims); |
| 781 |
test_buffered_skip_buffered_rereads(ims); |
| 782 |
} |
| 783 |
UTEST_END |
| 784 |
|
| 785 |
|