From b7140de190f63a6931e3cd90fb7757df56d2edd2 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Tue, 8 Oct 2024 12:40:42 +0300 Subject: [PATCH] tests file for ByteSize.hpp, fixed typos in ByteSize.hpp --- openVulkanoCpp/Math/ByteSize.hpp | 4 +- tests/Math/ByteSize.cpp | 100 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 tests/Math/ByteSize.cpp diff --git a/openVulkanoCpp/Math/ByteSize.hpp b/openVulkanoCpp/Math/ByteSize.hpp index 9ba1e59..1e3bfef 100644 --- a/openVulkanoCpp/Math/ByteSize.hpp +++ b/openVulkanoCpp/Math/ByteSize.hpp @@ -134,9 +134,9 @@ namespace OpenVulkano operator std::string() const { return Format(); } ByteSize operator +(const ByteSize other) const { return bytes + other.bytes; } - ByteSize operator -(const ByteSize other) const { return bytes + other.bytes; } + ByteSize operator -(const ByteSize other) const { return bytes - other.bytes; } ByteSize operator +(const size_t other) const { return bytes + other; } - ByteSize operator -(const size_t other) const { return bytes + other; } + ByteSize operator -(const size_t other) const { return bytes - other; } ByteSize& operator =(const size_t other) { bytes = other; return *this; } ByteSize& operator =(const ByteSize other) { bytes = other.bytes; return *this; } ByteSize& operator +=(const size_t other) { bytes += other; return *this; } diff --git a/tests/Math/ByteSize.cpp b/tests/Math/ByteSize.cpp new file mode 100644 index 0000000..52aaed8 --- /dev/null +++ b/tests/Math/ByteSize.cpp @@ -0,0 +1,100 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include +#include + +#include "Math/ByteSize.hpp" + +using namespace OpenVulkano; + +namespace +{ + bool almostEqual(float a, float b, float epsilon = 0.001f) { return std::fabs(a - b) < epsilon; } +} + +TEST_CASE("BuildFactors and BuildDivisors", "[ByteSize]") +{ + constexpr auto factors = ByteSizeUnitHelper::BuildFactors(); + constexpr auto divisors = ByteSizeUnitHelper::BuildDivisors(); + + REQUIRE(factors.size() == 13); + REQUIRE(factors[0] == 1); + REQUIRE(factors[1] == (1uLL << 10)); // KiB + REQUIRE(factors[6] == (1uLL << 60)); // EiB + REQUIRE(factors[7] == 1000); // kB + REQUIRE(factors[12] == 1'000'000'000'000'000'000); // EB + + REQUIRE(divisors.size() == 13); + REQUIRE(almostEqual(divisors[0], 1.0)); + REQUIRE(almostEqual(divisors[1], 1.0 / (1uLL << 10))); // KiB + REQUIRE(almostEqual(divisors[12], 1.0 / 1'000'000'000'000'000'000)); // EB +} + +TEST_CASE("GetFactor, GetDivisor, and GetName", "[ByteSize]") +{ + ByteSizeUnit kib(ByteSizeUnit::kiB); + ByteSizeUnit gb(ByteSizeUnit::GB); + + REQUIRE(kib.GetFactor() == (1uLL << 10)); // KiB + REQUIRE(almostEqual(kib.GetDivisor(), 1.0 / (1uLL << 10))); + REQUIRE(kib.GetName() == "kiB"); + + REQUIRE(gb.GetFactor() == 1'000'000'000); // GB + REQUIRE(almostEqual(gb.GetDivisor(), 1.0 / 1'000'000'000)); + REQUIRE(gb.GetName() == "GB"); +} + +TEST_CASE("FromName and GetClosestUnit", "[ByteSize]") +{ + auto kib = ByteSizeUnit::FromName("kiB"); + auto tb = ByteSizeUnit::FromName("TB"); + + REQUIRE(kib.GetFactor() == (1uLL << 10)); + REQUIRE(tb.GetFactor() == 1'000'000'000'000); + + uint64_t size1 = 2 * 1'000'000'000; + uint64_t size2 = 3 * (1uLL << 40); + ByteSizeUnit unit1 = ByteSizeUnit::GetClosestUnit(size1, true); // SI + ByteSizeUnit unit2 = ByteSizeUnit::GetClosestUnit(size2, false); // Binary + + REQUIRE(unit1.GetName() == "GB"); + REQUIRE(unit2.GetName() == "TiB"); +} + +TEST_CASE("Constructors and parsing from string", "[ByteSize]") +{ + ByteSize size1(1'024, ByteSizeUnit(ByteSizeUnit::kiB)); + ByteSize size2("1.5 MiB"); + + REQUIRE(static_cast(size1) == 1'024 * (1uLL << 10)); + REQUIRE(almostEqual(static_cast(size2), 1.5 * (1uLL << 20))); + + REQUIRE_THROWS_AS(ByteSize("invalid size"), std::runtime_error); +} + +TEST_CASE("Formatting", "[ByteSize]") +{ + ByteSize size2(1'024 + 512, ByteSizeUnit(ByteSizeUnit::kiB)); // This is 1.5 MiB + + REQUIRE(size2.Format(true) == "1.57 MB"); + REQUIRE(size2.Format(false) == "1.5 MiB"); +} + +TEST_CASE("Operator overloads", "[ByteSize]") +{ + ByteSize size1(1'024, ByteSizeUnit(ByteSizeUnit::kiB)); + ByteSize size2(512, ByteSizeUnit(ByteSizeUnit::kiB)); + + ByteSize result = size1 + size2; + REQUIRE(static_cast(result) == (1'024 + 512) * (1uLL << 10)); + + result = size1 - size2; + REQUIRE(static_cast(result) == (1'024 - 512) * (1uLL << 10)); + + size1 += size2; + REQUIRE(static_cast(size1) == (1'024 + 512) * (1uLL << 10)); +} \ No newline at end of file