diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/string/cast_ut.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/string/cast_ut.cpp')
-rw-r--r-- | util/string/cast_ut.cpp | 544 |
1 files changed, 272 insertions, 272 deletions
diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index 033450c38c..2b7a8bc98b 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -1,44 +1,44 @@ -#include "cast.h" - +#include "cast.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/charset/wide.h> #include <util/system/defaults.h> #include <limits> // positive test (return true or no exception) -#define test1(t, v) \ - F<t>().CheckTryOK(v); \ - F<t>().CheckOK(v) - +#define test1(t, v) \ + F<t>().CheckTryOK(v); \ + F<t>().CheckOK(v) + // negative test (return false or exception) -#define test2(t, v) \ - F<t>().CheckTryFail(v); \ - F<t>().CheckExc(v) - -#define EPS 10E-7 - +#define test2(t, v) \ + F<t>().CheckTryFail(v); \ + F<t>().CheckExc(v) + +#define EPS 10E-7 + #define HEX_MACROS_MAP(mac, type, val) mac(type, val, 2) mac(type, val, 8) mac(type, val, 10) mac(type, val, 16) -#define OK_HEX_CHECK(type, val, base) UNIT_ASSERT_EQUAL((IntFromStringForCheck<base>(IntToString<base>(val))), val); +#define OK_HEX_CHECK(type, val, base) UNIT_ASSERT_EQUAL((IntFromStringForCheck<base>(IntToString<base>(val))), val); #define EXC_HEX_CHECK(type, val, base) UNIT_ASSERT_EXCEPTION((IntFromString<type, base>(IntToString<base>(val))), yexception); #define TRY_HEX_MACROS_MAP(mac, type, val, result, def) \ - mac(type, val, result, def, 2) \ - mac(type, val, result, def, 8) \ - mac(type, val, result, def, 10) \ - mac(type, val, result, def, 16) + mac(type, val, result, def, 2) \ + mac(type, val, result, def, 8) \ + mac(type, val, result, def, 10) \ + mac(type, val, result, def, 16) -#define TRY_OK_HEX_CHECK(type, val, result, def, base) \ - result = def; \ +#define TRY_OK_HEX_CHECK(type, val, result, def, base) \ + result = def; \ UNIT_ASSERT_EQUAL(TryIntFromStringForCheck<base>(IntToString<base>(val), result), true); \ UNIT_ASSERT_EQUAL(result, val); -#define TRY_FAIL_HEX_CHECK(type, val, result, def, base) \ - result = def; \ - UNIT_ASSERT_VALUES_EQUAL(TryIntFromStringForCheck<base>(IntToString<base>(val), result), false); \ - UNIT_ASSERT_VALUES_EQUAL(result, def); +#define TRY_FAIL_HEX_CHECK(type, val, result, def, base) \ + result = def; \ + UNIT_ASSERT_VALUES_EQUAL(TryIntFromStringForCheck<base>(IntToString<base>(val), result), false); \ + UNIT_ASSERT_VALUES_EQUAL(result, def); template <class A> struct TRet { @@ -46,7 +46,7 @@ struct TRet { inline A IntFromStringForCheck(const TString& str) { return IntFromString<A, base>(str); } - + template <int base> inline bool TryIntFromStringForCheck(const TString& str, A& result) { return TryIntFromString<base>(str, result); @@ -54,11 +54,11 @@ struct TRet { template <class B> inline void CheckOK(B v) { - UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToString(v)), v); // char - UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToWtring(v)), v); // wide char + UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToString(v)), v); // char + UNIT_ASSERT_VALUES_EQUAL(FromString<A>(ToWtring(v)), v); // wide char HEX_MACROS_MAP(OK_HEX_CHECK, A, v); } - + template <class B> inline void CheckExc(B v) { UNIT_ASSERT_EXCEPTION(FromString<A>(ToString(v)), yexception); // char @@ -70,10 +70,10 @@ struct TRet { inline void CheckTryOK(B v) { static const A defaultV = 42; A convV; - UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), true); // char - UNIT_ASSERT_VALUES_EQUAL(v, convV); - UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToWtring(v), convV), true); // wide char - UNIT_ASSERT_VALUES_EQUAL(v, convV); + UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), true); // char + UNIT_ASSERT_VALUES_EQUAL(v, convV); + UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToWtring(v), convV), true); // wide char + UNIT_ASSERT_VALUES_EQUAL(v, convV); TRY_HEX_MACROS_MAP(TRY_OK_HEX_CHECK, A, v, convV, defaultV); } @@ -81,10 +81,10 @@ struct TRet { template <class B> inline void CheckTryFail(B v) { static const A defaultV = 42; - A convV = defaultV; // to check that original value is not trashed on bad cast - UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), false); // char + A convV = defaultV; // to check that original value is not trashed on bad cast + UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToString(v), convV), false); // char UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); - UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToWtring(v), convV), false); // wide char + UNIT_ASSERT_VALUES_EQUAL(TryFromString<A>(ToWtring(v), convV), false); // wide char UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); TRY_HEX_MACROS_MAP(TRY_FAIL_HEX_CHECK, A, v, convV, defaultV); @@ -95,14 +95,14 @@ template <> struct TRet<bool> { template <class B> inline void CheckOK(B v) { - UNIT_ASSERT_VALUES_EQUAL(FromString<bool>(ToString(v)), v); + UNIT_ASSERT_VALUES_EQUAL(FromString<bool>(ToString(v)), v); } template <class B> inline void CheckTryOK(B v) { B convV; - UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), true); - UNIT_ASSERT_VALUES_EQUAL(v, convV); + UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), true); + UNIT_ASSERT_VALUES_EQUAL(v, convV); } template <class B> @@ -114,7 +114,7 @@ struct TRet<bool> { inline void CheckTryFail(B v) { static const bool defaultV = false; bool convV = defaultV; - UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), false); + UNIT_ASSERT_VALUES_EQUAL(TryFromString<bool>(ToString(v), convV), false); UNIT_ASSERT_VALUES_EQUAL(defaultV, convV); } }; @@ -124,7 +124,7 @@ inline TRet<A> F() { return TRet<A>(); }; -#if 0 +#if 0 template <class T> inline void CheckConvertToBuffer(const T& value, const size_t size, const TString& canonValue) { const size_t maxSize = 256; @@ -136,51 +136,51 @@ inline void CheckConvertToBuffer(const T& value, const size_t size, const TStrin UNIT_ASSERT_EXCEPTION(length = ToString(value, buffer, size), yexception); // check that no bytes after size was trashed for (size_t i = size; i < maxSize; ++i) - UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); + UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); } else { length = ToString(value, buffer, size); UNIT_ASSERT(length < maxSize); // check that no bytes after length was trashed for (size_t i = length; i < maxSize; ++i) - UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); + UNIT_ASSERT_VALUES_EQUAL(buffer[i], magic); TStringBuf result(buffer, length); - UNIT_ASSERT_VALUES_EQUAL(result, TStringBuf(canonValue)); + UNIT_ASSERT_VALUES_EQUAL(result, TStringBuf(canonValue)); } } -#endif +#endif Y_UNIT_TEST_SUITE(TCastTest) { - template <class A> - inline TRet<A> F() { - return TRet<A>(); - }; - - template <class TFloat> - void GoodFloatTester(const char* str, const TFloat canonValue, const double eps) { - TFloat f = canonValue + 42.0; // shift value to make it far from proper - UNIT_ASSERT_VALUES_EQUAL(TryFromString<TFloat>(str, f), true); - UNIT_ASSERT_DOUBLES_EQUAL(f, canonValue, eps); - f = FromString<TFloat>(str); - UNIT_ASSERT_DOUBLES_EQUAL(f, canonValue, eps); - } - - template <class TFloat> - void BadFloatTester(const char* str) { - const double eps = 10E-5; - TFloat f = 42.0; // make it far from proper - auto res = TryFromString<TFloat>(str, f); - - UNIT_ASSERT_VALUES_EQUAL(res, false); - UNIT_ASSERT_DOUBLES_EQUAL(f, 42.0, eps); // check value was not trashed - UNIT_ASSERT_EXCEPTION(f = FromString<TFloat>(str), TFromStringException); + template <class A> + inline TRet<A> F() { + return TRet<A>(); + }; + + template <class TFloat> + void GoodFloatTester(const char* str, const TFloat canonValue, const double eps) { + TFloat f = canonValue + 42.0; // shift value to make it far from proper + UNIT_ASSERT_VALUES_EQUAL(TryFromString<TFloat>(str, f), true); + UNIT_ASSERT_DOUBLES_EQUAL(f, canonValue, eps); + f = FromString<TFloat>(str); + UNIT_ASSERT_DOUBLES_EQUAL(f, canonValue, eps); + } + + template <class TFloat> + void BadFloatTester(const char* str) { + const double eps = 10E-5; + TFloat f = 42.0; // make it far from proper + auto res = TryFromString<TFloat>(str, f); + + UNIT_ASSERT_VALUES_EQUAL(res, false); + UNIT_ASSERT_DOUBLES_EQUAL(f, 42.0, eps); // check value was not trashed + UNIT_ASSERT_EXCEPTION(f = FromString<TFloat>(str), TFromStringException); Y_UNUSED(f); // shut up compiler about 'assigned value that is not used' - } + } Y_UNIT_TEST(TestToFrom) { - test1(bool, true); - test1(bool, false); - test2(bool, ""); - test2(bool, "a"); + test1(bool, true); + test1(bool, false); + test2(bool, ""); + test2(bool, "a"); test2(ui8, -1); test1(i8, -1); @@ -194,91 +194,91 @@ Y_UNIT_TEST_SUITE(TCastTest) { test1(ui8, UCHAR_MAX - 1); test2(ui8, (int)UCHAR_MAX + 1); test2(ui8, -1); - test1(int, -1); - test2(unsigned int, -1); - test1(short int, -1); - test2(unsigned short int, -1); - test1(long int, -1); - test2(unsigned long int, -1); - test1(int, INT_MAX); - test1(int, INT_MIN); - test1(int, INT_MAX - 1); - test1(int, INT_MIN + 1); - test2(int, (long long int)INT_MAX + 1); - test2(int, (long long int)INT_MIN - 1); - test1(unsigned int, UINT_MAX); - test1(unsigned int, UINT_MAX - 1); - test2(unsigned int, (long long int)UINT_MAX + 1); - test1(short int, SHRT_MAX); - test1(short int, SHRT_MIN); - test1(short int, SHRT_MAX - 1); - test1(short int, SHRT_MIN + 1); - test2(short int, (long long int)SHRT_MAX + 1); - test2(short int, (long long int)SHRT_MIN - 1); - test1(unsigned short int, USHRT_MAX); - test1(unsigned short int, USHRT_MAX - 1); - test2(unsigned short int, (long long int)USHRT_MAX + 1); - test1(long int, LONG_MAX); - test1(long int, LONG_MIN); - test1(long int, LONG_MAX - 1); - test1(long int, LONG_MIN + 1); + test1(int, -1); + test2(unsigned int, -1); + test1(short int, -1); + test2(unsigned short int, -1); + test1(long int, -1); + test2(unsigned long int, -1); + test1(int, INT_MAX); + test1(int, INT_MIN); + test1(int, INT_MAX - 1); + test1(int, INT_MIN + 1); + test2(int, (long long int)INT_MAX + 1); + test2(int, (long long int)INT_MIN - 1); + test1(unsigned int, UINT_MAX); + test1(unsigned int, UINT_MAX - 1); + test2(unsigned int, (long long int)UINT_MAX + 1); + test1(short int, SHRT_MAX); + test1(short int, SHRT_MIN); + test1(short int, SHRT_MAX - 1); + test1(short int, SHRT_MIN + 1); + test2(short int, (long long int)SHRT_MAX + 1); + test2(short int, (long long int)SHRT_MIN - 1); + test1(unsigned short int, USHRT_MAX); + test1(unsigned short int, USHRT_MAX - 1); + test2(unsigned short int, (long long int)USHRT_MAX + 1); + test1(long int, LONG_MAX); + test1(long int, LONG_MIN); + test1(long int, LONG_MAX - 1); + test1(long int, LONG_MIN + 1); test1(long long int, LLONG_MAX); test1(long long int, LLONG_MIN); test1(long long int, LLONG_MAX - 1); test1(long long int, LLONG_MIN + 1); - } - + } + Y_UNIT_TEST(TestVolatile) { - volatile int x = 1; - UNIT_ASSERT_VALUES_EQUAL(ToString(x), "1"); - } - + volatile int x = 1; + UNIT_ASSERT_VALUES_EQUAL(ToString(x), "1"); + } + Y_UNIT_TEST(TestStrToD) { UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1", nullptr), 1.1, EPS); UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.12345678", nullptr), 1.12345678, EPS); UNIT_ASSERT_DOUBLES_EQUAL(StrToD("10E-5", nullptr), 10E-5, EPS); UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1E+5", nullptr), 1.1E+5, EPS); - + char* ret = nullptr; - - UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1y", &ret), 1.1, EPS); - UNIT_ASSERT_VALUES_EQUAL(*ret, 'y'); - UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.12345678z", &ret), 1.12345678, EPS); - UNIT_ASSERT_VALUES_EQUAL(*ret, 'z'); - UNIT_ASSERT_DOUBLES_EQUAL(StrToD("10E-5y", &ret), 10E-5, EPS); - UNIT_ASSERT_VALUES_EQUAL(*ret, 'y'); - UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1E+5z", &ret), 1.1E+5, EPS); - UNIT_ASSERT_VALUES_EQUAL(*ret, 'z'); - } - + + UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1y", &ret), 1.1, EPS); + UNIT_ASSERT_VALUES_EQUAL(*ret, 'y'); + UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.12345678z", &ret), 1.12345678, EPS); + UNIT_ASSERT_VALUES_EQUAL(*ret, 'z'); + UNIT_ASSERT_DOUBLES_EQUAL(StrToD("10E-5y", &ret), 10E-5, EPS); + UNIT_ASSERT_VALUES_EQUAL(*ret, 'y'); + UNIT_ASSERT_DOUBLES_EQUAL(StrToD("1.1E+5z", &ret), 1.1E+5, EPS); + UNIT_ASSERT_VALUES_EQUAL(*ret, 'z'); + } + Y_UNIT_TEST(TestFloats) { // "%g" mode - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_NDIGITS, 6), "0.1"); // drop trailing zeroes - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_NDIGITS, 6), "0.123457"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_NDIGITS, 6), "1e-20"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_NDIGITS, 6), "0.1"); // drop trailing zeroes + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_NDIGITS, 6), "0.123457"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_NDIGITS, 6), "1e-20"); // "%f" mode - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_POINT_DIGITS, 6), "0.100000"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_POINT_DIGITS, 6), "0.123457"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_POINT_DIGITS, 6), "0.000000"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(12.34f, PREC_POINT_DIGITS, 0), "12"); // rounding to integers drops '.' + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_POINT_DIGITS, 6), "0.100000"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_POINT_DIGITS, 6), "0.123457"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_POINT_DIGITS, 6), "0.000000"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(12.34f, PREC_POINT_DIGITS, 0), "12"); // rounding to integers drops '.' // strip trailing zeroes - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0.1"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0.123457"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(12.34f, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "12"); // rounding to integers drops '.' - UNIT_ASSERT_VALUES_EQUAL(FloatToString(10000.0f, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "10000"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0.1"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0.123457"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-20f, PREC_POINT_DIGITS_STRIP_ZEROES, 6), "0"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(12.34f, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "12"); // rounding to integers drops '.' + UNIT_ASSERT_VALUES_EQUAL(FloatToString(10000.0f, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "10000"); // automatic selection of ndigits - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f), "0.1"); // drop trailing zeroes - UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f), "0.12345678"); // 8 valid digits - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1000.00006f), "1000.00006"); // 9 valid digits - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-45f), "1e-45"); // denormalized: 1 valid digit - UNIT_ASSERT_VALUES_EQUAL(FloatToString(-0.0f), "-0"); // sign must be preserved + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.1f), "0.1"); // drop trailing zeroes + UNIT_ASSERT_VALUES_EQUAL(FloatToString(0.12345678f), "0.12345678"); // 8 valid digits + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1000.00006f), "1000.00006"); // 9 valid digits + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1e-45f), "1e-45"); // denormalized: 1 valid digit + UNIT_ASSERT_VALUES_EQUAL(FloatToString(-0.0f), "-0"); // sign must be preserved // version for double - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.0 / 10000), "0.0001"); // trailing zeroes - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.2345678901234567), "1.2345678901234567"); // no truncation - UNIT_ASSERT_VALUES_EQUAL(FloatToString(5e-324), "5e-324"); // denormalized - UNIT_ASSERT_VALUES_EQUAL(FloatToString(-0.0), "-0"); // sign must be preserved + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.0 / 10000), "0.0001"); // trailing zeroes + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.2345678901234567), "1.2345678901234567"); // no truncation + UNIT_ASSERT_VALUES_EQUAL(FloatToString(5e-324), "5e-324"); // denormalized + UNIT_ASSERT_VALUES_EQUAL(FloatToString(-0.0), "-0"); // sign must be preserved UNIT_ASSERT_STRINGS_EQUAL(FloatToString(std::numeric_limits<double>::quiet_NaN()), "nan"); UNIT_ASSERT_STRINGS_EQUAL(FloatToString(std::numeric_limits<double>::infinity()), "inf"); @@ -287,154 +287,154 @@ Y_UNIT_TEST_SUITE(TCastTest) { UNIT_ASSERT_STRINGS_EQUAL(FloatToString(std::numeric_limits<float>::quiet_NaN()), "nan"); UNIT_ASSERT_STRINGS_EQUAL(FloatToString(std::numeric_limits<float>::infinity()), "inf"); UNIT_ASSERT_STRINGS_EQUAL(FloatToString(-std::numeric_limits<float>::infinity()), "-inf"); - } - + } + Y_UNIT_TEST(TestReadFloats) { - GoodFloatTester<float>("0.0001", 0.0001f, EPS); - GoodFloatTester<double>("0.0001", 0.0001, EPS); - GoodFloatTester<long double>("0.0001", 0.0001, EPS); - GoodFloatTester<float>("10E-5", 10E-5f, EPS); - GoodFloatTester<double>("1.0001E5", 1.0001E5, EPS); - GoodFloatTester<long double>("1.0001e5", 1.0001e5, EPS); - GoodFloatTester<long double>(".0001e5", .0001e5, EPS); - BadFloatTester<float>("a10E-5"); - BadFloatTester<float>("10 "); - BadFloatTester<float>("10\t"); - //BadFloatTester<float>("10E"); - //BadFloatTester<float>("10.E"); - BadFloatTester<float>("..0"); + GoodFloatTester<float>("0.0001", 0.0001f, EPS); + GoodFloatTester<double>("0.0001", 0.0001, EPS); + GoodFloatTester<long double>("0.0001", 0.0001, EPS); + GoodFloatTester<float>("10E-5", 10E-5f, EPS); + GoodFloatTester<double>("1.0001E5", 1.0001E5, EPS); + GoodFloatTester<long double>("1.0001e5", 1.0001e5, EPS); + GoodFloatTester<long double>(".0001e5", .0001e5, EPS); + BadFloatTester<float>("a10E-5"); + BadFloatTester<float>("10 "); + BadFloatTester<float>("10\t"); + //BadFloatTester<float>("10E"); + //BadFloatTester<float>("10.E"); + BadFloatTester<float>("..0"); BadFloatTester<float>(""); // IGNIETFERRO-300 - BadFloatTester<double>("1.00.01"); - BadFloatTester<double>("1.0001E5b"); - BadFloatTester<double>("1.0001s"); - BadFloatTester<double>("1..01"); + BadFloatTester<double>("1.00.01"); + BadFloatTester<double>("1.0001E5b"); + BadFloatTester<double>("1.0001s"); + BadFloatTester<double>("1..01"); BadFloatTester<double>(""); // IGNIETFERRO-300 - BadFloatTester<long double>(".1.00"); - BadFloatTester<long double>("1.00."); - BadFloatTester<long double>("1.0001e5-"); - BadFloatTester<long double>("10e 2"); + BadFloatTester<long double>(".1.00"); + BadFloatTester<long double>("1.00."); + BadFloatTester<long double>("1.0001e5-"); + BadFloatTester<long double>("10e 2"); BadFloatTester<long double>(""); // IGNIETFERRO-300 - } + } Y_UNIT_TEST(TestLiteral) { - UNIT_ASSERT_VALUES_EQUAL(ToString("abc"), TString("abc")); - } + UNIT_ASSERT_VALUES_EQUAL(ToString("abc"), TString("abc")); + } Y_UNIT_TEST(TestFromStringStringBuf) { TString a = "xyz"; - TStringBuf b = FromString<TStringBuf>(a); - UNIT_ASSERT_VALUES_EQUAL(a, b); + TStringBuf b = FromString<TStringBuf>(a); + UNIT_ASSERT_VALUES_EQUAL(a, b); UNIT_ASSERT_VALUES_EQUAL((void*)a.data(), (void*)b.data()); - } + } -#if 0 +#if 0 Y_UNIT_TEST(TestBufferOverflow) { - CheckConvertToBuffer<float>(1.f, 5, "1"); - CheckConvertToBuffer<float>(1.005f, 3, "1.005"); - CheckConvertToBuffer<float>(1.00000000f, 3, "1"); + CheckConvertToBuffer<float>(1.f, 5, "1"); + CheckConvertToBuffer<float>(1.005f, 3, "1.005"); + CheckConvertToBuffer<float>(1.00000000f, 3, "1"); - CheckConvertToBuffer<double>(1.f, 5, "1"); - CheckConvertToBuffer<double>(1.005f, 3, "1.005"); - CheckConvertToBuffer<double>(1.00000000f, 3, "1"); + CheckConvertToBuffer<double>(1.f, 5, "1"); + CheckConvertToBuffer<double>(1.005f, 3, "1.005"); + CheckConvertToBuffer<double>(1.00000000f, 3, "1"); - CheckConvertToBuffer<int>(2, 5, "2"); - CheckConvertToBuffer<int>(1005, 3, "1005"); + CheckConvertToBuffer<int>(2, 5, "2"); + CheckConvertToBuffer<int>(1005, 3, "1005"); - CheckConvertToBuffer<size_t>(2, 5, "2"); - CheckConvertToBuffer<ui64>(1005000000000000ull, 32, "1005000000000000"); - CheckConvertToBuffer<ui64>(1005000000000000ull, 3, "1005000000000000"); + CheckConvertToBuffer<size_t>(2, 5, "2"); + CheckConvertToBuffer<ui64>(1005000000000000ull, 32, "1005000000000000"); + CheckConvertToBuffer<ui64>(1005000000000000ull, 3, "1005000000000000"); - // TString longNumber = TString("1.") + TString(1 << 20, '1'); - // UNIT_ASSERT_EXCEPTION(FromString<double>(longNumber), yexception); - } -#endif + // TString longNumber = TString("1.") + TString(1 << 20, '1'); + // UNIT_ASSERT_EXCEPTION(FromString<double>(longNumber), yexception); + } +#endif Y_UNIT_TEST(TestWide) { TUtf16String iw = u"-100500"; - int iv = 0; - UNIT_ASSERT_VALUES_EQUAL(TryFromString(iw, iv), true); - UNIT_ASSERT_VALUES_EQUAL(iv, -100500); + int iv = 0; + UNIT_ASSERT_VALUES_EQUAL(TryFromString(iw, iv), true); + UNIT_ASSERT_VALUES_EQUAL(iv, -100500); - ui64 uv = 0; + ui64 uv = 0; TUtf16String uw = u"21474836470"; - UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); - UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); + UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); + UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); TWtringBuf bw(uw.data(), uw.size()); - uv = 0; - UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); - UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); + uv = 0; + UNIT_ASSERT_VALUES_EQUAL(TryFromString(uw, uv), true); + UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); const wchar16* beg = uw.data(); - uv = 0; + uv = 0; UNIT_ASSERT_VALUES_EQUAL(TryFromString(beg, uw.size(), uv), true); - UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); - } + UNIT_ASSERT_VALUES_EQUAL(uv, 21474836470ull); + } Y_UNIT_TEST(TestDefault) { size_t res = 0; const size_t def1 = 42; TString s1("100500"); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res, def1), true); - UNIT_ASSERT_VALUES_EQUAL(res, 100500); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res, def1), true); + UNIT_ASSERT_VALUES_EQUAL(res, 100500); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res), true); - UNIT_ASSERT_VALUES_EQUAL(res, 100500); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res), true); + UNIT_ASSERT_VALUES_EQUAL(res, 100500); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100500", res, def1), true); - UNIT_ASSERT_VALUES_EQUAL(res, 100500); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100500", res, def1), true); + UNIT_ASSERT_VALUES_EQUAL(res, 100500); UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s1, def1), yexception); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s1, def1), 100500); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s1), 100500); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault("100500", def1), 100500); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s1, def1), 100500); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s1), 100500); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault("100500", def1), 100500); TString s2("100q500"); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res), false); - UNIT_ASSERT_VALUES_EQUAL(res, size_t()); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res), false); + UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res, def1), false); - UNIT_ASSERT_VALUES_EQUAL(res, def1); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res, def1), false); + UNIT_ASSERT_VALUES_EQUAL(res, def1); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100q500", res), false); - UNIT_ASSERT_VALUES_EQUAL(res, size_t()); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100q500", res), false); + UNIT_ASSERT_VALUES_EQUAL(res, size_t()); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100 500", res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s2, def1), yexception); UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault("100q500", def1), yexception); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s2, def1), def1); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s2), size_t()); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>("100q500"), size_t()); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s2, def1), def1); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s2), size_t()); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>("100q500"), size_t()); UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); int res2 = 0; const int def2 = -6; TUtf16String s3 = u"-100500"; - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2, def2), true); - UNIT_ASSERT_VALUES_EQUAL(res2, -100500); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2, def2), true); + UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2), true); - UNIT_ASSERT_VALUES_EQUAL(res2, -100500); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2), true); + UNIT_ASSERT_VALUES_EQUAL(res2, -100500); UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3, def2), -100500); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s3), size_t()); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3, def2), -100500); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s3), size_t()); TUtf16String s4 = u"-f100500"; - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2, def2), false); - UNIT_ASSERT_VALUES_EQUAL(res2, def2); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2, def2), false); + UNIT_ASSERT_VALUES_EQUAL(res2, def2); - UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2), false); - UNIT_ASSERT_VALUES_EQUAL(res2, size_t()); + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2), false); + UNIT_ASSERT_VALUES_EQUAL(res2, size_t()); UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s4, def2), def2); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s4, def2), def2); UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); - UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s4), size_t()); + UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s4), size_t()); } Y_UNIT_TEST(TestBool) { @@ -450,21 +450,21 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestAutoDetectType) { - UNIT_ASSERT_DOUBLES_EQUAL((float)FromString("0.0001"), 0.0001, EPS); - UNIT_ASSERT_DOUBLES_EQUAL((double)FromString("0.0015", sizeof("0.0015") - 2), 0.001, EPS); + UNIT_ASSERT_DOUBLES_EQUAL((float)FromString("0.0001"), 0.0001, EPS); + UNIT_ASSERT_DOUBLES_EQUAL((double)FromString("0.0015", sizeof("0.0015") - 2), 0.001, EPS); UNIT_ASSERT_DOUBLES_EQUAL((long double)FromString(TStringBuf("0.0001")), 0.0001, EPS); UNIT_ASSERT_DOUBLES_EQUAL((float)FromString(TString("10E-5")), 10E-5, EPS); - UNIT_ASSERT_VALUES_EQUAL((bool)FromString("da"), true); - UNIT_ASSERT_VALUES_EQUAL((bool)FromString("no"), false); + UNIT_ASSERT_VALUES_EQUAL((bool)FromString("da"), true); + UNIT_ASSERT_VALUES_EQUAL((bool)FromString("no"), false); UNIT_ASSERT_VALUES_EQUAL((short)FromString(u"9000"), 9000); UNIT_ASSERT_VALUES_EQUAL((int)FromString(u"-100500"), -100500); UNIT_ASSERT_VALUES_EQUAL((unsigned long long)FromString(TWtringBuf(u"42", 1)), 4); - int integer = FromString("125"); + int integer = FromString("125"); ui16 wideCharacterCode = FromString(u"125"); UNIT_ASSERT_VALUES_EQUAL(integer, wideCharacterCode); - } + } - static void CheckMessage(TFromStringException& exc, const TString& phrase) { + static void CheckMessage(TFromStringException& exc, const TString& phrase) { TString message = exc.what(); if (!message.Contains(phrase)) { Cerr << message << Endl; @@ -476,14 +476,14 @@ Y_UNIT_TEST_SUITE(TCastTest) { try { FromString<ui32>(""); UNIT_ASSERT(false); - } catch (TFromStringException& e) { + } catch (TFromStringException& e) { CheckMessage(e, "empty string as number"); } try { FromString<ui32>("-"); UNIT_ASSERT(false); - } catch (TFromStringException& e) { + } catch (TFromStringException& e) { // Unsigned should have no sign at all, so - is not expected CheckMessage(e, "Unexpected symbol \"-\" at pos 0 in string \"-\""); } @@ -491,21 +491,21 @@ Y_UNIT_TEST_SUITE(TCastTest) { try { FromString<i32>("-"); UNIT_ASSERT(false); - } catch (TFromStringException& e) { + } catch (TFromStringException& e) { CheckMessage(e, "Cannot parse string \"-\" as number"); } try { FromString<i32>("+"); UNIT_ASSERT(false); - } catch (TFromStringException& e) { + } catch (TFromStringException& e) { CheckMessage(e, "Cannot parse string \"+\" as number"); } try { FromString<ui32>("0.328413745072"); UNIT_ASSERT(false); - } catch (TFromStringException& e) { + } catch (TFromStringException& e) { CheckMessage(e, "Unexpected symbol \".\" at pos 1 in string \"0.328413745072\""); } } @@ -515,69 +515,69 @@ Y_UNIT_TEST_SUITE(TCastTest) { constexpr TStringBuf hello = "hello"; TStringBuf out; UNIT_ASSERT(TryFromString(hello, out)); - UNIT_ASSERT_VALUES_EQUAL(hello, out); + UNIT_ASSERT_VALUES_EQUAL(hello, out); } { constexpr TStringBuf empty = ""; TStringBuf out; UNIT_ASSERT(TryFromString(empty, out)); - UNIT_ASSERT_VALUES_EQUAL(empty, out); + UNIT_ASSERT_VALUES_EQUAL(empty, out); } { constexpr TStringBuf empty; TStringBuf out; UNIT_ASSERT(TryFromString(empty, out)); - UNIT_ASSERT_VALUES_EQUAL(empty, out); + UNIT_ASSERT_VALUES_EQUAL(empty, out); } { const auto hello = u"hello"; TWtringBuf out; UNIT_ASSERT(TryFromString(hello, out)); - UNIT_ASSERT_VALUES_EQUAL(hello, out); + UNIT_ASSERT_VALUES_EQUAL(hello, out); } { const TUtf16String empty; TWtringBuf out; UNIT_ASSERT(TryFromString(empty, out)); - UNIT_ASSERT_VALUES_EQUAL(empty, out); + UNIT_ASSERT_VALUES_EQUAL(empty, out); } { constexpr TWtringBuf empty; TWtringBuf out; UNIT_ASSERT(TryFromString(empty, out)); - UNIT_ASSERT_VALUES_EQUAL(empty, out); + UNIT_ASSERT_VALUES_EQUAL(empty, out); } } - + Y_UNIT_TEST(Nan) { - double xx = 0; - - UNIT_ASSERT(!TryFromString("NaN", xx)); - UNIT_ASSERT(!TryFromString("NAN", xx)); - UNIT_ASSERT(!TryFromString("nan", xx)); - } - + double xx = 0; + + UNIT_ASSERT(!TryFromString("NaN", xx)); + UNIT_ASSERT(!TryFromString("NAN", xx)); + UNIT_ASSERT(!TryFromString("nan", xx)); + } + Y_UNIT_TEST(Infinity) { - double xx = 0; - - UNIT_ASSERT(!TryFromString("Infinity", xx)); - UNIT_ASSERT(!TryFromString("INFINITY", xx)); - UNIT_ASSERT(!TryFromString("infinity", xx)); - } - + double xx = 0; + + UNIT_ASSERT(!TryFromString("Infinity", xx)); + UNIT_ASSERT(!TryFromString("INFINITY", xx)); + UNIT_ASSERT(!TryFromString("infinity", xx)); + } + Y_UNIT_TEST(TestBorderCases) { - UNIT_ASSERT_VALUES_EQUAL(ToString(0.0), "0"); - UNIT_ASSERT_VALUES_EQUAL(ToString(1.0), "1"); - UNIT_ASSERT_VALUES_EQUAL(ToString(10.0), "10"); - UNIT_ASSERT_VALUES_EQUAL(ToString(NAN), "nan"); - UNIT_ASSERT_VALUES_EQUAL(ToString(-NAN), "nan"); - UNIT_ASSERT_VALUES_EQUAL(ToString(INFINITY), "inf"); - UNIT_ASSERT_VALUES_EQUAL(ToString(-INFINITY), "-inf"); - UNIT_ASSERT_VALUES_EQUAL(ToString(1.1e+100), "1.1e+100"); - UNIT_ASSERT_VALUES_EQUAL(ToString(1e+100), "1e+100"); - UNIT_ASSERT_VALUES_EQUAL(ToString(87423.2031250000001), "87423.20313"); - UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.0e60, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "1e+60"); - } + UNIT_ASSERT_VALUES_EQUAL(ToString(0.0), "0"); + UNIT_ASSERT_VALUES_EQUAL(ToString(1.0), "1"); + UNIT_ASSERT_VALUES_EQUAL(ToString(10.0), "10"); + UNIT_ASSERT_VALUES_EQUAL(ToString(NAN), "nan"); + UNIT_ASSERT_VALUES_EQUAL(ToString(-NAN), "nan"); + UNIT_ASSERT_VALUES_EQUAL(ToString(INFINITY), "inf"); + UNIT_ASSERT_VALUES_EQUAL(ToString(-INFINITY), "-inf"); + UNIT_ASSERT_VALUES_EQUAL(ToString(1.1e+100), "1.1e+100"); + UNIT_ASSERT_VALUES_EQUAL(ToString(1e+100), "1e+100"); + UNIT_ASSERT_VALUES_EQUAL(ToString(87423.2031250000001), "87423.20313"); + UNIT_ASSERT_VALUES_EQUAL(FloatToString(1.0e60, PREC_POINT_DIGITS_STRIP_ZEROES, 0), "1e+60"); + } Y_UNIT_TEST(TestChar) { // Given a character ch, ToString(ch) returns @@ -599,4 +599,4 @@ Y_UNIT_TEST_SUITE(TCastTest) { UNIT_ASSERT_VALUES_EQUAL(ToString(U'я'), "1103"); UNIT_ASSERT_VALUES_EQUAL(ToString(U'\U0001F600'), "128512"); // 'GRINNING FACE' (U+1F600) } -}; +}; |