duckhandy/test/unit/int_conv_test.cpp

293 lines
13 KiB
C++

/* Copyright 2016-2024 Michele Santullo
* This file is part of "duckhandy".
*
* "duckhandy" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "duckhandy" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
*/
#include "catch2/catch_test_macros.hpp"
#include "duckhandy/int_conv.hpp"
#include "duckhandy/string_bt.hpp"
#include "sprout/cstring/strlen.hpp"
#include "sprout/preprocessor/comma.hpp"
#include <cstdint>
#include <random>
#include <string>
#include <ctime>
template <typename T> using int_info_10 = dhandy::implem::int_info<T, 10>;
template <typename T> using int_info_16 = dhandy::implem::int_info<T, 16>;
template <typename T> using int_info_2 = dhandy::implem::int_info<T, 2>;
namespace {
template <unsigned int Base>
struct ASCIITranslatorIns : public dhandy::ASCIITranslator<char> {
static const constexpr char AltLetter = 'A';
static constexpr int from_digit (char dig) {
if (dig >= FirstLetter and dig <= FirstLetter + Base - 10 - 1)
return dig - FirstLetter + 10;
else
return dig - AltLetter + 10;
}
};
template <typename T, unsigned int Base>
void AryConversionTestHelper (const std::string_view& s, T expected, bool expect_sse) {
using AryConversion = dhandy::implem::AryConversion<T, Base, dhandy::ASCIITranslator<char>, false>;
CHECK(AryConversion::is_sse == expect_sse);
CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected);
}
template <typename T, unsigned int Base>
void AryConversionTestHelperUp (const std::string_view& s, T expected, bool expect_sse) {
using AryConversion = dhandy::implem::AryConversion<T, Base, dhandy::ASCIITranslatorUpcase<char>, false>;
CHECK(AryConversion::is_sse == expect_sse);
CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected);
}
template <typename T, unsigned int Base>
void AryConversionTestHelperIns (const std::string_view& s, T expected, bool expect_sse) {
using AryConversion = dhandy::implem::AryConversion<T, Base, ASCIITranslatorIns<Base>, false>;
CHECK(AryConversion::is_sse == expect_sse);
CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected);
}
} //unnamed namespace
TEST_CASE ("Check int to char array conversions", "[i2s][int_conv]") {
using dhandy::int_to_ary;
using dhandy::bt::string;
using dhandy::bt::make_string;
using sprout::strlen;
CHECK(int_info_10<uint8_t>::max_len == 3);
CHECK(int_info_10<uint16_t>::max_len == 5);
CHECK(int_info_10<uint32_t>::max_len == 10);
CHECK(int_info_10<int8_t>::max_len == 4);
CHECK(int_info_10<int16_t>::max_len == 6);
CHECK(int_info_10<int32_t>::max_len == 11);
CHECK(int_info_16<uint8_t>::max_len == 2);
CHECK(int_info_16<uint16_t>::max_len == 4);
CHECK(int_info_16<uint32_t>::max_len == 8);
CHECK(int_info_16<int8_t>::max_len == 2);
CHECK(int_info_16<int16_t>::max_len == 4);
CHECK(int_info_16<int32_t>::max_len == 8);
CHECK(int_info_2<uint8_t>::max_len == 8);
CHECK(int_info_2<uint16_t>::max_len == 16);
CHECK(int_info_2<uint32_t>::max_len == 32);
CHECK(int_info_2<int8_t>::max_len == 8);
CHECK(int_info_2<int16_t>::max_len == 16);
CHECK(int_info_2<int32_t>::max_len == 32);
static_assert(int_to_ary(5)[0] == '5', "Algorithm error");
static_assert(string<strlen(int_to_ary(10).data()) + 1>(int_to_ary(10).data()) == make_string("10"), "Algorithm error");
static_assert(string<strlen(int_to_ary(101).data()) + 1>(int_to_ary(101).data()) == make_string("101"), "Algorithm error");
static_assert(string<strlen(int_to_ary<uint16_t>(0xAB12).data()) + 1>(int_to_ary<uint16_t>(0xAB12).data()) == make_string("43794"), "Algorithm error");
static_assert(int_info_10<int16_t>::is_signed == true, "Wrong sign detection");
static_assert(string<strlen(int_to_ary<int16_t>(0xAB12).data()) + 1>(int_to_ary<int16_t>(0xAB12).data()) == make_string("-21742"), "Algorithm error");
CHECK(int_to_ary<int64_t>(0x123456789A) == "78187493530");
CHECK(int_to_ary<int64_t>(-1) == "-1");
CHECK(int_to_ary<int64_t>(0x1000000000000000) == "1152921504606846976");
CHECK(int_to_ary<int64_t>(0xF000000000000000) == "-1152921504606846976");
CHECK(int_to_ary<int, 16, dhandy::ASCIITranslatorUpcase<char>>(255) == "FF");
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xFFFF) == "ffff");
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xCACA) == "caca");
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0x10) == "10");
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0x10) == "10");
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0xF000) == "f000");
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xFEFE) == "fefe");
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0xFEFE) == "fefe");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0423) == "423");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0777) == "777");
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 8>(0) == "0");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0) == "0");
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 2>(0) == "0");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 2>(0) == "0");
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 16>(0) == "0");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 16>(0) == "0");
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 10>(0) == "0");
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 10>(0) == "0");
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 8>(false) == "0");
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 8>(true) == "1");
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 10>(false) == "0");
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 10>(true) == "1");
CHECK(int_to_ary<uint8_t SPROUT_PP_COMMA() 2>(0b10101010) == "10101010");
CHECK(int_to_ary<int8_t SPROUT_PP_COMMA() 2>(0b10101010) == "10101010");
CHECK(int_to_ary<uint8_t SPROUT_PP_COMMA() 2>(0b11111111) == "11111111");
CHECK(int_to_ary<int8_t SPROUT_PP_COMMA() 2>(0b11111111) == "11111111");
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 2>(0b111100001111) == "111100001111");
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 2>(0b111100001111) == "111100001111");
CHECK(int_to_ary<int64_t SPROUT_PP_COMMA() 36>(9223372036854775807) == "1y2p0ij32e8e7");
CHECK(int_to_ary<int64_t SPROUT_PP_COMMA() 36>(0x8000000000000001) == "1y2p0ij32e8e9");
#if defined(__GNUC__)
static_assert(std::is_integral<__int128_t>::value, "Warning, int128 won't pickup the optimized base 10 conversion path");
__int128_t num = 10000000000000000000U;
CHECK(int_to_ary<__int128_t SPROUT_PP_COMMA() 10>(num * 100) == "1000000000000000000000");
num = 0xFFFFFFFFFFFFFFFF;
CHECK(int_to_ary<__int128_t SPROUT_PP_COMMA() 16>(num * 0x10000 + 0xffff) == "ffffffffffffffffffff");
#endif
{
//Try a random test, which should not compile as constexpr
std::mt19937 gen;
gen.seed(std::time(nullptr));
for (int z = 0; z < 10; ++z) {
const int num = gen();
CHECK(int_to_ary(num) == std::to_string(num));
}
const int num = gen();
std::string hello = "hello world";
std::string hello1 = hello + " " + int_to_ary(num);
std::string hello2 = hello + " " + std::to_string(num);
CHECK(hello1 == hello2);
}
}
TEST_CASE ("Check char array to int conversions", "[s2i][int_conv]") {
AryConversionTestHelper<std::uint32_t, 10>("0", 0, true);
AryConversionTestHelper<std::int32_t, 10>("0", 0, true);
AryConversionTestHelper<std::int16_t, 10>("0", 0, true);
AryConversionTestHelper<std::int16_t, 16>("ff", 0xff, true);
AryConversionTestHelper<std::uint8_t, 16>("ff", 0xff, true);
AryConversionTestHelper<std::uint32_t, 36>("rs", 1000, true);
AryConversionTestHelper<std::uint16_t, 8>("20", 16, true);
AryConversionTestHelper<std::int32_t, 10>("1", 1, true);
AryConversionTestHelper<std::int32_t, 10>("10", 10, true);
AryConversionTestHelper<std::int32_t, 10>("100", 100, true);
AryConversionTestHelper<std::int32_t, 10>("999", 999, true);
AryConversionTestHelper<std::int32_t, 10>("1000", 1000, true);
AryConversionTestHelper<std::int32_t, 10>("1001", 1001, true);
AryConversionTestHelper<std::int32_t, 10>("12345", 12345, true);
AryConversionTestHelper<std::int32_t, 10>("123456", 123456, true);
AryConversionTestHelper<std::int32_t, 10>("1234567", 1234567, true);
AryConversionTestHelper<std::int32_t, 10>("12345678", 12345678, true);
AryConversionTestHelper<std::int32_t, 10>("123456789", 123456789, true);
AryConversionTestHelper<std::int32_t, 10>("2147483647", 2147483647, true);
AryConversionTestHelper<std::uint64_t, 16>("ffffffffffffffff", 0xffffffffffffffff, false);
AryConversionTestHelper<std::int64_t, 16>("ffffffffffffffff", 0xffffffffffffffff, false);
AryConversionTestHelper<std::int64_t, 16>("7fffffffffffffff", 0x7fffffffffffffff, false);
AryConversionTestHelper<std::int32_t, 16>("7fffffff", 0x7fffffff, true);
AryConversionTestHelper<bool, 10>("1", true, false);
AryConversionTestHelper<bool, 10>("0", false, false);
AryConversionTestHelperUp<std::uint64_t, 16>("ABCDEF123456ABCD", 0xabcdef123456abcd, false);
AryConversionTestHelper<signed int, 10>("-1", -1, sizeof(signed int) <= sizeof(std::uint32_t));
AryConversionTestHelper<signed int, 10>("-50000", -50000, sizeof(signed int) <= sizeof(std::uint32_t));
AryConversionTestHelper<std::int64_t, 10>("-1", -1, false);
AryConversionTestHelper<std::int64_t, 10>("-510123123123", -510123123123, false);
//case insensitive SSE conversions
AryConversionTestHelperIns<std::int32_t, 16>("7FfFfFfF", 0x7fffffff, true);
AryConversionTestHelperIns<std::int32_t, 16>("AbCdEf01", 0xabcdef01, true);
AryConversionTestHelperIns<std::int32_t, 16>("aBcDeF01", 0xabcdef01, true);
AryConversionTestHelperIns<std::uint8_t, 16>("Ff", 0xff, true);
AryConversionTestHelperIns<std::int16_t, 16>("AfBe", 0xafbe, true);
AryConversionTestHelperIns<std::int32_t, 16>("aAb", 0xaab, true);
AryConversionTestHelperIns<std::int32_t, 16>("aAbBc", 0xaabbc, true);
}
TEST_CASE ("Check string_view conversions work as expected", "[s2i][int_conv]") {
using dhandy::int_conv;
using dhandy::int_conv_raw;
using std::string_view;
using std::integral_constant;
using std::string;
constexpr auto str71 = int_conv<string_view>(integral_constant<short int, 71>{});
CHECK("71" == str71);
{
//test random number to force non-constexpr
std::mt19937 gen;
gen.seed(std::time(nullptr));
const int num = gen();
const string num_str {int_conv_raw(num).to_string_view()};
CHECK(num_str == std::to_string(num));
}
const auto str123 = int_conv<string>(123);
CHECK("123" == str123);
}
TEST_CASE ("Check std::array conversion works as expected", "[i2s][int_conv]") {
using std::array;
using dhandy::int_conv;
auto arr748 = int_conv<array<char, 3>>(748);
CHECK(arr748[0] == '7');
CHECK(arr748[1] == '4');
CHECK(arr748[2] == '8');
auto arr19000 = int_conv<array<char, 5>>(19);
CHECK(arr19000[0] == '1');
CHECK(arr19000[1] == '9');
CHECK(arr19000[2] == '\0');
CHECK(arr19000[3] == '\0');
CHECK(arr19000[4] == '\0');
}
TEST_CASE ("Check upcase/downcase int to array conversions", "[i2s][int_conv]") {
using dhandy::int_conv;
using std::string_view;
using std::integral_constant;
constexpr auto bool1 = int_conv<string_view>(integral_constant<bool, true>{});
CHECK("1" == bool1);
constexpr auto bool2 = int_conv<string_view>(integral_constant<bool, false>{});
CHECK("0" == bool2);
constexpr auto int1 = int_conv<string_view>(integral_constant<int, 42>{});
CHECK("42" == int1);
constexpr auto int2 = int_conv<string_view>(integral_constant<unsigned int, 7777777>{});
CHECK("7777777" == int2);
constexpr auto int3 = int_conv<string_view>(integral_constant<int, -1234>{});
CHECK("-1234" == int3);
constexpr auto int4 = int_conv<string_view>(integral_constant<short int, -256>{});
CHECK("-256" == int4);
}
TEST_CASE ("Try int conv with non-char", "[s2i][int_conv]") {
using dhandy::buildtime_int_to_ary;
using dhandy::ASCIITranslator;
std::wstring_view exp1(L"235713");
auto val1 = buildtime_int_to_ary<int, 235713, 10, ASCIITranslator<wchar_t>>().to_string_view();
CHECK(exp1 == val1);
}
TEST_CASE ("Check some fancy conversions", "[i2s][int_conv]") {
using dhandy::int_to_ary;
using std::string;
using MyFancyTranslator = dhandy::ASCIITranslator<char, '0', 'a', '+', '-', '\0', 4>;
string str1 = int_to_ary<int, 10, MyFancyTranslator>(3).to_string();
CHECK("3" == str1);
string str2 = int_to_ary<int, 10, MyFancyTranslator>(5).to_string();
CHECK("b" == str2);
string str3 = int_to_ary<int, 10, MyFancyTranslator>(11).to_string();
CHECK("11" == str3);
string str4 = int_to_ary<int, 10, MyFancyTranslator>(537).to_string();
CHECK("b3d" == str4);
}