Skip to content

Commit

Permalink
Refactor Converter class and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryKogan committed Dec 4, 2023
1 parent 53b0075 commit c25ae70
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ endif()
add_executable(
tests
${SOURCES}
test/path_manipulations_test.cpp
test/dirname_basename.cpp
test/converter.cpp
)
target_link_libraries(tests GTest::gtest_main)

Expand Down
8 changes: 4 additions & 4 deletions src/FileSystem/Converter/Converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ auto Converter::to_bytes(std::uint64_t value) -> std::vector<std::byte> {
auto Converter::to_uint64(const std::vector<std::byte> &bytes) -> std::uint64_t {
const uint8_t BYTES_IN_UINT64 = 8;

if (bytes.size() != BYTES_IN_UINT64) throw std::runtime_error("Invalid bytes size");
if (bytes.size() != BYTES_IN_UINT64) throw std::invalid_argument("Invalid bytes size");

std::uint64_t value = 0;

Expand All @@ -33,7 +33,7 @@ auto Converter::to_bytes(std::string const &value, std::uint64_t size) -> std::v
for (char character : value) bytes.push_back(static_cast<std::byte>(character));

if (size != 0) {
if (bytes.size() > size) throw std::runtime_error("Invalid string size");
if (bytes.size() > size) throw std::invalid_argument("Invalid string size");
bytes.resize(size, std::byte{0});
}

Expand Down Expand Up @@ -61,9 +61,9 @@ auto Converter::to_bytes(bool value) -> std::vector<std::byte> {
}

auto Converter::to_bool(std::vector<std::byte> const &bytes) -> bool {
if (bytes.size() != 1) throw std::runtime_error("Invalid bytes size");
if (bytes.size() != 1) throw std::invalid_argument("Invalid bytes size");

if (bytes[0] == std::byte{0}) return false;
if (bytes[0] == std::byte{1}) return true;
throw std::runtime_error("Invalid bool value");
throw std::invalid_argument("Invalid bool value");
}
12 changes: 6 additions & 6 deletions src/FileSystem/Converter/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
class Converter {
public:
// uint64_t
static auto to_bytes(std::uint64_t value) -> std::vector<std::byte>;
static auto to_uint64(const std::vector<std::byte> &bytes) -> std::uint64_t;
[[nodiscard]] static auto to_bytes(std::uint64_t value) -> std::vector<std::byte>;
[[nodiscard]] static auto to_uint64(const std::vector<std::byte> &bytes) -> std::uint64_t;

// string
static auto to_bytes(std::string const &value, std::uint64_t size = 0) -> std::vector<std::byte>;
static auto to_string(std::vector<std::byte> const &bytes) -> std::string;
[[nodiscard]] static auto to_bytes(std::string const &value, std::uint64_t size = 0) -> std::vector<std::byte>;
[[nodiscard]] static auto to_string(std::vector<std::byte> const &bytes) -> std::string;

// bool
static auto to_bytes(bool value) -> std::vector<std::byte>;
static auto to_bool(std::vector<std::byte> const &bytes) -> bool;
[[nodiscard]] static auto to_bytes(bool value) -> std::vector<std::byte>;
[[nodiscard]] static auto to_bool(std::vector<std::byte> const &bytes) -> bool;
};
100 changes: 100 additions & 0 deletions test/converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "../src/FileSystem/Converter/Converter.hpp"
#include <gtest/gtest.h>

TEST(ConverterTest, ToBytesUint64) {
std::uint64_t const value = 0x0123456789ABCDEF;
std::vector<std::byte> const expected = {
std::byte{0x01}, std::byte{0x23}, std::byte{0x45}, std::byte{0x67},
std::byte{0x89}, std::byte{0xAB}, std::byte{0xCD}, std::byte{0xEF},
};

auto const actual = Converter::to_bytes(value);

EXPECT_EQ(actual, expected);
}

TEST(ConverterTest, ToUint64) {
std::vector<std::byte> const bytes = {
std::byte{0x01}, std::byte{0x23}, std::byte{0x45}, std::byte{0x67},
std::byte{0x89}, std::byte{0xAB}, std::byte{0xCD}, std::byte{0xEF},
};
std::uint64_t const expected = 0x0123456789ABCDEF;

auto const actual = Converter::to_uint64(bytes);

EXPECT_EQ(actual, expected);
}

TEST(ConverterTest, ToUint64InvalidSize) {
std::vector<std::byte> const bytes = {
std::byte{0xEF}, std::byte{0xCD}, std::byte{0xAB}, std::byte{0x89},
std::byte{0x67}, std::byte{0x45}, std::byte{0x23},
};

EXPECT_THROW(auto res = Converter::to_uint64(bytes), std::invalid_argument);
}

TEST(ConverterTest, ToBytesString) {
std::string const value = "Hello, World!";
std::vector<std::byte> const expected = {
std::byte{'H'}, std::byte{'e'}, std::byte{'l'}, std::byte{'l'}, std::byte{'o'}, std::byte{','}, std::byte{' '},
std::byte{'W'}, std::byte{'o'}, std::byte{'r'}, std::byte{'l'}, std::byte{'d'}, std::byte{'!'}, std::byte{'\0'},
std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}};

auto const actual = Converter::to_bytes(value, 21);

EXPECT_EQ(actual, expected);
}

TEST(ConverterTest, ToBytesStringInvalidSize) {
std::string const value = "Hello, World!";

EXPECT_THROW(auto res = Converter::to_bytes(value, 5), std::invalid_argument);
}

TEST(ConverterTest, ToString) {
std::vector<std::byte> const bytes = {
std::byte{'H'}, std::byte{'e'}, std::byte{'l'}, std::byte{'l'}, std::byte{'o'}, std::byte{','}, std::byte{' '},
std::byte{'W'}, std::byte{'o'}, std::byte{'r'}, std::byte{'l'}, std::byte{'d'}, std::byte{'!'}, std::byte{'\0'},
std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}, std::byte{0}};

std::string const expected = "Hello, World!";

auto const actual = Converter::to_string(bytes);

EXPECT_EQ(actual, expected);
}

TEST(ConverterTest, ToBytesBool) {
std::vector<std::byte> const expected_true = {std::byte{1}};
std::vector<std::byte> const expected_false = {std::byte{0}};

auto const actual_true = Converter::to_bytes(true);
auto const actual_false = Converter::to_bytes(false);

EXPECT_EQ(actual_true, expected_true);
EXPECT_EQ(actual_false, expected_false);
}

TEST(ConverterTest, ToBool) {
std::vector<std::byte> const bytes_true = {std::byte{1}};
std::vector<std::byte> const bytes_false = {std::byte{0}};

auto const actual_true = Converter::to_bool(bytes_true);
auto const actual_false = Converter::to_bool(bytes_false);

EXPECT_TRUE(actual_true);
EXPECT_FALSE(actual_false);
}

TEST(ConverterTest, ToBoolInvalidSize) {
std::vector<std::byte> const bytes = {std::byte{1}, std::byte{0}};

EXPECT_THROW(auto res = Converter::to_bool(bytes), std::invalid_argument);
}

TEST(ConverterTest, ToBoolInvalidValue) {
std::vector<std::byte> const bytes = {std::byte{2}};

EXPECT_THROW(auto res = Converter::to_bool(bytes), std::invalid_argument);
}

0 comments on commit c25ae70

Please sign in to comment.