161 lines
5.2 KiB
C++
161 lines
5.2 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include "matador/utils/convert.hpp"
|
|
//#include "matador/utils/date.hpp"
|
|
//#include "matador/utils/time.hpp"
|
|
#include "matador/utils/types.hpp"
|
|
|
|
using namespace matador::utils;
|
|
|
|
template<typename From, typename To>
|
|
void validate_conversion(From from)
|
|
{
|
|
auto res = to<To>(from);
|
|
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(static_cast<To>(from) == *res);
|
|
}
|
|
|
|
template<typename From, typename To>
|
|
void validate_conversion(const From &from, To expected_to)
|
|
{
|
|
auto res = to<To>(from);
|
|
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == expected_to);
|
|
}
|
|
|
|
template<typename From>
|
|
void validate_conversion(const From &from, const std::string &expected_to)
|
|
{
|
|
auto res = to<std::string>(from);
|
|
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(*res == expected_to);
|
|
}
|
|
|
|
template<typename From>
|
|
void validate_conversion(const From &from, const char *expected_to)
|
|
{
|
|
validate_conversion<From>(from, std::string{expected_to});
|
|
}
|
|
|
|
template<typename From>
|
|
void validate_integral_conversion(From from) {
|
|
validate_conversion<From, int8_t>(from);
|
|
validate_conversion<From, int16_t>(from);
|
|
validate_conversion<From, int32_t>(from);
|
|
validate_conversion<From, int64_t>(from);
|
|
}
|
|
|
|
TEST_CASE("Test integral conversion", "[convert][integral]") {
|
|
validate_integral_conversion<int8_t>(-56);
|
|
validate_integral_conversion<int16_t>(-127);
|
|
validate_integral_conversion<int32_t>(-9876543);
|
|
validate_integral_conversion<int64_t>(-123456790);
|
|
|
|
validate_integral_conversion<uint8_t>(56);
|
|
validate_integral_conversion<uint16_t>(127);
|
|
validate_integral_conversion<uint32_t>(9876543);
|
|
validate_integral_conversion<uint64_t>(123456790);
|
|
|
|
validate_conversion<int16_t, int8_t>(513, 1);
|
|
validate_conversion<int32_t, int8_t>(515, 3);
|
|
validate_conversion<int64_t, int8_t>(516, 4);
|
|
}
|
|
TEST_CASE("Test floating point conversion", "[convert][floating_point]") {
|
|
validate_conversion<float, float>(-0.1f);
|
|
validate_conversion<float, double>(-0.1f);
|
|
validate_conversion<double, double>(-0.44444);
|
|
validate_conversion<double, float>(-0.44444);
|
|
}
|
|
|
|
TEST_CASE("Test integral to string conversion", "[convert][integral][string]") {
|
|
validate_conversion<int8_t>(-56, "-56");
|
|
validate_conversion<int16_t>(-127, "-127");
|
|
validate_conversion<int32_t>(-9876543, "-9876543");
|
|
validate_conversion<int64_t>(-123456790, "-123456790");
|
|
|
|
validate_conversion<uint8_t>(56, "56");
|
|
validate_conversion<uint16_t>(127, "127");
|
|
validate_conversion<uint32_t>(9876543, "9876543");
|
|
validate_conversion<uint64_t>(123456790, "123456790");
|
|
}
|
|
|
|
TEST_CASE("Test floating point to string conversion", "[convert][floating_point][string]") {
|
|
validate_conversion<float>(-56.1234f, "-56.1234");
|
|
validate_conversion<double>(-127.444449, "-127.444449");
|
|
}
|
|
|
|
TEST_CASE("Test string to integral conversion", "[convert][string][integral]") {
|
|
validate_conversion<std::string, int8_t>("-56", -56);
|
|
validate_conversion<std::string, int16_t>("-127", -127);
|
|
validate_conversion<std::string, int32_t>("-9876543", -9876543);
|
|
validate_conversion<std::string, int64_t>("-123456790", -123456790);
|
|
|
|
validate_conversion<std::string, uint8_t>("56", 56);
|
|
validate_conversion<std::string, uint16_t>("127", 127);
|
|
validate_conversion<std::string, uint32_t>("9876543", 9876543);
|
|
validate_conversion<std::string, uint64_t>("123456790", 123456790);
|
|
|
|
validate_conversion<const char*, int8_t>("-56", -56);
|
|
validate_conversion<const char*, int16_t>("-127", -127);
|
|
validate_conversion<const char*, int32_t>("-9876543", -9876543);
|
|
validate_conversion<const char*, int64_t>("-123456790", -123456790);
|
|
|
|
validate_conversion<const char*, uint8_t>("56", 56);
|
|
validate_conversion<const char*, uint16_t>("127", 127);
|
|
validate_conversion<const char*, uint32_t>("9876543", 9876543);
|
|
validate_conversion<const char*, uint64_t>("123456790", 123456790);
|
|
}
|
|
|
|
TEST_CASE("Test string to floating point conversion", "[convert][string][floating_point]") {
|
|
validate_conversion<std::string, float>("-56.1234", -56.1234f);
|
|
validate_conversion<std::string, double>("-127.444449", -127.444449);
|
|
|
|
validate_conversion<const char*, float>("-56.1234", -56.1234f);
|
|
validate_conversion<const char*, double>("-127.444449", -127.444449);
|
|
}
|
|
|
|
TEST_CASE("Test blob to blob conversion", "[convert][blob]") {
|
|
blob from{1, 2, 3, 4};
|
|
const auto res = to<blob>(from);
|
|
|
|
REQUIRE(res.is_ok());
|
|
REQUIRE(from == *res);
|
|
}
|
|
|
|
// TEST_CASE("Validate date to string conversion", "[convert][date][string]") {
|
|
// matador::date today;
|
|
// const auto expected_string = matador::utils::to_string(today);
|
|
// std::string to;
|
|
//
|
|
// convert(to, today);
|
|
//
|
|
// REQUIRE(expected_string == to);
|
|
// }
|
|
//
|
|
// TEST_CASE("Validate date conversion leads to an exception", "[convert][date][exception]") {
|
|
// matador::date today;
|
|
// int to{};
|
|
// convert(to, today);
|
|
// REQUIRE(to == today.julian_date());
|
|
// }
|
|
//
|
|
// TEST_CASE("Validate time to string conversion", "[convert][time][string]") {
|
|
// matador::time now;
|
|
// const auto expected_string = matador::utils::to_string(now);
|
|
// std::string to;
|
|
//
|
|
// convert(to, now);
|
|
//
|
|
// REQUIRE(expected_string == to);
|
|
// }
|
|
//
|
|
// TEST_CASE("Validate time conversion leads to an exception", "[convert][time][exception]") {
|
|
// matador::time now;
|
|
// int to;
|
|
// convert(to, now);
|
|
// REQUIRE(to == now.get_time_info().seconds_since_epoch);
|
|
// }
|