diff --git a/include/etl/to_arithmetic.h b/include/etl/to_arithmetic.h index 5c15d580..c9d5a51b 100644 --- a/include/etl/to_arithmetic.h +++ b/include/etl/to_arithmetic.h @@ -348,19 +348,7 @@ namespace etl value += digit; value_is_valid = true; } - else - { - int i = 0; - } } - else - { - int i = 0; // <<<<<<<< Test to catch this. - } - } - else - { - int i = 0; } return value_is_valid; @@ -401,23 +389,21 @@ namespace etl //*************************************************************************** /// Text to integral from view and radix value type. //*************************************************************************** - template + template ETL_NODISCARD ETL_CONSTEXPR14 - etl::optional to_arithmetic_integral(const etl::basic_string_view& view, - const etl::radix::value_type radix, - const TIntermediate maximum) + etl::optional to_arithmetic_integral(const etl::basic_string_view& view, + const etl::radix::value_type radix, + const TAccumulatorType maximum) { - etl::optional intermediate_result; + etl::optional intermediate_result; etl::basic_string_view::const_iterator itr = view.begin(); const etl::basic_string_view::const_iterator itr_end = view.end(); if (itr != itr_end) { - const char first_char = convert(*itr); - - integral_accumulator accumulator(radix, maximum); + integral_accumulator accumulator(radix, maximum); while ((itr != itr_end) && accumulator.add(convert(*itr))) { diff --git a/test/test_string_to_arithmetic.cpp b/test/test_string_to_arithmetic.cpp index c3a57e8f..c1a6185b 100644 --- a/test/test_string_to_arithmetic.cpp +++ b/test/test_string_to_arithmetic.cpp @@ -48,37 +48,41 @@ namespace //************************************************************************* TEST(test_invalid_radixes) { - //const std::string text("128"); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 0), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 1), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 3), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 4), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 5), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 6), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 7), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 9), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 11), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 12), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 13), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 14), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 15), etl::to_arithmetic_invalid_radix); - //CHECK_THROW((void)etl::to_arithmetic(text.c_str(), text.size(), 17), etl::to_arithmetic_invalid_radix); + const std::string text("128"); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 0)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 1)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 3)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 4)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 5)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 6)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 7)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 9)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 11)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 12)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 13)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 14)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 15)); + CHECK(!etl::to_arithmetic(text.c_str(), text.size(), 17)); } //************************************************************************* TEST(test_invalid_binary_numerics) { - //const std::string text1(" 101"); - //const std::string text2("101 "); - //const std::string text3("121"); - //const std::string text4(""); - //const std::string text5("-101"); + const std::string text1(" 101"); + const std::string text2("101 "); + const std::string text3("121"); + const std::string text4(""); + const std::string text5("-101"); + const std::string text6("100000000"); + const std::string text7("-100000000"); - //CHECK_THROW((void)etl::to_arithmetic(text1.c_str(), text1.size(), etl::bin)); - //CHECK_THROW((void)etl::to_arithmetic(text2.c_str(), text2.size(), etl::bin)); - //CHECK_THROW((void)etl::to_arithmetic(text3.c_str(), text3.size(), etl::bin)); - //CHECK_THROW((void)etl::to_arithmetic(text4.c_str(), text4.size(), etl::bin)); - //CHECK_THROW((void)etl::to_arithmetic(text5.c_str(), text5.size(), etl::bin), etl::to_arithmetic_signed_to_unsigned); + CHECK(!etl::to_arithmetic(text1.c_str(), text1.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text2.c_str(), text2.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text3.c_str(), text3.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text4.c_str(), text4.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text5.c_str(), text5.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text6.c_str(), text6.size(), etl::bin)); + CHECK(!etl::to_arithmetic(text7.c_str(), text7.size(), etl::bin)); } //************************************************************************* @@ -102,61 +106,261 @@ namespace } //************************************************************************* - TEST(test_big_binary_numerics) + TEST(test_maximum_binary_numerics) { - //const std::string value1("1000000000000000000000000000000000000000000000000000000000000000"); - //const std::string value2("0111111111111111111111111111111111111111111111111111111111111111"); - //const std::string value3("1111111111111111111111111111111111111111111111111111111111111111"); + const std::string int8_max("01111111"); + const std::string int8_min_neg("-10000000"); + const std::string int8_min_2c("10000000"); - //CHECK_EQUAL(uint64_t(0x8000000000000000ULL), etl::to_arithmetic(value1.c_str(), value1.size(), etl::bin).value()); - //CHECK_EQUAL(uint64_t(0x7FFFFFFFFFFFFFFFULL), etl::to_arithmetic(value2.c_str(), value2.size(), etl::bin).value()); - //CHECK_EQUAL(uint64_t(0xFFFFFFFFFFFFFFFFULL), etl::to_arithmetic(value3.c_str(), value3.size(), etl::bin).value()); + const std::string uint8_max("11111111"); + const std::string uint8_min("00000000"); + + const std::string int16_max("0111111111111111"); + const std::string int16_min_neg("-1000000000000000"); + const std::string int16_min_2c("1000000000000000"); + + const std::string uint16_max("1111111111111111"); + const std::string uint16_min("0000000000000000"); + + const std::string int32_max("01111111111111111111111111111111"); + const std::string int32_min_neg("-10000000000000000000000000000000"); + const std::string int32_min_2c("10000000000000000000000000000000"); + + const std::string uint32_max("11111111111111111111111111111111"); + const std::string uint32_min("0"); + + const std::string int64_max("0111111111111111111111111111111111111111111111111111111111111111"); + const std::string int64_min_neg("-1000000000000000000000000000000000000000000000000000000000000000"); + const std::string int64_min_2c("1000000000000000000000000000000000000000000000000000000000000000"); + + const std::string uint64_max("1111111111111111111111111111111111111111111111111111111111111111"); + const std::string uint64_min("0"); + + CHECK_EQUAL(127, etl::to_arithmetic(int8_max.c_str(), int8_max.size(), etl::bin)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_neg.c_str(), int8_min_neg.size(), etl::bin)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_2c.c_str(), int8_min_2c.size(), etl::bin)); + + CHECK_EQUAL(255, etl::to_arithmetic(uint8_max.c_str(), uint8_max.size(), etl::bin)); + CHECK_EQUAL(0, etl::to_arithmetic(uint8_min.c_str(), uint8_min.size(), etl::bin)); + + CHECK_EQUAL(32767, etl::to_arithmetic(int16_max.c_str(), int16_max.size(), etl::bin)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_neg.c_str(), int16_min_neg.size(), etl::bin)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_2c.c_str(), int16_min_2c.size(), etl::bin)); + + CHECK_EQUAL(65535, etl::to_arithmetic(uint16_max.c_str(), uint16_max.size(), etl::bin)); + CHECK_EQUAL(0, etl::to_arithmetic(uint16_min.c_str(), uint16_min.size(), etl::bin)); + + CHECK_EQUAL(2147483647LL, etl::to_arithmetic(int32_max.c_str(), int32_max.size(), etl::bin)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_neg.c_str(), int32_min_neg.size(), etl::bin)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_2c.c_str(), int32_min_2c.size(), etl::bin)); + + CHECK_EQUAL(4294967295, etl::to_arithmetic(uint32_max.c_str(), uint32_max.size(), etl::bin)); + CHECK_EQUAL(0, etl::to_arithmetic(uint32_min.c_str(), uint32_min.size(), etl::bin)); + + CHECK_EQUAL(9223372036854775807LL, etl::to_arithmetic(int64_max.c_str(), int64_max.size(), etl::bin)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_neg.c_str(), int64_min_neg.size(), etl::bin)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_2c.c_str(), int64_min_2c.size(), etl::bin)); + + CHECK_EQUAL(18446744073709551615ULL, etl::to_arithmetic(uint64_max.c_str(), uint64_max.size(), etl::bin)); + CHECK_EQUAL(0ULL, etl::to_arithmetic(uint64_min.c_str(), uint64_min.size(), etl::bin)); + } + + //************************************************************************* + TEST(test_binary_numerics_overflow) + { + const std::string int8_overflow_max("100000000"); + const std::string int8_overflow_min("-100000000"); + + const std::string uint8_overflow_max("100000000"); + const std::string uint8_overflow_min("-00000001"); + + const std::string int16_overflow_max("10000000000000000"); + const std::string int16_overflow_min("-10000000000000000"); + + const std::string uint16_overflow_max("10000000000000000"); + const std::string uint16_overflow_min("-0000000000000001"); + + const std::string int32_overflow_max("100000000000000000000000000000000"); + const std::string int32_overflow_min("-100000000000000000000000000000000"); + + const std::string uint32_overflow_max("100000000000000000000000000000000"); + const std::string uint32_overflow_min("-00000000000000000000000000000001"); + + const std::string int64_overflow_max("10000000000000000000000000000000000000000000000000000000000000000"); + const std::string int64_overflow_min("-10000000000000000000000000000000000000000000000000000000000000000"); + + const std::string uint64_overflow_max("10000000000000000000000000000000000000000000000000000000000000000"); + const std::string uint64_overflow_min("-0000000000000000000000000000000000000000000000000000000000000001"); + + CHECK(!etl::to_arithmetic(int8_overflow_max.c_str(), int8_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(int8_overflow_min.c_str(), int8_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(uint8_overflow_max.c_str(), uint8_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(uint8_overflow_min.c_str(), uint8_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(int16_overflow_max.c_str(), int16_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(int16_overflow_min.c_str(), int16_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(uint16_overflow_max.c_str(), uint16_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(uint16_overflow_min.c_str(), uint16_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(int32_overflow_max.c_str(), int32_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(int32_overflow_min.c_str(), int32_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(uint32_overflow_max.c_str(), uint32_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(uint32_overflow_min.c_str(), uint32_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(int64_overflow_max.c_str(), int64_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(int64_overflow_min.c_str(), int64_overflow_min.size(), etl::bin)); + + CHECK(!etl::to_arithmetic(uint64_overflow_max.c_str(), uint64_overflow_max.size(), etl::bin)); + CHECK(!etl::to_arithmetic(uint64_overflow_min.c_str(), uint64_overflow_min.size(), etl::bin)); } //************************************************************************* TEST(test_invalid_octal_numerics) { - //const std::string text1(" 127"); - //const std::string text2("127 "); - //const std::string text3("187"); - //const std::string text4("-187"); + const std::string text1(" 127"); + const std::string text2("127 "); + const std::string text3("187"); + const std::string text4("-187"); - //CHECK_THROW((void)etl::to_arithmetic(text1.c_str(), text1.size(), etl::oct)); - //CHECK_THROW((void)etl::to_arithmetic(text2.c_str(), text2.size(), etl::oct)); - //CHECK_THROW((void)etl::to_arithmetic(text3.c_str(), text3.size(), etl::oct)); - //CHECK_THROW((void)etl::to_arithmetic(text4.c_str(), text4.size(), etl::oct), etl::to_arithmetic_signed_to_unsigned); + CHECK(!etl::to_arithmetic(text1.c_str(), text1.size(), etl::oct)); + CHECK(!etl::to_arithmetic(text2.c_str(), text2.size(), etl::oct)); + CHECK(!etl::to_arithmetic(text3.c_str(), text3.size(), etl::oct)); + CHECK(!etl::to_arithmetic(text4.c_str(), text4.size(), etl::oct)); } //************************************************************************* TEST(test_valid_octal_numerics) { - //const std::string value1("0"); - //const std::string value2("1"); - //const std::string value3("5"); - //const std::string value4("12"); - //const std::string value5("123"); - //const std::string value6("254"); - //const std::string value7("-123"); + const std::string value1("0"); + const std::string value2("01"); + const std::string value3("05"); + const std::string value4("012"); + const std::string value5("0123"); + const std::string value6("0254"); + const std::string value7("-0124"); - //CHECK_EQUAL(int(0), int(etl::to_arithmetic(value1.c_str(), value1.size(), etl::oct).value())); - //CHECK_EQUAL(int(1), int(etl::to_arithmetic(value2.c_str(), value2.size(), etl::oct).value())); - //CHECK_EQUAL(int(5), int(etl::to_arithmetic(value3.c_str(), value3.size(), etl::oct).value())); - //CHECK_EQUAL(int(10), int(etl::to_arithmetic(value4.c_str(), value4.size(), etl::oct).value())); - //CHECK_EQUAL(int(83), int(etl::to_arithmetic(value5.c_str(), value5.size(), etl::oct).value())); - //CHECK_EQUAL(int(-84), int(etl::to_arithmetic(value6.c_str(), value6.size(), etl::oct).value())); - //CHECK_EQUAL(int(-83), int(etl::to_arithmetic(value7.c_str(), value7.size(), etl::oct).value())); + CHECK_EQUAL(int(0), int(etl::to_arithmetic(value1.c_str(), value1.size(), etl::oct).value())); + CHECK_EQUAL(int(1), int(etl::to_arithmetic(value2.c_str(), value2.size(), etl::oct).value())); + CHECK_EQUAL(int(5), int(etl::to_arithmetic(value3.c_str(), value3.size(), etl::oct).value())); + CHECK_EQUAL(int(10), int(etl::to_arithmetic(value4.c_str(), value4.size(), etl::oct).value())); + CHECK_EQUAL(int(83), int(etl::to_arithmetic(value5.c_str(), value5.size(), etl::oct).value())); + CHECK_EQUAL(int(-84), int(etl::to_arithmetic(value6.c_str(), value6.size(), etl::oct).value())); + CHECK_EQUAL(int(-84), int(etl::to_arithmetic(value7.c_str(), value7.size(), etl::oct).value())); } //************************************************************************* - TEST(test_big_octal_numerics) + TEST(test_maximum_octal_numerics) { - //const std::string value1("1000000000000000000000"); - //const std::string value2("0777777777777777777777"); - //const std::string value3("1777777777777777777777"); + const std::string int8_max("177"); + const std::string int8_min_neg("-200"); + const std::string int8_min_2c("200"); - //CHECK_EQUAL(uint64_t(0x8000000000000000ULL), etl::to_arithmetic(value1.c_str(), value1.size(), etl::oct).value()); - //CHECK_EQUAL(uint64_t(0x7FFFFFFFFFFFFFFFULL), etl::to_arithmetic(value2.c_str(), value2.size(), etl::oct).value()); - //CHECK_EQUAL(uint64_t(0xFFFFFFFFFFFFFFFFULL), etl::to_arithmetic(value3.c_str(), value3.size(), etl::oct).value()); + const std::string uint8_max("377"); + const std::string uint8_min("0"); + + const std::string int16_max("77777"); + const std::string int16_min_neg("-100000"); + const std::string int16_min_2c("100000"); + + const std::string uint16_max("177777"); + const std::string uint16_min("0"); + + const std::string int32_max("17777777777"); + const std::string int32_min_neg("-20000000000"); + const std::string int32_min_2c("20000000000"); + + const std::string uint32_max("37777777777"); + const std::string uint32_min("0"); + + const std::string int64_max("777777777777777777777"); + const std::string int64_min_neg("-1000000000000000000000"); + const std::string int64_min_2c("1000000000000000000000"); + + const std::string uint64_max("1777777777777777777777"); + const std::string uint64_min("0"); + + CHECK_EQUAL(127, etl::to_arithmetic(int8_max.c_str(), int8_max.size(), etl::oct)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_neg.c_str(), int8_min_neg.size(), etl::oct)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_2c.c_str(), int8_min_2c.size(), etl::oct)); + + CHECK_EQUAL(255, etl::to_arithmetic(uint8_max.c_str(), uint8_max.size(), etl::oct)); + CHECK_EQUAL(0, etl::to_arithmetic(uint8_min.c_str(), uint8_min.size(), etl::oct)); + + CHECK_EQUAL(32767, etl::to_arithmetic(int16_max.c_str(), int16_max.size(), etl::oct)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_neg.c_str(), int16_min_neg.size(), etl::oct)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_2c.c_str(), int16_min_2c.size(), etl::oct)); + + CHECK_EQUAL(65535, etl::to_arithmetic(uint16_max.c_str(), uint16_max.size(), etl::oct)); + CHECK_EQUAL(0, etl::to_arithmetic(uint16_min.c_str(), uint16_min.size(), etl::oct)); + + CHECK_EQUAL(2147483647LL, etl::to_arithmetic(int32_max.c_str(), int32_max.size(), etl::oct)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_neg.c_str(), int32_min_neg.size(), etl::oct)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_2c.c_str(), int32_min_2c.size(), etl::oct)); + + CHECK_EQUAL(4294967295, etl::to_arithmetic(uint32_max.c_str(), uint32_max.size(), etl::oct)); + CHECK_EQUAL(0, etl::to_arithmetic(uint32_min.c_str(), uint32_min.size(), etl::oct)); + + CHECK_EQUAL(9223372036854775807LL, etl::to_arithmetic(int64_max.c_str(), int64_max.size(), etl::oct)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_neg.c_str(), int64_min_neg.size(), etl::oct)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_2c.c_str(), int64_min_2c.size(), etl::oct)); + + CHECK_EQUAL(18446744073709551615ULL, etl::to_arithmetic(uint64_max.c_str(), uint64_max.size(), etl::oct)); + CHECK_EQUAL(0ULL, etl::to_arithmetic(uint64_min.c_str(), uint64_min.size(), etl::oct)); + } + + //************************************************************************* + TEST(test_octal_numerics_overflow) + { + const std::string int8_overflow_max("400"); + const std::string int8_overflow_min("-400"); + + const std::string uint8_overflow_max("400"); + const std::string uint8_overflow_min("-001"); + + const std::string int16_overflow_max("200000"); + const std::string int16_overflow_min("-200000"); + + const std::string uint16_overflow_max("200000"); + const std::string uint16_overflow_min("-000001"); + + const std::string int32_overflow_max("40000000000"); + const std::string int32_overflow_min("-40000000000"); + + const std::string uint32_overflow_max("40000000000"); + const std::string uint32_overflow_min("-00000000001"); + + const std::string int64_overflow_max("2000000000000000000000"); + const std::string int64_overflow_min("-2000000000000000000000"); + + const std::string uint64_overflow_max("2000000000000000000000"); + const std::string uint64_overflow_min("-0000000000000000000001"); + + CHECK(!etl::to_arithmetic(int8_overflow_max.c_str(), int8_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(int8_overflow_min.c_str(), int8_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(uint8_overflow_max.c_str(), uint8_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(uint8_overflow_min.c_str(), uint8_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(int16_overflow_max.c_str(), int16_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(int16_overflow_min.c_str(), int16_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(uint16_overflow_max.c_str(), uint16_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(uint16_overflow_min.c_str(), uint16_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(int32_overflow_max.c_str(), int32_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(int32_overflow_min.c_str(), int32_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(uint32_overflow_max.c_str(), uint32_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(uint32_overflow_min.c_str(), uint32_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(int64_overflow_max.c_str(), int64_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(int64_overflow_min.c_str(), int64_overflow_min.size(), etl::oct)); + + CHECK(!etl::to_arithmetic(uint64_overflow_max.c_str(), uint64_overflow_max.size(), etl::oct)); + CHECK(!etl::to_arithmetic(uint64_overflow_min.c_str(), uint64_overflow_min.size(), etl::oct)); } //************************************************************************* @@ -257,11 +461,14 @@ namespace } //************************************************************************* - TEST(test_decimal_overflow) + TEST(test_decimal_numerics_overflow) { const std::string int8_overflow_max("128"); const std::string int8_overflow_min("-129"); + const std::string int8_overflow_max_plus("1000"); + const std::string int8_overflow_min_plus("-1000"); + const std::string uint8_overflow_max("256"); const std::string uint8_overflow_min("-1"); @@ -286,6 +493,9 @@ namespace CHECK(!etl::to_arithmetic(int8_overflow_max.c_str(), int8_overflow_max.size(), etl::dec)); CHECK(!etl::to_arithmetic(int8_overflow_min.c_str(), int8_overflow_min.size(), etl::dec)); + CHECK(!etl::to_arithmetic(int8_overflow_max_plus.c_str(), int8_overflow_max_plus.size(), etl::dec)); + CHECK(!etl::to_arithmetic(int8_overflow_min_plus.c_str(), int8_overflow_min_plus.size(), etl::dec)); + CHECK(!etl::to_arithmetic(uint8_overflow_max.c_str(), uint8_overflow_max.size(), etl::dec)); CHECK(!etl::to_arithmetic(uint8_overflow_min.c_str(), uint8_overflow_min.size(), etl::dec)); @@ -311,47 +521,147 @@ namespace //************************************************************************* TEST(test_invalid_hex_numerics) { - //const std::string text1(" 1Af"); - //const std::string text2("1Af "); - //const std::string text3("1Gf"); - //const std::string text4("-1Af"); + const std::string text1(" 1Af"); + const std::string text2("1Af "); + const std::string text3("1Gf"); + const std::string text4("-1Af"); - //CHECK_THROW((void)etl::to_arithmetic(text1.c_str(), text1.size(), etl::dec)); - //CHECK_THROW((void)etl::to_arithmetic(text2.c_str(), text2.size(), etl::dec)); - //CHECK_THROW((void)etl::to_arithmetic(text3.c_str(), text3.size(), etl::dec)); - //CHECK_THROW((void)etl::to_arithmetic(text4.c_str(), text4.size(), etl::dec), etl::to_arithmetic_signed_to_unsigned); + CHECK(!etl::to_arithmetic(text1.c_str(), text1.size(), etl::hex)); + CHECK(!etl::to_arithmetic(text2.c_str(), text2.size(), etl::hex)); + CHECK(!etl::to_arithmetic(text3.c_str(), text3.size(), etl::hex)); + CHECK(!etl::to_arithmetic(text4.c_str(), text4.size(), etl::hex)); } //************************************************************************* TEST(test_valid_hex_numerics) { - //const std::string text1("0"); - //const std::string text2("1"); - //const std::string text3("5"); - //const std::string text4("a"); - //const std::string text5("53"); - //const std::string text6("Ac"); - //const std::string text7("-53"); + const std::string text1("0"); + const std::string text2("1"); + const std::string text3("5"); + const std::string text4("a"); + const std::string text5("53"); + const std::string text6("Ac"); + const std::string text7("-54"); - //CHECK_EQUAL(int(0), int(etl::to_arithmetic(text1.c_str(), text1.size(), etl::hex).value())); - //CHECK_EQUAL(int(1), int(etl::to_arithmetic(text2.c_str(), text2.size(), etl::hex).value())); - //CHECK_EQUAL(int(5), int(etl::to_arithmetic(text3.c_str(), text3.size(), etl::hex).value())); - //CHECK_EQUAL(int(10), int(etl::to_arithmetic(text4.c_str(), text4.size(), etl::hex).value())); - //CHECK_EQUAL(int(83), int(etl::to_arithmetic(text5.c_str(), text5.size(), etl::hex).value())); - //CHECK_EQUAL(int(-84), int(etl::to_arithmetic(text6.c_str(), text6.size(), etl::hex).value())); - //CHECK_EQUAL(int(-83), int(etl::to_arithmetic(text7.c_str(), text7.size(), etl::hex).value())); + CHECK_EQUAL(int(0), int(etl::to_arithmetic(text1.c_str(), text1.size(), etl::hex).value())); + CHECK_EQUAL(int(1), int(etl::to_arithmetic(text2.c_str(), text2.size(), etl::hex).value())); + CHECK_EQUAL(int(5), int(etl::to_arithmetic(text3.c_str(), text3.size(), etl::hex).value())); + CHECK_EQUAL(int(10), int(etl::to_arithmetic(text4.c_str(), text4.size(), etl::hex).value())); + CHECK_EQUAL(int(83), int(etl::to_arithmetic(text5.c_str(), text5.size(), etl::hex).value())); + CHECK_EQUAL(int(-84), int(etl::to_arithmetic(text6.c_str(), text6.size(), etl::hex).value())); + CHECK_EQUAL(int(-84), int(etl::to_arithmetic(text7.c_str(), text7.size(), etl::hex).value())); } //************************************************************************* - TEST(test_big_hex_numerics) + TEST(test_maximum_hex_numerics) { - //const std::string text1("8000000000000000"); - //const std::string text2("7FFFFFFFFFFFFFFF"); - //const std::string text3("FFFFFFFFFFFFFFFF"); + const std::string int8_max("7F"); + const std::string int8_min_neg("-80"); + const std::string int8_min_2c("80"); - //CHECK_EQUAL(uint64_t(0x8000000000000000ULL), etl::to_arithmetic(text1.c_str(), text1.size(), etl::hex).value()); - //CHECK_EQUAL(uint64_t(0x7FFFFFFFFFFFFFFFULL), etl::to_arithmetic(text2.c_str(), text2.size(), etl::hex).value()); - //CHECK_EQUAL(uint64_t(0xFFFFFFFFFFFFFFFFULL), etl::to_arithmetic(text3.c_str(), text3.size(), etl::hex).value()); + const std::string uint8_max("FF"); + const std::string uint8_min("0"); + + const std::string int16_max("7FFF"); + const std::string int16_min_neg("-8000"); + const std::string int16_min_2c("8000"); + + const std::string uint16_max("FFFF"); + const std::string uint16_min("0"); + + const std::string int32_max("7FFFFFFF"); + const std::string int32_min_neg("-80000000"); + const std::string int32_min_2c("80000000"); + + const std::string uint32_max("FFFFFFFF"); + const std::string uint32_min("0"); + + const std::string int64_max("7FFFFFFFFFFFFFFF"); + const std::string int64_min_neg("-8000000000000000"); + const std::string int64_min_2c("8000000000000000"); + + const std::string uint64_max("FFFFFFFFFFFFFFFF"); + const std::string uint64_min("0"); + + CHECK_EQUAL(127, etl::to_arithmetic(int8_max.c_str(), int8_max.size(), etl::hex)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_neg.c_str(), int8_min_neg.size(), etl::hex)); + CHECK_EQUAL(-128, etl::to_arithmetic(int8_min_2c.c_str(), int8_min_2c.size(), etl::hex)); + + CHECK_EQUAL(255, etl::to_arithmetic(uint8_max.c_str(), uint8_max.size(), etl::hex)); + CHECK_EQUAL(0, etl::to_arithmetic(uint8_min.c_str(), uint8_min.size(), etl::hex)); + + CHECK_EQUAL(32767, etl::to_arithmetic(int16_max.c_str(), int16_max.size(), etl::hex)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_neg.c_str(), int16_min_neg.size(), etl::hex)); + CHECK_EQUAL(-32768, etl::to_arithmetic(int16_min_2c.c_str(), int16_min_2c.size(), etl::hex)); + + CHECK_EQUAL(65535, etl::to_arithmetic(uint16_max.c_str(), uint16_max.size(), etl::hex)); + CHECK_EQUAL(0, etl::to_arithmetic(uint16_min.c_str(), uint16_min.size(), etl::hex)); + + CHECK_EQUAL(2147483647LL, etl::to_arithmetic(int32_max.c_str(), int32_max.size(), etl::hex)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_neg.c_str(), int32_min_neg.size(), etl::hex)); + CHECK_EQUAL(-2147483648LL, etl::to_arithmetic(int32_min_2c.c_str(), int32_min_2c.size(), etl::hex)); + + CHECK_EQUAL(4294967295, etl::to_arithmetic(uint32_max.c_str(), uint32_max.size(), etl::hex)); + CHECK_EQUAL(0, etl::to_arithmetic(uint32_min.c_str(), uint32_min.size(), etl::hex)); + + CHECK_EQUAL(9223372036854775807LL, etl::to_arithmetic(int64_max.c_str(), int64_max.size(), etl::hex)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_neg.c_str(), int64_min_neg.size(), etl::hex)); + CHECK_EQUAL(-9223372036854775808LL, etl::to_arithmetic(int64_min_2c.c_str(), int64_min_2c.size(), etl::hex)); + + CHECK_EQUAL(18446744073709551615ULL, etl::to_arithmetic(uint64_max.c_str(), uint64_max.size(), etl::hex)); + CHECK_EQUAL(0ULL, etl::to_arithmetic(uint64_min.c_str(), uint64_min.size(), etl::hex)); + } + + //************************************************************************* + TEST(test_hex_numerics_overflow) + { + const std::string int8_overflow_max("100"); + const std::string int8_overflow_min("-100"); + + const std::string uint8_overflow_max("100"); + const std::string uint8_overflow_min("-01"); + + const std::string int16_overflow_max("10000"); + const std::string int16_overflow_min("-10000"); + + const std::string uint16_overflow_max("10000"); + const std::string uint16_overflow_min("-01"); + + const std::string int32_overflow_max("100000000"); + const std::string int32_overflow_min("-100000000"); + + const std::string uint32_overflow_max("100000000"); + const std::string uint32_overflow_min("-000000001"); + + const std::string int64_overflow_max("10000000000000000"); + const std::string int64_overflow_min("-10000000000000000"); + + const std::string uint64_overflow_max("10000000000000000"); + const std::string uint64_overflow_min("-0000000000000001"); + + CHECK(!etl::to_arithmetic(int8_overflow_max.c_str(), int8_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(int8_overflow_min.c_str(), int8_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(uint8_overflow_max.c_str(), uint8_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(uint8_overflow_min.c_str(), uint8_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(int16_overflow_max.c_str(), int16_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(int16_overflow_min.c_str(), int16_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(uint16_overflow_max.c_str(), uint16_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(uint16_overflow_min.c_str(), uint16_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(int32_overflow_max.c_str(), int32_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(int32_overflow_min.c_str(), int32_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(uint32_overflow_max.c_str(), uint32_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(uint32_overflow_min.c_str(), uint32_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(int64_overflow_max.c_str(), int64_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(int64_overflow_min.c_str(), int64_overflow_min.size(), etl::hex)); + + CHECK(!etl::to_arithmetic(uint64_overflow_max.c_str(), uint64_overflow_max.size(), etl::hex)); + CHECK(!etl::to_arithmetic(uint64_overflow_min.c_str(), uint64_overflow_min.size(), etl::hex)); } //*************************************************************************