toolbox/format_bytes/src/unit_test.cpp
2024-11-07 22:23:49 +01:00

47 lines
2.7 KiB
C++

#include <iostream>
#include "toolbox.hpp"
#include "format_bytes.hpp"
/**
* @brief Test if a given result is equal of the expected one and log result
*
* @tparam T type of returning values
* @param name of the unit test
* @param expected result of the function call
* @param result of the function
*/
template<typename T>
void Assert(const char* const name, const T& expected, const T& result) noexcept {
if (expected != result)
std::cerr << AnsiColor::Red << "[ ] - " << name << AnsiColor::Reset << " - Expected '" << expected << "' but got '" << result << "' instead\n";
else
std::cout << AnsiColor::Green << "[✓] - " << name << AnsiColor::Reset << "\n";
}
/**
* @brief Test suite for the format_byte output
*/
void format_byte_test(void) noexcept {
std::cout << AnsiColor::BoldWhite << "\tTesting format_byte_size str suite" << AnsiColor::Reset << "\n";
Assert("format_bytes str null", std::string("0B"), format_bytes(u64(0)));
Assert("format_bytes str byte", std::string("1B"), format_bytes(u64(1)));
Assert("format_bytes str kilobyte", std::string("1KB"), format_bytes(u64(1) << 10));
Assert("format_bytes str megabyte", std::string("1MB"), format_bytes(u64(1) << 20));
Assert("format_bytes str gigabyte", std::string("1GB"), format_bytes(u64(1) << 30));
Assert("format_bytes str terabyte", std::string("1TB"), format_bytes(u64(1) << 40));
Assert("format_bytes str petabyte", std::string("1PB"), format_bytes(u64(1) << 50));
Assert("format_bytes str exabyte", std::string("1EB"), format_bytes(u64(1) << 60));
// Unsupported due to number of byte bigger than currently supported by ISO c++
// Assert("format_bytes zettabyte", std::string("1ZB"), format_bytes(u64(1)<<70));
// Assert("format_bytes yottabyte", std::string("1YB"), format_bytes(u64(1)<<80));
// uint64_t_MAX == 2**64 == 18446744073709551615 == -1
Assert("format_bytes str max", std::string("15EB 1023PB 1023TB 1023GB 1023MB 1023KB 1023B"), format_bytes(u64(-1)));
Assert("format_bytes str max", std::string("15EB 1023PB 1023TB 1023GB 1023MB 1023KB 1023B"), format_bytes(18446744073709551615ull));
Assert("format_bytes str longest", std::string("10EB 1000PB 1000TB 1000GB 1000MB 1000KB 1000B"), format_bytes(12656215539330294760ull));
}
int32_t main(void) noexcept {
format_byte_test();
}