From f87cddeeeb391921f944350a60c6d2a27c4e0e14 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 22 Jul 2018 01:01:59 +0100 Subject: [PATCH] Get bool conversion to work --- include/duckhandy/int_conv.hpp | 16 ++++++++++++++-- test/unit/int_conv_test.cpp | 7 +++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/duckhandy/int_conv.hpp b/include/duckhandy/int_conv.hpp index 3498ccf..a8a3d13 100644 --- a/include/duckhandy/int_conv.hpp +++ b/include/duckhandy/int_conv.hpp @@ -74,7 +74,7 @@ namespace dhandy { static constexpr CastedType cast (I in) { return static_cast(in); } }; - template + template struct IntConversion { static constexpr ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1> to_ary (I in) { using RetType = ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1>; @@ -94,8 +94,20 @@ namespace dhandy { } }; + template + struct IntConversion { + static constexpr ReversedSizedArray().to_digit(1))>, 2> to_ary (bool in) { + using RetType = ReversedSizedArray().to_digit(1))>, 2>; + + RetType arr; + arr.push_front('\0'); + arr.push_front(in ? Tr::to_digit(1) : Tr::to_digit(0)); + return arr; + } + }; + template - struct IntConversion { + struct IntConversion::value and not std::is_same::value>::type> { static constexpr ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1> to_ary (I in) { using RetType = ReversedSizedArray().to_digit(1))>, implem::int_info::max_len + 1>; using Num = implem::NumberAdaptation; diff --git a/test/unit/int_conv_test.cpp b/test/unit/int_conv_test.cpp index 5f2086b..feb8741 100644 --- a/test/unit/int_conv_test.cpp +++ b/test/unit/int_conv_test.cpp @@ -81,8 +81,10 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") { CHECK(to_string_view(int_to_ary(0)) == "0"); CHECK(to_string_view(int_to_ary(0)) == "0"); CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(false)) == "0"); - //CHECK(to_string_view(int_to_ary(true)) == "0"); + CHECK(to_string_view(int_to_ary(false)) == "0"); + CHECK(to_string_view(int_to_ary(true)) == "1"); + CHECK(to_string_view(int_to_ary(false)) == "0"); + CHECK(to_string_view(int_to_ary(true)) == "1"); CHECK(to_string_view(int_to_ary(0b10101010)) == "10101010"); CHECK(to_string_view(int_to_ary(0b10101010)) == "10101010"); CHECK(to_string_view(int_to_ary(0b11111111)) == "11111111"); @@ -92,6 +94,7 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") { CHECK(to_string_view(int_to_ary(9223372036854775807)) == "1y2p0ij32e8e7"); CHECK(to_string_view(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(to_string_view(int_to_ary<__int128_t, 10>(num * 100)) == "1000000000000000000000"); num = 0xFFFFFFFFFFFFFFFF;