@@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
253
253
BOOST_CHECK (false );
254
254
} catch (const std::exception& e) {
255
255
BOOST_CHECK (strstr (e.what (),
256
- " Read attempted past buffer limit" ) != nullptr );
256
+ " Attempt to position past buffer limit" ) != nullptr );
257
257
}
258
258
// The default argument removes the limit completely.
259
259
BOOST_CHECK (bf.SetLimit ());
@@ -322,14 +322,63 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
322
322
BOOST_CHECK (!bf.SetPos (0 ));
323
323
// But we should now be positioned at least as far back as allowed
324
324
// by the rewind window (relative to our farthest read position, 40).
325
- BOOST_CHECK (bf.GetPos () <= 30 );
325
+ BOOST_CHECK (bf.GetPos () <= 30U );
326
326
327
327
// We can explicitly close the file, or the destructor will do it.
328
328
bf.fclose ();
329
329
330
330
fs::remove (streams_test_filename);
331
331
}
332
332
333
+ BOOST_AUTO_TEST_CASE (streams_buffered_file_skip)
334
+ {
335
+ fs::path streams_test_filename = m_args.GetDataDirBase () / " streams_test_tmp" ;
336
+ FILE* file = fsbridge::fopen (streams_test_filename, " w+b" );
337
+ // The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
338
+ for (uint8_t j = 0 ; j < 40 ; ++j) {
339
+ fwrite (&j, 1 , 1 , file);
340
+ }
341
+ rewind (file);
342
+
343
+ // The buffer is 25 bytes, allow rewinding 10 bytes.
344
+ CBufferedFile bf (file, 25 , 10 , 222 , 333 );
345
+
346
+ uint8_t i;
347
+ // This is like bf >> (7-byte-variable), in that it will cause data
348
+ // to be read from the file into memory, but it's not copied to us.
349
+ bf.SkipTo (7 );
350
+ BOOST_CHECK_EQUAL (bf.GetPos (), 7U );
351
+ bf >> i;
352
+ BOOST_CHECK_EQUAL (i, 7 );
353
+
354
+ // The bytes in the buffer up to offset 7 are valid and can be read.
355
+ BOOST_CHECK (bf.SetPos (0 ));
356
+ bf >> i;
357
+ BOOST_CHECK_EQUAL (i, 0 );
358
+ bf >> i;
359
+ BOOST_CHECK_EQUAL (i, 1 );
360
+
361
+ bf.SkipTo (11 );
362
+ bf >> i;
363
+ BOOST_CHECK_EQUAL (i, 11 );
364
+
365
+ // SkipTo() honors the transfer limit; we can't position beyond the limit.
366
+ bf.SetLimit (13 );
367
+ try {
368
+ bf.SkipTo (14 );
369
+ BOOST_CHECK (false );
370
+ } catch (const std::exception& e) {
371
+ BOOST_CHECK (strstr (e.what (), " Attempt to position past buffer limit" ) != nullptr );
372
+ }
373
+
374
+ // We can position exactly to the transfer limit.
375
+ bf.SkipTo (13 );
376
+ BOOST_CHECK_EQUAL (bf.GetPos (), 13U );
377
+
378
+ bf.fclose ();
379
+ fs::remove (streams_test_filename);
380
+ }
381
+
333
382
BOOST_AUTO_TEST_CASE (streams_buffered_file_rand)
334
383
{
335
384
// Make this test deterministic.
@@ -361,7 +410,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
361
410
// sizes; the boundaries of the objects can interact arbitrarily
362
411
// with the CBufferFile's internal buffer. These first three
363
412
// cases simulate objects of various sizes (1, 2, 5 bytes).
364
- switch (InsecureRandRange (5 )) {
413
+ switch (InsecureRandRange (6 )) {
365
414
case 0 : {
366
415
uint8_t a[1 ];
367
416
if (currentPos + 1 > fileSize)
@@ -399,6 +448,16 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
399
448
break ;
400
449
}
401
450
case 3 : {
451
+ // SkipTo is similar to the "read" cases above, except
452
+ // we don't receive the data.
453
+ size_t skip_length{static_cast <size_t >(InsecureRandRange (5 ))};
454
+ if (currentPos + skip_length > fileSize) continue ;
455
+ bf.SetLimit (currentPos + skip_length);
456
+ bf.SkipTo (currentPos + skip_length);
457
+ currentPos += skip_length;
458
+ break ;
459
+ }
460
+ case 4 : {
402
461
// Find a byte value (that is at or ahead of the current position).
403
462
size_t find = currentPos + InsecureRandRange (8 );
404
463
if (find >= fileSize)
@@ -415,7 +474,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
415
474
currentPos++;
416
475
break ;
417
476
}
418
- case 4 : {
477
+ case 5 : {
419
478
size_t requestPos = InsecureRandRange (maxPos + 4 );
420
479
bool okay = bf.SetPos (requestPos);
421
480
// The new position may differ from the requested position
0 commit comments