@@ -30,6 +30,7 @@ POSSIBILITY OF SUCH DAMAGE.
30
30
31
31
*/
32
32
33
+ #include < iostream>
33
34
#include " test.hpp"
34
35
#include " setup_transfer.hpp"
35
36
@@ -368,12 +369,196 @@ TORRENT_TEST(piece_range)
368
369
TEST_CHECK (aux::file_piece_range_exclusive (fs, file_index_t (1 )) == std::make_tuple (piece_index_t (3 ), piece_index_t (7 )));
369
370
}
370
371
371
- // TODO: test file_storage::optimize
372
- // TODO: test map_block
373
- // TODO: test piece_size(int piece)
374
- // TODO: test file_index_at_offset
372
+ void test_optimize (std::vector<int > file_sizes
373
+ , int const alignment
374
+ , int const pad_file_limit
375
+ , bool const tail_padding
376
+ , std::vector<int > const expected_order)
377
+ {
378
+ file_storage fs;
379
+ int i = 0 ;
380
+ for (int s : file_sizes)
381
+ {
382
+ fs.add_file (combine_path (" test" , std::to_string (i++)), s);
383
+ }
384
+ fs.optimize (pad_file_limit, alignment, tail_padding);
385
+
386
+ TEST_EQUAL (fs.num_files (), int (expected_order.size ()));
387
+ if (fs.num_files () != int (expected_order.size ())) return ;
388
+
389
+ file_index_t idx{0 };
390
+ int num_pad_files = 0 ;
391
+ std::cout << " { " ;
392
+ for (file_index_t i{0 }; i != fs.end_file (); ++i)
393
+ {
394
+ if (fs.file_flags (i) & file_storage::flag_pad_file) std::cout << " *" ;
395
+ std::cout << fs.file_size (i) << " " ;
396
+ }
397
+ std::cout << " }\n " ;
398
+ for (int expect : expected_order)
399
+ {
400
+ if (expect == -1 )
401
+ {
402
+ TEST_CHECK (fs.file_flags (idx) & file_storage::flag_pad_file);
403
+ TEST_EQUAL (fs.file_name (idx), std::to_string (num_pad_files++));
404
+ }
405
+ else
406
+ {
407
+ TEST_EQUAL (fs.file_name (idx), std::to_string (expect));
408
+ TEST_EQUAL (fs.file_size (idx), file_sizes[expect]);
409
+ }
410
+ ++idx;
411
+ }
412
+ }
413
+
414
+ TORRENT_TEST (optimize_order_large_first)
415
+ {
416
+ test_optimize ({1000 , 3000 , 10000 }, 1024 , 1024 , false , {2 , -1 , 1 , 0 });
417
+ }
418
+
419
+ TORRENT_TEST (optimize_tail_padding2)
420
+ {
421
+ // when tail padding is enabled, a pad file is added at the end
422
+ test_optimize ({2000 }, 1024 , 1024 , true , {0 , -1 });
423
+ }
424
+
425
+ TORRENT_TEST (optimize_tail_padding3)
426
+ {
427
+ // when tail padding is enabled, a pad file is added at the end, even if the
428
+ // file is smaller than the alignment, as long as pad_file_limit is 0 *(which
429
+ // means files are aligned unconditionally)
430
+ test_optimize ({1000 }, 1024 , 0 , true , {0 , -1 });
431
+ }
432
+
433
+ TORRENT_TEST (optimize_tail_padding_small_files)
434
+ {
435
+ // files smaller than the pad file limit are not tail-padded
436
+ test_optimize ({1000 , 1 , 2 }, 1024 , 50 , true , {0 , -1 , 2 , 1 });
437
+ }
438
+
439
+ TORRENT_TEST (optimize_tail_padding_small_files2)
440
+ {
441
+ // files larger than the pad file limit are not tail-padded
442
+ test_optimize ({1000 , 1 , 2 }, 1024 , 0 , true , {0 , -1 , 2 , -1 , 1 , -1 });
443
+ }
444
+
445
+ TORRENT_TEST (optimize_prioritize_aligned_size)
446
+ {
447
+ // file 0 of size 1024 will be chosen over the larger file, since it won't
448
+ // affect the alignment of the next file
449
+ test_optimize ({1024 , 3000 , 10 }, 1024 , 1024 , false , {0 , 1 , 2 });
450
+ }
451
+
452
+ TORRENT_TEST (optimize_fill_with_small_files)
453
+ {
454
+ // fill in space that otherwise would just be a pad file with other small
455
+ // files.
456
+ test_optimize ({2000 , 5000 , 48 , 120 }, 1024 , 1024 , false , {1 , 3 , 0 , 2 });
457
+ }
458
+
459
+ TORRENT_TEST (optimize_pad_all)
460
+ {
461
+ // when pad_size_limit is 0, every file is padded to alignment, regardless of
462
+ // how big it is
463
+ // the empty file is first, since it doesn't affect alignment of the next
464
+ // file
465
+ test_optimize ({48 , 1 , 0 , 5000 }, 1024 , 0 , false , {2 , 3 , -1 , 0 , -1 , 1 });
466
+ }
467
+
468
+ TORRENT_TEST (optimize_pad_all_with_tail)
469
+ {
470
+ // when pad_size_limit is 0, every file is padded to alignment, regardless of
471
+ // how big it is, also with tail-padding enabled
472
+ test_optimize ({48 , 1 , 0 , 5000 }, 1024 , 0 , true , {2 , 3 , -1 , 0 , -1 , 1 , -1 });
473
+ }
474
+
475
+ TORRENT_TEST (piece_size_last_piece)
476
+ {
477
+ file_storage fs;
478
+ fs.set_piece_length (1024 );
479
+ fs.add_file (" 0" , 100 );
480
+ fs.set_num_pieces (int ((fs.total_size () + 1023 ) / 1024 ));
481
+ TEST_EQUAL (fs.piece_size (piece_index_t {0 }), 100 );
482
+ }
483
+
484
+ TORRENT_TEST (piece_size_middle_piece)
485
+ {
486
+ file_storage fs;
487
+ fs.set_piece_length (1024 );
488
+ fs.add_file (" 0" , 2000 );
489
+ fs.set_num_pieces (int ((fs.total_size () + 1023 ) / 1024 ));
490
+ TEST_EQUAL (fs.piece_size (piece_index_t {0 }), 1024 );
491
+ TEST_EQUAL (fs.piece_size (piece_index_t {1 }), 2000 - 1024 );
492
+ }
493
+
494
+ TORRENT_TEST (file_index_at_offset)
495
+ {
496
+ file_storage fs;
497
+ fs.set_piece_length (1024 );
498
+ fs.add_file (" test/0" , 1 );
499
+ fs.add_file (" test/1" , 2 );
500
+ fs.add_file (" test/2" , 3 );
501
+ fs.add_file (" test/3" , 4 );
502
+ fs.add_file (" test/4" , 5 );
503
+ std::int64_t offset = 0 ;
504
+ for (int f : {0 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 })
505
+ {
506
+ TEST_EQUAL (fs.file_index_at_offset (offset++), file_index_t {f});
507
+ }
508
+ }
509
+
510
+ TORRENT_TEST (map_block_start)
511
+ {
512
+ file_storage fs;
513
+ fs.set_piece_length (1024 );
514
+ fs.add_file (" test/0" , 1 );
515
+ fs.add_file (" test/1" , 2 );
516
+ fs.add_file (" test/2" , 3 );
517
+ fs.add_file (" test/3" , 4 );
518
+ fs.add_file (" test/4" , 5 );
519
+ fs.set_num_pieces (int ((fs.total_size () + 1023 ) / 1024 ));
520
+ int len = 0 ;
521
+ for (int f : {0 , 1 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 5 , 5 , 5 , 5 , 5 })
522
+ {
523
+ std::vector<file_slice> const map = fs.map_block (piece_index_t {0 }, 0 , len);
524
+ TEST_EQUAL (int (map.size ()), f);
525
+ file_index_t file_index{0 };
526
+ std::int64_t actual_len = 0 ;
527
+ for (auto file : map)
528
+ {
529
+ TEST_EQUAL (file.file_index , file_index++);
530
+ TEST_EQUAL (file.offset , 0 );
531
+ actual_len += file.size ;
532
+ }
533
+ TEST_EQUAL (actual_len, len);
534
+ ++len;
535
+ }
536
+ }
537
+
538
+ TORRENT_TEST (map_block_mid)
539
+ {
540
+ file_storage fs;
541
+ fs.set_piece_length (1024 );
542
+ fs.add_file (" test/0" , 1 );
543
+ fs.add_file (" test/1" , 2 );
544
+ fs.add_file (" test/2" , 3 );
545
+ fs.add_file (" test/3" , 4 );
546
+ fs.add_file (" test/4" , 5 );
547
+ fs.set_num_pieces (int ((fs.total_size () + 1023 ) / 1024 ));
548
+ int offset = 0 ;
549
+ for (int f : {0 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 4 })
550
+ {
551
+ std::vector<file_slice> const map = fs.map_block (piece_index_t {0 }, offset, 1 );
552
+ TEST_EQUAL (int (map.size ()), 1 );
553
+ auto const & file = map[0 ];
554
+ TEST_EQUAL (file.file_index , file_index_t {f});
555
+ TEST_CHECK (file.offset <= offset);
556
+ TEST_EQUAL (file.size , 1 );
557
+ ++offset;
558
+ }
559
+ }
560
+
375
561
// TODO: test file attributes
376
562
// TODO: test symlinks
377
- // TODO: test pad_files
378
563
// TODO: test reorder_file (make sure internal_file_entry::swap() is used)
379
564
0 commit comments