transmission/tests/libtransmission/strbuf-test.cc

212 lines
5.5 KiB
C++
Raw Normal View History

// This file Copyright (C) 2022 Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <string_view>
#include "transmission.h"
#include "tr-strbuf.h"
#include "test-fixtures.h"
using StrbufTest = ::testing::Test;
using namespace std::literals;
TEST_F(StrbufTest, append)
{
static auto constexpr Value = "Hello, World!"sv;
auto buf = tr_pathbuf{};
buf.append(Value.substr(0, 5));
EXPECT_EQ(Value.substr(0, 5), buf.sv());
buf.append(Value.substr(5));
EXPECT_EQ(Value, buf.sv());
}
TEST_F(StrbufTest, assign)
{
static auto constexpr Value = "Hello, World!"sv;
auto buf = tr_pathbuf{};
buf = Value;
EXPECT_EQ(Value, buf.sv());
}
TEST_F(StrbufTest, cStr)
{
static char const* const Value = "Hello, World!";
auto buf = tr_pathbuf{ Value };
EXPECT_STREQ(Value, buf.c_str());
EXPECT_EQ(strlen(Value), std::size(buf));
buf = tr_pathbuf{ "H", Value + 1 };
EXPECT_STREQ(Value, buf.c_str());
EXPECT_EQ(strlen(Value), std::size(buf));
}
TEST_F(StrbufTest, clear)
{
static auto constexpr Value = "Hello, World!"sv;
auto buf = tr_pathbuf{ Value };
EXPECT_EQ(Value, buf.sv());
buf.clear();
EXPECT_TRUE(std::empty(buf));
EXPECT_EQ(0U, std::size(buf));
EXPECT_EQ(""sv, buf.sv());
}
TEST_F(StrbufTest, constructorDefault)
{
auto buf = tr_pathbuf{};
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(0U, std::size(buf));
EXPECT_TRUE(std::empty(buf));
}
TEST_F(StrbufTest, constructorAssign)
{
static auto constexpr Value = "Hello, World!"sv;
auto buf = tr_pathbuf{ Value };
EXPECT_EQ(Value, buf.sv());
buf = tr_pathbuf{ Value.substr(7, 5), Value.substr(5, 2), Value.substr(0, 5), Value.substr(12, 1) };
EXPECT_EQ("World, Hello!"sv, buf.sv());
buf = tr_pathbuf{ "Hello, ", "World!" };
EXPECT_EQ(Value, buf.sv());
}
TEST_F(StrbufTest, heap)
{
static auto constexpr Value = "Hello, World!"sv;
auto buf = tr_strbuf<char, 10>{};
buf.append(Value.substr(0, 5));
auto const* const data_stack = std::data(buf);
buf.append(Value.substr(5));
auto const* const data_heap = std::data(buf);
EXPECT_EQ(Value, buf.sv());
EXPECT_NE(data_stack, data_heap);
}
TEST_F(StrbufTest, indexOperator)
{
static auto constexpr Value1 = "Hello, World!"sv;
static auto constexpr Value2 = "Wello, World!"sv;
// mutable
{
auto buf = tr_pathbuf{ Value1 };
buf.at(0) = 'W';
EXPECT_EQ(Value2, buf.sv());
}
// const
{
auto const buf = tr_pathbuf{ Value1 };
EXPECT_EQ(Value1.front(), buf.at(0));
}
}
TEST_F(StrbufTest, iterators)
{
static auto constexpr Value = "Hello, World!"sv;
// mutable
{
auto buf = tr_pathbuf{ Value };
auto begin = std::begin(buf);
auto end = std::end(buf);
EXPECT_EQ(Value.front(), *begin);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(std::size(Value), static_cast<size_t>(std::distance(begin, end)));
}
// const
{
auto const buf = tr_pathbuf{ Value };
auto const begin = std::begin(buf);
auto const end = std::end(buf);
EXPECT_EQ(Value.front(), *begin);
fix: sonarcloud (#2865) * refactor: implement FileTreeItem::children_ with a std::vector * fix: std::move should not be called on forwarding reference * fix: uninitialized scalar variable * fix: unchecked return value from library * fix: dereference before null check * fix: unchecked return value from library * fix: unchecked return value from library * fixup! refactor: implement FileTreeItem::children_ with a std::vector * fix: signed-unsigned comparison in libtransmission tests * fix: avoid unnecessary copy by using const reference * fix: function should be declared const * refactor: use fmt::format to build log timestamps * fix: use init-statement to reduce variable scope * fixup! refactor: use fmt::format to build log timestamps * fix: remove tau_tracker destructor for rule-of-zero * fix: remove tr_peerIo destructor for rule-of-zero * Revert "fix: dereference before null check" This reverts commit cd789678156bb987a8dc6b2eb49cb1db4195d441. * fix: signed-unsigned comparison in libtransmission tests * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: extract nested code block into separate method * fix: use init-statement to reduce variable scope * fix: extract nested code block into separate method * fix: signed-unsigned comparison in libtransmission tests * fixup! fix: extract nested code block into separate method * fix: mark possibly-unused as [[maybe_unused]] * fix: invalid stack memory reference in tr_found_file_t * fix: signed-unsigned comparison in libtransmission tests
2022-04-02 14:06:02 +00:00
EXPECT_EQ(std::size(Value), static_cast<size_t>(std::distance(begin, end)));
}
}
TEST_F(StrbufTest, join)
{
auto buf = tr_pathbuf{};
buf.clear();
buf.join(' ', 'A', "short", "phrase"sv);
EXPECT_EQ("A short phrase"sv, buf.sv());
buf.clear();
buf.join(" ", 'A', "short", "phrase"sv);
EXPECT_EQ("A short phrase"sv, buf.sv());
buf.clear();
buf.join("--"sv, 'A', "short", "phrase"sv);
EXPECT_EQ("A--short--phrase"sv, buf.sv());
}
TEST_F(StrbufTest, move)
{
static auto constexpr Value = "/hello/world"sv;
auto generator = []()
{
return tr_pathbuf{ Value };
};
auto const path = generator();
EXPECT_EQ(Value, path.sv());
EXPECT_EQ(Value, path.c_str());
}
TEST_F(StrbufTest, startsWith)
{
auto const buf = tr_pathbuf{ "/hello/world" };
EXPECT_TRUE(buf.starts_with('/'));
EXPECT_TRUE(buf.starts_with("/"));
EXPECT_TRUE(buf.starts_with("/"sv));
EXPECT_TRUE(buf.starts_with("/hello"));
EXPECT_TRUE(buf.starts_with("/hello"sv));
EXPECT_TRUE(buf.starts_with("/hello/world"));
EXPECT_TRUE(buf.starts_with("/hello/world"sv));
EXPECT_FALSE(buf.starts_with('g'));
EXPECT_FALSE(buf.starts_with("g"));
EXPECT_FALSE(buf.starts_with("g"sv));
EXPECT_FALSE(buf.starts_with("ghello"));
EXPECT_FALSE(buf.starts_with("ghello"sv));
EXPECT_FALSE(buf.starts_with("/hellg"));
EXPECT_FALSE(buf.starts_with("/hellg"sv));
EXPECT_FALSE(buf.starts_with("/hellg/world"));
EXPECT_FALSE(buf.starts_with("/hellg/world"sv));
}
TEST_F(StrbufTest, endsWith)
{
auto const buf = tr_pathbuf{ "/hello/world" };
EXPECT_TRUE(buf.ends_with('d'));
EXPECT_TRUE(buf.ends_with("d"));
EXPECT_TRUE(buf.ends_with("d"sv));
EXPECT_TRUE(buf.ends_with("world"));
EXPECT_TRUE(buf.ends_with("world"sv));
EXPECT_TRUE(buf.ends_with("/hello/world"));
EXPECT_TRUE(buf.ends_with("/hello/world"sv));
EXPECT_FALSE(buf.ends_with('g'));
EXPECT_FALSE(buf.ends_with("g"));
EXPECT_FALSE(buf.ends_with("g"sv));
EXPECT_FALSE(buf.ends_with("gorld"));
EXPECT_FALSE(buf.ends_with("gorld"sv));
EXPECT_FALSE(buf.ends_with("worlg"));
EXPECT_FALSE(buf.ends_with("worlg"sv));
EXPECT_FALSE(buf.ends_with("/hellg/world"));
EXPECT_FALSE(buf.ends_with("/hellg/world"sv));
}