File indexing completed on 2026-10-29 08:25:02
0001
0002
0003
0004
0005
0006 #include <algorithms/detail/upcast.h>
0007
0008 #include <catch2/catch_test_macros.hpp>
0009
0010 #include <cstdint>
0011 #include <string>
0012 #include <string_view>
0013 #include <type_traits>
0014 #include <vector>
0015
0016 using namespace algorithms::detail;
0017
0018 namespace {
0019 enum class Color { kRed, kGreen };
0020 enum Plain { kPlainA = 7 };
0021 struct Custom {};
0022 }
0023
0024 TEST_CASE("Unsigned integers upcast to uint64_t", "[upcast]") {
0025 STATIC_REQUIRE(std::is_same_v<upcast_type_t<uint8_t>, uint64_t>);
0026 STATIC_REQUIRE(std::is_same_v<upcast_type_t<uint16_t>, uint64_t>);
0027 STATIC_REQUIRE(std::is_same_v<upcast_type_t<uint32_t>, uint64_t>);
0028 STATIC_REQUIRE(std::is_same_v<upcast_type_t<uint64_t>, uint64_t>);
0029 STATIC_REQUIRE(std::is_same_v<upcast_type_t<std::size_t>, uint64_t>);
0030 }
0031
0032 TEST_CASE("Signed integers upcast to int64_t", "[upcast]") {
0033 STATIC_REQUIRE(std::is_same_v<upcast_type_t<int8_t>, int64_t>);
0034 STATIC_REQUIRE(std::is_same_v<upcast_type_t<int16_t>, int64_t>);
0035 STATIC_REQUIRE(std::is_same_v<upcast_type_t<int32_t>, int64_t>);
0036 STATIC_REQUIRE(std::is_same_v<upcast_type_t<int64_t>, int64_t>);
0037 }
0038
0039 TEST_CASE("Enums upcast to int64_t", "[upcast]") {
0040 STATIC_REQUIRE(std::is_same_v<upcast_type_t<Color>, int64_t>);
0041 STATIC_REQUIRE(std::is_same_v<upcast_type_t<Plain>, int64_t>);
0042 }
0043
0044 TEST_CASE("Floating point types upcast to double", "[upcast]") {
0045 STATIC_REQUIRE(std::is_same_v<upcast_type_t<float>, double>);
0046 STATIC_REQUIRE(std::is_same_v<upcast_type_t<double>, double>);
0047 }
0048
0049 TEST_CASE("String-like types upcast to std::string", "[upcast]") {
0050 STATIC_REQUIRE(std::is_same_v<upcast_type_t<std::string>, std::string>);
0051 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const char*>, std::string>);
0052 STATIC_REQUIRE(std::is_same_v<upcast_type_t<char*>, std::string>);
0053 }
0054
0055 TEST_CASE("Booleans and other types are not upcast", "[upcast]") {
0056 STATIC_REQUIRE(std::is_same_v<upcast_type_t<bool>, bool>);
0057 STATIC_REQUIRE(std::is_same_v<upcast_type_t<Custom>, Custom>);
0058 STATIC_REQUIRE(std::is_same_v<upcast_type_t<std::vector<int>>, std::vector<int>>);
0059 }
0060
0061 TEST_CASE("References and cv-qualifiers are removed before upcasting", "[upcast]") {
0062 STATIC_REQUIRE(std::is_same_v<upcast_type_t<int&>, int64_t>);
0063 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const int&>, int64_t>);
0064 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const unsigned>, uint64_t>);
0065 STATIC_REQUIRE(std::is_same_v<upcast_type_t<float&&>, double>);
0066 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const Color&>, int64_t>);
0067 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const bool&>, bool>);
0068 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const std::string&>, std::string>);
0069 STATIC_REQUIRE(std::is_same_v<upcast_type_t<const char(&)[5]>, std::string>);
0070 }
0071
0072 TEST_CASE("upcast converts values without changing them", "[upcast]") {
0073 CHECK(upcast(uint8_t{255}) == uint64_t{255});
0074 CHECK(upcast(int8_t{-128}) == int64_t{-128});
0075 CHECK(upcast(1.5f) == 1.5);
0076 CHECK(upcast("text") == std::string{"text"});
0077 CHECK(upcast(true) == true);
0078 CHECK(upcast(kPlainA) == int64_t{7});
0079
0080 STATIC_REQUIRE(std::is_same_v<decltype(upcast(uint8_t{1})), uint64_t>);
0081 STATIC_REQUIRE(std::is_same_v<decltype(upcast(1.5f)), double>);
0082 STATIC_REQUIRE(std::is_same_v<decltype(upcast("text")), std::string>);
0083
0084 const int16_t lvalue = -3;
0085 CHECK(upcast(lvalue) == int64_t{-3});
0086 STATIC_REQUIRE(std::is_same_v<decltype(upcast(lvalue)), int64_t>);
0087 }