/* Copyright 2017, 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 "catch.hpp"
#include "duckhandy/lexical_cast.hpp"
#include "duckhandy/int_to_string_ary.hpp"
#include
#include
#include
#include
template (C==E)>
struct CountDigitsDebugger { static constexpr const bool value = true; };
template
struct CountDigitsDebugger {};
TEST_CASE ("Check string to int conversions", "[s2i][lexical_cast]") {
using dhandy::lexical_cast;
using dhandy::tags::dec;
using dhandy::tags::hex;
using dhandy::tags::bin;
static_assert(CountDigitsDebugger::count_digits_bt(0), 1>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(1), 1>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(2), 2>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(3), 2>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(4), 3>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(5), 3>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(6), 3>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(7), 3>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(8), 4>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(9), 4>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(255), 8>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(256), 9>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(255), 8>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(256), 9>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(255), 8>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(256), 9>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(255), 8>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(255), 8>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(-1), 2>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(-2), 2>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(-10), 3>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(-99), 3>::value, "Wrong digits count");
static_assert(static_cast(sprout::log10(sprout::abs(-10000000.0))) == 7, "Wrong log10");
static_assert(CountDigitsDebugger::count_digits_bt(-10000000), 9>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(-123456789), 10>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(INT_MAX), 11>::value, "Wrong digits count");
static_assert(CountDigitsDebugger::count_digits_bt(INT_MIN), 11>::value, "Wrong digits count");
CHECK(lexical_cast(std::string("0")) == 0);
CHECK(lexical_cast(std::string("1")) == 1);
CHECK(lexical_cast(std::string("9")) == 9);
CHECK(lexical_cast(std::string("10")) == 10);
CHECK(lexical_cast(std::string("11")) == 11);
CHECK(lexical_cast(std::string("99")) == 99);
CHECK(lexical_cast(std::string("512")) == 512);
CHECK(lexical_cast(std::string("513")) == 513);
CHECK(lexical_cast(std::string("15000")) == 15000);
CHECK(lexical_cast(std::string("-1")) == -1);
CHECK(lexical_cast(std::string("-2")) == -2);
CHECK(lexical_cast(std::string("-10")) == -10);
CHECK(lexical_cast(std::string("-100000")) == -100000);
}
TEST_CASE ("Check int to string conversions", "[i2s][lexical_cast]") {
using std::string;
using dhandy::lexical_cast;
using dhandy::tags::bin;
using dhandy::int_to_string_ary;
CHECK(lexical_cast(1) == "1");
CHECK(lexical_cast(static_cast(0xFFFF)) == "65535");
CHECK(lexical_cast(static_cast(0xFFFF)) == "65535");
CHECK((lexical_cast(static_cast(0xFFFF)) == "1111111111111111"));
CHECK((lexical_cast(static_cast(0x7FFF)) == "111111111111111"));
CHECK((lexical_cast(static_cast(0x0)) == "0"));
CHECK((lexical_cast(static_cast(0x1)) == "1"));
{
auto fixed = int_to_string_ary(1234);
std::string str = lexical_cast(1234);
REQUIRE(fixed.size() == str.size());
CHECK(std::equal(fixed.begin(), fixed.end(), str.begin()));
}
{
auto fixed = int_to_string_ary(-1234);
std::string str = lexical_cast(-1234);
REQUIRE(fixed.size() == str.size());
CHECK(std::equal(fixed.begin(), fixed.end(), str.begin()));
}
}