/* Copyright 2016-2020 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 . */ #include "catch2/catch.hpp" #include "duckhandy/int_conv.hpp" #include "duckhandy/string_bt.hpp" #include "sprout/cstring/strlen.hpp" #include "sprout/preprocessor/comma.hpp" #include #include #include template using int_info_10 = dhandy::implem::int_info; template using int_info_16 = dhandy::implem::int_info; template using int_info_2 = dhandy::implem::int_info; namespace { template void AryConversionTestHelper (const std::string_view& s, T expected) { using AryConversion = dhandy::implem::AryConversion, false>; CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected); } template void AryConversionTestHelperUp (const std::string_view& s, T expected) { using AryConversion = dhandy::implem::AryConversion, false>; CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected); } } //unnamed namespace TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") { using dhandy::int_to_ary; using dhandy::bt::string; using dhandy::bt::make_string; using sprout::strlen; CHECK(int_info_10::max_len == 3); CHECK(int_info_10::max_len == 5); CHECK(int_info_10::max_len == 10); CHECK(int_info_10::max_len == 4); CHECK(int_info_10::max_len == 6); CHECK(int_info_10::max_len == 11); CHECK(int_info_16::max_len == 2); CHECK(int_info_16::max_len == 4); CHECK(int_info_16::max_len == 8); CHECK(int_info_16::max_len == 2); CHECK(int_info_16::max_len == 4); CHECK(int_info_16::max_len == 8); CHECK(int_info_2::max_len == 8); CHECK(int_info_2::max_len == 16); CHECK(int_info_2::max_len == 32); CHECK(int_info_2::max_len == 8); CHECK(int_info_2::max_len == 16); CHECK(int_info_2::max_len == 32); static_assert(int_to_ary(5)[0] == '5', "Algorithm error"); static_assert(string(int_to_ary(10).data()) == make_string("10"), "Algorithm error"); static_assert(string(int_to_ary(101).data()) == make_string("101"), "Algorithm error"); static_assert(string(0xAB12).data()) + 1>(int_to_ary(0xAB12).data()) == make_string("43794"), "Algorithm error"); static_assert(int_info_10::is_signed == true, "Wrong sign detection"); static_assert(string(0xAB12).data()) + 1>(int_to_ary(0xAB12).data()) == make_string("-21742"), "Algorithm error"); CHECK(int_to_ary(0x123456789A) == "78187493530"); CHECK(int_to_ary(-1) == "-1"); CHECK(int_to_ary(0x1000000000000000) == "1152921504606846976"); CHECK(int_to_ary(0xF000000000000000) == "-1152921504606846976"); CHECK(int_to_ary>(255) == "FF"); CHECK(int_to_ary(0xFFFF) == "ffff"); CHECK(int_to_ary(0xCACA) == "caca"); CHECK(int_to_ary(0x10) == "10"); CHECK(int_to_ary(0x10) == "10"); CHECK(int_to_ary(0xF000) == "f000"); CHECK(int_to_ary(0xFEFE) == "fefe"); CHECK(int_to_ary(0xFEFE) == "fefe"); CHECK(int_to_ary(0423) == "423"); CHECK(int_to_ary(0777) == "777"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(0) == "0"); CHECK(int_to_ary(false) == "0"); CHECK(int_to_ary(true) == "1"); CHECK(int_to_ary(false) == "0"); CHECK(int_to_ary(true) == "1"); CHECK(int_to_ary(0b10101010) == "10101010"); CHECK(int_to_ary(0b10101010) == "10101010"); CHECK(int_to_ary(0b11111111) == "11111111"); CHECK(int_to_ary(0b11111111) == "11111111"); CHECK(int_to_ary(0b111100001111) == "111100001111"); CHECK(int_to_ary(0b111100001111) == "111100001111"); CHECK(int_to_ary(9223372036854775807) == "1y2p0ij32e8e7"); CHECK(int_to_ary(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(1234); 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", "[i2s][int_conv]") { AryConversionTestHelper("0", 0); AryConversionTestHelper("0", 0); AryConversionTestHelper("0", 0); AryConversionTestHelper("ff", 0xff); AryConversionTestHelper("ff", 0xff); AryConversionTestHelper("rs", 1000); AryConversionTestHelper("20", 16); AryConversionTestHelper("1", 1); AryConversionTestHelper("10", 10); AryConversionTestHelper("100", 100); AryConversionTestHelper("999", 999); AryConversionTestHelper("1000", 1000); AryConversionTestHelper("1001", 1001); AryConversionTestHelper("12345", 12345); AryConversionTestHelper("123456", 123456); AryConversionTestHelper("1234567", 1234567); AryConversionTestHelper("12345678", 12345678); AryConversionTestHelper("123456789", 123456789); AryConversionTestHelper("2147483647", 2147483647); AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff); AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff); AryConversionTestHelper("7fffffffffffffff", 0x7fffffffffffffff); AryConversionTestHelper("7fffffff", 0x7fffffff); AryConversionTestHelper("1", true); AryConversionTestHelper("0", false); AryConversionTestHelperUp("ABCDEF123456ABCD", 0xabcdef123456abcd); AryConversionTestHelper("-1", -1); AryConversionTestHelper("-50000", -50000); AryConversionTestHelper("-1", -1); AryConversionTestHelper("-510123123123", -510123123123); }