diff options
author | nkmakarov <nkmakarov@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
commit | f1803fa4ac9e2ee6cbfde317571ec330013392ff (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 | |
parent | 324348a37ed08cf66897faefb0ec4bebfe7804e1 (diff) | |
download | ydb-f1803fa4ac9e2ee6cbfde317571ec330013392ff.tar.gz |
Restoring authorship annotation for <nkmakarov@yandex-team.ru>. Commit 2 of 2.
25 files changed, 475 insertions, 475 deletions
diff --git a/contrib/python/six/ya.make b/contrib/python/six/ya.make index 45819f14a0..e0c7849214 100644 --- a/contrib/python/six/ya.make +++ b/contrib/python/six/ya.make @@ -1,7 +1,7 @@ OWNER(g:python-contrib) PY23_LIBRARY() - + LICENSE(MIT) VERSION(1.16.0) diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index 396ddec757..71e1442717 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -50,13 +50,13 @@ namespace NPrivate { } const TPtr Get(TArgs... args) const { - return GetValue<true>(args...); + return GetValue<true>(args...); + } + + const TPtr GetUnsafe(TArgs... args) const { + return GetValue<false>(args...); } - const TPtr GetUnsafe(TArgs... args) const { - return GetValue<false>(args...); - } - void Clear() { TWriteGuard w(Mutex); Cache.Clear(); @@ -107,9 +107,9 @@ namespace NPrivate { } private: - template <bool AllowNullValues> - const TPtr GetValue(TArgs... args) const { - Key key = Callbacks.GetKey(args...); + template <bool AllowNullValues> + const TPtr GetValue(TArgs... args) const { + Key key = Callbacks.GetKey(args...); switch (GettersPromotionPolicy) { case EGettersPromotionPolicy::Promoted: break; @@ -120,21 +120,21 @@ namespace NPrivate { return i.Value(); } break; - } - } - TWriteGuard w(Mutex); - typename TInternalCache::TIterator i = Cache.Find(key); - if (i != Cache.End()) { - return i.Value(); - } - TPtr value = Callbacks.CreateObject(args...); - if (value || AllowNullValues) { - Cache.Insert(key, value); - } - return value; - } - - private: + } + } + TWriteGuard w(Mutex); + typename TInternalCache::TIterator i = Cache.Find(key); + if (i != Cache.End()) { + return i.Value(); + } + TPtr value = Callbacks.CreateObject(args...); + if (value || AllowNullValues) { + Cache.Insert(key, value); + } + return value; + } + + private: using TInternalCache = TCache<Key, TPtr, List<Key, TPtr>, TNoopDelete>; template <class TCallbacks> diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 2a9af30d10..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -376,7 +376,7 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { }; Y_UNIT_TEST(SimpleTest) { - for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) { + for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) { const TString data = *TCache::Get<TCallbacks>(i); UNIT_ASSERT(data == VALS[i]); } @@ -405,39 +405,39 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { UNIT_ASSERT(*item == "hjk"); } } - -Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { - typedef TThreadSafeCache<ui32, TString, ui32> TCache; - - const char* VALS[] = {"abcd", "defg", "hjkl"}; - const ui32 FAILED_IDX = 1; - - class TCallbacks: public TCache::ICallbacks { - public: - TKey GetKey(ui32 i) const override { - return i; - } - TValue* CreateObject(ui32 i) const override { - if (i == FAILED_IDX) { - return nullptr; - } - return new TString(VALS[i]); - } - }; - - Y_UNIT_TEST(SimpleTest) { - TCallbacks callbacks; - TCache cache(callbacks, Y_ARRAY_SIZE(VALS)); - for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) { - const TString* data = cache.GetUnsafe(i).Get(); - if (i == FAILED_IDX) { - UNIT_ASSERT(data == nullptr); - } else { - UNIT_ASSERT(*data == VALS[i]); - } - } - } -} + +Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { + typedef TThreadSafeCache<ui32, TString, ui32> TCache; + + const char* VALS[] = {"abcd", "defg", "hjkl"}; + const ui32 FAILED_IDX = 1; + + class TCallbacks: public TCache::ICallbacks { + public: + TKey GetKey(ui32 i) const override { + return i; + } + TValue* CreateObject(ui32 i) const override { + if (i == FAILED_IDX) { + return nullptr; + } + return new TString(VALS[i]); + } + }; + + Y_UNIT_TEST(SimpleTest) { + TCallbacks callbacks; + TCache cache(callbacks, Y_ARRAY_SIZE(VALS)); + for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) { + const TString* data = cache.GetUnsafe(i).Get(); + if (i == FAILED_IDX) { + UNIT_ASSERT(data == nullptr); + } else { + UNIT_ASSERT(*data == VALS[i]); + } + } + } +} Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) { typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache; diff --git a/library/cpp/deprecated/ya.make b/library/cpp/deprecated/ya.make index 301adff9ae..6c753f68a9 100644 --- a/library/cpp/deprecated/ya.make +++ b/library/cpp/deprecated/ya.make @@ -28,7 +28,7 @@ RECURSE( omni/print_omni omni/usage omni/ut - prog_options + prog_options prog_options/ut sgi_hash threadable diff --git a/library/cpp/getopt/small/modchooser.cpp b/library/cpp/getopt/small/modchooser.cpp index 020de1c9df..2fa5cfd070 100644 --- a/library/cpp/getopt/small/modchooser.cpp +++ b/library/cpp/getopt/small/modchooser.cpp @@ -70,7 +70,7 @@ TModChooser::TMode::TMode(const TString& name, TMainClass* main, const TString& } TModChooser::TModChooser() - : ModesHelpOption("-?") // Default help option in last_getopt + : ModesHelpOption("-?") // Default help option in last_getopt , VersionHandler(nullptr) , ShowSeparated(true) , SvnRevisionOptionDisabled(false) diff --git a/library/cpp/getopt/ut/modchooser_ut.cpp b/library/cpp/getopt/ut/modchooser_ut.cpp index f756a4c8fb..a14c8a5853 100644 --- a/library/cpp/getopt/ut/modchooser_ut.cpp +++ b/library/cpp/getopt/ut/modchooser_ut.cpp @@ -1,9 +1,9 @@ #include <library/cpp/getopt/modchooser.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/stream/str.h> - + +#include <util/stream/str.h> + void ValidateArgcArgv(int argc, const char** argv) { UNIT_ASSERT_EQUAL(argc, 1); UNIT_ASSERT_EQUAL(argv[argc], nullptr); @@ -11,61 +11,61 @@ void ValidateArgcArgv(int argc, const char** argv) { int One(int argc, const char** argv) { ValidateArgcArgv(argc, argv); - return 1; -} - + return 1; +} + int Two(int argc, const char** argv) { ValidateArgcArgv(argc, argv); - return 2; -} - + return 2; +} + int Three(int argc, const char** argv) { ValidateArgcArgv(argc, argv); - return 3; -} - + return 3; +} + int Four(int argc, const char** argv) { ValidateArgcArgv(argc, argv); - return 4; -} - + return 4; +} + int Five(int argc, const char** argv) { ValidateArgcArgv(argc, argv); - return 5; -} - -typedef int (*F_PTR)(int, const char**); -static const F_PTR FUNCTIONS[] = {One, Two, Three, Four, Five}; -static const char* NAMES[] = {"one", "two", "three", "four", "five"}; + return 5; +} + +typedef int (*F_PTR)(int, const char**); +static const F_PTR FUNCTIONS[] = {One, Two, Three, Four, Five}; +static const char* NAMES[] = {"one", "two", "three", "four", "five"}; static_assert(Y_ARRAY_SIZE(FUNCTIONS) == Y_ARRAY_SIZE(NAMES), "Incorrect input tests data"); - + Y_UNIT_TEST_SUITE(TModChooserTest) { Y_UNIT_TEST(TestModesSimpleRunner) { - TModChooser chooser; + TModChooser chooser; for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) { - chooser.AddMode(NAMES[idx], FUNCTIONS[idx], NAMES[idx]); - } - + chooser.AddMode(NAMES[idx], FUNCTIONS[idx], NAMES[idx]); + } + // test argc, argv for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) { - int argc = 2; + int argc = 2; const char* argv[] = {"UNITTEST", NAMES[idx], nullptr}; - UNIT_ASSERT_EQUAL(static_cast<int>(idx) + 1, chooser.Run(argc, argv)); - } + UNIT_ASSERT_EQUAL(static_cast<int>(idx) + 1, chooser.Run(argc, argv)); + } // test TVector<TString> argv for (size_t idx = 0; idx < Y_ARRAY_SIZE(NAMES); ++idx) { const TVector<TString> argv = {"UNITTEST", NAMES[idx]}; UNIT_ASSERT_EQUAL(static_cast<int>(idx) + 1, chooser.Run(argv)); } - } - + } + Y_UNIT_TEST(TestHelpMessage) { - TModChooser chooser; - - int argc = 2; + TModChooser chooser; + + int argc = 2; const char* argv[] = {"UNITTEST", "-?", nullptr}; - - chooser.Run(argc, argv); - } -} + + chooser.Run(argc, argv); + } +} diff --git a/library/cpp/getopt/ut/ya.make b/library/cpp/getopt/ut/ya.make index 2d3b4625e0..8d00669efb 100644 --- a/library/cpp/getopt/ut/ya.make +++ b/library/cpp/getopt/ut/ya.make @@ -4,7 +4,7 @@ OWNER(pg) SRCS( last_getopt_ut.cpp - modchooser_ut.cpp + modchooser_ut.cpp opt2_ut.cpp opt_ut.cpp posix_getopt_ut.cpp diff --git a/library/cpp/linear_regression/linear_model.h b/library/cpp/linear_regression/linear_model.h index 4efd857374..8bb050cff7 100644 --- a/library/cpp/linear_regression/linear_model.h +++ b/library/cpp/linear_regression/linear_model.h @@ -5,8 +5,8 @@ #include <util/ysaveload.h> -#include <utility> - +#include <utility> + class TLinearModel { private: TVector<double> Coefficients; diff --git a/library/cpp/linear_regression/linear_regression_ut.cpp b/library/cpp/linear_regression/linear_regression_ut.cpp index 0a31a6e25c..e71a16b67a 100644 --- a/library/cpp/linear_regression/linear_regression_ut.cpp +++ b/library/cpp/linear_regression/linear_regression_ut.cpp @@ -298,77 +298,77 @@ Y_UNIT_TEST_SUITE(TLinearRegressionTest) { Y_UNIT_TEST(SigmaTest10000000) { TransformationTest(ETransformationType::TT_SIGMA, 10000000); } - + Y_UNIT_TEST(ResetCalculatorTest) { TVector<double> arguments; TVector<double> weights; - const double eps = 1e-10; - - const size_t argumentsCount = 100; - for (size_t i = 0; i < argumentsCount; ++i) { - arguments.push_back(i); - weights.push_back(i); - } - - TDeviationCalculator deviationCalculator1, deviationCalculator2; - TMeanCalculator meanCalculator1, meanCalculator2; - TCovariationCalculator covariationCalculator1, covariationCalculator2; - for (size_t i = 0; i < arguments.size(); ++i) { - meanCalculator1.Add(arguments[i], weights[i]); - meanCalculator2.Add(arguments[i], weights[i]); - deviationCalculator1.Add(arguments[i], weights[i]); - deviationCalculator2.Add(arguments[i], weights[i]); - covariationCalculator1.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); - covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); - } - - UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), eps); - - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps); - - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps); - - meanCalculator2.Reset(); - deviationCalculator2.Reset(); - covariationCalculator2.Reset(); - - UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetSumWeights(), eps); - - UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetDeviation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetStdDev(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetSumWeights(), eps); - - UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetFirstValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSecondValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetCovariation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSumWeights(), eps); - - for (size_t i = 0; i < arguments.size(); ++i) { - meanCalculator2.Add(arguments[i], weights[i]); - deviationCalculator2.Add(arguments[i], weights[i]); - covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); - } - - UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), 1e-10); - UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), 1e-10); - - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps); - - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps); - UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps); - } + const double eps = 1e-10; + + const size_t argumentsCount = 100; + for (size_t i = 0; i < argumentsCount; ++i) { + arguments.push_back(i); + weights.push_back(i); + } + + TDeviationCalculator deviationCalculator1, deviationCalculator2; + TMeanCalculator meanCalculator1, meanCalculator2; + TCovariationCalculator covariationCalculator1, covariationCalculator2; + for (size_t i = 0; i < arguments.size(); ++i) { + meanCalculator1.Add(arguments[i], weights[i]); + meanCalculator2.Add(arguments[i], weights[i]); + deviationCalculator1.Add(arguments[i], weights[i]); + deviationCalculator2.Add(arguments[i], weights[i]); + covariationCalculator1.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); + covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); + } + + UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), eps); + + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps); + + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps); + + meanCalculator2.Reset(); + deviationCalculator2.Reset(); + covariationCalculator2.Reset(); + + UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, meanCalculator2.GetSumWeights(), eps); + + UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetDeviation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetStdDev(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, deviationCalculator2.GetSumWeights(), eps); + + UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetFirstValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSecondValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetCovariation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(0.0, covariationCalculator2.GetSumWeights(), eps); + + for (size_t i = 0; i < arguments.size(); ++i) { + meanCalculator2.Add(arguments[i], weights[i]); + deviationCalculator2.Add(arguments[i], weights[i]); + covariationCalculator2.Add(arguments[i], arguments[arguments.size() - i - 1], weights[i]); + } + + UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetMean(), meanCalculator2.GetMean(), 1e-10); + UNIT_ASSERT_DOUBLES_EQUAL(meanCalculator1.GetSumWeights(), meanCalculator2.GetSumWeights(), 1e-10); + + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetMean(), deviationCalculator2.GetMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetDeviation(), deviationCalculator2.GetDeviation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetStdDev(), deviationCalculator2.GetStdDev(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(deviationCalculator1.GetSumWeights(), deviationCalculator2.GetSumWeights(), eps); + + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetFirstValueMean(), covariationCalculator2.GetFirstValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSecondValueMean(), covariationCalculator2.GetSecondValueMean(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetCovariation(), covariationCalculator2.GetCovariation(), eps); + UNIT_ASSERT_DOUBLES_EQUAL(covariationCalculator1.GetSumWeights(), covariationCalculator2.GetSumWeights(), eps); + } } diff --git a/library/cpp/linear_regression/welford.cpp b/library/cpp/linear_regression/welford.cpp index 509e80e970..e27b1994f6 100644 --- a/library/cpp/linear_regression/welford.cpp +++ b/library/cpp/linear_regression/welford.cpp @@ -1,5 +1,5 @@ #include "welford.h" - + #include <util/generic/ymath.h> void TMeanCalculator::Multiply(const double value) { @@ -28,10 +28,10 @@ double TMeanCalculator::GetSumWeights() const { return SumWeights.Get(); } -void TMeanCalculator::Reset() { - *this = TMeanCalculator(); -} - +void TMeanCalculator::Reset() { + *this = TMeanCalculator(); +} + void TCovariationCalculator::Add(const double firstValue, const double secondValue, const double weight /*= 1.*/) { SumWeights += weight; if (SumWeights.Get()) { @@ -66,10 +66,10 @@ double TCovariationCalculator::GetSumWeights() const { return SumWeights.Get(); } -void TCovariationCalculator::Reset() { - *this = TCovariationCalculator(); -} - +void TCovariationCalculator::Reset() { + *this = TCovariationCalculator(); +} + void TDeviationCalculator::Add(const double value, const double weight /*= 1.*/) { const double lastMean = MeanCalculator.GetMean(); MeanCalculator.Add(value, weight); @@ -101,7 +101,7 @@ double TDeviationCalculator::GetStdDev() const { double TDeviationCalculator::GetSumWeights() const { return MeanCalculator.GetSumWeights(); } - -void TDeviationCalculator::Reset() { - *this = TDeviationCalculator(); -} + +void TDeviationCalculator::Reset() { + *this = TDeviationCalculator(); +} diff --git a/library/cpp/linear_regression/welford.h b/library/cpp/linear_regression/welford.h index 0706ac2cfa..ee865d6693 100644 --- a/library/cpp/linear_regression/welford.h +++ b/library/cpp/linear_regression/welford.h @@ -18,7 +18,7 @@ public: void Remove(const double value, const double weight = 1.); double GetMean() const; double GetSumWeights() const; - void Reset(); + void Reset(); bool operator<(const TMeanCalculator& other) const { return Mean < other.Mean; @@ -51,8 +51,8 @@ public: double GetCovariation() const; double GetSumWeights() const; - - void Reset(); + + void Reset(); }; // accurately computes (w_1 * x_1 * x_1 + w_2 * x_2 * x_2 + ... + w_n * x_n * x_n) / (w_1 + w_2 + ... + w_n) @@ -72,6 +72,6 @@ public: double GetStdDev() const; double GetSumWeights() const; - - void Reset(); + + void Reset(); }; diff --git a/library/cpp/scheme/tests/ut/scheme_path_ut.cpp b/library/cpp/scheme/tests/ut/scheme_path_ut.cpp index f5b8a8e50c..0d4d79d483 100644 --- a/library/cpp/scheme/tests/ut/scheme_path_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_path_ut.cpp @@ -14,17 +14,17 @@ Y_UNIT_TEST_SUITE(TSchemePathTest) { NSc::TValue v; UNIT_ASSERT(!v.PathExists(path)); UNIT_ASSERT(NSc::TValue::PathValid(path)); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null())); *v.TrySelectOrAdd(path) = 1; NSc::NUt::AssertSchemeJson(expected, v); UNIT_ASSERT(v.PathExists(path)); UNIT_ASSERT(1 == v.TrySelectOrAdd(path)->GetNumber()); UNIT_ASSERT(1 == v.TrySelect(path).GetNumber()); UNIT_ASSERT(1 == v.TrySelectAndDelete(path).GetNumber()); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete(path), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete(path), NSc::Null())); NSc::NUt::AssertSchemeJson(delexpected, v); UNIT_ASSERT(!v.PathExists(path)); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null())); } Y_UNIT_TEST(TestSelect) { @@ -35,19 +35,19 @@ Y_UNIT_TEST_SUITE(TSchemePathTest) { UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//"))); NSc::NUt::AssertSchemeJson("null", v); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); NSc::NUt::AssertSchemeJson("null", v); v.SetDict(); UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//"))); NSc::NUt::AssertSchemeJson("{}", v); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); NSc::NUt::AssertSchemeJson("{}", v); v.SetArray(); UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//"))); NSc::NUt::AssertSchemeJson("[]", v); - UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null())); NSc::NUt::AssertSchemeJson("[]", v); DoTestSelect("[]", "{'':1}", "{}"); diff --git a/library/cpp/scheme/tests/ut/scheme_ut.cpp b/library/cpp/scheme/tests/ut/scheme_ut.cpp index 1e9afb124f..1a5d07c31b 100644 --- a/library/cpp/scheme/tests/ut/scheme_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_ut.cpp @@ -372,8 +372,8 @@ Y_UNIT_TEST_SUITE(TSchemeTest) { Y_UNIT_TEST(TestFrontBack) { NSc::TValue v; const NSc::TValue& vv = v; - UNIT_ASSERT(NSc::TValue::Same(vv.Front(), NSc::Null())); - UNIT_ASSERT(NSc::TValue::Same(vv.Back(), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(vv.Front(), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(vv.Back(), NSc::Null())); UNIT_ASSERT(!vv.IsArray()); v.Back() = "a"; UNIT_ASSERT_VALUES_EQUAL("a", vv.Front().GetString()); @@ -390,8 +390,8 @@ Y_UNIT_TEST_SUITE(TSchemeTest) { UNIT_ASSERT_VALUES_EQUAL("a", v.Pop().GetString()); - UNIT_ASSERT(NSc::TValue::Same(vv.Front(), NSc::Null())); - UNIT_ASSERT(NSc::TValue::Same(vv.Back(), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(vv.Front(), NSc::Null())); + UNIT_ASSERT(NSc::TValue::Same(vv.Back(), NSc::Null())); v.Front() = "a"; UNIT_ASSERT_VALUES_EQUAL("a", vv.Front().GetString()); diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index 5687d83429..44517a0092 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -547,7 +547,7 @@ public: \ } \ UNIT_ASSERT_C(0, "Exception hasn't been thrown, but it should have happened " << C); \ } while (false) - + #define UNIT_CHECK_GENERATED_EXCEPTION(A, E) UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, "") #define UNIT_CHECK_GENERATED_NO_EXCEPTION_C(A, E, C) \ @@ -560,7 +560,7 @@ public: \ UNIT_ASSERT_C(0, "Exception has been thrown, but it shouldn't have happened " << C); \ } \ } while (false) - + #define UNIT_CHECK_GENERATED_NO_EXCEPTION(A, E) UNIT_CHECK_GENERATED_NO_EXCEPTION_C(A, E, "and exception message is:\n" << CurrentExceptionMessage()) // Same as UNIT_ASSERT_EXCEPTION_SATISFIES but prints additional string C when nothing was thrown diff --git a/library/cpp/testing/unittest/ut/main.cpp b/library/cpp/testing/unittest/ut/main.cpp index 41000890eb..e303e21e30 100644 --- a/library/cpp/testing/unittest/ut/main.cpp +++ b/library/cpp/testing/unittest/ut/main.cpp @@ -55,11 +55,11 @@ namespace { TEST_F(TFixture, Test1) { ASSERT_EQ(I, 5); } - -TEST(ETest, Test1) { - UNIT_CHECK_GENERATED_EXCEPTION(ythrow yexception(), yexception); - UNIT_CHECK_GENERATED_NO_EXCEPTION(true, yexception); -} + +TEST(ETest, Test1) { + UNIT_CHECK_GENERATED_EXCEPTION(ythrow yexception(), yexception); + UNIT_CHECK_GENERATED_NO_EXCEPTION(true, yexception); +} Y_UNIT_TEST_SUITE(TestSingleTestFixture) { diff --git a/library/cpp/threading/skip_list/perf/main.cpp b/library/cpp/threading/skip_list/perf/main.cpp index bc6982d7c2..4ad52049e7 100644 --- a/library/cpp/threading/skip_list/perf/main.cpp +++ b/library/cpp/threading/skip_list/perf/main.cpp @@ -1,7 +1,7 @@ #include <library/cpp/threading/skip_list/skiplist.h> #include <library/cpp/getopt/small/last_getopt.h> - + #include <library/cpp/charset/ci_string.h> #include <util/datetime/base.h> #include <util/generic/map.h> diff --git a/tools/archiver/main.cpp b/tools/archiver/main.cpp index 5582f77c53..6cda54c1ea 100644 --- a/tools/archiver/main.cpp +++ b/tools/archiver/main.cpp @@ -488,7 +488,7 @@ int main(int argc, char** argv) { opts.AddHelpOption('?'); opts.SetTitle( "Archiver\n" - "Docs: https://wiki.yandex-team.ru/Development/Poisk/arcadia/tools/archiver" + "Docs: https://wiki.yandex-team.ru/Development/Poisk/arcadia/tools/archiver" ); bool hexdump = false; diff --git a/util/draft/date.cpp b/util/draft/date.cpp index 2ad2989c9e..a290c46050 100644 --- a/util/draft/date.cpp +++ b/util/draft/date.cpp @@ -17,23 +17,23 @@ time_t GetDateStart(time_t ts) { return mktime(&dateTm); } -static time_t ParseDate(const char* date, const char* format) { +static time_t ParseDate(const char* date, const char* format) { tm dateTm; memset(&dateTm, 0, sizeof(tm)); if (!strptime(date, format, &dateTm)) { - ythrow yexception() << "Invalid date string and format: " << date << ", " << format; + ythrow yexception() << "Invalid date string and format: " << date << ", " << format; } return mktime(&dateTm); } -static time_t ParseDate(const char* dateStr) { +static time_t ParseDate(const char* dateStr) { if (strlen(dateStr) != 8) { - ythrow yexception() << "Invalid date string: " << dateStr; + ythrow yexception() << "Invalid date string: " << dateStr; } - - return ParseDate(dateStr, "%Y%m%d"); -} - + + return ParseDate(dateStr, "%Y%m%d"); +} + template <> TDate FromStringImpl<TDate>(const char* data, size_t len) { return TDate(ParseDate(TString(data, len).data())); @@ -56,9 +56,9 @@ TDate::TDate(time_t ts) TDate::TDate(const TString& date, const TString& format) : Timestamp(GetDateStart(ParseDate(date.data(), format.data()))) -{ -} - +{ +} + TDate::TDate(unsigned year, unsigned month, unsigned monthDay) { tm dateTm; Zero(dateTm); diff --git a/util/draft/date.h b/util/draft/date.h index 2a9cbc3997..e3eb616fe5 100644 --- a/util/draft/date.h +++ b/util/draft/date.h @@ -3,7 +3,7 @@ #include <util/stream/output.h> #include <util/stream/input.h> #include <util/generic/string.h> -#include <util/datetime/constants.h> +#include <util/datetime/constants.h> #include <ctime> @@ -43,31 +43,31 @@ public: TString ToStroka(const char* format = "%Y%m%d") const; TDate& operator++() { - Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2)); + Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2)); return *this; } TDate& operator--() { - Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2); return *this; } TDate& operator+=(unsigned days) { - Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); return *this; } TDate& operator-=(unsigned days) { - Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); return *this; } TDate operator+(unsigned days) const { - return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); } TDate operator-(unsigned days) const { - return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); } unsigned GetWeekDay() const; // days since Sunday @@ -113,7 +113,7 @@ inline int operator-(const TDate& left, const TDate& right) { if (left < right) { return -(right - left); } - return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY); + return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY); } inline IInputStream& operator>>(IInputStream& left, TDate& right) { diff --git a/util/draft/date_ut.cpp b/util/draft/date_ut.cpp index 223fa4e41c..8c33a6c1cf 100644 --- a/util/draft/date_ut.cpp +++ b/util/draft/date_ut.cpp @@ -24,13 +24,13 @@ Y_UNIT_TEST_SUITE(TDateTest) { UNIT_ASSERT_EQUAL(d.ToStroka("____%Y__%m____%d"), "____2005__06____03"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1117756800); } - { - TDate d("2011-02-15", "%Y-%m-%d"); - UNIT_ASSERT_EQUAL(d.GetYear(), 2011); - UNIT_ASSERT_EQUAL(d.GetMonth(), 2); - UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); - UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); + { + TDate d("2011-02-15", "%Y-%m-%d"); + UNIT_ASSERT_EQUAL(d.GetYear(), 2011); + UNIT_ASSERT_EQUAL(d.GetMonth(), 2); + UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); + UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1297728000); - } + } } } diff --git a/util/draft/enum.h b/util/draft/enum.h index c65f54357c..18002b7df2 100644 --- a/util/draft/enum.h +++ b/util/draft/enum.h @@ -3,7 +3,7 @@ #include <bitset> #include <util/generic/strbuf.h> -#include <util/stream/str.h> +#include <util/stream/str.h> #include <util/string/cast.h> #include <util/string/split.h> #include <utility> @@ -12,11 +12,11 @@ class TEnumNotFoundException: public yexception { }; #define EnumFromString(key, entries) EnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) +#define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) #define FindEnumFromString(key, entries) FindEnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define FindEnumFromStringWithSize(key, entries, size) FindEnumFromStringImpl(key, entries, size) +#define FindEnumFromStringWithSize(key, entries, size) FindEnumFromStringImpl(key, entries, size) #define EnumToString(key, entries) EnumToStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) +#define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) #define PrintEnumItems(entries) PrintEnumItemsImpl(entries, Y_ARRAY_SIZE(entries)) template <class K1, class K2, class V> @@ -80,39 +80,39 @@ const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arrayS /////////////////////////////////// -template <class B> -inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { - if (allIfEmpty) { - flags.set(); - } else { - flags.reset(); - } -} - +template <class B> +inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { + if (allIfEmpty) { + flags.set(); + } else { + flags.reset(); + } +} + // all set by default template <class E, size_t N, size_t B> inline void SetEnumFlags(const std::pair<const char*, E> (&str2Enum)[N], TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); - } else { - flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + SetEnumFlagsForEmptySpec(flags, allIfEmpty); + } else { + flags.reset(); + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, N); - flags.set(e); + flags.set(e); } - } -} - -template <class E, size_t B> + } +} + +template <class E, size_t B> inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf optSpec, - std::bitset<B>& flags, const size_t size, + std::bitset<B>& flags, const size_t size, bool allIfEmpty = true) { - if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); + if (optSpec.empty()) { + SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, size); flags.set(e); } @@ -123,13 +123,13 @@ inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf o template <class E, size_t B> inline void SetEnumFlags(TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); + SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e; if (!TryFromString(it.Token(), e)) - ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; + ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; flags.set((size_t)e); } } diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index 4e87544109..badfb88993 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -694,25 +694,25 @@ static inline std::pair<I1, I2> Mismatch(I1 b1, I1 e1, I2 b2, I2 e2, P p) { return std::make_pair(b1, b2); } -template <class It, class Val> -static inline bool BinarySearch(It begin, It end, const Val& val) { +template <class It, class Val> +static inline bool BinarySearch(It begin, It end, const Val& val) { return std::binary_search(begin, end, val); -} +} -template <class It, class Val, class Comp> -static inline bool BinarySearch(It begin, It end, const Val& val, Comp comp) { +template <class It, class Val, class Comp> +static inline bool BinarySearch(It begin, It end, const Val& val, Comp comp) { return std::binary_search(begin, end, val, comp); -} - -template <class It, class Val> +} + +template <class It, class Val> static inline std::pair<It, It> EqualRange(It begin, It end, const Val& val) { return std::equal_range(begin, end, val); -} - -template <class It, class Val, class Comp> +} + +template <class It, class Val, class Comp> static inline std::pair<It, It> EqualRange(It begin, It end, const Val& val, Comp comp) { return std::equal_range(begin, end, val, comp); -} +} template <class ForwardIt> bool IsSorted(ForwardIt begin, ForwardIt end) { @@ -723,36 +723,36 @@ template <class ForwardIt, class Compare> bool IsSorted(ForwardIt begin, ForwardIt end, Compare comp) { return std::is_sorted(begin, end, comp); } - + template <class TIterator, typename TGetKey> bool IsSortedBy(TIterator begin, TIterator end, const TGetKey& getKey) { return IsSorted(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); } -template <class It, class Val> -void Iota(It begin, It end, Val val) { +template <class It, class Val> +void Iota(It begin, It end, Val val) { std::iota(begin, end, val); -} - -template <class TI, class TO, class S> -TO CopyN(TI from, S s, TO to) { - return std::copy_n(from, s, to); -} - -template <class TI, class TO, class P> -TO CopyIf(TI begin, TI end, TO to, P pred) { - return std::copy_if(begin, end, to, pred); -} - -template <class T> +} + +template <class TI, class TO, class S> +TO CopyN(TI from, S s, TO to) { + return std::copy_n(from, s, to); +} + +template <class TI, class TO, class P> +TO CopyIf(TI begin, TI end, TO to, P pred) { + return std::copy_if(begin, end, to, pred); +} + +template <class T> std::pair<const T&, const T&> MinMax(const T& first, const T& second) { - return std::minmax(first, second); -} - -template <class It> + return std::minmax(first, second); +} + +template <class It> std::pair<It, It> MinMaxElement(It first, It last) { - return std::minmax_element(first, last); -} + return std::minmax_element(first, last); +} template <class TIterator, class TGenerator> void Generate(TIterator first, TIterator last, TGenerator generator) { diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 1c73ab255a..8d732fcc0c 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -347,74 +347,74 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } } } - + Y_UNIT_TEST(BinarySearchTest) { - { + { TVector<TString> v; - bool test = BinarySearch(v.begin(), v.end(), "test"); - UNIT_ASSERT_EQUAL(test, false); - } - - { - int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - + bool test = BinarySearch(v.begin(), v.end(), "test"); + UNIT_ASSERT_EQUAL(test, false); + } + + { + int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + bool test = BinarySearch(data, data + Y_ARRAY_SIZE(data), 2); - UNIT_ASSERT_EQUAL(test, true); - + UNIT_ASSERT_EQUAL(test, true); + test = BinarySearch(data, data + Y_ARRAY_SIZE(data), 10); - UNIT_ASSERT_EQUAL(test, false); - } - - { + UNIT_ASSERT_EQUAL(test, false); + } + + { TVector<size_t> data = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; - + bool test = BinarySearch(data.begin(), data.end(), (size_t)9, TGreater<size_t>()); - UNIT_ASSERT_EQUAL(test, true); - + UNIT_ASSERT_EQUAL(test, true); + test = BinarySearch(data.begin(), data.end(), (size_t)11, TGreater<size_t>()); - UNIT_ASSERT_EQUAL(test, false); - + UNIT_ASSERT_EQUAL(test, false); + test = BinarySearch(data.rbegin(), data.rend(), (size_t)1); - UNIT_ASSERT_EQUAL(test, true); - } - } - + UNIT_ASSERT_EQUAL(test, true); + } + } + Y_UNIT_TEST(EqualRangeTest) { - { + { TVector<TString> v; using PairOfVector = std::pair<TVector<TString>::iterator, TVector<TString>::iterator>; - PairOfVector tmp = EqualRange(v.begin(), v.end(), "tmp"); - - UNIT_ASSERT_EQUAL(tmp.first, tmp.second); - UNIT_ASSERT_EQUAL(tmp.first, v.end()); - } - - { - int data[] = {1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5}; + PairOfVector tmp = EqualRange(v.begin(), v.end(), "tmp"); + + UNIT_ASSERT_EQUAL(tmp.first, tmp.second); + UNIT_ASSERT_EQUAL(tmp.first, v.end()); + } + + { + int data[] = {1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5}; using PairOfInt = std::pair<int*, int*>; PairOfInt tmp = EqualRange(data, data + Y_ARRAY_SIZE(data), 3); - - UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 4); - UNIT_ASSERT_EQUAL(tmp.first - data, 7); + + UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 4); + UNIT_ASSERT_EQUAL(tmp.first - data, 7); UNIT_ASSERT_EQUAL(data + Y_ARRAY_SIZE(data) - tmp.second, 2); - } - - { + } + + { TVector<size_t> data = {9, 9, 8, 8, 8, 5, 4, 3, 3, 0, 0}; - + using PairOfVector = std::pair<TVector<size_t>::iterator, TVector<size_t>::iterator>; - PairOfVector tmp = EqualRange(data.begin(), data.end(), 8, TGreater<size_t>()); - - UNIT_ASSERT_EQUAL(tmp.first - data.begin(), 2); - UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 3); - + PairOfVector tmp = EqualRange(data.begin(), data.end(), 8, TGreater<size_t>()); + + UNIT_ASSERT_EQUAL(tmp.first - data.begin(), 2); + UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 3); + using PairOfVectorReverse = std::pair<TVector<size_t>::reverse_iterator, TVector<size_t>::reverse_iterator>; PairOfVectorReverse tmpR = EqualRange(data.rbegin(), data.rend(), (size_t)0); - - UNIT_ASSERT_EQUAL(tmpR.first, data.rbegin()); - UNIT_ASSERT_EQUAL(tmpR.second - tmpR.first, 2); - } - } + + UNIT_ASSERT_EQUAL(tmpR.first, data.rbegin()); + UNIT_ASSERT_EQUAL(tmpR.second - tmpR.first, 2); + } + } Y_UNIT_TEST(IsSortedTest) { TVector<int> v0; @@ -507,7 +507,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { TVector<int> expected = {10, 7, 2}; UNIT_ASSERT_VALUES_EQUAL(collection, expected); } - + Y_UNIT_TEST(StableSortByTest) { TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; StableSortBy(collection, [](int x) { return x / 100; }); @@ -531,86 +531,86 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(IotaTest) { TVector<int> v(10); - - Iota(v.begin(), v.end(), 0); - UNIT_ASSERT_VALUES_EQUAL(v[0], 0); - UNIT_ASSERT_VALUES_EQUAL(v[5], 5); - UNIT_ASSERT_VALUES_EQUAL(v[9], 9); - - Iota(v.begin() + 2, v.begin() + 5, 162); - UNIT_ASSERT_VALUES_EQUAL(v[0], 0); - UNIT_ASSERT_VALUES_EQUAL(v[3], 163); - UNIT_ASSERT_VALUES_EQUAL(v[9], 9); - } - + + Iota(v.begin(), v.end(), 0); + UNIT_ASSERT_VALUES_EQUAL(v[0], 0); + UNIT_ASSERT_VALUES_EQUAL(v[5], 5); + UNIT_ASSERT_VALUES_EQUAL(v[9], 9); + + Iota(v.begin() + 2, v.begin() + 5, 162); + UNIT_ASSERT_VALUES_EQUAL(v[0], 0); + UNIT_ASSERT_VALUES_EQUAL(v[3], 163); + UNIT_ASSERT_VALUES_EQUAL(v[9], 9); + } + Y_UNIT_TEST(CopyNTest) { int data[] = {1, 2, 3, 4, 8, 7, 6, 5}; - const size_t vSize = 10; + const size_t vSize = 10; TVector<int> result(10, 0); - size_t toCopy = 5; - + size_t toCopy = 5; + TVector<int>::iterator iter = CopyN(data, toCopy, result.begin()); - UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), toCopy); + UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), toCopy); UNIT_ASSERT_VALUES_EQUAL(result.size(), 10); - for (size_t idx = 0; idx < toCopy; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(data[idx], result[idx]); - } - for (size_t idx = toCopy; idx < vSize; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - - toCopy = 8; - const size_t start = 1; - result.assign(vSize, 0); - iter = CopyN(data, toCopy, result.begin() + start); - UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), start + toCopy); - for (size_t idx = 0; idx < start; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - for (size_t idx = 0; idx < toCopy; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[start + idx], data[idx]); - } - for (size_t idx = start + toCopy; idx < vSize; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - } - + for (size_t idx = 0; idx < toCopy; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(data[idx], result[idx]); + } + for (size_t idx = toCopy; idx < vSize; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + + toCopy = 8; + const size_t start = 1; + result.assign(vSize, 0); + iter = CopyN(data, toCopy, result.begin() + start); + UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), start + toCopy); + for (size_t idx = 0; idx < start; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + for (size_t idx = 0; idx < toCopy; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[start + idx], data[idx]); + } + for (size_t idx = start + toCopy; idx < vSize; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + } + Y_UNIT_TEST(CopyIfTest) { - const size_t count = 9; - int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - const size_t vSize = 10; + const size_t count = 9; + int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + const size_t vSize = 10; TVector<int> v(vSize, 0); - + TVector<int>::iterator iter = CopyIf(data, data + count, v.begin(), [](int x) { return !(x % 3); }); UNIT_ASSERT_VALUES_EQUAL(v.size(), vSize); - UNIT_ASSERT_VALUES_EQUAL(iter - v.begin(), 3); - v.resize(iter - v.begin()); - for (size_t idx = 0; idx < v.size(); ++idx) { - UNIT_ASSERT_VALUES_EQUAL(v[idx], 3 * (idx + 1)); - } - } - + UNIT_ASSERT_VALUES_EQUAL(iter - v.begin(), 3); + v.resize(iter - v.begin()); + for (size_t idx = 0; idx < v.size(); ++idx) { + UNIT_ASSERT_VALUES_EQUAL(v[idx], 3 * (idx + 1)); + } + } + Y_UNIT_TEST(MinMaxElementTest) { TVector<int> v(10); - Iota(v.begin(), v.end(), 0); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, 0); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 9); - - v[3] = -2; - v[7] = 11; - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, -2); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 11); - } - + Iota(v.begin(), v.end(), 0); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, 0); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 9); + + v[3] = -2; + v[7] = 11; + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, -2); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 11); + } + Y_UNIT_TEST(MinMaxTest) { std::pair<int, int> p1 = MinMax(5, 12); - UNIT_ASSERT_EQUAL(p1.first, 5); - UNIT_ASSERT_EQUAL(p1.second, 12); - + UNIT_ASSERT_EQUAL(p1.first, 5); + UNIT_ASSERT_EQUAL(p1.second, 12); + std::pair<TString, TString> p2 = MinMax(TString("test"), TString("data")); UNIT_ASSERT_EQUAL(p2.first, TString("data")); UNIT_ASSERT_EQUAL(p2.second, TString("test")); - } + } Y_UNIT_TEST(TestMaxElementBy) { const int array[] = {1, 2, 5, 3, 4, 5}; diff --git a/util/string/cast.h b/util/string/cast.h index de44a87370..90e925c194 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -236,15 +236,15 @@ inline bool TryFromString(const TChar* data, T& result) { return TryFromString<T>(data, std::char_traits<TChar>::length(data), result); } -template <class T, class TChar> -inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { - if (TryFromString<T>(data, len, result)) { - return true; - } - result = def; - return false; -} - +template <class T, class TChar> +inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { + if (TryFromString<T>(data, len, result)) { + return true; + } + result = def; + return false; +} + template <class T> inline bool TryFromString(const TStringBuf& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); @@ -275,39 +275,39 @@ inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& d return TryFromString<T>(s.data(), s.size(), result, def); } -template <class T> -inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { - return TryFromStringWithDefault<T>(TStringBuf(s), result, def); -} - +template <class T> +inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { + return TryFromStringWithDefault<T>(TStringBuf(s), result, def); +} + template <class T, class TStringType> inline bool TryFromStringWithDefault(const TStringType& s, T& result) { - return TryFromStringWithDefault<T>(s, result, T()); -} - -// FromString methods with default value if data is invalid -template <class T, class TChar> -inline T FromString(const TChar* data, const size_t len, const T& def) { - T result; - TryFromString<T>(data, len, result, def); - return result; -} - + return TryFromStringWithDefault<T>(s, result, T()); +} + +// FromString methods with default value if data is invalid +template <class T, class TChar> +inline T FromString(const TChar* data, const size_t len, const T& def) { + T result; + TryFromString<T>(data, len, result, def); + return result; +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s, const T& def) { return FromString<T>(s.data(), s.size(), def); } -template <class T> -inline T FromStringWithDefault(const char* s, const T& def) { - return FromStringWithDefault<T>(TStringBuf(s), def); -} - +template <class T> +inline T FromStringWithDefault(const char* s, const T& def) { + return FromStringWithDefault<T>(TStringBuf(s), def); +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s) { - return FromStringWithDefault<T>(s, T()); -} - + return FromStringWithDefault<T>(s, T()); +} + double StrToD(const char* b, char** se); double StrToD(const char* b, const char* e, char** se); diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index edb6df2771..033450c38c 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -2,7 +2,7 @@ #include <library/cpp/testing/unittest/registar.h> -#include <util/charset/wide.h> +#include <util/charset/wide.h> #include <util/system/defaults.h> #include <limits> @@ -372,71 +372,71 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestDefault) { - size_t res = 0; - const size_t def1 = 42; - + 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), 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_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); - + 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, 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("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_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_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); - - int res2 = 0; - const int def2 = -6; - + 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), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); + + 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()); - + 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), false); UNIT_ASSERT_VALUES_EQUAL(res2, size_t()); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s4, def2), def2); - UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); + UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s4), size_t()); - } - + } + Y_UNIT_TEST(TestBool) { // True cases UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("yes"), true); |