2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2021-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/block-info.h>
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using BlockInfoTest = ::testing::Test;
|
|
|
|
|
|
|
|
TEST_F(BlockInfoTest, fieldsAreSet)
|
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024 } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 4U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * PieceCount;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
EXPECT_EQ(ExpectedBlockSize, info.blockSize(info.blockCount() - 1));
|
|
|
|
EXPECT_EQ(PieceCount, info.pieceCount());
|
|
|
|
EXPECT_EQ(PieceSize, info.pieceSize(info.pieceCount() - 1));
|
|
|
|
EXPECT_EQ(PieceSize, info.pieceSize());
|
|
|
|
EXPECT_EQ(TotalSize, info.totalSize());
|
2021-11-25 20:30:13 +00:00
|
|
|
|
|
|
|
info.initSizes(0, 0);
|
2022-04-14 01:22:59 +00:00
|
|
|
EXPECT_EQ(0U, info.blockSize(info.blockCount() - 1));
|
|
|
|
EXPECT_EQ(0U, info.pieceCount());
|
|
|
|
EXPECT_EQ(0U, info.pieceSize(info.pieceCount() - 1));
|
|
|
|
EXPECT_EQ(0U, info.pieceSize());
|
|
|
|
EXPECT_EQ(0U, info.totalSize());
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlockInfoTest, handlesOddSize)
|
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
EXPECT_EQ(1U, info.blockSize(info.blockCount() - 1));
|
|
|
|
EXPECT_EQ(1U, info.pieceSize(info.pieceCount() - 1));
|
|
|
|
EXPECT_EQ(PieceCount, info.pieceCount());
|
|
|
|
EXPECT_EQ(PieceSize, info.pieceSize());
|
|
|
|
EXPECT_EQ(TotalSize, info.totalSize());
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
TEST_F(BlockInfoTest, pieceSize)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
EXPECT_EQ(PieceSize, info.pieceSize(info.pieceCount() - 2));
|
|
|
|
EXPECT_EQ(1U, info.pieceSize(info.pieceCount() - 1));
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
TEST_F(BlockInfoTest, blockSize)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1) + 1;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
EXPECT_EQ(ExpectedBlockSize, info.blockSize(info.blockCount() - 2));
|
|
|
|
EXPECT_EQ(1U, info.blockSize(info.blockCount() - 1));
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
TEST_F(BlockInfoTest, blockSpanForPiece)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, info.blockSpanForPiece(0).begin);
|
|
|
|
EXPECT_EQ(4U, info.blockSpanForPiece(0).end);
|
|
|
|
EXPECT_EQ(12U, info.blockSpanForPiece(3).begin);
|
|
|
|
EXPECT_EQ(16U, info.blockSpanForPiece(3).end);
|
|
|
|
EXPECT_EQ(16U, info.blockSpanForPiece(4).begin);
|
|
|
|
EXPECT_EQ(17U, info.blockSpanForPiece(4).end);
|
2022-02-18 23:17:19 +00:00
|
|
|
|
|
|
|
// test that uninitialized block_info returns an invalid span
|
|
|
|
info = tr_block_info{};
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, info.blockSpanForPiece(0).begin);
|
|
|
|
EXPECT_EQ(0U, info.blockSpanForPiece(0).end);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
TEST_F(BlockInfoTest, blockLoc)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2021-11-24 14:48:52 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// begin
|
|
|
|
auto loc = info.blockLoc(0);
|
|
|
|
EXPECT_EQ(tr_block_info::Location{}, loc);
|
|
|
|
|
|
|
|
// third block is halfway through the first piece
|
|
|
|
loc = info.blockLoc(2);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(ExpectedBlockSize * 2U, loc.byte);
|
|
|
|
EXPECT_EQ(2U, loc.block);
|
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
|
|
|
EXPECT_EQ(0U, loc.piece);
|
|
|
|
EXPECT_EQ(ExpectedBlockSize * 2U, loc.piece_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
|
|
|
|
// second piece aligns with fifth block
|
|
|
|
loc = info.blockLoc(4);
|
|
|
|
EXPECT_EQ(PieceSize, loc.byte);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(4U, loc.block);
|
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
|
|
|
EXPECT_EQ(1U, loc.piece);
|
|
|
|
EXPECT_EQ(0U, loc.piece_offset);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
TEST_F(BlockInfoTest, pieceLoc)
|
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2022-02-18 23:17:19 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2022-02-18 23:17:19 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
|
|
|
// begin
|
|
|
|
auto loc = info.pieceLoc(0);
|
|
|
|
EXPECT_EQ(tr_block_info::Location{}, loc);
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < PieceCount; ++i)
|
|
|
|
{
|
|
|
|
loc = info.pieceLoc(i);
|
|
|
|
EXPECT_EQ(info.blockLoc(i * ExpectedBlocksPerPiece), loc);
|
|
|
|
EXPECT_EQ(PieceSize * i, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece * i, loc.block);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(i, loc.piece);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.piece_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loc = info.pieceLoc(0, PieceSize - 1);
|
|
|
|
EXPECT_EQ(PieceSize - 1, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece - 1, loc.block);
|
|
|
|
EXPECT_EQ(ExpectedBlockSize - 1, loc.block_offset);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.piece);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(PieceSize - 1, loc.piece_offset);
|
|
|
|
|
|
|
|
loc = info.pieceLoc(0, PieceSize);
|
|
|
|
EXPECT_EQ(PieceSize, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece, loc.block);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
|
|
|
EXPECT_EQ(1U, loc.piece);
|
|
|
|
EXPECT_EQ(0U, loc.piece_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
|
|
|
|
loc = info.pieceLoc(0, PieceSize + 1);
|
|
|
|
EXPECT_EQ(PieceSize + 1, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece, loc.block);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, loc.block_offset);
|
|
|
|
EXPECT_EQ(1U, loc.piece);
|
|
|
|
EXPECT_EQ(1U, loc.piece_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlockInfoTest, byteLoc)
|
|
|
|
{
|
2022-11-14 20:16:29 +00:00
|
|
|
static auto constexpr ExpectedBlockSize = uint64_t{ 1024U } * 16U;
|
|
|
|
static auto constexpr ExpectedBlocksPerPiece = uint64_t{ 4U };
|
|
|
|
static auto constexpr PieceSize = ExpectedBlockSize * ExpectedBlocksPerPiece;
|
|
|
|
static auto constexpr PieceCount = uint64_t{ 5U };
|
|
|
|
static auto constexpr TotalSize = PieceSize * (PieceCount - 1U) + 1U;
|
2022-02-18 23:17:19 +00:00
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto info = tr_block_info{};
|
2022-02-18 23:17:19 +00:00
|
|
|
info.initSizes(TotalSize, PieceSize);
|
|
|
|
|
|
|
|
auto loc = info.byteLoc(0);
|
|
|
|
EXPECT_EQ(tr_block_info::Location{}, loc);
|
|
|
|
|
|
|
|
loc = info.byteLoc(1);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, loc.byte);
|
|
|
|
EXPECT_EQ(0U, loc.block);
|
|
|
|
EXPECT_EQ(1U, loc.block_offset);
|
|
|
|
EXPECT_EQ(0U, loc.piece);
|
|
|
|
EXPECT_EQ(1U, loc.piece_offset);
|
2022-02-18 23:17:19 +00:00
|
|
|
|
|
|
|
auto n = ExpectedBlockSize - 1;
|
|
|
|
loc = info.byteLoc(n);
|
|
|
|
EXPECT_EQ(n, loc.byte);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.block);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(n, loc.block_offset);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.piece);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(n, loc.piece_offset);
|
|
|
|
|
|
|
|
n = ExpectedBlockSize;
|
|
|
|
loc = info.byteLoc(n);
|
|
|
|
EXPECT_EQ(n, loc.byte);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, loc.block);
|
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
|
|
|
EXPECT_EQ(0U, loc.piece);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(n, loc.piece_offset);
|
|
|
|
|
|
|
|
n = ExpectedBlockSize + 1;
|
|
|
|
loc = info.byteLoc(n);
|
|
|
|
EXPECT_EQ(n, loc.byte);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(1U, loc.block);
|
|
|
|
EXPECT_EQ(1U, loc.block_offset);
|
|
|
|
EXPECT_EQ(0U, loc.piece);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(n, loc.piece_offset);
|
|
|
|
|
|
|
|
n = PieceSize - 1;
|
|
|
|
loc = info.byteLoc(n);
|
|
|
|
EXPECT_EQ(n, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece - 1, loc.block);
|
|
|
|
EXPECT_EQ(ExpectedBlockSize - 1, loc.block_offset);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.piece);
|
2022-02-18 23:17:19 +00:00
|
|
|
EXPECT_EQ(n, loc.piece_offset);
|
|
|
|
|
|
|
|
n = PieceSize;
|
|
|
|
loc = info.byteLoc(n);
|
|
|
|
EXPECT_EQ(n, loc.byte);
|
|
|
|
EXPECT_EQ(ExpectedBlocksPerPiece, loc.block);
|
2022-04-02 14:06:02 +00:00
|
|
|
EXPECT_EQ(0U, loc.block_offset);
|
|
|
|
EXPECT_EQ(1U, loc.piece);
|
|
|
|
EXPECT_EQ(0U, loc.piece_offset);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|