diff options
author | Vasily Gerasimov <UgnineSirdis@gmail.com> | 2022-02-10 16:49:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:09 +0300 |
commit | 6cdc8f140213c595e4ad38bc3d97fcef1146b8c3 (patch) | |
tree | f69637041e6fed76ebae0c74ae1fa0c4be6ab5b4 /library/cpp | |
parent | e5d4696304c6689379ac7ce334512404d4b7836c (diff) | |
download | ydb-6cdc8f140213c595e4ad38bc3d97fcef1146b8c3.tar.gz |
Restoring authorship annotation for Vasily Gerasimov <UgnineSirdis@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp')
113 files changed, 2922 insertions, 2922 deletions
diff --git a/library/cpp/actors/core/actor.h b/library/cpp/actors/core/actor.h index ed29bd14b9..7c08f2a49d 100644 --- a/library/cpp/actors/core/actor.h +++ b/library/cpp/actors/core/actor.h @@ -401,7 +401,7 @@ namespace NActors { template <typename T> struct HasActorName<T, decltype((void)T::ActorName, (const char*)nullptr)>: std::true_type { }; - static ui32 GetActivityTypeIndex() { + static ui32 GetActivityTypeIndex() { if constexpr(HasActorName<TDerived>::value) { return TLocalProcessKey<TActorActivityTag, TDerived::ActorName>::GetIndex(); } else { @@ -425,8 +425,8 @@ namespace NActors { // static constexpr char ActorName[] = "UNNAMED"; - TActor(void (TDerived::*func)(TAutoPtr<IEventHandle>& ev, const TActorContext& ctx), ui32 activityType = GetActivityTypeIndex()) - : IActor(static_cast<TReceiveFunc>(func), activityType) + TActor(void (TDerived::*func)(TAutoPtr<IEventHandle>& ev, const TActorContext& ctx), ui32 activityType = GetActivityTypeIndex()) + : IActor(static_cast<TReceiveFunc>(func), activityType) { } public: diff --git a/library/cpp/actors/core/executor_thread.cpp b/library/cpp/actors/core/executor_thread.cpp index 446b651efd..5d292462bb 100644 --- a/library/cpp/actors/core/executor_thread.cpp +++ b/library/cpp/actors/core/executor_thread.cpp @@ -72,7 +72,7 @@ namespace NActors { } void TExecutorThread::Schedule(TInstant deadline, TAutoPtr<IEventHandle> ev, ISchedulerCookie* cookie) { - ++CurrentActorScheduledEventsCounter; + ++CurrentActorScheduledEventsCounter; Ctx.Executor->Schedule(deadline, ev, cookie, Ctx.WorkerId); } @@ -153,7 +153,7 @@ namespace NActors { TCallstack::GetTlsCallstack().SetLinesToSkip(); #endif CurrentRecipient = recipient; - CurrentActorScheduledEventsCounter = 0; + CurrentActorScheduledEventsCounter = 0; if (executed == 0) { double usec = Ctx.AddActivationStats(AtomicLoad(&mailbox->ScheduleMoment), hpprev); diff --git a/library/cpp/actors/core/mon_stats.h b/library/cpp/actors/core/mon_stats.h index d55552af0c..96ba808854 100644 --- a/library/cpp/actors/core/mon_stats.h +++ b/library/cpp/actors/core/mon_stats.h @@ -79,7 +79,7 @@ namespace NActors { TVector<NHPTimer::STime> ElapsedTicksByActivity; TVector<ui64> ReceivedEventsByActivity; TVector<i64> ActorsAliveByActivity; // the sum should be positive, but per-thread might be negative - TVector<ui64> ScheduledEventsByActivity; + TVector<ui64> ScheduledEventsByActivity; ui64 PoolActorRegistrations = 0; ui64 PoolDestroyedActors = 0; ui64 PoolAllocatedMailboxes = 0; @@ -91,7 +91,7 @@ namespace NActors { : ElapsedTicksByActivity(activityVecSize) , ReceivedEventsByActivity(activityVecSize) , ActorsAliveByActivity(activityVecSize) - , ScheduledEventsByActivity(activityVecSize) + , ScheduledEventsByActivity(activityVecSize) {} template <typename T> @@ -126,7 +126,7 @@ namespace NActors { AggregateOne(ElapsedTicksByActivity, other.ElapsedTicksByActivity); AggregateOne(ReceivedEventsByActivity, other.ReceivedEventsByActivity); AggregateOne(ActorsAliveByActivity, other.ActorsAliveByActivity); - AggregateOne(ScheduledEventsByActivity, other.ScheduledEventsByActivity); + AggregateOne(ScheduledEventsByActivity, other.ScheduledEventsByActivity); RelaxedStore( &PoolActorRegistrations, diff --git a/library/cpp/actors/helpers/selfping_actor.cpp b/library/cpp/actors/helpers/selfping_actor.cpp index f9bfaf8dc0..9fe2e7875f 100644 --- a/library/cpp/actors/helpers/selfping_actor.cpp +++ b/library/cpp/actors/helpers/selfping_actor.cpp @@ -64,7 +64,7 @@ private: const NMonitoring::TDynamicCounters::TCounterPtr Counter; const NMonitoring::TDynamicCounters::TCounterPtr CalculationTimeCounter; - NSlidingWindow::TSlidingWindow<NSlidingWindow::TMaxOperation<ui64>> SlidingWindow; + NSlidingWindow::TSlidingWindow<NSlidingWindow::TMaxOperation<ui64>> SlidingWindow; NSlidingWindow::TSlidingWindow<TAvgOperation<ui64>> CalculationSlidingWindow; THPTimer Timer; diff --git a/library/cpp/actors/ya.make b/library/cpp/actors/ya.make index 737c7fbc18..3c308c0899 100644 --- a/library/cpp/actors/ya.make +++ b/library/cpp/actors/ya.make @@ -1,7 +1,7 @@ RECURSE_FOR_TESTS(ut) RECURSE( - log_backend + log_backend core dnsresolver examples diff --git a/library/cpp/colorizer/colors.cpp b/library/cpp/colorizer/colors.cpp index decc5c9847..47408ac08f 100644 --- a/library/cpp/colorizer/colors.cpp +++ b/library/cpp/colorizer/colors.cpp @@ -1,6 +1,6 @@ #include "colors.h" -#include <util/stream/output.h> +#include <util/stream/output.h> #include <util/generic/singleton.h> #include <util/system/env.h> @@ -166,10 +166,10 @@ bool TColors::CalcIsTTY(FILE* file) { #endif } -TColors::TColors(FILE* f) +TColors::TColors(FILE* f) : IsTTY_(true) { - SetIsTTY(CalcIsTTY(f)); + SetIsTTY(CalcIsTTY(f)); } TColors::TColors(bool ontty) @@ -418,7 +418,7 @@ TStringBuf TColors::WhiteColor() const noexcept { } -namespace { +namespace { class TStdErrColors: public TColors { public: TStdErrColors() @@ -426,24 +426,24 @@ namespace { { } }; - - class TStdOutColors: public TColors { - public: - TStdOutColors() - : TColors(stdout) - { - } - }; - - class TDisabledColors: public TColors { - public: - TDisabledColors() - : TColors(false) - { - } - }; -} // anonymous namespace - + + class TStdOutColors: public TColors { + public: + TStdOutColors() + : TColors(stdout) + { + } + }; + + class TDisabledColors: public TColors { + public: + TDisabledColors() + : TColors(false) + { + } + }; +} // anonymous namespace + TColors& NColorizer::StdErr() { return *Singleton<TStdErrColors>(); } @@ -453,13 +453,13 @@ TColors& NColorizer::StdOut() { } TColors& NColorizer::AutoColors(IOutputStream& os) { - if (&os == &Cerr) { - return StdErr(); - } - if (&os == &Cout) { - return StdOut(); - } - return *Singleton<TDisabledColors>(); + if (&os == &Cerr) { + return StdErr(); + } + if (&os == &Cout) { + return StdOut(); + } + return *Singleton<TDisabledColors>(); } size_t NColorizer::TotalAnsiEscapeCodeLen(TStringBuf text) { diff --git a/library/cpp/colorizer/colors.h b/library/cpp/colorizer/colors.h index 474a918994..c40570c9bc 100644 --- a/library/cpp/colorizer/colors.h +++ b/library/cpp/colorizer/colors.h @@ -1,6 +1,6 @@ #pragma once -#include "fwd.h" +#include "fwd.h" #include <util/generic/string.h> #include <util/generic/strbuf.h> @@ -104,8 +104,8 @@ namespace NColorizer { static bool CalcIsTTY(FILE* file); public: - explicit TColors(FILE* = stderr); - explicit TColors(bool ontty); + explicit TColors(FILE* = stderr); + explicit TColors(bool ontty); TStringBuf Reset() const noexcept; @@ -213,9 +213,9 @@ namespace NColorizer { }; /// Singletone `TColors` instances for stderr/stdout. - TColors& StdErr(); - TColors& StdOut(); - + TColors& StdErr(); + TColors& StdOut(); + /// Choose `TColors` depending on output stream. If passed stream is stderr/stdout, return a corresponding /// singletone. Otherwise, return a disabled singletone (which you can, but should *not* enable). TColors& AutoColors(IOutputStream& os); diff --git a/library/cpp/colorizer/fwd.h b/library/cpp/colorizer/fwd.h index d71efdc053..d94f728284 100644 --- a/library/cpp/colorizer/fwd.h +++ b/library/cpp/colorizer/fwd.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + class IOutputStream; - -namespace NColorizer { - class TColors; - - TColors& StdErr(); - TColors& StdOut(); + +namespace NColorizer { + class TColors; + + TColors& StdErr(); + TColors& StdOut(); TColors& AutoColors(IOutputStream&); } diff --git a/library/cpp/containers/comptrie/comptrie_builder.h b/library/cpp/containers/comptrie/comptrie_builder.h index cf7d2e39a3..f8a4926ef0 100644 --- a/library/cpp/containers/comptrie/comptrie_builder.h +++ b/library/cpp/containers/comptrie/comptrie_builder.h @@ -46,7 +46,7 @@ public: typedef typename TCompactTrieKeySelector<TSymbol>::TKey TKey; typedef typename TCompactTrieKeySelector<TSymbol>::TKeyBuf TKeyBuf; - explicit TCompactTrieBuilder(TCompactTrieBuilderFlags flags = CTBF_NONE, TPacker packer = TPacker(), IAllocator* alloc = TDefaultAllocator::Instance()); + explicit TCompactTrieBuilder(TCompactTrieBuilderFlags flags = CTBF_NONE, TPacker packer = TPacker(), IAllocator* alloc = TDefaultAllocator::Instance()); // All Add.. methods return true if it was a new key, false if the key already existed. @@ -72,14 +72,14 @@ public: } bool Find(const TSymbol* key, size_t keylen, TData* value) const; - bool Find(const TKeyBuf& key, TData* value = nullptr) const { + bool Find(const TKeyBuf& key, TData* value = nullptr) const { return Find(key.data(), key.size(), value); } - bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value = nullptr) const; - bool FindLongestPrefix(const TKeyBuf& key, size_t* prefixLen, TData* value = nullptr) const { + bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value = nullptr) const; + bool FindLongestPrefix(const TKeyBuf& key, size_t* prefixLen, TData* value = nullptr) const { return FindLongestPrefix(key.data(), key.size(), prefixLen, value); - } + } size_t Save(IOutputStream& os) const; size_t SaveAndDestroy(IOutputStream& os); diff --git a/library/cpp/containers/comptrie/comptrie_builder.inl b/library/cpp/containers/comptrie/comptrie_builder.inl index f273fa6571..e1a99da902 100644 --- a/library/cpp/containers/comptrie/comptrie_builder.inl +++ b/library/cpp/containers/comptrie/comptrie_builder.inl @@ -1,20 +1,20 @@ #pragma once #include "comptrie_impl.h" -#include "comptrie_trie.h" +#include "comptrie_trie.h" #include "make_fast_layout.h" #include "array_with_size.h" #include <library/cpp/containers/compact_vector/compact_vector.h> -#include <util/memory/alloc.h> +#include <util/memory/alloc.h> #include <util/memory/blob.h> #include <util/memory/pool.h> #include <util/memory/tempbuf.h> #include <util/memory/smallobj.h> #include <util/generic/algorithm.h> #include <util/generic/buffer.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <util/system/align.h> #include <util/stream/buffer.h> @@ -49,8 +49,8 @@ protected: void ConvertSymbolArrayToChar(const TSymbol* key, size_t keylen, TTempBuf& buf, size_t ckeylen) const; void NodeLinkTo(TNode* thiz, const TBlob& label, TNode* node); TNode* NodeForwardAdd(TNode* thiz, const char* label, size_t len, size_t& passed, size_t* nodeCount); - bool FindEntryImpl(const char* key, size_t keylen, TData* value) const; - bool FindLongestPrefixImpl(const char* keyptr, size_t keylen, size_t* prefixLen, TData* value) const; + bool FindEntryImpl(const char* key, size_t keylen, TData* value) const; + bool FindLongestPrefixImpl(const char* keyptr, size_t keylen, size_t* prefixLen, TData* value) const; size_t NodeMeasureSubtree(TNode* thiz) const; ui64 NodeSaveSubtree(TNode* thiz, IOutputStream& os) const; @@ -67,7 +67,7 @@ protected: ui64 ArcSaveAndDestroy(const TArc* thiz, IOutputStream& os); public: - TCompactTrieBuilderImpl(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc); + TCompactTrieBuilderImpl(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc); virtual ~TCompactTrieBuilderImpl(); void DestroyNode(TNode* node); @@ -81,14 +81,14 @@ public: bool AddSubtreeInFile(const TSymbol* key, size_t keylen, const TString& fileName); bool AddSubtreeInBuffer(const TSymbol* key, size_t keylen, TArrayWithSizeHolder<char>&& buffer); bool FindEntry(const TSymbol* key, size_t keylen, TData* value) const; - bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const; + bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const; size_t Save(IOutputStream& os) const; size_t SaveAndDestroy(IOutputStream& os); void Clear(); - // lies if some key was added at least twice + // lies if some key was added at least twice size_t GetEntryCount() const; size_t GetNodeCount() const; @@ -121,25 +121,25 @@ public: virtual ui64 Save(const TBuilderImpl* builder, IOutputStream& os) const = 0; virtual ui64 SaveAndDestroy(TBuilderImpl* builder, IOutputStream& os) = 0; virtual void Destroy(TBuilderImpl*) { } - - // Tries to find key in subtree. - // Returns next node to find the key in (to avoid recursive calls) - // If it has end result, writes it to @value and @result arguments and returns nullptr - virtual const TNode* Find(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const = 0; - virtual const TNode* FindLongestPrefix(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const = 0; + + // Tries to find key in subtree. + // Returns next node to find the key in (to avoid recursive calls) + // If it has end result, writes it to @value and @result arguments and returns nullptr + virtual const TNode* Find(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const = 0; + virtual const TNode* FindLongestPrefix(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const = 0; }; class TArcSet: public ISubtree, public TCompactVector<TArc> { public: typedef typename TCompactVector<TArc>::iterator iterator; - typedef typename TCompactVector<TArc>::const_iterator const_iterator; + typedef typename TCompactVector<TArc>::const_iterator const_iterator; - TArcSet() { + TArcSet() { Y_ASSERT(reinterpret_cast<ISubtree*>(this) == static_cast<void*>(this)); // This assumption is used in TNode::Subtree() - } - + } + iterator Find(char ch); - const_iterator Find(char ch) const; + const_iterator Find(char ch) const; void Add(const TBlob& s, TNode* node); bool IsLast() const override { @@ -148,9 +148,9 @@ public: const TNode* Find(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const override; const TNode* FindLongestPrefix(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const override { - return Find(key, value, result, packer); - } - + return Find(key, value, result, packer); + } + ui64 Measure(const TBuilderImpl* builder) const override { return MeasureRange(builder, 0, this->size()); } @@ -217,40 +217,40 @@ public: struct TBufferedSubtree: public ISubtree { TArrayWithSizeHolder<char> Buffer; - TBufferedSubtree() { + TBufferedSubtree() { Y_ASSERT(reinterpret_cast<ISubtree*>(this) == static_cast<void*>(this)); // This assumption is used in TNode::Subtree() - } - + } + bool IsLast() const override { return Buffer.Empty(); } const TNode* Find(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const override { - if (Buffer.Empty()) { - result = false; - return nullptr; - } - - TCompactTrie<char, D, S> trie(Buffer.Get(), Buffer.Size(), packer); + if (Buffer.Empty()) { + result = false; + return nullptr; + } + + TCompactTrie<char, D, S> trie(Buffer.Get(), Buffer.Size(), packer); result = trie.Find(key.data(), key.size(), value); - - return nullptr; - } - + + return nullptr; + } + const TNode* FindLongestPrefix(TStringBuf& key, TData* value, bool& result, const TPacker& packer) const override { - if (Buffer.Empty()) { - result = false; - return nullptr; - } - - TCompactTrie<char, D, S> trie(Buffer.Get(), Buffer.Size(), packer); - size_t prefixLen = 0; + if (Buffer.Empty()) { + result = false; + return nullptr; + } + + TCompactTrie<char, D, S> trie(Buffer.Get(), Buffer.Size(), packer); + size_t prefixLen = 0; result = trie.FindLongestPrefix(key.data(), key.size(), &prefixLen, value); - key = key.SubStr(prefixLen); - - return nullptr; - } - + key = key.SubStr(prefixLen); + + return nullptr; + } + ui64 Measure(const TBuilderImpl*) const override { return Buffer.Size(); } @@ -283,7 +283,7 @@ public: Data.Reset(new TData); Data->FileName = fileName; Data->Size = size; - + Y_ASSERT(reinterpret_cast<ISubtree*>(this) == static_cast<void*>(this)); // This assumption is used in TNode::Subtree() } @@ -292,30 +292,30 @@ public: } const TNode* Find(TStringBuf& key, typename TCompactTrieBuilder::TData* value, bool& result, const TPacker& packer) const override { - if (!Data) { - result = false; - return nullptr; - } - - TCompactTrie<char, D, S> trie(TBlob::FromFile(Data->FileName), packer); + if (!Data) { + result = false; + return nullptr; + } + + TCompactTrie<char, D, S> trie(TBlob::FromFile(Data->FileName), packer); result = trie.Find(key.data(), key.size(), value); - return nullptr; - } - + return nullptr; + } + const TNode* FindLongestPrefix(TStringBuf& key, typename TCompactTrieBuilder::TData* value, bool& result, const TPacker& packer) const override { - if (!Data) { - result = false; - return nullptr; - } - - TCompactTrie<char, D, S> trie(TBlob::FromFile(Data->FileName), packer); - size_t prefixLen = 0; + if (!Data) { + result = false; + return nullptr; + } + + TCompactTrie<char, D, S> trie(TBlob::FromFile(Data->FileName), packer); + size_t prefixLen = 0; result = trie.FindLongestPrefix(key.data(), key.size(), &prefixLen, value); - key = key.SubStr(prefixLen); - - return nullptr; - } - + key = key.SubStr(prefixLen); + + return nullptr; + } + ui64 Measure(const TBuilderImpl*) const override { return Data->Size; } @@ -351,26 +351,26 @@ public: EPayload PayloadType; - inline const char* PayloadPtr() const { - return ((const char*) this) + sizeof(TNode); - } - + inline const char* PayloadPtr() const { + return ((const char*) this) + sizeof(TNode); + } + inline char* PayloadPtr() { return ((char*) this) + sizeof(TNode); } // *Payload() - inline const char*& PayloadAsPtr() const { - const char** payload = (const char**) PayloadPtr(); - return *payload; - } - + inline const char*& PayloadAsPtr() const { + const char** payload = (const char**) PayloadPtr(); + return *payload; + } + inline char*& PayloadAsPtr() { char** payload = (char**) PayloadPtr(); return *payload; } - inline const char* GetPayload() const { + inline const char* GetPayload() const { switch (PayloadType) { case DATA_INSIDE: return PayloadPtr(); @@ -383,11 +383,11 @@ public: } } - inline char* GetPayload() { - const TNode* thiz = this; - return const_cast<char*>(thiz->GetPayload()); // const_cast is to avoid copy-paste style - } - + inline char* GetPayload() { + const TNode* thiz = this; + return const_cast<char*>(thiz->GetPayload()); // const_cast is to avoid copy-paste style + } + bool IsFinal() const { return PayloadType != DATA_ABSENT; } @@ -420,8 +420,8 @@ public: // TCompactTrieBuilder template <class T, class D, class S> -TCompactTrieBuilder<T, D, S>::TCompactTrieBuilder(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc) - : Impl(new TCompactTrieBuilderImpl(flags, packer, alloc)) +TCompactTrieBuilder<T, D, S>::TCompactTrieBuilder(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc) + : Impl(new TCompactTrieBuilderImpl(flags, packer, alloc)) { } @@ -452,7 +452,7 @@ bool TCompactTrieBuilder<T, D, S>::Find(const TSymbol* key, size_t keylen, TData template <class T, class D, class S> bool TCompactTrieBuilder<T, D, S>::FindLongestPrefix( - const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const { + const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const { return Impl->FindLongestPrefix(key, keylen, prefixlen, value); } @@ -484,10 +484,10 @@ size_t TCompactTrieBuilder<T, D, S>::GetNodeCount() const { // TCompactTrieBuilder::TCompactTrieBuilderImpl template <class T, class D, class S> -TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TCompactTrieBuilderImpl(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc) - : Pool(1000000, TMemoryPool::TLinearGrow::Instance(), alloc) +TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TCompactTrieBuilderImpl(TCompactTrieBuilderFlags flags, TPacker packer, IAllocator* alloc) + : Pool(1000000, TMemoryPool::TLinearGrow::Instance(), alloc) , PayloadSize(sizeof(void*)) // XXX: find better value - , NodeAllocator(new TFixedSizeAllocator(sizeof(TNode) + PayloadSize, alloc)) + , NodeAllocator(new TFixedSizeAllocator(sizeof(TNode) + PayloadSize, alloc)) , Flags(flags) , EntryCount(0) , NodeCount(1) @@ -662,81 +662,81 @@ template <class T, class D, class S> bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindEntry(const TSymbol* key, size_t keylen, TData* value) const { using namespace NCompactTrie; - if (!keylen) { - const char zero = '\0'; - return FindEntryImpl(&zero, 1, value); - } else { - size_t ckeylen = keylen * sizeof(TSymbol); - TTempBuf ckeybuf(ckeylen); - ConvertSymbolArrayToChar(key, keylen, ckeybuf, ckeylen); - return FindEntryImpl(ckeybuf.Data(), ckeylen, value); + if (!keylen) { + const char zero = '\0'; + return FindEntryImpl(&zero, 1, value); + } else { + size_t ckeylen = keylen * sizeof(TSymbol); + TTempBuf ckeybuf(ckeylen); + ConvertSymbolArrayToChar(key, keylen, ckeybuf, ckeylen); + return FindEntryImpl(ckeybuf.Data(), ckeylen, value); } -} - -template <class T, class D, class S> -bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindEntryImpl(const char* keyptr, size_t keylen, TData* value) const { - const TNode* node = Root; - bool result = false; - TStringBuf key(keyptr, keylen); - while (key && (node = node->Subtree()->Find(key, value, result, Packer))) { +} + +template <class T, class D, class S> +bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindEntryImpl(const char* keyptr, size_t keylen, TData* value) const { + const TNode* node = Root; + bool result = false; + TStringBuf key(keyptr, keylen); + while (key && (node = node->Subtree()->Find(key, value, result, Packer))) { } - return result; + return result; } template <class T, class D, class S> bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindLongestPrefix( - const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const { + const TSymbol* key, size_t keylen, size_t* prefixlen, TData* value) const { using namespace NCompactTrie; - if (!keylen) { - const char zero = '\0'; - const bool ret = FindLongestPrefixImpl(&zero, 1, prefixlen, value); - if (ret && prefixlen) - *prefixlen = 0; // empty key found - return ret; - } else { - size_t ckeylen = keylen * sizeof(TSymbol); - TTempBuf ckeybuf(ckeylen); - ConvertSymbolArrayToChar(key, keylen, ckeybuf, ckeylen); - bool ret = FindLongestPrefixImpl(ckeybuf.Data(), ckeylen, prefixlen, value); - if (ret && prefixlen && *prefixlen == 1 && ckeybuf.Data()[0] == '\0') - *prefixlen = 0; // if we have found empty key, set prefixlen to zero - else if (!ret) // try to find value with empty key, because empty key is prefix of a every key - ret = FindLongestPrefix(nullptr, 0, prefixlen, value); - - if (ret && prefixlen) - *prefixlen /= sizeof(TSymbol); - - return ret; + if (!keylen) { + const char zero = '\0'; + const bool ret = FindLongestPrefixImpl(&zero, 1, prefixlen, value); + if (ret && prefixlen) + *prefixlen = 0; // empty key found + return ret; + } else { + size_t ckeylen = keylen * sizeof(TSymbol); + TTempBuf ckeybuf(ckeylen); + ConvertSymbolArrayToChar(key, keylen, ckeybuf, ckeylen); + bool ret = FindLongestPrefixImpl(ckeybuf.Data(), ckeylen, prefixlen, value); + if (ret && prefixlen && *prefixlen == 1 && ckeybuf.Data()[0] == '\0') + *prefixlen = 0; // if we have found empty key, set prefixlen to zero + else if (!ret) // try to find value with empty key, because empty key is prefix of a every key + ret = FindLongestPrefix(nullptr, 0, prefixlen, value); + + if (ret && prefixlen) + *prefixlen /= sizeof(TSymbol); + + return ret; } -} - -template <class T, class D, class S> -bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindLongestPrefixImpl(const char* keyptr, size_t keylen, size_t* prefixLen, TData* value) const { - const TNode* node = Root; - const TNode* lastFinalNode = nullptr; - bool endResult = false; - TStringBuf key(keyptr, keylen); - TStringBuf keyTail = key; - TStringBuf lastFinalKeyTail; - while (keyTail && (node = node->Subtree()->FindLongestPrefix(keyTail, value, endResult, Packer))) { - if (endResult) // no more ways to find prefix and prefix has been found - break; - - if (node->IsFinal()) { - lastFinalNode = node; - lastFinalKeyTail = keyTail; +} + +template <class T, class D, class S> +bool TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::FindLongestPrefixImpl(const char* keyptr, size_t keylen, size_t* prefixLen, TData* value) const { + const TNode* node = Root; + const TNode* lastFinalNode = nullptr; + bool endResult = false; + TStringBuf key(keyptr, keylen); + TStringBuf keyTail = key; + TStringBuf lastFinalKeyTail; + while (keyTail && (node = node->Subtree()->FindLongestPrefix(keyTail, value, endResult, Packer))) { + if (endResult) // no more ways to find prefix and prefix has been found + break; + + if (node->IsFinal()) { + lastFinalNode = node; + lastFinalKeyTail = keyTail; } } - if (!endResult && lastFinalNode) { + if (!endResult && lastFinalNode) { if (value) - Packer.UnpackLeaf(lastFinalNode->GetPayload(), *value); - keyTail = lastFinalKeyTail; - endResult = true; + Packer.UnpackLeaf(lastFinalNode->GetPayload(), *value); + keyTail = lastFinalKeyTail; + endResult = true; } - if (endResult && prefixLen) + if (endResult && prefixLen) *prefixLen = keyTail ? key.size() - keyTail.size() : key.size(); - return endResult; + return endResult; } template <class T, class D, class S> @@ -991,60 +991,60 @@ typename TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet:: } template <class T, class D, class S> -typename TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::const_iterator - TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::Find(char ch) const { - using namespace NCompTriePrivate; - const_iterator it = LowerBound(this->begin(), this->end(), ch, TCmp()); - - if (it != this->end() && it->Label[0] == (unsigned char)ch) { - return it; - } - - return this->end(); -} - -template <class T, class D, class S> +typename TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::const_iterator + TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::Find(char ch) const { + using namespace NCompTriePrivate; + const_iterator it = LowerBound(this->begin(), this->end(), ch, TCmp()); + + if (it != this->end() && it->Label[0] == (unsigned char)ch) { + return it; + } + + return this->end(); +} + +template <class T, class D, class S> void TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::Add(const TBlob& s, TNode* node) { using namespace NCompTriePrivate; this->insert(LowerBound(this->begin(), this->end(), s[0], TCmp()), TArc(s, node)); } -template <class T, class D, class S> -const typename TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode* - TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::Find( - TStringBuf& key, TData* value, bool& result, const TPacker& packer) const { - result = false; - if (!key) - return nullptr; - - const const_iterator it = Find(key[0]); - if (it != this->end()) { - const char* const arcLabel = it->Label.AsCharPtr(); - const size_t arcLabelLen = it->Label.Length(); +template <class T, class D, class S> +const typename TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode* + TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::TNode::TArcSet::Find( + TStringBuf& key, TData* value, bool& result, const TPacker& packer) const { + result = false; + if (!key) + return nullptr; + + const const_iterator it = Find(key[0]); + if (it != this->end()) { + const char* const arcLabel = it->Label.AsCharPtr(); + const size_t arcLabelLen = it->Label.Length(); if (key.size() >= arcLabelLen && memcmp(key.data(), arcLabel, arcLabelLen) == 0) { - const TStringBuf srcKey = key; - key = key.SubStr(arcLabelLen); - const TNode* const node = it->Node; + const TStringBuf srcKey = key; + key = key.SubStr(arcLabelLen); + const TNode* const node = it->Node; if (srcKey.size() == arcLabelLen) { - // unpack value of it->Node, if it has value - if (!node->IsFinal()) - return nullptr; - - if (value) - packer.UnpackLeaf(node->GetPayload(), *value); - - result = true; - return nullptr; - } - - // find in subtree - return node; - } - } - - return nullptr; -} - + // unpack value of it->Node, if it has value + if (!node->IsFinal()) + return nullptr; + + if (value) + packer.UnpackLeaf(node->GetPayload(), *value); + + result = true; + return nullptr; + } + + // find in subtree + return node; + } + } + + return nullptr; +} + // Different //---------------------------------------------------------------------------------------------------------------------- diff --git a/library/cpp/containers/comptrie/comptrie_impl.h b/library/cpp/containers/comptrie/comptrie_impl.h index f41c38311a..d0ef94a518 100644 --- a/library/cpp/containers/comptrie/comptrie_impl.h +++ b/library/cpp/containers/comptrie/comptrie_impl.h @@ -180,7 +180,7 @@ namespace NCompactTrie { // Advances the data pointer to the root of the subtrie beginning after the symbol, // zeroes it if this subtrie is empty. // If there is a value associated with the symbol, makes the value pointer point to it, - // otherwise sets it to nullptr. + // otherwise sets it to nullptr. // Returns true if the symbol was succesfully found in the trie, false otherwise. template <typename TSymbol, class TPacker> Y_FORCE_INLINE bool Advance(const char*& datapos, const char* const dataend, const char*& value, @@ -193,7 +193,7 @@ namespace NCompactTrie { return false; // no such arc } - value = nullptr; + value = nullptr; Y_ASSERT(datapos <= dataend); if ((flags & MT_FINAL)) { @@ -203,7 +203,7 @@ namespace NCompactTrie { if (!(flags & MT_NEXT)) { if (i == 0) { - datapos = nullptr; + datapos = nullptr; return true; } return false; // no further way diff --git a/library/cpp/containers/comptrie/comptrie_trie.h b/library/cpp/containers/comptrie/comptrie_trie.h index 40ec1e52b3..f006f3cf79 100644 --- a/library/cpp/containers/comptrie/comptrie_trie.h +++ b/library/cpp/containers/comptrie/comptrie_trie.h @@ -80,8 +80,8 @@ public: bool IsInitialized() const; bool IsEmpty() const; - bool Find(const TSymbol* key, size_t keylen, TData* value = nullptr) const; - bool Find(const TKeyBuf& key, TData* value = nullptr) const { + bool Find(const TSymbol* key, size_t keylen, TData* value = nullptr) const; + bool Find(const TKeyBuf& key, TData* value = nullptr) const { return Find(key.data(), key.size(), value); } @@ -122,8 +122,8 @@ public: void FindPhrases(const TKeyBuf& key, TPhraseMatchVector& matches, TSymbol separator = TSymbol(' ')) const { return FindPhrases(key.data(), key.size(), matches, separator); } - bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value = nullptr, bool* hasNext = nullptr) const; - bool FindLongestPrefix(const TKeyBuf& key, size_t* prefixLen, TData* value = nullptr, bool* hasNext = nullptr) const { + bool FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value = nullptr, bool* hasNext = nullptr) const; + bool FindLongestPrefix(const TKeyBuf& key, size_t* prefixLen, TData* value = nullptr, bool* hasNext = nullptr) const { return FindLongestPrefix(key.data(), key.size(), prefixLen, value, hasNext); } @@ -315,18 +315,18 @@ void TCompactTrie<T, D, S>::Init(const TBlob& data, TPacker packer) { template <class T, class D, class S> bool TCompactTrie<T, D, S>::IsInitialized() const { - return DataHolder.Data() != nullptr; + return DataHolder.Data() != nullptr; } template <class T, class D, class S> bool TCompactTrie<T, D, S>::IsEmpty() const { - return DataHolder.Size() == 0 && EmptyValue == nullptr; + return DataHolder.Size() == 0 && EmptyValue == nullptr; } template <class T, class D, class S> -bool TCompactTrie<T, D, S>::Find(const TSymbol* key, size_t keylen, TData* value) const { +bool TCompactTrie<T, D, S>::Find(const TSymbol* key, size_t keylen, TData* value) const { size_t prefixLen = 0; - const char* valuepos = nullptr; + const char* valuepos = nullptr; bool hasNext; if (!LookupLongestPrefix(key, keylen, prefixLen, valuepos, hasNext) || prefixLen != keylen) return false; @@ -366,7 +366,7 @@ bool TCompactTrie<T, D, S>::FindTails(const TSymbol* key, size_t keylen, TCompac const char* const dataend = datapos + len; const TSymbol* keyend = key + keylen; - const char* value = nullptr; + const char* value = nullptr; while (key != keyend) { T label = *(key++); @@ -400,7 +400,7 @@ inline bool TCompactTrie<T, D, S>::FindTails(TSymbol label, TCompactTrie<T, D, S const char* datastart = DataHolder.AsCharPtr(); const char* dataend = datastart + len; const char* datapos = datastart; - const char* value = nullptr; + const char* value = nullptr; if (!NCompactTrie::Advance(datapos, dataend, value, label, Packer)) return false; @@ -460,8 +460,8 @@ void TCompactTrie<T, D, S>::Print(IOutputStream& os) { } template <class T, class D, class S> -bool TCompactTrie<T, D, S>::FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value, bool* hasNext) const { - const char* valuepos = nullptr; +bool TCompactTrie<T, D, S>::FindLongestPrefix(const TSymbol* key, size_t keylen, size_t* prefixLen, TData* value, bool* hasNext) const { + const char* valuepos = nullptr; size_t tempPrefixLen = 0; bool tempHasNext; bool found = LookupLongestPrefix(key, keylen, tempPrefixLen, valuepos, tempHasNext); @@ -475,7 +475,7 @@ bool TCompactTrie<T, D, S>::FindLongestPrefix(const TSymbol* key, size_t keylen, } template <class T, class D, class S> -bool TCompactTrie<T, D, S>::LookupLongestPrefix(const TSymbol* key, size_t keylen, size_t& prefixLen, const char*& valuepos, bool& hasNext) const { +bool TCompactTrie<T, D, S>::LookupLongestPrefix(const TSymbol* key, size_t keylen, size_t& prefixLen, const char*& valuepos, bool& hasNext) const { using namespace NCompactTrie; const char* datapos = DataHolder.AsCharPtr(); diff --git a/library/cpp/containers/comptrie/comptrie_ut.cpp b/library/cpp/containers/comptrie/comptrie_ut.cpp index 74bee09b5d..707468d90e 100644 --- a/library/cpp/containers/comptrie/comptrie_ut.cpp +++ b/library/cpp/containers/comptrie/comptrie_ut.cpp @@ -5,7 +5,7 @@ #include <utility> #include <util/charset/wide.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/buffer.h> #include <util/generic/map.h> #include <util/generic/vector.h> @@ -17,9 +17,9 @@ #include <util/random/random.h> #include <util/random/fast.h> -#include <util/string/hex.h> +#include <util/string/hex.h> #include <util/string/cast.h> - + #include "comptrie.h" #include "set.h" #include "first_symbol_iterator.h" @@ -27,8 +27,8 @@ #include "pattern_searcher.h" #include <array> -#include <iterator> - +#include <iterator> + class TCompactTrieTest: public TTestBase { private: @@ -108,7 +108,7 @@ private: UNIT_TEST(TestBuilderFindLongestPrefix); UNIT_TEST(TestBuilderFindLongestPrefixWithEmptyValue); - + UNIT_TEST(TestPatternSearcherEmpty); UNIT_TEST(TestPatternSearcherSimple); UNIT_TEST(TestPatternSearcherRandom); @@ -242,10 +242,10 @@ public: void TestFirstSymbolIteratorChar32(); void TestArrayPacker(); - - void TestBuilderFindLongestPrefix(); - void TestBuilderFindLongestPrefix(size_t keysCount, double branchProbability, bool isPrefixGrouped, bool hasEmptyKey); - void TestBuilderFindLongestPrefixWithEmptyValue(); + + void TestBuilderFindLongestPrefix(); + void TestBuilderFindLongestPrefix(size_t keysCount, double branchProbability, bool isPrefixGrouped, bool hasEmptyKey); + void TestBuilderFindLongestPrefixWithEmptyValue(); void TestPatternSearcherOnDataset( const TVector<TString>& patterns, @@ -396,7 +396,7 @@ void TCompactTrieTest::CheckData(const char* data, size_t datalen) { UNIT_ASSERT(trie.FindLongestPrefix(key, &prefixLen, &value)); UNIT_ASSERT_EQUAL(len, prefixLen); UNIT_ASSERT_EQUAL(len * 2, value); - UNIT_ASSERT(trie.FindLongestPrefix(key, &prefixLen, nullptr)); + UNIT_ASSERT(trie.FindLongestPrefix(key, &prefixLen, nullptr)); UNIT_ASSERT_EQUAL(len, prefixLen); } @@ -646,21 +646,21 @@ void TCompactTrieTest::TestUninitializedNonEmpty() { UNIT_ASSERT(it == tails.End()); } -static char RandChar() { - return char(RandomNumber<size_t>() % 256); -} - +static char RandChar() { + return char(RandomNumber<size_t>() % 256); +} + static TString RandStr(const size_t max) { size_t len = RandomNumber<size_t>() % max; TString key; for (size_t j = 0; j < len; ++j) - key += RandChar(); + key += RandChar(); return key; } template <class T, bool minimize> void TCompactTrieTest::TestRandom(const size_t n, const size_t maxKeySize) { - const TStringBuf EMPTY_KEY = TStringBuf("", 1); + const TStringBuf EMPTY_KEY = TStringBuf("", 1); TCompactTrieBuilder<char, typename T::TData, T> builder; typedef TMap<TString, typename T::TData> TKeys; TKeys keys; @@ -668,7 +668,7 @@ void TCompactTrieTest::TestRandom(const size_t n, const size_t maxKeySize) { typename T::TData dummy; for (size_t i = 0; i < n; ++i) { const TString key = RandStr(maxKeySize); - if (key != EMPTY_KEY && keys.find(key) == keys.end()) { + if (key != EMPTY_KEY && keys.find(key) == keys.end()) { const typename T::TData val = T::Data(key); keys[key] = val; UNIT_ASSERT_C(!builder.Find(key.data(), key.size(), &dummy), "key = " << HexEncode(TString(key))); @@ -691,7 +691,7 @@ void TCompactTrieTest::TestRandom(const size_t n, const size_t maxKeySize) { TCompactTrieBuilder<char, typename T::TData, T> prefixGroupedBuilder(CTBF_PREFIX_GROUPED); for (typename TKeys::const_iterator i = keys.begin(), mi = keys.end(); i != mi; ++i) { - UNIT_ASSERT(!prefixGroupedBuilder.Find(i->first.c_str(), i->first.size(), &dummy)); + UNIT_ASSERT(!prefixGroupedBuilder.Find(i->first.c_str(), i->first.size(), &dummy)); UNIT_ASSERT(trie.Find(i->first.c_str(), i->first.size(), &dummy)); UNIT_ASSERT(dummy == i->second); if (minimize) { @@ -700,17 +700,17 @@ void TCompactTrieTest::TestRandom(const size_t n, const size_t maxKeySize) { } prefixGroupedBuilder.Add(i->first.c_str(), i->first.size(), dummy); - UNIT_ASSERT(prefixGroupedBuilder.Find(i->first.c_str(), i->first.size(), &dummy)); - - for (typename TKeys::const_iterator j = keys.begin(), end = keys.end(); j != end; ++j) { - typename T::TData valFound; - if (j->first <= i->first) { - UNIT_ASSERT(prefixGroupedBuilder.Find(j->first.c_str(), j->first.size(), &valFound)); - UNIT_ASSERT_VALUES_EQUAL(j->second, valFound); - } else { - UNIT_ASSERT(!prefixGroupedBuilder.Find(j->first.c_str(), j->first.size(), &valFound)); - } - } + UNIT_ASSERT(prefixGroupedBuilder.Find(i->first.c_str(), i->first.size(), &dummy)); + + for (typename TKeys::const_iterator j = keys.begin(), end = keys.end(); j != end; ++j) { + typename T::TData valFound; + if (j->first <= i->first) { + UNIT_ASSERT(prefixGroupedBuilder.Find(j->first.c_str(), j->first.size(), &valFound)); + UNIT_ASSERT_VALUES_EQUAL(j->second, valFound); + } else { + UNIT_ASSERT(!prefixGroupedBuilder.Find(j->first.c_str(), j->first.size(), &valFound)); + } + } } TBufferStream prefixGroupedBuffer; @@ -790,18 +790,18 @@ void TCompactTrieTest::TestPrefixGrouped() { }; for (size_t i = 0; i < Y_ARRAY_SIZE(data); ++i) { - ui32 val = strlen(data[i]) + 1; - b1.Add(data[i], strlen(data[i]), val); + ui32 val = strlen(data[i]) + 1; + b1.Add(data[i], strlen(data[i]), val); for (size_t j = 0; j < Y_ARRAY_SIZE(data); ++j) { - ui32 mustHave = strlen(data[j]) + 1; - ui32 found = 0; - if (j <= i) { - UNIT_ASSERT(b1.Find(data[j], strlen(data[j]), &found)); - UNIT_ASSERT_VALUES_EQUAL(mustHave, found); - } else { - UNIT_ASSERT(!b1.Find(data[j], strlen(data[j]), &found)); - } - } + ui32 mustHave = strlen(data[j]) + 1; + ui32 found = 0; + if (j <= i) { + UNIT_ASSERT(b1.Find(data[j], strlen(data[j]), &found)); + UNIT_ASSERT_VALUES_EQUAL(mustHave, found); + } else { + UNIT_ASSERT(!b1.Find(data[j], strlen(data[j]), &found)); + } + } } { @@ -1017,7 +1017,7 @@ class TCompactTrieTest::TDummyPacker: public TNullPacker<T> { public: static T Data(const TString&) { T data; - TNullPacker<T>().UnpackLeaf(nullptr, data); + TNullPacker<T>().UnpackLeaf(nullptr, data); return data; } @@ -1280,7 +1280,7 @@ void TCompactTrieTest::TestFindLongestPrefixWithEmptyValue() { } { TCompactTrie<wchar16, ui32> trie(buffer.Buffer().Data(), buffer.Buffer().Size()); - size_t prefixLen = 123; + size_t prefixLen = 123; ui32 value = 0; UNIT_ASSERT(trie.FindLongestPrefix(u"google", &prefixLen, &value)); @@ -1465,121 +1465,121 @@ void TCompactTrieTest::TestArrayPacker() { UNIT_ASSERT_VALUES_EQUAL(dataZzz.second, trieTwo.Get(dataZzz.first)); UNIT_ASSERT_VALUES_EQUAL(dataWww.second, trieTwo.Get(dataWww.first)); } - -void TCompactTrieTest::TestBuilderFindLongestPrefix() { - const size_t sizes[] = {10, 100}; + +void TCompactTrieTest::TestBuilderFindLongestPrefix() { + const size_t sizes[] = {10, 100}; const double branchProbabilities[] = {0.01, 0.1, 0.5, 0.9, 0.99}; - for (size_t size : sizes) { - for (double branchProbability : branchProbabilities) { - TestBuilderFindLongestPrefix(size, branchProbability, false, false); - TestBuilderFindLongestPrefix(size, branchProbability, false, true); - TestBuilderFindLongestPrefix(size, branchProbability, true, false); - TestBuilderFindLongestPrefix(size, branchProbability, true, true); - } - } -} - -void TCompactTrieTest::TestBuilderFindLongestPrefix(size_t keysCount, double branchProbability, bool isPrefixGrouped, bool hasEmptyKey) { + for (size_t size : sizes) { + for (double branchProbability : branchProbabilities) { + TestBuilderFindLongestPrefix(size, branchProbability, false, false); + TestBuilderFindLongestPrefix(size, branchProbability, false, true); + TestBuilderFindLongestPrefix(size, branchProbability, true, false); + TestBuilderFindLongestPrefix(size, branchProbability, true, true); + } + } +} + +void TCompactTrieTest::TestBuilderFindLongestPrefix(size_t keysCount, double branchProbability, bool isPrefixGrouped, bool hasEmptyKey) { TVector<TString> keys; TString keyToAdd; - for (size_t i = 0; i < keysCount; ++i) { - const size_t prevKeyLen = keyToAdd.Size(); - // add two random chars to prev key - keyToAdd += RandChar(); - keyToAdd += RandChar(); - const bool changeBranch = prevKeyLen && RandomNumber<double>() < branchProbability; - if (changeBranch) { - const size_t branchPlace = RandomNumber<size_t>(prevKeyLen + 1); // random place in [0, prevKeyLen] - *(keyToAdd.begin() + branchPlace) = RandChar(); - } - keys.push_back(keyToAdd); - } - - if (isPrefixGrouped) - Sort(keys.begin(), keys.end()); - else + for (size_t i = 0; i < keysCount; ++i) { + const size_t prevKeyLen = keyToAdd.Size(); + // add two random chars to prev key + keyToAdd += RandChar(); + keyToAdd += RandChar(); + const bool changeBranch = prevKeyLen && RandomNumber<double>() < branchProbability; + if (changeBranch) { + const size_t branchPlace = RandomNumber<size_t>(prevKeyLen + 1); // random place in [0, prevKeyLen] + *(keyToAdd.begin() + branchPlace) = RandChar(); + } + keys.push_back(keyToAdd); + } + + if (isPrefixGrouped) + Sort(keys.begin(), keys.end()); + else Shuffle(keys.begin(), keys.end()); - + TCompactTrieBuilder<char, TString> builder(isPrefixGrouped ? CTBF_PREFIX_GROUPED : CTBF_NONE); const TString EMPTY_VALUE = "empty"; - if (hasEmptyKey) - builder.Add(nullptr, 0, EMPTY_VALUE); - - for (size_t i = 0; i < keysCount; ++i) { + if (hasEmptyKey) + builder.Add(nullptr, 0, EMPTY_VALUE); + + for (size_t i = 0; i < keysCount; ++i) { const TString& key = keys[i]; - - for (size_t j = 0; j < keysCount; ++j) { + + for (size_t j = 0; j < keysCount; ++j) { const TString& otherKey = keys[j]; - const bool exists = j < i; - size_t expectedSize = 0; - if (exists) { - expectedSize = otherKey.size(); - } else { - size_t max = 0; - for (size_t k = 0; k < i; ++k) + const bool exists = j < i; + size_t expectedSize = 0; + if (exists) { + expectedSize = otherKey.size(); + } else { + size_t max = 0; + for (size_t k = 0; k < i; ++k) if (keys[k].Size() < otherKey.Size() && keys[k].Size() > max && otherKey.StartsWith(keys[k])) - max = keys[k].Size(); - expectedSize = max; - } - - size_t prefixSize = 0xfcfcfc; + max = keys[k].Size(); + expectedSize = max; + } + + size_t prefixSize = 0xfcfcfc; TString value = "abcd"; - const bool expectedResult = hasEmptyKey || expectedSize != 0; + const bool expectedResult = hasEmptyKey || expectedSize != 0; UNIT_ASSERT_VALUES_EQUAL_C(expectedResult, builder.FindLongestPrefix(otherKey.data(), otherKey.size(), &prefixSize, &value), "otherKey = " << HexEncode(otherKey)); - if (expectedResult) { - UNIT_ASSERT_VALUES_EQUAL(expectedSize, prefixSize); - if (expectedSize) { - UNIT_ASSERT_VALUES_EQUAL(TStringBuf(otherKey).SubStr(0, prefixSize), value); - } else { - UNIT_ASSERT_VALUES_EQUAL(EMPTY_VALUE, value); - } - } else { - UNIT_ASSERT_VALUES_EQUAL("abcd", value); - UNIT_ASSERT_VALUES_EQUAL(0xfcfcfc, prefixSize); - } - - for (int c = 0; c < 10; ++c) { + if (expectedResult) { + UNIT_ASSERT_VALUES_EQUAL(expectedSize, prefixSize); + if (expectedSize) { + UNIT_ASSERT_VALUES_EQUAL(TStringBuf(otherKey).SubStr(0, prefixSize), value); + } else { + UNIT_ASSERT_VALUES_EQUAL(EMPTY_VALUE, value); + } + } else { + UNIT_ASSERT_VALUES_EQUAL("abcd", value); + UNIT_ASSERT_VALUES_EQUAL(0xfcfcfc, prefixSize); + } + + for (int c = 0; c < 10; ++c) { TString extendedKey = otherKey; - extendedKey += RandChar(); - size_t extendedPrefixSize = 0xdddddd; + extendedKey += RandChar(); + size_t extendedPrefixSize = 0xdddddd; TString extendedValue = "dcba"; UNIT_ASSERT_VALUES_EQUAL(expectedResult, builder.FindLongestPrefix(extendedKey.data(), extendedKey.size(), &extendedPrefixSize, &extendedValue)); - if (expectedResult) { - UNIT_ASSERT_VALUES_EQUAL(value, extendedValue); - UNIT_ASSERT_VALUES_EQUAL(prefixSize, extendedPrefixSize); - } else { - UNIT_ASSERT_VALUES_EQUAL("dcba", extendedValue); - UNIT_ASSERT_VALUES_EQUAL(0xdddddd, extendedPrefixSize); - } - } - } + if (expectedResult) { + UNIT_ASSERT_VALUES_EQUAL(value, extendedValue); + UNIT_ASSERT_VALUES_EQUAL(prefixSize, extendedPrefixSize); + } else { + UNIT_ASSERT_VALUES_EQUAL("dcba", extendedValue); + UNIT_ASSERT_VALUES_EQUAL(0xdddddd, extendedPrefixSize); + } + } + } builder.Add(key.data(), key.size(), key); - } - - TBufferOutput buffer; - builder.Save(buffer); -} - -void TCompactTrieTest::TestBuilderFindLongestPrefixWithEmptyValue() { - TCompactTrieBuilder<wchar16, ui32> builder; + } + + TBufferOutput buffer; + builder.Save(buffer); +} + +void TCompactTrieTest::TestBuilderFindLongestPrefixWithEmptyValue() { + TCompactTrieBuilder<wchar16, ui32> builder; builder.Add(u"", 42); builder.Add(u"yandex", 271828); builder.Add(u"ya", 31415); - - size_t prefixLen = 123; - ui32 value = 0; - + + size_t prefixLen = 123; + ui32 value = 0; + UNIT_ASSERT(builder.FindLongestPrefix(u"google", &prefixLen, &value)); - UNIT_ASSERT_VALUES_EQUAL(prefixLen, 0); - UNIT_ASSERT_VALUES_EQUAL(value, 42); - + UNIT_ASSERT_VALUES_EQUAL(prefixLen, 0); + UNIT_ASSERT_VALUES_EQUAL(value, 42); + UNIT_ASSERT(builder.FindLongestPrefix(u"yahoo", &prefixLen, &value)); - UNIT_ASSERT_VALUES_EQUAL(prefixLen, 2); - UNIT_ASSERT_VALUES_EQUAL(value, 31415); - - TBufferOutput buffer; - builder.Save(buffer); -} + UNIT_ASSERT_VALUES_EQUAL(prefixLen, 2); + UNIT_ASSERT_VALUES_EQUAL(value, 31415); + + TBufferOutput buffer; + builder.Save(buffer); +} void TCompactTrieTest::TestPatternSearcherEmpty() { TCompactPatternSearcherBuilder<char, ui32> builder; diff --git a/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.cpp b/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.cpp index 7334a43c36..ff3b6c2ff6 100644 --- a/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.cpp +++ b/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.cpp @@ -1 +1 @@ -#include "disjoint_interval_tree.h" +#include "disjoint_interval_tree.h" diff --git a/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h b/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h index 1f899c9991..d3bd6f7748 100644 --- a/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h +++ b/library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h @@ -1,227 +1,227 @@ -#pragma once - -#include <util/generic/map.h> -#include <util/system/yassert.h> - -#include <type_traits> - -template <class T> -class TDisjointIntervalTree { -private: - static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value"); - - using TTree = TMap<T, T>; // [key, value) - using TIterator = typename TTree::iterator; - using TConstIterator = typename TTree::const_iterator; - using TReverseIterator = typename TTree::reverse_iterator; - using TThis = TDisjointIntervalTree<T>; - - TTree Tree; - size_t NumElements; - -public: - TDisjointIntervalTree() - : NumElements() - { - } - - void Insert(const T t) { - InsertInterval(t, t + 1); - } - - // we assume that none of elements from [begin, end) belong to tree. - void InsertInterval(const T begin, const T end) { - InsertIntervalImpl(begin, end); - NumElements += (size_t)(end - begin); - } - - bool Has(const T t) const { - return const_cast<TThis*>(this)->FindContaining(t) != Tree.end(); - } - - bool Intersects(const T begin, const T end) { - if (Empty()) { - return false; - } - - TIterator l = Tree.lower_bound(begin); - if (l != Tree.end()) { - if (l->first < end) { - return true; - } else if (l != Tree.begin()) { - --l; - return l->second > begin; - } else { - return false; - } - } else { - auto last = Tree.rbegin(); - return begin < last->second; - } - } - - TConstIterator FindContaining(const T t) const { - return const_cast<TThis*>(this)->FindContaining(t); - } - - // Erase element. Returns true when element has been deleted, otherwise false. - bool Erase(const T t) { - TIterator n = FindContaining(t); - if (n == Tree.end()) { - return false; - } - - --NumElements; - - T& begin = const_cast<T&>(n->first); - T& end = const_cast<T&>(n->second); - - // Optimization hack. - if (t == begin) { - if (++begin == end) { // OK to change key since intervals do not intersect. - Tree.erase(n); - return true; - } - - } else if (t == end - 1) { - --end; - - } else { - const T e = end; - end = t; - InsertIntervalImpl(t + 1, e); - } - - Y_ASSERT(begin < end); - return true; - } - - // Erase interval. Returns number of elements removed from set. - size_t EraseInterval(const T begin, const T end) { - Y_ASSERT(begin < end); - - if (Empty()) { - return 0; - } - - size_t elementsRemoved = 0; - - TIterator completelyRemoveBegin = Tree.lower_bound(begin); - if ((completelyRemoveBegin != Tree.end() && completelyRemoveBegin->first > begin && completelyRemoveBegin != Tree.begin()) - || completelyRemoveBegin == Tree.end()) { - // Look at the interval. It could contain [begin, end). - TIterator containingBegin = completelyRemoveBegin; - --containingBegin; - if (containingBegin->first < begin && begin < containingBegin->second) { // Contains begin. - if (containingBegin->second > end) { // Contains end. - const T prevEnd = containingBegin->second; - Y_ASSERT(containingBegin->second - begin <= NumElements); - - Y_ASSERT(containingBegin->second - containingBegin->first > end - begin); - containingBegin->second = begin; - InsertIntervalImpl(end, prevEnd); - - elementsRemoved = end - begin; - NumElements -= elementsRemoved; - return elementsRemoved; - } else { - elementsRemoved += containingBegin->second - begin; - containingBegin->second = begin; - } - } - } - - TIterator completelyRemoveEnd = completelyRemoveBegin != Tree.end() ? Tree.lower_bound(end) : Tree.end(); - if (completelyRemoveEnd != Tree.end() && completelyRemoveEnd != Tree.begin() && completelyRemoveEnd->first != end) { - TIterator containingEnd = completelyRemoveEnd; - --containingEnd; - if (containingEnd->second > end) { - T& leftBorder = const_cast<T&>(containingEnd->first); - - Y_ASSERT(leftBorder < end); - - --completelyRemoveEnd; // Don't remove the whole interval. - - // Optimization hack. - elementsRemoved += end - leftBorder; - leftBorder = end; // OK to change key since intervals do not intersect. - } - } - - for (TIterator i = completelyRemoveBegin; i != completelyRemoveEnd; ++i) { - elementsRemoved += i->second - i->first; - } - - Tree.erase(completelyRemoveBegin, completelyRemoveEnd); - - Y_ASSERT(elementsRemoved <= NumElements); - NumElements -= elementsRemoved; - - return elementsRemoved; - } - - void Swap(TDisjointIntervalTree& rhv) { - Tree.swap(rhv.Tree); - std::swap(NumElements, rhv.NumElements); - } - - void Clear() { - Tree.clear(); - NumElements = 0; - } - - bool Empty() const { - return Tree.empty(); - } - - size_t GetNumElements() const { - return NumElements; - } - - size_t GetNumIntervals() const { - return Tree.size(); - } - - T Min() const { - Y_ASSERT(!Empty()); - return Tree.begin()->first; - } - - T Max() const { - Y_ASSERT(!Empty()); - return Tree.rbegin()->second; - } - - TConstIterator begin() const { - return Tree.begin(); - } - - TConstIterator end() const { - return Tree.end(); - } - -private: - void InsertIntervalImpl(const T begin, const T end) { - Y_ASSERT(begin < end); - Y_ASSERT(!Intersects(begin, end)); - - TIterator l = Tree.lower_bound(begin); - TIterator p = Tree.end(); - if (l != Tree.begin()) { - p = l; - --p; - } - -#ifndef NDEBUG - TIterator u = Tree.upper_bound(begin); - Y_VERIFY_DEBUG(u == Tree.end() || u->first >= end, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, u->first, u->second); - Y_VERIFY_DEBUG(l == Tree.end() || l == u, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, l->first, l->second); - Y_VERIFY_DEBUG(p == Tree.end() || p->second <= begin, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, p->first, p->second); -#endif - - // try to extend interval - if (p != Tree.end() && p->second == begin) { - p->second = end; +#pragma once + +#include <util/generic/map.h> +#include <util/system/yassert.h> + +#include <type_traits> + +template <class T> +class TDisjointIntervalTree { +private: + static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value"); + + using TTree = TMap<T, T>; // [key, value) + using TIterator = typename TTree::iterator; + using TConstIterator = typename TTree::const_iterator; + using TReverseIterator = typename TTree::reverse_iterator; + using TThis = TDisjointIntervalTree<T>; + + TTree Tree; + size_t NumElements; + +public: + TDisjointIntervalTree() + : NumElements() + { + } + + void Insert(const T t) { + InsertInterval(t, t + 1); + } + + // we assume that none of elements from [begin, end) belong to tree. + void InsertInterval(const T begin, const T end) { + InsertIntervalImpl(begin, end); + NumElements += (size_t)(end - begin); + } + + bool Has(const T t) const { + return const_cast<TThis*>(this)->FindContaining(t) != Tree.end(); + } + + bool Intersects(const T begin, const T end) { + if (Empty()) { + return false; + } + + TIterator l = Tree.lower_bound(begin); + if (l != Tree.end()) { + if (l->first < end) { + return true; + } else if (l != Tree.begin()) { + --l; + return l->second > begin; + } else { + return false; + } + } else { + auto last = Tree.rbegin(); + return begin < last->second; + } + } + + TConstIterator FindContaining(const T t) const { + return const_cast<TThis*>(this)->FindContaining(t); + } + + // Erase element. Returns true when element has been deleted, otherwise false. + bool Erase(const T t) { + TIterator n = FindContaining(t); + if (n == Tree.end()) { + return false; + } + + --NumElements; + + T& begin = const_cast<T&>(n->first); + T& end = const_cast<T&>(n->second); + + // Optimization hack. + if (t == begin) { + if (++begin == end) { // OK to change key since intervals do not intersect. + Tree.erase(n); + return true; + } + + } else if (t == end - 1) { + --end; + + } else { + const T e = end; + end = t; + InsertIntervalImpl(t + 1, e); + } + + Y_ASSERT(begin < end); + return true; + } + + // Erase interval. Returns number of elements removed from set. + size_t EraseInterval(const T begin, const T end) { + Y_ASSERT(begin < end); + + if (Empty()) { + return 0; + } + + size_t elementsRemoved = 0; + + TIterator completelyRemoveBegin = Tree.lower_bound(begin); + if ((completelyRemoveBegin != Tree.end() && completelyRemoveBegin->first > begin && completelyRemoveBegin != Tree.begin()) + || completelyRemoveBegin == Tree.end()) { + // Look at the interval. It could contain [begin, end). + TIterator containingBegin = completelyRemoveBegin; + --containingBegin; + if (containingBegin->first < begin && begin < containingBegin->second) { // Contains begin. + if (containingBegin->second > end) { // Contains end. + const T prevEnd = containingBegin->second; + Y_ASSERT(containingBegin->second - begin <= NumElements); + + Y_ASSERT(containingBegin->second - containingBegin->first > end - begin); + containingBegin->second = begin; + InsertIntervalImpl(end, prevEnd); + + elementsRemoved = end - begin; + NumElements -= elementsRemoved; + return elementsRemoved; + } else { + elementsRemoved += containingBegin->second - begin; + containingBegin->second = begin; + } + } + } + + TIterator completelyRemoveEnd = completelyRemoveBegin != Tree.end() ? Tree.lower_bound(end) : Tree.end(); + if (completelyRemoveEnd != Tree.end() && completelyRemoveEnd != Tree.begin() && completelyRemoveEnd->first != end) { + TIterator containingEnd = completelyRemoveEnd; + --containingEnd; + if (containingEnd->second > end) { + T& leftBorder = const_cast<T&>(containingEnd->first); + + Y_ASSERT(leftBorder < end); + + --completelyRemoveEnd; // Don't remove the whole interval. + + // Optimization hack. + elementsRemoved += end - leftBorder; + leftBorder = end; // OK to change key since intervals do not intersect. + } + } + + for (TIterator i = completelyRemoveBegin; i != completelyRemoveEnd; ++i) { + elementsRemoved += i->second - i->first; + } + + Tree.erase(completelyRemoveBegin, completelyRemoveEnd); + + Y_ASSERT(elementsRemoved <= NumElements); + NumElements -= elementsRemoved; + + return elementsRemoved; + } + + void Swap(TDisjointIntervalTree& rhv) { + Tree.swap(rhv.Tree); + std::swap(NumElements, rhv.NumElements); + } + + void Clear() { + Tree.clear(); + NumElements = 0; + } + + bool Empty() const { + return Tree.empty(); + } + + size_t GetNumElements() const { + return NumElements; + } + + size_t GetNumIntervals() const { + return Tree.size(); + } + + T Min() const { + Y_ASSERT(!Empty()); + return Tree.begin()->first; + } + + T Max() const { + Y_ASSERT(!Empty()); + return Tree.rbegin()->second; + } + + TConstIterator begin() const { + return Tree.begin(); + } + + TConstIterator end() const { + return Tree.end(); + } + +private: + void InsertIntervalImpl(const T begin, const T end) { + Y_ASSERT(begin < end); + Y_ASSERT(!Intersects(begin, end)); + + TIterator l = Tree.lower_bound(begin); + TIterator p = Tree.end(); + if (l != Tree.begin()) { + p = l; + --p; + } + +#ifndef NDEBUG + TIterator u = Tree.upper_bound(begin); + Y_VERIFY_DEBUG(u == Tree.end() || u->first >= end, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, u->first, u->second); + Y_VERIFY_DEBUG(l == Tree.end() || l == u, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, l->first, l->second); + Y_VERIFY_DEBUG(p == Tree.end() || p->second <= begin, "Trying to add [%" PRIu64 ", %" PRIu64 ") which intersects with existing [%" PRIu64 ", %" PRIu64 ")", begin, end, p->first, p->second); +#endif + + // try to extend interval + if (p != Tree.end() && p->second == begin) { + p->second = end; //Try to merge 2 intervals - p and next one if possible auto next = p; // Next is not Tree.end() here. @@ -231,42 +231,42 @@ private: Tree.erase(next); } // Maybe new interval extends right interval - } else if (l != Tree.end() && end == l->first) { - T& leftBorder = const_cast<T&>(l->first); - // Optimization hack. - leftBorder = begin; // OK to change key since intervals do not intersect. - } else { - Tree.insert(std::make_pair(begin, end)); - } - } - - TIterator FindContaining(const T t) { - TIterator l = Tree.lower_bound(t); - if (l != Tree.end()) { - if (l->first == t) { - return l; - } - Y_ASSERT(l->first > t); - - if (l == Tree.begin()) { - return Tree.end(); - } - - --l; - Y_ASSERT(l->first != t); - - if (l->first < t && t < l->second) { - return l; - } - - } else if (!Tree.empty()) { // l is larger than Begin of any interval, but maybe it belongs to last interval? - TReverseIterator last = Tree.rbegin(); - Y_ASSERT(last->first != t); - - if (last->first < t && t < last->second) { - return (++last).base(); - } - } - return Tree.end(); - } -}; + } else if (l != Tree.end() && end == l->first) { + T& leftBorder = const_cast<T&>(l->first); + // Optimization hack. + leftBorder = begin; // OK to change key since intervals do not intersect. + } else { + Tree.insert(std::make_pair(begin, end)); + } + } + + TIterator FindContaining(const T t) { + TIterator l = Tree.lower_bound(t); + if (l != Tree.end()) { + if (l->first == t) { + return l; + } + Y_ASSERT(l->first > t); + + if (l == Tree.begin()) { + return Tree.end(); + } + + --l; + Y_ASSERT(l->first != t); + + if (l->first < t && t < l->second) { + return l; + } + + } else if (!Tree.empty()) { // l is larger than Begin of any interval, but maybe it belongs to last interval? + TReverseIterator last = Tree.rbegin(); + Y_ASSERT(last->first != t); + + if (last->first < t && t < last->second) { + return (++last).base(); + } + } + return Tree.end(); + } +}; diff --git a/library/cpp/containers/disjoint_interval_tree/ut/disjoint_interval_tree_ut.cpp b/library/cpp/containers/disjoint_interval_tree/ut/disjoint_interval_tree_ut.cpp index 8474ae89b0..0292c72282 100644 --- a/library/cpp/containers/disjoint_interval_tree/ut/disjoint_interval_tree_ut.cpp +++ b/library/cpp/containers/disjoint_interval_tree/ut/disjoint_interval_tree_ut.cpp @@ -1,60 +1,60 @@ -#include <library/cpp/testing/unittest/registar.h> - -#include <library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h> - -Y_UNIT_TEST_SUITE(DisjointIntervalTreeTest) { - Y_UNIT_TEST(GenericTest) { - TDisjointIntervalTree<ui64> tree; - tree.Insert(1); - tree.Insert(50); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 2); - - tree.InsertInterval(10, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 22); - - UNIT_ASSERT_VALUES_EQUAL(tree.Min(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.Max(), 51); - - tree.Erase(20); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 4); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 21); - - tree.Clear(); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); - } - - Y_UNIT_TEST(MergeIntervalsTest) { - TDisjointIntervalTree<ui64> tree; - tree.Insert(5); - - // Insert interval from right side. - tree.Insert(6); - - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 2); - - { - auto begin = tree.begin(); - UNIT_ASSERT_VALUES_EQUAL(begin->first, 5); - UNIT_ASSERT_VALUES_EQUAL(begin->second, 7); - - ++begin; - UNIT_ASSERT_EQUAL(begin, tree.end()); - } - - // Insert interval from left side. - tree.InsertInterval(2, 5); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - { - auto begin = tree.begin(); - UNIT_ASSERT_VALUES_EQUAL(begin->first, 2); - UNIT_ASSERT_VALUES_EQUAL(begin->second, 7); - } +#include <library/cpp/testing/unittest/registar.h> + +#include <library/cpp/containers/disjoint_interval_tree/disjoint_interval_tree.h> + +Y_UNIT_TEST_SUITE(DisjointIntervalTreeTest) { + Y_UNIT_TEST(GenericTest) { + TDisjointIntervalTree<ui64> tree; + tree.Insert(1); + tree.Insert(50); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 2); + + tree.InsertInterval(10, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 22); + + UNIT_ASSERT_VALUES_EQUAL(tree.Min(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.Max(), 51); + + tree.Erase(20); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 4); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 21); + + tree.Clear(); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); + } + + Y_UNIT_TEST(MergeIntervalsTest) { + TDisjointIntervalTree<ui64> tree; + tree.Insert(5); + + // Insert interval from right side. + tree.Insert(6); + + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 2); + + { + auto begin = tree.begin(); + UNIT_ASSERT_VALUES_EQUAL(begin->first, 5); + UNIT_ASSERT_VALUES_EQUAL(begin->second, 7); + + ++begin; + UNIT_ASSERT_EQUAL(begin, tree.end()); + } + + // Insert interval from left side. + tree.InsertInterval(2, 5); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + { + auto begin = tree.begin(); + UNIT_ASSERT_VALUES_EQUAL(begin->first, 2); + UNIT_ASSERT_VALUES_EQUAL(begin->second, 7); + } // Merge all intervals. { @@ -71,209 +71,209 @@ Y_UNIT_TEST_SUITE(DisjointIntervalTreeTest) { UNIT_ASSERT_VALUES_EQUAL(begin->second, 10); } - } - - Y_UNIT_TEST(EraseIntervalTest) { - // 1. Remove from empty tree. - { - TDisjointIntervalTree<ui64> tree; - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(1, 3), 0); - } - - // 2. No such interval in set. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(1, 3), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(20, 30), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - } - - // 3. Remove the whole tree. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(0, 100), 5); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); - UNIT_ASSERT(tree.Empty()); - } - - // 4. Remove the whole tree with borders specified exactly as in tree. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(5, 10), 5); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); - UNIT_ASSERT(tree.Empty()); - } - - // 5. Specify left border exactly as in existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(15, 100500), 10); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - } - - // 6. Specify left border somewhere in existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(16, 100500), 9); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 6); - } - - // 7. Remove from the center of existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(17, 19), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 4); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 13); - - UNIT_ASSERT(tree.Has(16)); - UNIT_ASSERT(tree.Has(19)); - } - - // 8. Remove from the center of the only existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(15, 20); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(17, 19), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 3); - - UNIT_ASSERT(tree.Has(16)); - UNIT_ASSERT(tree.Has(19)); - } - - // 9. Specify borders between existing intervals. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(10, 15), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(13, 15), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(10, 13), 0); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - } - - // 10. Specify right border exactly as in existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(0, 20), 10); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); - } - - // 11. Specify right border somewhere in existing interval. - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(15, 20); - tree.InsertInterval(25, 30); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); - - UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(2, 17), 7); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); - UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 8); - } - } - - Y_UNIT_TEST(IntersectsTest) { - { - TDisjointIntervalTree<ui64> tree; - UNIT_ASSERT(!tree.Intersects(1, 2)); - } - - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - - UNIT_ASSERT(tree.Intersects(5, 10)); - UNIT_ASSERT(tree.Intersects(5, 6)); - UNIT_ASSERT(tree.Intersects(9, 10)); - UNIT_ASSERT(tree.Intersects(6, 8)); - UNIT_ASSERT(tree.Intersects(1, 8)); - UNIT_ASSERT(tree.Intersects(8, 15)); - UNIT_ASSERT(tree.Intersects(3, 14)); - - UNIT_ASSERT(!tree.Intersects(3, 5)); - UNIT_ASSERT(!tree.Intersects(10, 13)); - } - - { - TDisjointIntervalTree<ui64> tree; - tree.InsertInterval(5, 10); - tree.InsertInterval(20, 30); - - UNIT_ASSERT(tree.Intersects(5, 10)); - UNIT_ASSERT(tree.Intersects(5, 6)); - UNIT_ASSERT(tree.Intersects(9, 10)); - UNIT_ASSERT(tree.Intersects(6, 8)); - UNIT_ASSERT(tree.Intersects(1, 8)); - UNIT_ASSERT(tree.Intersects(8, 15)); - UNIT_ASSERT(tree.Intersects(3, 14)); - UNIT_ASSERT(tree.Intersects(18, 21)); - UNIT_ASSERT(tree.Intersects(3, 50)); - - UNIT_ASSERT(!tree.Intersects(3, 5)); - UNIT_ASSERT(!tree.Intersects(10, 13)); - UNIT_ASSERT(!tree.Intersects(15, 18)); - } - } -} + } + + Y_UNIT_TEST(EraseIntervalTest) { + // 1. Remove from empty tree. + { + TDisjointIntervalTree<ui64> tree; + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(1, 3), 0); + } + + // 2. No such interval in set. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(1, 3), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(20, 30), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + } + + // 3. Remove the whole tree. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(0, 100), 5); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); + UNIT_ASSERT(tree.Empty()); + } + + // 4. Remove the whole tree with borders specified exactly as in tree. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(5, 10), 5); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 0); + UNIT_ASSERT(tree.Empty()); + } + + // 5. Specify left border exactly as in existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(15, 100500), 10); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + } + + // 6. Specify left border somewhere in existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(16, 100500), 9); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 6); + } + + // 7. Remove from the center of existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(17, 19), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 4); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 13); + + UNIT_ASSERT(tree.Has(16)); + UNIT_ASSERT(tree.Has(19)); + } + + // 8. Remove from the center of the only existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(15, 20); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(17, 19), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 3); + + UNIT_ASSERT(tree.Has(16)); + UNIT_ASSERT(tree.Has(19)); + } + + // 9. Specify borders between existing intervals. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(10, 15), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(13, 15), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(10, 13), 0); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + } + + // 10. Specify right border exactly as in existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(0, 20), 10); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 1); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 5); + } + + // 11. Specify right border somewhere in existing interval. + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(15, 20); + tree.InsertInterval(25, 30); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 3); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 15); + + UNIT_ASSERT_VALUES_EQUAL(tree.EraseInterval(2, 17), 7); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumIntervals(), 2); + UNIT_ASSERT_VALUES_EQUAL(tree.GetNumElements(), 8); + } + } + + Y_UNIT_TEST(IntersectsTest) { + { + TDisjointIntervalTree<ui64> tree; + UNIT_ASSERT(!tree.Intersects(1, 2)); + } + + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + + UNIT_ASSERT(tree.Intersects(5, 10)); + UNIT_ASSERT(tree.Intersects(5, 6)); + UNIT_ASSERT(tree.Intersects(9, 10)); + UNIT_ASSERT(tree.Intersects(6, 8)); + UNIT_ASSERT(tree.Intersects(1, 8)); + UNIT_ASSERT(tree.Intersects(8, 15)); + UNIT_ASSERT(tree.Intersects(3, 14)); + + UNIT_ASSERT(!tree.Intersects(3, 5)); + UNIT_ASSERT(!tree.Intersects(10, 13)); + } + + { + TDisjointIntervalTree<ui64> tree; + tree.InsertInterval(5, 10); + tree.InsertInterval(20, 30); + + UNIT_ASSERT(tree.Intersects(5, 10)); + UNIT_ASSERT(tree.Intersects(5, 6)); + UNIT_ASSERT(tree.Intersects(9, 10)); + UNIT_ASSERT(tree.Intersects(6, 8)); + UNIT_ASSERT(tree.Intersects(1, 8)); + UNIT_ASSERT(tree.Intersects(8, 15)); + UNIT_ASSERT(tree.Intersects(3, 14)); + UNIT_ASSERT(tree.Intersects(18, 21)); + UNIT_ASSERT(tree.Intersects(3, 50)); + + UNIT_ASSERT(!tree.Intersects(3, 5)); + UNIT_ASSERT(!tree.Intersects(10, 13)); + UNIT_ASSERT(!tree.Intersects(15, 18)); + } + } +} diff --git a/library/cpp/containers/disjoint_interval_tree/ut/ya.make b/library/cpp/containers/disjoint_interval_tree/ut/ya.make index 6736ce0c2b..0885923e71 100644 --- a/library/cpp/containers/disjoint_interval_tree/ut/ya.make +++ b/library/cpp/containers/disjoint_interval_tree/ut/ya.make @@ -1,12 +1,12 @@ -UNITTEST_FOR(library/cpp/containers/disjoint_interval_tree) - -OWNER( - dcherednik - galaxycrab -) - -SRCS( - disjoint_interval_tree_ut.cpp -) - -END() +UNITTEST_FOR(library/cpp/containers/disjoint_interval_tree) + +OWNER( + dcherednik + galaxycrab +) + +SRCS( + disjoint_interval_tree_ut.cpp +) + +END() diff --git a/library/cpp/containers/disjoint_interval_tree/ya.make b/library/cpp/containers/disjoint_interval_tree/ya.make index b4f5a52a67..cafad0281e 100644 --- a/library/cpp/containers/disjoint_interval_tree/ya.make +++ b/library/cpp/containers/disjoint_interval_tree/ya.make @@ -1,10 +1,10 @@ -OWNER( - dcherednik - galaxycrab -) - -LIBRARY() - -SRCS(disjoint_interval_tree.cpp) - -END() +OWNER( + dcherednik + galaxycrab +) + +LIBRARY() + +SRCS(disjoint_interval_tree.cpp) + +END() diff --git a/library/cpp/containers/ring_buffer/ring_buffer.h b/library/cpp/containers/ring_buffer/ring_buffer.h index 41220dcf6b..c9f0acf7c2 100644 --- a/library/cpp/containers/ring_buffer/ring_buffer.h +++ b/library/cpp/containers/ring_buffer/ring_buffer.h @@ -12,12 +12,12 @@ public: Items.reserve(MaxSize); } - TSimpleRingBuffer(const TSimpleRingBuffer&) = default; - TSimpleRingBuffer(TSimpleRingBuffer&&) = default; - - TSimpleRingBuffer& operator=(const TSimpleRingBuffer&) = default; - TSimpleRingBuffer& operator=(TSimpleRingBuffer&&) = default; - + TSimpleRingBuffer(const TSimpleRingBuffer&) = default; + TSimpleRingBuffer(TSimpleRingBuffer&&) = default; + + TSimpleRingBuffer& operator=(const TSimpleRingBuffer&) = default; + TSimpleRingBuffer& operator=(TSimpleRingBuffer&&) = default; + // First available item size_t FirstIndex() const { return Begin; @@ -55,11 +55,11 @@ public: } } - void Clear() { - Items.clear(); - Begin = 0; - } - + void Clear() { + Items.clear(); + Begin = 0; + } + private: size_t RealIndex(size_t index) const { return index % MaxSize; diff --git a/library/cpp/containers/ya.make b/library/cpp/containers/ya.make index 4b1b315e6a..6ca17c9c2f 100644 --- a/library/cpp/containers/ya.make +++ b/library/cpp/containers/ya.make @@ -20,8 +20,8 @@ RECURSE( dense_hash/ut dictionary dictionary/ut - disjoint_interval_tree - disjoint_interval_tree/ut + disjoint_interval_tree + disjoint_interval_tree/ut ext_priority_queue ext_priority_queue/ut fast_trie diff --git a/library/cpp/digest/md5/md5_ut.cpp b/library/cpp/digest/md5/md5_ut.cpp index 1c3e4ad0a9..0833ad5250 100644 --- a/library/cpp/digest/md5/md5_ut.cpp +++ b/library/cpp/digest/md5/md5_ut.cpp @@ -44,7 +44,7 @@ Y_UNIT_TEST_SUITE(TMD5Test) { fileHash = MD5::File(tmpFile); UNIT_ASSERT_NO_DIFF(fileHash, memoryHash); - NFs::Remove(tmpFile); + NFs::Remove(tmpFile); fileHash = MD5::File(tmpFile); UNIT_ASSERT_EQUAL(fileHash.size(), 0); } diff --git a/library/cpp/getopt/small/last_getopt_opts.cpp b/library/cpp/getopt/small/last_getopt_opts.cpp index 03c432849f..dfa528b996 100644 --- a/library/cpp/getopt/small/last_getopt_opts.cpp +++ b/library/cpp/getopt/small/last_getopt_opts.cpp @@ -4,7 +4,7 @@ #include "last_getopt_parser.h" #include <library/cpp/colorizer/colors.h> - + #include <util/stream/format.h> #include <util/charset/utf8.h> @@ -62,7 +62,7 @@ namespace NLastGetopt { return opt->ToShortString(); } } - + TOpts::TOpts(const TStringBuf& optstring) : ArgPermutation_(DEFAULT_ARG_PERMUTATION) , AllowSingleDashForLong_(false) @@ -392,14 +392,14 @@ namespace NLastGetopt { if (requiredOptionsCount == 0) continue; os << Endl << colors.BoldColor() << "Required parameters" << colors.OldColor() << ":" << Endl; - } else { + } else { if (requiredOptionsCount == Opts_.size()) continue; if (requiredOptionsCount == 0) os << Endl << colors.BoldColor() << "Options" << colors.OldColor() << ":" << Endl; else os << Endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << Endl; // optional options would be a tautology - } + } for (size_t i = 0; i < Opts_.size(); i++) { const TOpt* opt = Opts_[i].Get(); @@ -469,7 +469,7 @@ namespace NLastGetopt { void TOpts::PrintUsage(const TStringBuf& program, IOutputStream& os) const { PrintUsage(program, os, NColorizer::AutoColors(os)); } - + void TOpts::PrintFreeArgsDesc(IOutputStream& os, const NColorizer::TColors& colors) const { if (0 == FreeArgsMax_) return; diff --git a/library/cpp/getopt/small/last_getopt_opts.h b/library/cpp/getopt/small/last_getopt_opts.h index 825b99c871..74a8e67237 100644 --- a/library/cpp/getopt/small/last_getopt_opts.h +++ b/library/cpp/getopt/small/last_getopt_opts.h @@ -3,7 +3,7 @@ #include "last_getopt_opt.h" #include <library/cpp/colorizer/fwd.h> - + #include <util/generic/map.h> namespace NLastGetopt { @@ -594,7 +594,7 @@ namespace NLastGetopt { * @param colors colorizer */ void PrintUsage(const TStringBuf& program, IOutputStream& os, const NColorizer::TColors& colors) const; - + /** * Print usage string * diff --git a/library/cpp/getopt/small/last_getopt_parser.cpp b/library/cpp/getopt/small/last_getopt_parser.cpp index 7668b12a03..e6c90aaacd 100644 --- a/library/cpp/getopt/small/last_getopt_parser.cpp +++ b/library/cpp/getopt/small/last_getopt_parser.cpp @@ -1,7 +1,7 @@ #include "last_getopt_parser.h" #include <library/cpp/colorizer/colors.h> - + #include <util/string/escape.h> namespace NLastGetopt { @@ -385,5 +385,5 @@ namespace NLastGetopt { void TOptsParser::PrintUsage(IOutputStream& os) const { PrintUsage(os, NColorizer::AutoColors(os)); } - -} + +} diff --git a/library/cpp/getopt/small/last_getopt_parser.h b/library/cpp/getopt/small/last_getopt_parser.h index 2cf8a6c308..8a38b3ae63 100644 --- a/library/cpp/getopt/small/last_getopt_parser.h +++ b/library/cpp/getopt/small/last_getopt_parser.h @@ -3,7 +3,7 @@ #include "last_getopt_opts.h" #include <library/cpp/colorizer/fwd.h> - + #include <util/generic/hash_set.h> #include <util/generic/list.h> @@ -146,9 +146,9 @@ namespace NLastGetopt { const TString& ProgramName() const { return ProgramName_; } - + void PrintUsage(IOutputStream& os = Cout) const; - + void PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const; }; } //namespace NLastGetopt diff --git a/library/cpp/getopt/small/modchooser.cpp b/library/cpp/getopt/small/modchooser.cpp index 2fa5cfd070..b84fd37c9a 100644 --- a/library/cpp/getopt/small/modchooser.cpp +++ b/library/cpp/getopt/small/modchooser.cpp @@ -5,7 +5,7 @@ #include "modchooser.h" #include <library/cpp/colorizer/colors.h> - + #include <util/stream/output.h> #include <util/stream/format.h> #include <util/generic/yexception.h> @@ -279,9 +279,9 @@ TString TModChooser::TMode::FormatFullName(size_t pad) const { void TModChooser::PrintHelp(const TString& progName) const { Cerr << Description << Endl << Endl; - Cerr << NColorizer::StdErr().BoldColor() << "Usage" << NColorizer::StdErr().OldColor() << ": " << progName << " MODE [MODE_OPTIONS]" << Endl; + Cerr << NColorizer::StdErr().BoldColor() << "Usage" << NColorizer::StdErr().OldColor() << ": " << progName << " MODE [MODE_OPTIONS]" << Endl; Cerr << Endl; - Cerr << NColorizer::StdErr().BoldColor() << "Modes" << NColorizer::StdErr().OldColor() << ":" << Endl; + Cerr << NColorizer::StdErr().BoldColor() << "Modes" << NColorizer::StdErr().OldColor() << ":" << Endl; size_t maxModeLen = 0; for (const auto& [name, mode] : Modes) { if (name != mode->Name) diff --git a/library/cpp/getopt/small/ya.make b/library/cpp/getopt/small/ya.make index 96de0f04b1..e3b8126a58 100644 --- a/library/cpp/getopt/small/ya.make +++ b/library/cpp/getopt/small/ya.make @@ -2,9 +2,9 @@ LIBRARY() OWNER(pg) -PEERDIR( +PEERDIR( library/cpp/colorizer -) +) SRCS( completer.cpp diff --git a/library/cpp/getopt/ut/last_getopt_ut.cpp b/library/cpp/getopt/ut/last_getopt_ut.cpp index c99a1d053d..6eb0bd5fca 100644 --- a/library/cpp/getopt/ut/last_getopt_ut.cpp +++ b/library/cpp/getopt/ut/last_getopt_ut.cpp @@ -4,10 +4,10 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/generic/array_size.h> -#include <util/string/subst.h> -#include <util/string/vector.h> +#include <util/string/subst.h> +#include <util/string/vector.h> #include <util/string/split.h> - + using namespace NLastGetopt; namespace { @@ -605,101 +605,101 @@ Y_UNIT_TEST_SUITE(TLastGetoptTests) { } Y_UNIT_TEST(TestColorPrint) { - TOpts opts; - const char* prog = "my_program"; - opts.AddLongOption("long_option").Required(); - opts.AddLongOption('o', "other"); - opts.AddCharOption('d').DefaultValue("42"); - opts.AddCharOption('s').DefaultValue("str_default"); - opts.SetFreeArgsNum(123, 456); + TOpts opts; + const char* prog = "my_program"; + opts.AddLongOption("long_option").Required(); + opts.AddLongOption('o', "other"); + opts.AddCharOption('d').DefaultValue("42"); + opts.AddCharOption('s').DefaultValue("str_default"); + opts.SetFreeArgsNum(123, 456); opts.SetFreeArgTitle(0, "first_free_arg", "help"); opts.SetFreeArgTitle(2, "second_free_arg"); opts.AddSection("Section", "Section\n text"); const char* cmd[] = {prog}; - TOptsParser parser(&opts, Y_ARRAY_SIZE(cmd), cmd); - TStringStream out; - NColorizer::TColors colors(true); - parser.PrintUsage(out, colors); - - // find options and green color + TOptsParser parser(&opts, Y_ARRAY_SIZE(cmd), cmd); + TStringStream out; + NColorizer::TColors colors(true); + parser.PrintUsage(out, colors); + + // find options and green color UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "--long_option" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "--other" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "-o" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "-d" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "-s" << colors.OldColor()) != TString::npos); - - // find default values + + // find default values UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.CyanColor() << "42" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.CyanColor() << "\"str_default\"" << colors.OldColor()) != TString::npos); - - // find free args + + // find free args UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "123" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "456" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "first_free_arg" << colors.OldColor()) != TString::npos); // free args without help not rendered even if they have custom title UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.GreenColor() << "second_free_arg" << colors.OldColor()) == TString::npos); - - // find signatures + + // find signatures UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.BoldColor() << "Usage" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.BoldColor() << "Required parameters" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.BoldColor() << "Optional parameters" << colors.OldColor()) != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.BoldColor() << "Free args" << colors.OldColor()) != TString::npos); - + // find sections UNIT_ASSERT(out.Str().find(TStringBuilder() << colors.BoldColor() << "Section" << colors.OldColor() << ":") != TString::npos); UNIT_ASSERT(out.Str().find(TStringBuilder() << " Section\n text") != TString::npos); - // print without colors - TStringStream out2; - opts.PrintUsage(prog, out2); + // print without colors + TStringStream out2; + opts.PrintUsage(prog, out2); UNIT_ASSERT(out2.Str().find(colors.GreenColor()) == TString::npos); UNIT_ASSERT(out2.Str().find(colors.CyanColor()) == TString::npos); UNIT_ASSERT(out2.Str().find(colors.BoldColor()) == TString::npos); UNIT_ASSERT(out2.Str().find(colors.OldColor()) == TString::npos); - } - + } + Y_UNIT_TEST(TestPadding) { - const bool withColorsOpt[] = {false, true}; - for (bool withColors : withColorsOpt) { - TOpts opts; - const char* prog = "my_program"; + const bool withColorsOpt[] = {false, true}; + for (bool withColors : withColorsOpt) { + TOpts opts; + const char* prog = "my_program"; opts.AddLongOption("option", "description 1").Required(); // long option opts.AddLongOption('o', "other", "description 2"); // char and long option opts.AddCharOption('d', "description 3").RequiredArgument("DD"); // char option - opts.AddCharOption('s', "description 4\ndescription 5\ndescription 6"); // multiline desc - opts.AddLongOption('l', "very_very_very_loooong_ooooption", "description 7").RequiredArgument("LONG_ARGUMENT"); + opts.AddCharOption('s', "description 4\ndescription 5\ndescription 6"); // multiline desc + opts.AddLongOption('l', "very_very_very_loooong_ooooption", "description 7").RequiredArgument("LONG_ARGUMENT"); const char* cmd[] = {prog}; - TOptsParser parser(&opts, Y_ARRAY_SIZE(cmd), cmd); - - TStringStream out; - NColorizer::TColors colors(withColors); - parser.PrintUsage(out, colors); - + TOptsParser parser(&opts, Y_ARRAY_SIZE(cmd), cmd); + + TStringStream out; + NColorizer::TColors colors(withColors); + parser.PrintUsage(out, colors); + TString printed = out.Str(); - if (withColors) { - // remove not printable characters + if (withColors) { + // remove not printable characters SubstGlobal(printed, TString(colors.BoldColor()), ""); SubstGlobal(printed, TString(colors.GreenColor()), ""); SubstGlobal(printed, TString(colors.CyanColor()), ""); SubstGlobal(printed, TString(colors.OldColor()), ""); - } + } TVector<TString> lines; StringSplitter(printed).Split('\n').SkipEmpty().Collect(&lines); - UNIT_ASSERT(!lines.empty()); + UNIT_ASSERT(!lines.empty()); TVector<size_t> indents; for (const TString& line : lines) { - const size_t indent = line.find("description "); + const size_t indent = line.find("description "); if (indent != TString::npos) - indents.push_back(indent); - } - UNIT_ASSERT_VALUES_EQUAL(indents.size(), 7); - const size_t theOnlyIndent = indents[0]; - for (size_t indent : indents) { - UNIT_ASSERT_VALUES_EQUAL_C(indent, theOnlyIndent, printed); - } - } - } - + indents.push_back(indent); + } + UNIT_ASSERT_VALUES_EQUAL(indents.size(), 7); + const size_t theOnlyIndent = indents[0]; + for (size_t indent : indents) { + UNIT_ASSERT_VALUES_EQUAL_C(indent, theOnlyIndent, printed); + } + } + } + Y_UNIT_TEST(TestAppendTo) { TVector<int> ints; diff --git a/library/cpp/grpc/client/grpc_client_low.h b/library/cpp/grpc/client/grpc_client_low.h index ab0a0627be..8fbd976414 100644 --- a/library/cpp/grpc/client/grpc_client_low.h +++ b/library/cpp/grpc/client/grpc_client_low.h @@ -45,7 +45,7 @@ class IQueueClientEvent { public: virtual ~IQueueClientEvent() = default; - //! Execute an action defined by implementation + //! Execute an action defined by implementation virtual bool Execute(bool ok) = 0; //! Finish and destroy event diff --git a/library/cpp/grpc/server/grpc_server.h b/library/cpp/grpc/server/grpc_server.h index d6814a90a0..93187f70be 100644 --- a/library/cpp/grpc/server/grpc_server.h +++ b/library/cpp/grpc/server/grpc_server.h @@ -105,7 +105,7 @@ class IQueueEvent { public: virtual ~IQueueEvent() = default; - //! Execute an action defined by implementation. + //! Execute an action defined by implementation. virtual bool Execute(bool ok) = 0; //! It is time to perform action requested by AcquireToken server method. It will be called under lock which is also diff --git a/library/cpp/json/json_writer.h b/library/cpp/json/json_writer.h index c7f5c9499a..5da559ac35 100644 --- a/library/cpp/json/json_writer.h +++ b/library/cpp/json/json_writer.h @@ -5,7 +5,7 @@ #include "json_value.h" #include <library/cpp/json/writer/json.h> - + #include <util/stream/output.h> #include <util/generic/hash.h> #include <util/generic/maybe.h> @@ -133,7 +133,7 @@ namespace NJson { Buf.WriteKey(key); Write(value); } - + // write raw json without checks void UnsafeWrite(const TStringBuf& key, const TStringBuf& value) { Buf.WriteKey(key); @@ -144,7 +144,7 @@ namespace NJson { Buf.WriteKey(key); WriteNull(); } - + template <typename T> void WriteOptional(const TStringBuf& key, const TMaybe<T>& value) { if (value) { diff --git a/library/cpp/json/writer/json.cpp b/library/cpp/json/writer/json.cpp index 02370c2d79..d4e5eaf6b3 100644 --- a/library/cpp/json/writer/json.cpp +++ b/library/cpp/json/writer/json.cpp @@ -258,16 +258,16 @@ namespace NJsonWriter { } else { ythrow TError() << "JSON writer: invalid float value: " << FloatToString(f); } - } + } size_t len = FloatToString(f, buf, Y_ARRAY_SIZE(buf), mode, ndigits); UnsafeWriteValue(buf, len); return TValueContext(*this); - } + } TValueContext TBuf::WriteFloat(float f, EFloatToStringMode mode, int ndigits) { return WriteFloatImpl(f, mode, ndigits); } - + TValueContext TBuf::WriteDouble(double f, EFloatToStringMode mode, int ndigits) { return WriteFloatImpl(f, mode, ndigits); } diff --git a/library/cpp/json/writer/json.h b/library/cpp/json/writer/json.h index 0aae2531b9..57770fce8f 100644 --- a/library/cpp/json/writer/json.h +++ b/library/cpp/json/writer/json.h @@ -77,14 +77,14 @@ namespace NJsonWriter { return *this; } - /*** NaN and Inf are not valid json values, - * so if WriteNanAsString is set, writer would write string - * intead of throwing exception (default case) */ + /*** NaN and Inf are not valid json values, + * so if WriteNanAsString is set, writer would write string + * intead of throwing exception (default case) */ TBuf& SetWriteNanAsString(bool writeNanAsString = true) { - WriteNanAsString = writeNanAsString; + WriteNanAsString = writeNanAsString; return *this; - } - + } + /*** Return the string formed in the internal TStringStream. * You may only call it if the `stream' parameter was NULL * at construction time. */ @@ -141,10 +141,10 @@ namespace NJsonWriter { void CheckAndPop(EJsonEntity e); EJsonEntity StackTop() const; - template <class TFloat> - TValueContext WriteFloatImpl(TFloat f, EFloatToStringMode mode, int ndigits); + template <class TFloat> + TValueContext WriteFloatImpl(TFloat f, EFloatToStringMode mode, int ndigits); - private: + private: IOutputStream* Stream; THolder<TStringStream> StringStream; typedef TVector<const TString*> TKeys; @@ -155,7 +155,7 @@ namespace NJsonWriter { bool NeedNewline; const EHtmlEscapeMode EscapeMode; int IndentSpaces; - bool WriteNanAsString; + bool WriteNanAsString; }; // Please don't try to instantiate the classes declared below this point. diff --git a/library/cpp/json/writer/json_ut.cpp b/library/cpp/json/writer/json_ut.cpp index 9980555683..de6df4e941 100644 --- a/library/cpp/json/writer/json_ut.cpp +++ b/library/cpp/json/writer/json_ut.cpp @@ -4,8 +4,8 @@ #include "json.h" #include <library/cpp/json/json_value.h> -#include <limits> - +#include <limits> + Y_UNIT_TEST_SUITE(JsonWriter) { Y_UNIT_TEST(Struct) { NJsonWriter::TBuf w; @@ -205,53 +205,53 @@ Y_UNIT_TEST_SUITE(JsonWriter) { const char exp[] = "[0.123457,0.1234567899,0.316,244.1,10385.83,{\"1\":1112,\"2\":1e+03}]"; UNIT_ASSERT_STRINGS_EQUAL(exp, buf.Str()); } - + Y_UNIT_TEST(NanFormatting) { - { - NJsonWriter::TBuf buf; - buf.BeginObject(); - buf.WriteKey("nanvalue"); - UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::quiet_NaN()), yexception); - } - - { - NJsonWriter::TBuf buf; - buf.BeginObject(); - buf.WriteKey("infvalue"); - UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::infinity()), yexception); - } - - { - NJsonWriter::TBuf buf; - buf.BeginList(); - UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::quiet_NaN()), yexception); - } - - { - NJsonWriter::TBuf buf; - buf.BeginList(); - UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::infinity()), yexception); - } - - { - NJsonWriter::TBuf buf; - buf.SetWriteNanAsString(); - - buf.BeginObject() + { + NJsonWriter::TBuf buf; + buf.BeginObject(); + buf.WriteKey("nanvalue"); + UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::quiet_NaN()), yexception); + } + + { + NJsonWriter::TBuf buf; + buf.BeginObject(); + buf.WriteKey("infvalue"); + UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::infinity()), yexception); + } + + { + NJsonWriter::TBuf buf; + buf.BeginList(); + UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::quiet_NaN()), yexception); + } + + { + NJsonWriter::TBuf buf; + buf.BeginList(); + UNIT_ASSERT_EXCEPTION(buf.WriteFloat(std::numeric_limits<double>::infinity()), yexception); + } + + { + NJsonWriter::TBuf buf; + buf.SetWriteNanAsString(); + + buf.BeginObject() .WriteKey("nanvalue") .WriteFloat(std::numeric_limits<double>::quiet_NaN()) .WriteKey("infvalue") .WriteFloat(std::numeric_limits<double>::infinity()) .WriteKey("minus_infvalue") .WriteFloat(-std::numeric_limits<float>::infinity()) - .WriteKey("l") + .WriteKey("l") .BeginList() .WriteFloat(std::numeric_limits<float>::quiet_NaN()) .EndList() .EndObject(); - - UNIT_ASSERT_STRINGS_EQUAL(buf.Str(), R"raw_json({"nanvalue":"nan","infvalue":"inf","minus_infvalue":"-inf","l":["nan"]})raw_json"); - } + + UNIT_ASSERT_STRINGS_EQUAL(buf.Str(), R"raw_json({"nanvalue":"nan","infvalue":"inf","minus_infvalue":"-inf","l":["nan"]})raw_json"); + } { NJsonWriter::TBuf buf; @@ -264,7 +264,7 @@ Y_UNIT_TEST_SUITE(JsonWriter) { UNIT_ASSERT_STRINGS_EQUAL(buf.Str(), R"({"\u003C\u003E&":"Ololo","<>&":"Ololo2"})"); } - } + } Y_UNIT_TEST(WriteUninitializedBoolDoesntCrashProgram) { // makes sense only in release build w/ address sanitizer diff --git a/library/cpp/lfalloc/lf_allocX64.h b/library/cpp/lfalloc/lf_allocX64.h index fd2a906d6f..af3cc1730a 100644 --- a/library/cpp/lfalloc/lf_allocX64.h +++ b/library/cpp/lfalloc/lf_allocX64.h @@ -115,7 +115,7 @@ static inline long AtomicSub(TAtomic& a, long b) { #ifndef _darwin_ -#ifndef Y_ARRAY_SIZE +#ifndef Y_ARRAY_SIZE #define Y_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #endif diff --git a/library/cpp/lwtrace/event.h b/library/cpp/lwtrace/event.h index e53a620c45..48fdc6b85a 100644 --- a/library/cpp/lwtrace/event.h +++ b/library/cpp/lwtrace/event.h @@ -2,7 +2,7 @@ #include "preprocessor.h" #include "signature.h" -#include "param_traits.h" +#include "param_traits.h" #include <library/cpp/lwtrace/protos/lwtrace.pb.h> diff --git a/library/cpp/lwtrace/param_traits.h b/library/cpp/lwtrace/param_traits.h index a0faeb50db..8ff87efd0b 100644 --- a/library/cpp/lwtrace/param_traits.h +++ b/library/cpp/lwtrace/param_traits.h @@ -1,66 +1,66 @@ -#pragma once -#include "signature.h" - -#include <util/datetime/base.h> -#include <util/string/builder.h> - -#include <limits> - -#ifndef LWTRACE_DISABLE - -namespace NLWTrace { - - template <> - struct TParamTraits<TInstant> { - using TStoreType = double; - using TFuncParam = TInstant; - - inline static void ToString(TStoreType value, TString* out) { - *out = TParamConv<TStoreType>::ToString(value); - } - - inline static TStoreType ToStoreType(TInstant value) { - if (value == TInstant::Max()) { - return std::numeric_limits<TStoreType>::infinity(); - } else { - return static_cast<TStoreType>(value.MicroSeconds()) / 1000000.0; // seconds count - } - } - }; - - template <> - struct TParamTraits<TDuration> { - using TStoreType = double; - using TFuncParam = TDuration; - - inline static void ToString(TStoreType value, TString* out) { - *out = TParamConv<TStoreType>::ToString(value); - } - - inline static TStoreType ToStoreType(TDuration value) { - if (value == TDuration::Max()) { - return std::numeric_limits<TStoreType>::infinity(); - } else { - return static_cast<TStoreType>(value.MicroSeconds()) / 1000.0; // milliseconds count - } - } - }; - - // Param for enum with GENERATE_ENUM_SERIALIZATION enabled or operator<< implemented - template <class TEnum> - struct TEnumParamWithSerialization { - using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType; - using TFuncParam = TEnum; - - inline static void ToString(TStoreType stored, TString* out) { - *out = TStringBuilder() << static_cast<TEnum>(stored) << " (" << stored << ")"; - } - - inline static TStoreType ToStoreType(TFuncParam v) { - return static_cast<TStoreType>(v); - } - }; - -} // namespace NLWTrace - -#endif // LWTRACE_DISABLE +#pragma once +#include "signature.h" + +#include <util/datetime/base.h> +#include <util/string/builder.h> + +#include <limits> + +#ifndef LWTRACE_DISABLE + +namespace NLWTrace { + + template <> + struct TParamTraits<TInstant> { + using TStoreType = double; + using TFuncParam = TInstant; + + inline static void ToString(TStoreType value, TString* out) { + *out = TParamConv<TStoreType>::ToString(value); + } + + inline static TStoreType ToStoreType(TInstant value) { + if (value == TInstant::Max()) { + return std::numeric_limits<TStoreType>::infinity(); + } else { + return static_cast<TStoreType>(value.MicroSeconds()) / 1000000.0; // seconds count + } + } + }; + + template <> + struct TParamTraits<TDuration> { + using TStoreType = double; + using TFuncParam = TDuration; + + inline static void ToString(TStoreType value, TString* out) { + *out = TParamConv<TStoreType>::ToString(value); + } + + inline static TStoreType ToStoreType(TDuration value) { + if (value == TDuration::Max()) { + return std::numeric_limits<TStoreType>::infinity(); + } else { + return static_cast<TStoreType>(value.MicroSeconds()) / 1000.0; // milliseconds count + } + } + }; + + // Param for enum with GENERATE_ENUM_SERIALIZATION enabled or operator<< implemented + template <class TEnum> + struct TEnumParamWithSerialization { + using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType; + using TFuncParam = TEnum; + + inline static void ToString(TStoreType stored, TString* out) { + *out = TStringBuilder() << static_cast<TEnum>(stored) << " (" << stored << ")"; + } + + inline static TStoreType ToStoreType(TFuncParam v) { + return static_cast<TStoreType>(v); + } + }; + +} // namespace NLWTrace + +#endif // LWTRACE_DISABLE diff --git a/library/cpp/lwtrace/preprocessor.h b/library/cpp/lwtrace/preprocessor.h index 40865467b2..205891fd5e 100644 --- a/library/cpp/lwtrace/preprocessor.h +++ b/library/cpp/lwtrace/preprocessor.h @@ -57,7 +57,7 @@ /**/ // Use for code generation to handle parameter lists -// NOTE: this is the only place to change if more parameters needed +// NOTE: this is the only place to change if more parameters needed #define FOREACH_PARAMNUM(MACRO, ...) \ MACRO(0, ##__VA_ARGS__) \ MACRO(1, ##__VA_ARGS__) \ @@ -114,9 +114,9 @@ #define LWTRACE_TEMPLATE_PARAMS_NODEF LWTRACE_EXPAND(LWTRACE_EAT FOREACH_PARAMNUM(LWTRACE_TEMPLATE_PARAMS_NODEF_I)(0)) #define LWTRACE_TEMPLATE_ARGS_I(i) (1) TP##i LWTRACE_COMMA #define LWTRACE_TEMPLATE_ARGS LWTRACE_EXPAND(LWTRACE_EAT FOREACH_PARAMNUM(LWTRACE_TEMPLATE_ARGS_I)(0)) -#define LWTRACE_FUNCTION_PARAMS_I(i) (1) typename ::NLWTrace::TParamTraits<TP##i>::TFuncParam p##i = ERROR_not_enough_parameters() LWTRACE_COMMA +#define LWTRACE_FUNCTION_PARAMS_I(i) (1) typename ::NLWTrace::TParamTraits<TP##i>::TFuncParam p##i = ERROR_not_enough_parameters() LWTRACE_COMMA #define LWTRACE_FUNCTION_PARAMS LWTRACE_EXPAND(LWTRACE_EAT FOREACH_PARAMNUM(LWTRACE_FUNCTION_PARAMS_I)(0)) -#define LWTRACE_PREPARE_PARAMS_I(i, params) params.Param[i].template CopyConstruct<typename ::NLWTrace::TParamTraits<TP##i>::TStoreType>(::NLWTrace::TParamTraits<TP##i>::ToStoreType(p##i)); +#define LWTRACE_PREPARE_PARAMS_I(i, params) params.Param[i].template CopyConstruct<typename ::NLWTrace::TParamTraits<TP##i>::TStoreType>(::NLWTrace::TParamTraits<TP##i>::ToStoreType(p##i)); #define LWTRACE_PREPARE_PARAMS(params) \ do { \ FOREACH_PARAMNUM(LWTRACE_PREPARE_PARAMS_I, params) \ diff --git a/library/cpp/lwtrace/probe.h b/library/cpp/lwtrace/probe.h index 31fa282da3..d4be4c4f4d 100644 --- a/library/cpp/lwtrace/probe.h +++ b/library/cpp/lwtrace/probe.h @@ -176,7 +176,7 @@ namespace NLWTrace { inline void PreparePtr<TNil>(const TNil&, const TNil*&) { } -#define LWTRACE_SCOPED_FUNCTION_PARAMS_I(i) (1) typename ::NLWTrace::TParamTraits<TP##i>::TFuncParam p##i = ERROR_not_enough_parameters() LWTRACE_COMMA +#define LWTRACE_SCOPED_FUNCTION_PARAMS_I(i) (1) typename ::NLWTrace::TParamTraits<TP##i>::TFuncParam p##i = ERROR_not_enough_parameters() LWTRACE_COMMA #define LWTRACE_SCOPED_FUNCTION_PARAMS LWTRACE_EXPAND(LWTRACE_EAT FOREACH_PARAMNUM(LWTRACE_SCOPED_FUNCTION_PARAMS_I)(0)) #define LWTRACE_SCOPED_FUNCTION_PARAMS_BY_REF_I(i) (1) typename ::NLWTrace::TParamTraits<TP##i>::TStoreType& p##i = *(ERROR_not_enough_parameters*)(HidePointerOrigin(nullptr))LWTRACE_COMMA #define LWTRACE_SCOPED_FUNCTION_PARAMS_BY_REF LWTRACE_EXPAND(LWTRACE_EAT FOREACH_PARAMNUM(LWTRACE_SCOPED_FUNCTION_PARAMS_BY_REF_I)(0)) diff --git a/library/cpp/lwtrace/signature.h b/library/cpp/lwtrace/signature.h index 868bd9bcf2..aaf2e95bc9 100644 --- a/library/cpp/lwtrace/signature.h +++ b/library/cpp/lwtrace/signature.h @@ -7,17 +7,17 @@ #include <util/generic/cast.h> #include <util/generic/string.h> #include <util/generic/typetraits.h> -#include <util/string/builder.h> +#include <util/string/builder.h> #include <util/string/cast.h> #include <util/string/printf.h> #include <google/protobuf/descriptor.h> #include <google/protobuf/generated_enum_reflection.h> - + #include <library/cpp/lwtrace/protos/lwtrace.pb.h> -#include <type_traits> - +#include <type_traits> + namespace NLWTrace { // Class to hold parameter values parsed from trace query predicate operators template <class T> @@ -465,76 +465,76 @@ namespace NLWTrace { #undef FOREACH_PARAMTYPE_MACRO template <class T> - struct TParamTraits; - - // Enum types traits impl. - template <class TEnum, class = std::enable_if_t<std::is_enum_v<TEnum>>> - struct TEnumParamTraitsImpl { - using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType; - using TFuncParam = TEnum; - + struct TParamTraits; + + // Enum types traits impl. + template <class TEnum, class = std::enable_if_t<std::is_enum_v<TEnum>>> + struct TEnumParamTraitsImpl { + using TStoreType = typename TParamTraits<std::underlying_type_t<TEnum>>::TStoreType; + using TFuncParam = TEnum; + inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { - if constexpr (google::protobuf::is_proto_enum<TEnum>::value) { - const google::protobuf::EnumValueDescriptor* valueDescriptor = google::protobuf::GetEnumDescriptor<TEnum>()->FindValueByNumber(stored); - if (valueDescriptor) { - *out = TStringBuilder() << valueDescriptor->name() << " (" << stored << ")"; - } else { - *out = TParamConv<TStoreType>::ToString(stored); - } - } else { - *out = TParamConv<TStoreType>::ToString(stored); - } - } - - inline static TStoreType ToStoreType(TFuncParam v) { - return static_cast<TStoreType>(v); - } + if constexpr (google::protobuf::is_proto_enum<TEnum>::value) { + const google::protobuf::EnumValueDescriptor* valueDescriptor = google::protobuf::GetEnumDescriptor<TEnum>()->FindValueByNumber(stored); + if (valueDescriptor) { + *out = TStringBuilder() << valueDescriptor->name() << " (" << stored << ")"; + } else { + *out = TParamConv<TStoreType>::ToString(stored); + } + } else { + *out = TParamConv<TStoreType>::ToString(stored); + } + } + + inline static TStoreType ToStoreType(TFuncParam v) { + return static_cast<TStoreType>(v); + } }; - template <class TCustomType> - struct TCustomTraitsImpl { - using TStoreType = typename TParamTraits<typename TCustomType::TStoreType>::TStoreType; //see STORE_TYPE_AS - using TFuncParam = typename TCustomType::TFuncParam; - - inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { - TCustomType::ToString(stored, out); - } - - inline static TStoreType ToStoreType(TFuncParam v) { - return TCustomType::ToStoreType(v); - } - }; - - template <class T, bool isEnum> - struct TParamTraitsImpl; - - template <class TEnum> - struct TParamTraitsImpl<TEnum, true> : TEnumParamTraitsImpl<TEnum> { - }; - - template <class TCustomType> - struct TParamTraitsImpl<TCustomType, false> : TCustomTraitsImpl<TCustomType> { - }; - - template <class T> - struct TParamTraits : TParamTraitsImpl<T, std::is_enum_v<T>> { - }; - - // Standard stored types traits. - + template <class TCustomType> + struct TCustomTraitsImpl { + using TStoreType = typename TParamTraits<typename TCustomType::TStoreType>::TStoreType; //see STORE_TYPE_AS + using TFuncParam = typename TCustomType::TFuncParam; + + inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { + TCustomType::ToString(stored, out); + } + + inline static TStoreType ToStoreType(TFuncParam v) { + return TCustomType::ToStoreType(v); + } + }; + + template <class T, bool isEnum> + struct TParamTraitsImpl; + + template <class TEnum> + struct TParamTraitsImpl<TEnum, true> : TEnumParamTraitsImpl<TEnum> { + }; + + template <class TCustomType> + struct TParamTraitsImpl<TCustomType, false> : TCustomTraitsImpl<TCustomType> { + }; + + template <class T> + struct TParamTraits : TParamTraitsImpl<T, std::is_enum_v<T>> { + }; + + // Standard stored types traits. + #define STORE_TYPE_AS(input_t, store_as_t) \ template <> \ struct TParamTraits<input_t> { \ - using TStoreType = store_as_t; \ - using TFuncParam = typename TTypeTraits<input_t>::TFuncParam; \ - \ - inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { \ + using TStoreType = store_as_t; \ + using TFuncParam = typename TTypeTraits<input_t>::TFuncParam; \ + \ + inline static void ToString(typename TTypeTraits<TStoreType>::TFuncParam stored, TString* out) { \ *out = TParamConv<TStoreType>::ToString(stored); \ } \ - \ - inline static TStoreType ToStoreType(TFuncParam v) { \ - return v; \ - } \ + \ + inline static TStoreType ToStoreType(TFuncParam v) { \ + return v; \ + } \ }; \ /**/ STORE_TYPE_AS(ui8, ui64); @@ -550,18 +550,18 @@ namespace NLWTrace { #undef STORE_TYPE_AS #undef FOREACH_PARAMTYPE_MACRO - // Nil type staits. + // Nil type staits. template <> struct TParamTraits<TNil> { - using TStoreType = TNil; - using TFuncParam = TTypeTraits<TNil>::TFuncParam; - - inline static void ToString(typename TTypeTraits<TNil>::TFuncParam, TString*) { - } - - inline static TNil ToStoreType(TFuncParam v) { - return v; - } + using TStoreType = TNil; + using TFuncParam = TTypeTraits<TNil>::TFuncParam; + + inline static void ToString(typename TTypeTraits<TNil>::TFuncParam, TString*) { + } + + inline static TNil ToStoreType(TFuncParam v) { + return v; + } }; inline EParamTypePb ParamTypeToProtobuf(const char* paramType) { diff --git a/library/cpp/lwtrace/trace_ut.cpp b/library/cpp/lwtrace/trace_ut.cpp index cb03e4fbde..fac6869892 100644 --- a/library/cpp/lwtrace/trace_ut.cpp +++ b/library/cpp/lwtrace/trace_ut.cpp @@ -1,28 +1,28 @@ #include "all.h" #include <library/cpp/lwtrace/protos/lwtrace.pb.h> - + #include <library/cpp/testing/unittest/registar.h> #include <google/protobuf/text_format.h> -enum ESimpleEnum { - ValueA, - ValueB, -}; - -enum class EEnumClass { - ValueC, - ValueD, -}; - +enum ESimpleEnum { + ValueA, + ValueB, +}; + +enum class EEnumClass { + ValueC, + ValueD, +}; + #define LWTRACE_UT_PROVIDER(PROBE, EVENT, GROUPS, TYPES, NAMES) \ PROBE(NoParam, GROUPS("Group"), TYPES(), NAMES()) \ PROBE(IntParam, GROUPS("Group"), TYPES(ui32), NAMES("value")) \ PROBE(StringParam, GROUPS("Group"), TYPES(TString), NAMES("value")) \ PROBE(SymbolParam, GROUPS("Group"), TYPES(NLWTrace::TSymbol), NAMES("symbol")) \ PROBE(CheckParam, GROUPS("Group"), TYPES(NLWTrace::TCheck), NAMES("value")) \ - PROBE(EnumParams, GROUPS("Group"), TYPES(ESimpleEnum, EEnumClass), NAMES("simpleEnum", "enumClass")) \ + PROBE(EnumParams, GROUPS("Group"), TYPES(ESimpleEnum, EEnumClass), NAMES("simpleEnum", "enumClass")) \ PROBE(InstantParam, GROUPS("Group"), TYPES(TInstant), NAMES("value")) \ PROBE(DurationParam, GROUPS("Group"), TYPES(TDuration), NAMES("value")) \ PROBE(ProtoEnum, GROUPS("Group"), TYPES(NLWTrace::EOperatorType), NAMES("value")) \ @@ -61,13 +61,13 @@ Y_UNIT_TEST_SUITE(LWTraceTrace) { } } reader; mngr.ReadLog("Query1", reader); - - LWPROBE(EnumParams, ValueA, EEnumClass::ValueC); - LWPROBE(InstantParam, TInstant::Seconds(42)); - LWPROBE(DurationParam, TDuration::MilliSeconds(146)); + + LWPROBE(EnumParams, ValueA, EEnumClass::ValueC); + LWPROBE(InstantParam, TInstant::Seconds(42)); + LWPROBE(DurationParam, TDuration::MilliSeconds(146)); LWPROBE(ProtoEnum, OT_EQ); } - + Y_UNIT_TEST(Predicate) { TManager mngr(*Singleton<TProbeRegistry>(), true); TQuery q; @@ -542,13 +542,13 @@ Y_UNIT_TEST_SUITE(LWTraceTrace) { UNIT_ASSERT(t1.NanoSeconds() - t0.NanoSeconds() >= sleepTimeNs); } - Y_UNIT_TEST(ProtoEnumTraits) { + Y_UNIT_TEST(ProtoEnumTraits) { using TPbEnumTraits = TParamTraits<EOperatorType>; - TString str; + TString str; TPbEnumTraits::ToString(TPbEnumTraits::ToStoreType(OT_EQ), &str); UNIT_ASSERT_STRINGS_EQUAL(str, "OT_EQ (0)"); - } - + } + Y_UNIT_TEST(Track) { TManager mngr(*Singleton<TProbeRegistry>(), true); TQuery q; diff --git a/library/cpp/messagebus/message_status.h b/library/cpp/messagebus/message_status.h index e1878960b3..be274d4252 100644 --- a/library/cpp/messagebus/message_status.h +++ b/library/cpp/messagebus/message_status.h @@ -43,15 +43,15 @@ namespace NBus { // For lwtrace struct TMessageStatusField { typedef int TStoreType; - typedef int TFuncParam; - + typedef int TFuncParam; + static void ToString(int value, TString* out) { *out = GetMessageStatus((NBus::EMessageStatus)value); } - - static int ToStoreType(int value) { - return value; - } + + static int ToStoreType(int value) { + return value; + } }; } // ns diff --git a/library/cpp/messagebus/remote_connection.cpp b/library/cpp/messagebus/remote_connection.cpp index 22932569db..283b277d7b 100644 --- a/library/cpp/messagebus/remote_connection.cpp +++ b/library/cpp/messagebus/remote_connection.cpp @@ -67,7 +67,7 @@ namespace NBus { } TRemoteConnection::TWriterData::~TWriterData() { - Y_VERIFY(AtomicGet(Down)); + Y_VERIFY(AtomicGet(Down)); Y_VERIFY(SendQueue.Empty()); } @@ -119,7 +119,7 @@ namespace NBus { } TRemoteConnection::TReaderData::~TReaderData() { - Y_VERIFY(AtomicGet(Down)); + Y_VERIFY(AtomicGet(Down)); } void TRemoteConnection::Send(TNonDestroyingAutoPtr<TBusMessage> msg) { diff --git a/library/cpp/monlib/dynamic_counters/counters.cpp b/library/cpp/monlib/dynamic_counters/counters.cpp index 3635d87d0d..b6371eb5c7 100644 --- a/library/cpp/monlib/dynamic_counters/counters.cpp +++ b/library/cpp/monlib/dynamic_counters/counters.cpp @@ -19,10 +19,10 @@ namespace { return dynamic_cast<TExpiringCounter*>(ptr.Get()); } - TExpiringHistogramCounter* AsExpiringHistogramCounter(const TIntrusivePtr<TCountableBase>& ptr) { - return dynamic_cast<TExpiringHistogramCounter*>(ptr.Get()); - } - + TExpiringHistogramCounter* AsExpiringHistogramCounter(const TIntrusivePtr<TCountableBase>& ptr) { + return dynamic_cast<TExpiringHistogramCounter*>(ptr.Get()); + } + THistogramCounter* AsHistogram(const TIntrusivePtr<TCountableBase>& ptr) { return dynamic_cast<THistogramCounter*>(ptr.Get()); } @@ -38,10 +38,10 @@ namespace { THistogramPtr AsHistogramRef(const TIntrusivePtr<TCountableBase>& ptr) { return VerifyDynamicCast<THistogramCounter*>(ptr.Get()); } - - bool IsExpiringCounter(const TIntrusivePtr<TCountableBase>& ptr) { - return AsExpiringCounter(ptr) != nullptr || AsExpiringHistogramCounter(ptr) != nullptr; - } + + bool IsExpiringCounter(const TIntrusivePtr<TCountableBase>& ptr) { + return AsExpiringCounter(ptr) != nullptr || AsExpiringHistogramCounter(ptr) != nullptr; + } } static constexpr TStringBuf INDENT = " "; @@ -76,11 +76,11 @@ THistogramPtr TDynamicCounters::GetHistogram(const TString& value, IHistogramCol THistogramPtr TDynamicCounters::GetNamedHistogram(const TString& name, const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) { return AsHistogramRef(GetNamedCounterImpl<false, THistogramCounter>(name, value, std::move(collector), derivative, vis)); -} +} THistogramPtr TDynamicCounters::GetExpiringHistogram(const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) { return GetExpiringNamedHistogram("sensor", value, std::move(collector), derivative, vis); -} +} THistogramPtr TDynamicCounters::GetExpiringNamedHistogram(const TString& name, const TString& value, IHistogramCollectorPtr collector, bool derivative, EVisibility vis) { return AsHistogramRef(GetNamedCounterImpl<true, TExpiringHistogramCounter>(name, value, std::move(collector), derivative, vis)); @@ -265,7 +265,7 @@ void TDynamicCounters::RemoveExpired() const { TAtomicBase count = 0; for (auto it = Counters.begin(); it != Counters.end();) { - if (IsExpiringCounter(it->second) && it->second->RefCount() == 1) { + if (IsExpiringCounter(it->second) && it->second->RefCount() == 1) { it = Counters.erase(it); ++count; } else { @@ -275,29 +275,29 @@ void TDynamicCounters::RemoveExpired() const { AtomicSub(ExpiringCount, count); } - -template <bool expiring, class TCounterType, class... TArgs> -TDynamicCounters::TCountablePtr TDynamicCounters::GetNamedCounterImpl(const TString& name, const TString& value, TArgs&&... args) { + +template <bool expiring, class TCounterType, class... TArgs> +TDynamicCounters::TCountablePtr TDynamicCounters::GetNamedCounterImpl(const TString& name, const TString& value, TArgs&&... args) { { TReadGuard g(Lock); auto it = Counters.find({name, value}); if (it != Counters.end()) { return it->second; } - } - + } + auto g = LockForUpdate("GetNamedCounterImpl", name, value); const TChildId key(name, value); auto it = Counters.lower_bound(key); if (it == Counters.end() || it->first != key) { auto value = MakeIntrusive<TCounterType>(std::forward<TArgs>(args)...); it = Counters.emplace_hint(it, key, value); - if constexpr (expiring) { + if constexpr (expiring) { AtomicIncrement(ExpiringCount); - } - } - return it->second; -} + } + } + return it->second; +} template <class TCounterType> TDynamicCounters::TCountablePtr TDynamicCounters::FindNamedCounterImpl(const TString& name, const TString& value) const { diff --git a/library/cpp/monlib/dynamic_counters/counters.h b/library/cpp/monlib/dynamic_counters/counters.h index dc178cfbe0..c25ac18605 100644 --- a/library/cpp/monlib/dynamic_counters/counters.h +++ b/library/cpp/monlib/dynamic_counters/counters.h @@ -175,10 +175,10 @@ namespace NMonitoring { bool Derivative_; }; - struct TExpiringHistogramCounter: public THistogramCounter { - using THistogramCounter::THistogramCounter; - }; - + struct TExpiringHistogramCounter: public THistogramCounter { + using THistogramCounter::THistogramCounter; + }; + using THistogramPtr = TIntrusivePtr<THistogramCounter>; #ifdef _MSC_VER @@ -280,7 +280,7 @@ namespace NMonitoring { const TString& value, bool derivative = false, TCountableBase::EVisibility visibility = TCountableBase::EVisibility::Public); - + THistogramPtr GetHistogram( const TString& value, IHistogramCollectorPtr collector, @@ -364,8 +364,8 @@ namespace NMonitoring { void RegisterCountable(const TString& name, const TString& value, TCountablePtr countable); void RemoveExpired() const; - template <bool expiring, class TCounterType, class... TArgs> - TCountablePtr GetNamedCounterImpl(const TString& name, const TString& value, TArgs&&... args); + template <bool expiring, class TCounterType, class... TArgs> + TCountablePtr GetNamedCounterImpl(const TString& name, const TString& value, TArgs&&... args); template <class TCounterType> TCountablePtr FindNamedCounterImpl(const TString& name, const TString& value) const; diff --git a/library/cpp/monlib/dynamic_counters/counters_ut.cpp b/library/cpp/monlib/dynamic_counters/counters_ut.cpp index 3591037e0a..7d44e682a8 100644 --- a/library/cpp/monlib/dynamic_counters/counters_ut.cpp +++ b/library/cpp/monlib/dynamic_counters/counters_ut.cpp @@ -230,15 +230,15 @@ Y_UNIT_TEST_SUITE(TDynamicCountersTest) { { auto c = rootGroup->GetExpiringCounter("foo"); - auto h = rootGroup->GetExpiringHistogram("bar", ExplicitHistogram({1, 42})); - h->Collect(15); + auto h = rootGroup->GetExpiringHistogram("bar", ExplicitHistogram({1, 42})); + h->Collect(15); TStringStream ss; TCountersPrinter printer(&ss); rootGroup->Accept("root", "counters", printer); UNIT_ASSERT_STRINGS_EQUAL(ss.Str(), "root:counters {\n" - " sensor:bar = {1: 0, 42: 1, inf: 0}\n" + " sensor:bar = {1: 0, 42: 1, inf: 0}\n" " sensor:foo = 0\n" "}\n"); } diff --git a/library/cpp/packers/ut/packers_ut.cpp b/library/cpp/packers/ut/packers_ut.cpp index 18ce2150d1..53304471c7 100644 --- a/library/cpp/packers/ut/packers_ut.cpp +++ b/library/cpp/packers/ut/packers_ut.cpp @@ -4,7 +4,7 @@ #include <utility> #include <util/charset/wide.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/buffer.h> #include <util/generic/map.h> #include <util/generic/vector.h> @@ -15,13 +15,13 @@ #include <util/random/random.h> -#include <util/string/hex.h> - +#include <util/string/hex.h> + #include "packers.h" #include <array> -#include <iterator> - +#include <iterator> + class TPackersTest: public TTestBase { private: UNIT_TEST_SUITE(TPackersTest); diff --git a/library/cpp/protobuf/json/config.h b/library/cpp/protobuf/json/config.h index dc84fb4d5d..3985b917bc 100644 --- a/library/cpp/protobuf/json/config.h +++ b/library/cpp/protobuf/json/config.h @@ -1,19 +1,19 @@ -#pragma once - -#include "string_transform.h" +#pragma once + +#include "string_transform.h" #include "name_generator.h" - -#include <util/generic/vector.h> + +#include <util/generic/vector.h> #include <util/generic/yexception.h> - + #include <functional> -namespace NProtobufJson { +namespace NProtobufJson { struct TProto2JsonConfig { using TSelf = TProto2JsonConfig; - + bool FormatOutput = false; - + enum MissingKeyMode { // Skip missing keys MissingKeySkip = 0, @@ -32,10 +32,10 @@ namespace NProtobufJson { }; MissingKeyMode MissingSingleKeyMode = MissingKeySkip; MissingKeyMode MissingRepeatedKeyMode = MissingKeySkip; - + /// Add null value for missing fields (false by default). bool AddMissingFields = false; - + enum EnumValueMode { EnumNumber = 0, // default EnumName, @@ -44,7 +44,7 @@ namespace NProtobufJson { EnumFullNameLowerCase, }; EnumValueMode EnumMode = EnumNumber; - + enum FldNameMode { FieldNameOriginalCase = 0, // default FieldNameLowerCase, @@ -54,7 +54,7 @@ namespace NProtobufJson { FieldNameSnakeCaseDense // ABC -> abc, UserID -> user_id }; FldNameMode FieldNameMode = FieldNameOriginalCase; - + enum ExtFldNameMode { ExtFldNameFull = 0, // default, field.full_name() ExtFldNameShort // field.name() @@ -64,14 +64,14 @@ namespace NProtobufJson { /// Use 'json_name' protobuf option for field name, mutually exclusive /// with FieldNameMode. bool UseJsonName = false; - + /// Transforms will be applied only to string values (== protobuf fields of string / bytes type). /// yajl_encode_string will be used if no transforms are specified. TVector<TStringTransformPtr> StringTransforms; /// Print map as object, otherwise print it as array of key/value objects bool MapAsObject = false; - + /// Stringify long integers which are not exactly representable by float or double values enum EStringifyLongNumbersMode { StringifyLongNumbersNever = 0, // default @@ -97,34 +97,34 @@ namespace NProtobufJson { MissingSingleKeyMode = mode; return *this; } - + TSelf& SetMissingRepeatedKeyMode(MissingKeyMode mode) { MissingRepeatedKeyMode = mode; return *this; } - + TSelf& SetAddMissingFields(bool add) { AddMissingFields = add; return *this; } - + TSelf& SetEnumMode(EnumValueMode mode) { EnumMode = mode; return *this; } - + TSelf& SetFieldNameMode(FldNameMode mode) { Y_ENSURE(mode == FieldNameOriginalCase || !UseJsonName, "FieldNameMode and UseJsonName are mutually exclusive"); FieldNameMode = mode; return *this; } - + TSelf& SetUseJsonName(bool jsonName) { Y_ENSURE(!jsonName || FieldNameMode == FieldNameOriginalCase, "FieldNameMode and UseJsonName are mutually exclusive"); UseJsonName = jsonName; return *this; } - + TSelf& SetExtensionFieldNameMode(ExtFldNameMode mode) { ExtensionFieldNameMode = mode; return *this; diff --git a/library/cpp/protobuf/json/field_option.h b/library/cpp/protobuf/json/field_option.h index c8a8bfbff5..3a2db53a92 100644 --- a/library/cpp/protobuf/json/field_option.h +++ b/library/cpp/protobuf/json/field_option.h @@ -1,10 +1,10 @@ -#pragma once - +#pragma once + #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - -namespace NProtobufJson { + +namespace NProtobufJson { // Functor that defines whether given field has some option set to true // // Example: @@ -20,13 +20,13 @@ namespace NProtobufJson { , Positive(positive) { } - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) const { const NProtoBuf::FieldOptions& opt = field->options(); const bool val = opt.GetExtension(Option); return Positive ? val : !val; } - + private: const TFieldOptionExtensionId& Option; bool Positive; @@ -35,6 +35,6 @@ namespace NProtobufJson { template <typename TFieldOptionExtensionId> TFieldOptionFunctor<TFieldOptionExtensionId> MakeFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true) { return TFieldOptionFunctor<TFieldOptionExtensionId>(option, positive); - } - -} + } + +} diff --git a/library/cpp/protobuf/json/filter.h b/library/cpp/protobuf/json/filter.h index 9a3ddb54fe..7033998e14 100644 --- a/library/cpp/protobuf/json/filter.h +++ b/library/cpp/protobuf/json/filter.h @@ -1,27 +1,27 @@ -#pragma once - -#include "config.h" -#include "proto2json_printer.h" -#include "json_output_create.h" - -#include <util/generic/yexception.h> -#include <util/generic/utility.h> - -#include <functional> - -namespace NProtobufJson { +#pragma once + +#include "config.h" +#include "proto2json_printer.h" +#include "json_output_create.h" + +#include <util/generic/yexception.h> +#include <util/generic/utility.h> + +#include <functional> + +namespace NProtobufJson { template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant class TFilteringPrinter: public TBasePrinter { public: using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*)>; - + template <typename... TArgs> TFilteringPrinter(TFieldPredicate isPrinted, TArgs&&... args) : TBasePrinter(std::forward<TArgs>(args)...) , IsPrinted(std::move(isPrinted)) { } - + virtual void PrintField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, @@ -37,12 +37,12 @@ namespace NProtobufJson { inline void PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, IJsonOutput& output, const TProto2JsonConfig& config = TProto2JsonConfig()) { TFilteringPrinter<> printer(std::move(filter), config); printer.Print(msg, output); - } - + } + inline TString PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, const TProto2JsonConfig& config = TProto2JsonConfig()) { TString ret; PrintWithFilter(msg, std::move(filter), *CreateJsonMapOutput(ret, config), config); return ret; - } - -} + } + +} diff --git a/library/cpp/protobuf/json/inline.h b/library/cpp/protobuf/json/inline.h index e2d7bb6ef0..03a4db8101 100644 --- a/library/cpp/protobuf/json/inline.h +++ b/library/cpp/protobuf/json/inline.h @@ -23,10 +23,10 @@ // // 4) And then serialize it to json string with inlining, e.g.: // -// Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; -// -// 5) Alternatively you can specify a some more abstract functor for defining raw json fields +// Cout << NProtobufJson::PrintInlined(o, MakeFieldOptionFunctor(this_is_json)) << Endl; // +// 5) Alternatively you can specify a some more abstract functor for defining raw json fields +// // which will print following json to stdout: // {"A":{"inner":"value"}} // instead of @@ -35,17 +35,17 @@ // // See ut/inline_ut.cpp for additional examples of usage. -#include "config.h" -#include "proto2json_printer.h" -#include "json_output_create.h" +#include "config.h" +#include "proto2json_printer.h" +#include "json_output_create.h" #include <library/cpp/protobuf/util/simple_reflection.h> -#include <util/generic/maybe.h> -#include <util/generic/yexception.h> -#include <util/generic/utility.h> +#include <util/generic/maybe.h> +#include <util/generic/yexception.h> +#include <util/generic/utility.h> -#include <functional> +#include <functional> namespace NProtobufJson { template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant @@ -100,7 +100,7 @@ namespace NProtobufJson { private: TFieldPredicate IsInlined; }; - + inline void PrintInlined(const NProtoBuf::Message& msg, TInliningPrinter<>::TFieldPredicate isInlined, IJsonOutput& output, const TProto2JsonConfig& config = TProto2JsonConfig()) { TInliningPrinter<> printer(std::move(isInlined), config); printer.Print(msg, output); diff --git a/library/cpp/protobuf/json/json_output.h b/library/cpp/protobuf/json/json_output.h index df143af57a..2fc7934171 100644 --- a/library/cpp/protobuf/json/json_output.h +++ b/library/cpp/protobuf/json/json_output.h @@ -1,9 +1,9 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/strbuf.h> - -namespace NProtobufJson { +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/strbuf.h> + +namespace NProtobufJson { class IJsonOutput { public: template <typename T> @@ -15,7 +15,7 @@ namespace NProtobufJson { DoWriteNull(); return *this; } - + IJsonOutput& BeginList() { DoBeginList(); return *this; @@ -24,7 +24,7 @@ namespace NProtobufJson { DoEndList(); return *this; } - + IJsonOutput& BeginObject() { DoBeginObject(); return *this; @@ -37,15 +37,15 @@ namespace NProtobufJson { DoEndObject(); return *this; } - + IJsonOutput& WriteRawJson(const TStringBuf& str) { DoWriteRawJson(str); return *this; } - + virtual ~IJsonOutput() { } - + protected: virtual void DoWrite(const TStringBuf& s) = 0; virtual void DoWrite(const TString& s) = 0; @@ -63,17 +63,17 @@ namespace NProtobufJson { virtual void DoWrite(double f) = 0; virtual void DoWrite(bool b) = 0; virtual void DoWriteNull() = 0; - + virtual void DoBeginList() = 0; virtual void DoEndList() = 0; - + virtual void DoBeginObject() = 0; virtual void DoWriteKey(const TStringBuf& key) = 0; virtual void DoEndObject() = 0; - + virtual void DoWriteRawJson(const TStringBuf& str) = 0; }; - + using TJsonMapOutputPtr = THolder<IJsonOutput>; - + } diff --git a/library/cpp/protobuf/json/json_output_create.cpp b/library/cpp/protobuf/json/json_output_create.cpp index 378e4ea65a..0c1575d0ca 100644 --- a/library/cpp/protobuf/json/json_output_create.cpp +++ b/library/cpp/protobuf/json/json_output_create.cpp @@ -1,18 +1,18 @@ -#include "json_output_create.h" - -#include "config.h" -#include "json_writer_output.h" -#include "json_value_output.h" - -namespace NProtobufJson { +#include "json_output_create.h" + +#include "config.h" +#include "json_writer_output.h" +#include "json_value_output.h" + +namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const NJson::TJsonWriterConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonWriter& writer) { return MakeHolder<TBaseJsonWriterOutput>(writer); } - + TJsonMapOutputPtr CreateJsonMapOutput(TString& str, const TProto2JsonConfig& config) { return MakeHolder<TJsonStringWriterOutput>(&str, config); } @@ -20,13 +20,13 @@ namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(TStringStream& out, const TProto2JsonConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const TProto2JsonConfig& config) { return MakeHolder<TJsonWriterOutput>(&out, config); } - + TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonValue& json) { return MakeHolder<TJsonValueOutput>(json); } - -} + +} diff --git a/library/cpp/protobuf/json/json_output_create.h b/library/cpp/protobuf/json/json_output_create.h index ad3889f5e9..9f7b195cc7 100644 --- a/library/cpp/protobuf/json/json_output_create.h +++ b/library/cpp/protobuf/json/json_output_create.h @@ -1,22 +1,22 @@ -#pragma once - -#include "config.h" -#include "json_output.h" - -namespace NJson { +#pragma once + +#include "config.h" +#include "json_output.h" + +namespace NJson { class TJsonValue; class TJsonWriter; struct TJsonWriterConfig; } - + class IOutputStream; -class TStringStream; - -namespace NProtobufJson { +class TStringStream; + +namespace NProtobufJson { TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const NJson::TJsonWriterConfig& config); TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonWriter& writer); TJsonMapOutputPtr CreateJsonMapOutput(IOutputStream& out, const TProto2JsonConfig& config = TProto2JsonConfig()); TJsonMapOutputPtr CreateJsonMapOutput(TString& str, const TProto2JsonConfig& config = TProto2JsonConfig()); TJsonMapOutputPtr CreateJsonMapOutput(NJson::TJsonValue& json); - + } diff --git a/library/cpp/protobuf/json/json_value_output.cpp b/library/cpp/protobuf/json/json_value_output.cpp index d845cc1c74..33a0f3d339 100644 --- a/library/cpp/protobuf/json/json_value_output.cpp +++ b/library/cpp/protobuf/json/json_value_output.cpp @@ -1,12 +1,12 @@ #include "json_value_output.h" #include <library/cpp/json/json_reader.h> - -namespace NProtobufJson { + +namespace NProtobufJson { template <typename T> void TJsonValueOutput::WriteImpl(const T& t) { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Value = t; Context.pop(); @@ -14,23 +14,23 @@ namespace NProtobufJson { Context.top().Value.AppendValue(t); } } - + void TJsonValueOutput::DoWrite(const TStringBuf& s) { WriteImpl(s); } - + void TJsonValueOutput::DoWrite(const TString& s) { WriteImpl(s); } - + void TJsonValueOutput::DoWrite(int i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(unsigned int i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(long long i) { WriteImpl(i); } @@ -38,26 +38,26 @@ namespace NProtobufJson { void TJsonValueOutput::DoWrite(unsigned long long i) { WriteImpl(i); } - + void TJsonValueOutput::DoWrite(float f) { WriteImpl(f); } - + void TJsonValueOutput::DoWrite(double f) { WriteImpl(f); } - + void TJsonValueOutput::DoWrite(bool b) { WriteImpl(b); } - + void TJsonValueOutput::DoWriteNull() { WriteImpl(NJson::JSON_NULL); } - + void TJsonValueOutput::DoBeginList() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Type = TContext::JSON_ARRAY; Context.top().Value.SetType(NJson::JSON_ARRAY); @@ -65,15 +65,15 @@ namespace NProtobufJson { Context.emplace(TContext::JSON_ARRAY, Context.top().Value.AppendValue(NJson::JSON_ARRAY)); } } - + void TJsonValueOutput::DoEndList() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY); Context.pop(); } - + void TJsonValueOutput::DoBeginObject() { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { Context.top().Type = TContext::JSON_MAP; Context.top().Value.SetType(NJson::JSON_MAP); @@ -81,20 +81,20 @@ namespace NProtobufJson { Context.emplace(TContext::JSON_MAP, Context.top().Value.AppendValue(NJson::JSON_MAP)); } } - + void TJsonValueOutput::DoWriteKey(const TStringBuf& key) { Y_ASSERT(Context.top().Type == TContext::JSON_MAP); Context.emplace(TContext::JSON_AFTER_KEY, Context.top().Value[key]); } - + void TJsonValueOutput::DoEndObject() { Y_ASSERT(Context.top().Type == TContext::JSON_MAP); Context.pop(); } - + void TJsonValueOutput::DoWriteRawJson(const TStringBuf& str) { Y_ASSERT(Context.top().Type == TContext::JSON_ARRAY || Context.top().Type == TContext::JSON_AFTER_KEY); - + if (Context.top().Type == TContext::JSON_AFTER_KEY) { NJson::ReadJsonTree(str, &Context.top().Value); Context.pop(); @@ -102,5 +102,5 @@ namespace NProtobufJson { NJson::ReadJsonTree(str, &Context.top().Value.AppendValue(NJson::JSON_UNDEFINED)); } } - -} + +} diff --git a/library/cpp/protobuf/json/json_value_output.h b/library/cpp/protobuf/json/json_value_output.h index 3fc6ff2ab0..8d71d36af4 100644 --- a/library/cpp/protobuf/json/json_value_output.h +++ b/library/cpp/protobuf/json/json_value_output.h @@ -1,12 +1,12 @@ -#pragma once - +#pragma once + #include "json_output.h" #include <library/cpp/json/writer/json_value.h> - + #include <util/generic/stack.h> -namespace NProtobufJson { +namespace NProtobufJson { class TJsonValueOutput: public IJsonOutput { public: TJsonValueOutput(NJson::TJsonValue& value) @@ -14,7 +14,7 @@ namespace NProtobufJson { { Context.emplace(TContext::JSON_AFTER_KEY, Root); } - + void DoWrite(const TStringBuf& s) override; void DoWrite(const TString& s) override; void DoWrite(int i) override; @@ -25,27 +25,27 @@ namespace NProtobufJson { void DoWrite(double f) override; void DoWrite(bool b) override; void DoWriteNull() override; - + void DoBeginList() override; void DoEndList() override; - + void DoBeginObject() override; void DoWriteKey(const TStringBuf& key) override; void DoEndObject() override; - + void DoWriteRawJson(const TStringBuf& str) override; - + private: template <typename T> void WriteImpl(const T& t); - + struct TContext { enum EType { JSON_MAP, JSON_ARRAY, JSON_AFTER_KEY, }; - + TContext(EType type, NJson::TJsonValue& value) : Type(type) , Value(value) @@ -55,9 +55,9 @@ namespace NProtobufJson { EType Type; NJson::TJsonValue& Value; }; - + NJson::TJsonValue& Root; TStack<TContext, TVector<TContext>> Context; }; - + } diff --git a/library/cpp/protobuf/json/json_writer_output.cpp b/library/cpp/protobuf/json/json_writer_output.cpp index 288f645bab..d6258eae1b 100644 --- a/library/cpp/protobuf/json/json_writer_output.cpp +++ b/library/cpp/protobuf/json/json_writer_output.cpp @@ -1,6 +1,6 @@ -#include "json_writer_output.h" - -namespace NProtobufJson { +#include "json_writer_output.h" + +namespace NProtobufJson { NJson::TJsonWriterConfig TJsonWriterOutput::CreateJsonWriterConfig(const TProto2JsonConfig& config) { NJson::TJsonWriterConfig jsonConfig; jsonConfig.FormatOutput = config.FormatOutput; @@ -8,15 +8,15 @@ namespace NProtobufJson { jsonConfig.ValidateUtf8 = false; jsonConfig.DontEscapeStrings = false; jsonConfig.WriteNanAsString = config.WriteNanAsString; - + for (size_t i = 0; i < config.StringTransforms.size(); ++i) { Y_ASSERT(config.StringTransforms[i]); if (config.StringTransforms[i]->GetType() == IStringTransform::EscapeTransform) { jsonConfig.DontEscapeStrings = true; break; } - } + } return jsonConfig; - } + } -} +} diff --git a/library/cpp/protobuf/json/json_writer_output.h b/library/cpp/protobuf/json/json_writer_output.h index 3d8a2daa56..a634d9d565 100644 --- a/library/cpp/protobuf/json/json_writer_output.h +++ b/library/cpp/protobuf/json/json_writer_output.h @@ -1,21 +1,21 @@ -#pragma once - -#include "json_output.h" -#include "config.h" - +#pragma once + +#include "json_output.h" +#include "config.h" + #include <library/cpp/json/json_writer.h> - + #include <util/string/builder.h> -#include <util/generic/store_policy.h> - -namespace NProtobufJson { +#include <util/generic/store_policy.h> + +namespace NProtobufJson { class TBaseJsonWriterOutput: public IJsonOutput { public: TBaseJsonWriterOutput(NJson::TJsonWriter& writer) : Writer(writer) { } - + private: void DoWrite(int i) override { Writer.Write(i); @@ -47,14 +47,14 @@ namespace NProtobufJson { void DoWrite(const TString& s) override { Writer.Write(s); } - + void DoBeginList() override { Writer.OpenArray(); } void DoEndList() override { Writer.CloseArray(); } - + void DoBeginObject() override { Writer.OpenMap(); } @@ -64,14 +64,14 @@ namespace NProtobufJson { void DoEndObject() override { Writer.CloseMap(); } - + void DoWriteRawJson(const TStringBuf& str) override { Writer.UnsafeWrite(str); } - + NJson::TJsonWriter& Writer; }; - + class TJsonWriterOutput: public TEmbedPolicy<NJson::TJsonWriter>, public TBaseJsonWriterOutput { public: TJsonWriterOutput(IOutputStream* outputStream, const NJson::TJsonWriterConfig& cfg) @@ -79,17 +79,17 @@ namespace NProtobufJson { , TBaseJsonWriterOutput(*Ptr()) { } - + TJsonWriterOutput(IOutputStream* outputStream, const TProto2JsonConfig& cfg) : TEmbedPolicy<NJson::TJsonWriter>(outputStream, CreateJsonWriterConfig(cfg)) , TBaseJsonWriterOutput(*Ptr()) { } - + private: static NJson::TJsonWriterConfig CreateJsonWriterConfig(const TProto2JsonConfig& cfg); }; - + class TJsonStringWriterOutput: public TEmbedPolicy<TStringOutput>, public TJsonWriterOutput { public: template <typename TConfig> @@ -99,5 +99,5 @@ namespace NProtobufJson { { } }; - + } diff --git a/library/cpp/protobuf/json/proto2json.cpp b/library/cpp/protobuf/json/proto2json.cpp index 3d76a91686..728b98d1f9 100644 --- a/library/cpp/protobuf/json/proto2json.cpp +++ b/library/cpp/protobuf/json/proto2json.cpp @@ -1,8 +1,8 @@ #include "proto2json.h" -#include "json_output_create.h" -#include "proto2json_printer.h" - +#include "json_output_create.h" +#include "proto2json_printer.h" + #include <library/cpp/json/json_reader.h> #include <library/cpp/json/json_value.h> #include <library/cpp/json/json_writer.h> @@ -13,7 +13,7 @@ #include <util/stream/str.h> #include <util/system/yassert.h> -namespace NProtobufJson { +namespace NProtobufJson { void Proto2Json(const NProtoBuf::Message& proto, IJsonOutput& jsonOutput, const TProto2JsonConfig& config, bool closeMap) { TProto2JsonPrinter printer(config); diff --git a/library/cpp/protobuf/json/proto2json.h b/library/cpp/protobuf/json/proto2json.h index 89a1781a40..191c406e5e 100644 --- a/library/cpp/protobuf/json/proto2json.h +++ b/library/cpp/protobuf/json/proto2json.h @@ -1,18 +1,18 @@ #pragma once -#include "config.h" -#include "json_output.h" +#include "config.h" +#include "json_output.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - + #include <util/generic/fwd.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> #include <util/stream/str.h> -#include <functional> +#include <functional> namespace NJson { class TJsonValue; diff --git a/library/cpp/protobuf/json/proto2json_printer.cpp b/library/cpp/protobuf/json/proto2json_printer.cpp index 6123eab0f2..833a0d294b 100644 --- a/library/cpp/protobuf/json/proto2json_printer.cpp +++ b/library/cpp/protobuf/json/proto2json_printer.cpp @@ -1,14 +1,14 @@ -#include "proto2json_printer.h" -#include "config.h" +#include "proto2json_printer.h" +#include "config.h" #include "util.h" - -#include <util/generic/yexception.h> + +#include <util/generic/yexception.h> #include <util/string/ascii.h> #include <util/string/cast.h> - -namespace NProtobufJson { + +namespace NProtobufJson { using namespace NProtoBuf; - + class TJsonKeyBuilder { public: TJsonKeyBuilder(const FieldDescriptor& field, const TProto2JsonConfig& config, TString& tmpBuf) @@ -30,7 +30,7 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; return; } - + switch (config.FieldNameMode) { case TProto2JsonConfig::FieldNameOriginalCase: { NewKeyBuf = field.name(); @@ -43,14 +43,14 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameUpperCase: { NewKeyStr = field.name(); NewKeyStr.to_upper(); NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameCamelCase: { NewKeyStr = field.name(); if (!NewKeyStr.empty()) { @@ -59,7 +59,7 @@ namespace NProtobufJson { NewKeyBuf = NewKeyStr; break; } - + case TProto2JsonConfig::FieldNameSnakeCase: { NewKeyStr = field.name(); ToSnakeCase(&NewKeyStr); @@ -81,7 +81,7 @@ namespace NProtobufJson { const TStringBuf& GetKey() const { return NewKeyBuf; - } + } private: TStringBuf NewKeyBuf; @@ -91,26 +91,26 @@ namespace NProtobufJson { TProto2JsonPrinter::TProto2JsonPrinter(const TProto2JsonConfig& cfg) : Config(cfg) { - } - + } + TProto2JsonPrinter::~TProto2JsonPrinter() { - } - + } + TStringBuf TProto2JsonPrinter::MakeKey(const FieldDescriptor& field) { return TJsonKeyBuilder(field, GetConfig(), TmpBuf).GetKey(); } - + template <bool InMapContext, typename T> std::enable_if_t<InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& json, const TStringBuf& key, const T& value) { json.WriteKey(key).Write(value); } - + template <bool InMapContext, typename T> std::enable_if_t<!InMapContext, void> WriteWithMaybeEmptyKey(IJsonOutput& array, const TStringBuf& key, const T& value) { Y_ASSERT(!key); array.Write(value); } - + template <bool InMapContext> void TProto2JsonPrinter::PrintStringValue(const FieldDescriptor& field, const TStringBuf& key, const TString& value, @@ -131,7 +131,7 @@ namespace NProtobufJson { WriteWithMaybeEmptyKey<InMapContext>(json, key, value); } } - + template <bool InMapContext> void TProto2JsonPrinter::PrintEnumValue(const TStringBuf& key, const EnumValueDescriptor* value, @@ -146,52 +146,52 @@ namespace NProtobufJson { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->number()); break; } - + case TProto2JsonConfig::EnumName: { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->name()); break; } - + case TProto2JsonConfig::EnumFullName: { WriteWithMaybeEmptyKey<InMapContext>(json, key, value->full_name()); break; - } - + } + case TProto2JsonConfig::EnumNameLowerCase: { TString newName = value->name(); newName.to_lower(); WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); break; } - + case TProto2JsonConfig::EnumFullNameLowerCase: { TString newName = value->full_name(); newName.to_lower(); WriteWithMaybeEmptyKey<InMapContext>(json, key, newName); break; } - + default: Y_VERIFY_DEBUG(false, "Unknown EnumMode."); } - } - + } + void TProto2JsonPrinter::PrintSingleField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, TStringBuf key) { Y_VERIFY(!field.is_repeated(), "field is repeated."); - + if (!key) { key = MakeKey(field); } - + #define FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ json.WriteKey(key).Write(reflection->ProtoGet(proto, &field)); \ break; \ - } - + } + #define INT_FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ const auto value = reflection->ProtoGet(proto, &field); \ @@ -204,7 +204,7 @@ namespace NProtobufJson { } const Reflection* reflection = proto.GetReflection(); - + bool shouldPrintField = reflection->HasField(proto, &field); if (!shouldPrintField && GetConfig().MissingSingleKeyMode == TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired) { if (field.has_default_value()) { @@ -225,25 +225,25 @@ namespace NProtobufJson { FIELD_TO_JSON(CPPTYPE_DOUBLE, GetDouble); FIELD_TO_JSON(CPPTYPE_FLOAT, GetFloat); FIELD_TO_JSON(CPPTYPE_BOOL, GetBool); - + case FieldDescriptor::CPPTYPE_MESSAGE: { json.WriteKey(key); Print(reflection->GetMessage(proto, &field), json); break; } - + case FieldDescriptor::CPPTYPE_ENUM: { PrintEnumValue<true>(key, reflection->GetEnum(proto, &field), json); break; } - + case FieldDescriptor::CPPTYPE_STRING: { TString scratch; const TString& value = reflection->GetStringReference(proto, &field, &scratch); PrintStringValue<true>(field, key, value, json); break; } - + default: ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; @@ -254,36 +254,36 @@ namespace NProtobufJson { json.WriteKey(key).WriteNull(); break; } - + case TProto2JsonConfig::MissingKeySkip: case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: default: break; } - } + } #undef FIELD_TO_JSON } - + void TProto2JsonPrinter::PrintRepeatedField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, TStringBuf key) { Y_VERIFY(field.is_repeated(), "field isn't repeated."); - + const bool isMap = field.is_map() && GetConfig().MapAsObject; if (!key) { key = MakeKey(field); - } - + } + #define REPEATED_FIELD_TO_JSON(EProtoCppType, ProtoGet) \ case FieldDescriptor::EProtoCppType: { \ - for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \ + for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) \ json.Write(reflection->ProtoGet(proto, &field, i)); \ break; \ - } - + } + const Reflection* reflection = proto.GetReflection(); - + if (reflection->FieldSize(proto, &field) > 0) { json.WriteKey(key); if (isMap) { @@ -291,7 +291,7 @@ namespace NProtobufJson { } else { json.BeginList(); } - + switch (field.cpp_type()) { REPEATED_FIELD_TO_JSON(CPPTYPE_INT32, GetRepeatedInt32); REPEATED_FIELD_TO_JSON(CPPTYPE_INT64, GetRepeatedInt64); @@ -300,7 +300,7 @@ namespace NProtobufJson { REPEATED_FIELD_TO_JSON(CPPTYPE_DOUBLE, GetRepeatedDouble); REPEATED_FIELD_TO_JSON(CPPTYPE_FLOAT, GetRepeatedFloat); REPEATED_FIELD_TO_JSON(CPPTYPE_BOOL, GetRepeatedBool); - + case FieldDescriptor::CPPTYPE_MESSAGE: { if (isMap) { for (size_t i = 0, endI = reflection->FieldSize(proto, &field); i < endI; ++i) { @@ -334,7 +334,7 @@ namespace NProtobufJson { ythrow yexception() << "Unknown protobuf field type: " << static_cast<int>(field.cpp_type()) << "."; } - + if (isMap) { json.EndObject(); } else { @@ -346,7 +346,7 @@ namespace NProtobufJson { json.WriteKey(key).WriteNull(); break; } - + case TProto2JsonConfig::MissingKeyDefault: { json.WriteKey(key); if (isMap) { @@ -361,12 +361,12 @@ namespace NProtobufJson { case TProto2JsonConfig::MissingKeyExplicitDefaultThrowRequired: default: break; - } - } - + } + } + #undef REPEATED_FIELD_TO_JSON } - + void TProto2JsonPrinter::PrintKeyValue(const NProtoBuf::Message& proto, IJsonOutput& json) { const FieldDescriptor* keyField = proto.GetDescriptor()->FindFieldByName("key"); @@ -376,7 +376,7 @@ namespace NProtobufJson { Y_VERIFY(valueField, "Map entry value field not found."); PrintField(proto, *valueField, json, key); } - + TString TProto2JsonPrinter::MakeKey(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field) { const Reflection* reflection = proto.GetReflection(); @@ -433,11 +433,11 @@ namespace NProtobufJson { break; default: ythrow yexception() << "Unsupported key type."; - } - + } + return result; - } - + } + void TProto2JsonPrinter::PrintField(const Message& proto, const FieldDescriptor& field, IJsonOutput& json, @@ -449,13 +449,13 @@ namespace NProtobufJson { else PrintSingleField(proto, field, json, key); } - + void TProto2JsonPrinter::Print(const Message& proto, IJsonOutput& json, bool closeMap) { const Descriptor* descriptor = proto.GetDescriptor(); Y_ASSERT(descriptor); - + json.BeginObject(); - + // Iterate over all non-extension fields for (int f = 0, endF = descriptor->field_count(); f < endF; ++f) { const FieldDescriptor* field = descriptor->field(f); @@ -485,8 +485,8 @@ namespace NProtobufJson { if (closeMap) { json.EndObject(); } - } - + } + template <class T, class U> std::enable_if_t<!std::is_unsigned<T>::value, bool> ValueInRange(T value, U range) { return value >= -range && value <= range; @@ -514,4 +514,4 @@ namespace NProtobufJson { return false; } -} +} diff --git a/library/cpp/protobuf/json/proto2json_printer.h b/library/cpp/protobuf/json/proto2json_printer.h index 9dc5aa86c6..866fd8b545 100644 --- a/library/cpp/protobuf/json/proto2json_printer.h +++ b/library/cpp/protobuf/json/proto2json_printer.h @@ -1,62 +1,62 @@ -#pragma once - -#include "json_output.h" - +#pragma once + +#include "json_output.h" + #include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.pb.h> #include <google/protobuf/message.h> - -#include <util/generic/strbuf.h> + +#include <util/generic/strbuf.h> #include <util/generic/string.h> - -namespace NProtobufJson { + +namespace NProtobufJson { struct TProto2JsonConfig; - + class TProto2JsonPrinter { public: TProto2JsonPrinter(const TProto2JsonConfig& config); virtual ~TProto2JsonPrinter(); - + virtual void Print(const NProtoBuf::Message& proto, IJsonOutput& json, bool closeMap = true); - + virtual const TProto2JsonConfig& GetConfig() const { return Config; } - + protected: virtual TStringBuf MakeKey(const NProtoBuf::FieldDescriptor& field); - + virtual void PrintField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintRepeatedField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintSingleField(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field, IJsonOutput& json, TStringBuf key = {}); - + void PrintKeyValue(const NProtoBuf::Message& proto, IJsonOutput& json); - + TString MakeKey(const NProtoBuf::Message& proto, const NProtoBuf::FieldDescriptor& field); - + template <bool InMapContext> void PrintEnumValue(const TStringBuf& key, const NProtoBuf::EnumValueDescriptor* value, IJsonOutput& json); - + template <bool InMapContext> void PrintStringValue(const NProtoBuf::FieldDescriptor& field, const TStringBuf& key, const TString& value, IJsonOutput& json); - + template <class T> bool NeedStringifyNumber(T value) const; @@ -64,5 +64,5 @@ namespace NProtobufJson { const TProto2JsonConfig& Config; TString TmpBuf; }; - + } diff --git a/library/cpp/protobuf/json/ut/filter_ut.cpp b/library/cpp/protobuf/json/ut/filter_ut.cpp index 95c227666f..a5f17f0230 100644 --- a/library/cpp/protobuf/json/ut/filter_ut.cpp +++ b/library/cpp/protobuf/json/ut/filter_ut.cpp @@ -1,47 +1,47 @@ #include <library/cpp/protobuf/json/ut/filter_ut.pb.h> - + #include <library/cpp/protobuf/json/filter.h> #include <library/cpp/protobuf/json/field_option.h> #include <library/cpp/protobuf/json/proto2json.h> #include <library/cpp/testing/unittest/registar.h> - -using namespace NProtobufJson; - -static NProtobufJsonUt::TFilterTest GetTestMsg() { - NProtobufJsonUt::TFilterTest msg; - msg.SetOptFiltered("1"); - msg.SetNotFiltered("23"); - msg.AddRepFiltered(45); - msg.AddRepFiltered(67); - msg.MutableInner()->AddNumber(100); - msg.MutableInner()->AddNumber(200); - msg.MutableInner()->SetInnerFiltered(235); - return msg; -} - + +using namespace NProtobufJson; + +static NProtobufJsonUt::TFilterTest GetTestMsg() { + NProtobufJsonUt::TFilterTest msg; + msg.SetOptFiltered("1"); + msg.SetNotFiltered("23"); + msg.AddRepFiltered(45); + msg.AddRepFiltered(67); + msg.MutableInner()->AddNumber(100); + msg.MutableInner()->AddNumber(200); + msg.MutableInner()->SetInnerFiltered(235); + return msg; +} + Y_UNIT_TEST_SUITE(TProto2JsonFilterTest){ Y_UNIT_TEST(TestFilterPrinter){ - NProtobufJsonUt::TFilterTest msg = GetTestMsg(); + NProtobufJsonUt::TFilterTest msg = GetTestMsg(); { TString expected = R"({"OptFiltered":"1","NotFiltered":"23","RepFiltered":[45,67],)" R"("Inner":{"Number":[100,200],"InnerFiltered":235}})"; TString my = Proto2Json(msg); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"NotFiltered":"23",)" R"("Inner":{"Number":[100,200]}})"; TString my = PrintWithFilter(msg, MakeFieldOptionFunctor(NProtobufJsonUt::filter_test, false)); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"OptFiltered":"1","RepFiltered":[45,67]})"; TString my = PrintWithFilter(msg, MakeFieldOptionFunctor(NProtobufJsonUt::filter_test)); UNIT_ASSERT_STRINGS_EQUAL(my, expected); } - + { TString expected = R"({"OptFiltered":"1","NotFiltered":"23",)" R"("Inner":{"Number":[100,200]}})"; @@ -66,25 +66,25 @@ Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { struct TFunctorMock { TFunctorMock(size_t* copyCount) : CopyCount(copyCount) - { + { UNIT_ASSERT(*CopyCount <= 1); - } - + } + TFunctorMock(const TFunctorMock& f) : CopyCount(f.CopyCount) - { + { ++*CopyCount; - } - + } + TFunctorMock(TFunctorMock&& f) = default; - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) const { return false; } - + size_t* CopyCount; }; - + TProto2JsonConfig cfg; TFilteringPrinter<> printer(TFunctorMock(&CopyCount), cfg); UNIT_ASSERT(CopyCount <= 1); diff --git a/library/cpp/protobuf/json/ut/filter_ut.proto b/library/cpp/protobuf/json/ut/filter_ut.proto index 29d630ade4..c4866ed813 100644 --- a/library/cpp/protobuf/json/ut/filter_ut.proto +++ b/library/cpp/protobuf/json/ut/filter_ut.proto @@ -1,20 +1,20 @@ import "google/protobuf/descriptor.proto"; - -package NProtobufJsonUt; - -extend google.protobuf.FieldOptions { - optional bool filter_test = 58255; - optional bool export_test = 58256; -} - -message TFilterTest { - optional string OptFiltered = 1 [(filter_test) = true, (export_test) = true]; - optional string NotFiltered = 2 [(export_test) = true]; - repeated uint64 RepFiltered = 3 [(filter_test) = true]; - - message TInner { - repeated uint32 Number = 1 [(export_test) = true]; - optional int32 InnerFiltered = 2 [(filter_test) = true]; - } - optional TInner Inner = 4 [(export_test) = true]; -} + +package NProtobufJsonUt; + +extend google.protobuf.FieldOptions { + optional bool filter_test = 58255; + optional bool export_test = 58256; +} + +message TFilterTest { + optional string OptFiltered = 1 [(filter_test) = true, (export_test) = true]; + optional string NotFiltered = 2 [(export_test) = true]; + repeated uint64 RepFiltered = 3 [(filter_test) = true]; + + message TInner { + repeated uint32 Number = 1 [(export_test) = true]; + optional int32 InnerFiltered = 2 [(filter_test) = true]; + } + optional TInner Inner = 4 [(export_test) = true]; +} diff --git a/library/cpp/protobuf/json/ut/inline_ut.cpp b/library/cpp/protobuf/json/ut/inline_ut.cpp index c29ad32e7d..048e3fa275 100644 --- a/library/cpp/protobuf/json/ut/inline_ut.cpp +++ b/library/cpp/protobuf/json/ut/inline_ut.cpp @@ -9,42 +9,42 @@ using namespace NProtobufJson; -static NProtobufJsonUt::TInlineTest GetTestMsg() { - NProtobufJsonUt::TInlineTest msg; - msg.SetOptJson(R"({"a":1,"b":"000"})"); - msg.SetNotJson("12{}34"); - msg.AddRepJson("{}"); - msg.AddRepJson("[1,2]"); - msg.MutableInner()->AddNumber(100); - msg.MutableInner()->AddNumber(200); - msg.MutableInner()->SetInnerJson(R"({"xxx":[]})"); - return msg; -} - +static NProtobufJsonUt::TInlineTest GetTestMsg() { + NProtobufJsonUt::TInlineTest msg; + msg.SetOptJson(R"({"a":1,"b":"000"})"); + msg.SetNotJson("12{}34"); + msg.AddRepJson("{}"); + msg.AddRepJson("[1,2]"); + msg.MutableInner()->AddNumber(100); + msg.MutableInner()->AddNumber(200); + msg.MutableInner()->SetInnerJson(R"({"xxx":[]})"); + return msg; +} + Y_UNIT_TEST_SUITE(TProto2JsonInlineTest){ Y_UNIT_TEST(TestNormalPrint){ - NProtobufJsonUt::TInlineTest msg = GetTestMsg(); + NProtobufJsonUt::TInlineTest msg = GetTestMsg(); // normal print should output these fields as just string values TString expRaw = R"({"OptJson":"{\"a\":1,\"b\":\"000\"}","NotJson":"12{}34","RepJson":["{}","[1,2]"],)" R"("Inner":{"Number":[100,200],"InnerJson":"{\"xxx\":[]}"}})"; TString myRaw; Proto2Json(msg, myRaw); UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw); - + myRaw = PrintInlined(msg, [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) { return false; }); UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw); // result is the same } - + Y_UNIT_TEST(TestInliningPrinter) { NProtobufJsonUt::TInlineTest msg = GetTestMsg(); // inlined print should output these fields as inlined json sub-objects TString expInlined = R"({"OptJson":{"a":1,"b":"000"},"NotJson":"12{}34","RepJson":[{},[1,2]],)" R"("Inner":{"Number":[100,200],"InnerJson":{"xxx":[]}}})"; - + { TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test)); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); - } + } { auto functor = [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) { return field->name() == "OptJson" || field->name() == "RepJson" || field->name() == "InnerJson"; @@ -53,7 +53,7 @@ Y_UNIT_TEST(TestInliningPrinter) { UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } } - + Y_UNIT_TEST(TestNoValues) { // no values - no printing NProtobufJsonUt::TInlineTest msg; @@ -61,35 +61,35 @@ Y_UNIT_TEST(TestNoValues) { msg.MutableInner()->AddNumber(200); TString expInlined = R"({"Inner":{"Number":[100,200]}})"; - + TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test)); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(TestMissingKeyModeNull) { NProtobufJsonUt::TInlineTest msg; msg.MutableInner()->AddNumber(100); msg.MutableInner()->AddNumber(200); - + TString expInlined = R"({"OptJson":null,"NotJson":null,"RepJson":null,"Inner":{"Number":[100,200],"InnerJson":null}})"; - + TProto2JsonConfig cfg; cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyNull).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyNull); TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(TestMissingKeyModeDefault) { NProtobufJsonUt::TInlineTestDefaultValues msg; - + TString expInlined = R"({"OptJson":{"default":1},"Number":0,"RepJson":[],"Inner":{"OptJson":{"default":2}}})"; - + TProto2JsonConfig cfg; cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyDefault); TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg); UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined); } - + Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { size_t CopyCount = 0; struct TFunctorMock { @@ -98,22 +98,22 @@ Y_UNIT_TEST(NoUnnecessaryCopyFunctor) { { UNIT_ASSERT(*CopyCount <= 1); } - + TFunctorMock(const TFunctorMock& f) : CopyCount(f.CopyCount) { ++*CopyCount; } - + TFunctorMock(TFunctorMock&& f) = default; - + bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) const { return false; } - + size_t* CopyCount; }; - + TProto2JsonConfig cfg; TInliningPrinter<> printer(TFunctorMock(&CopyCount), cfg); UNIT_ASSERT(CopyCount <= 1); diff --git a/library/cpp/protobuf/json/ut/inline_ut.proto b/library/cpp/protobuf/json/ut/inline_ut.proto index 76bd10232d..147c3369dd 100644 --- a/library/cpp/protobuf/json/ut/inline_ut.proto +++ b/library/cpp/protobuf/json/ut/inline_ut.proto @@ -17,13 +17,13 @@ message TInlineTest { } optional TInner Inner = 4; } - -message TInlineTestDefaultValues { - optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":1}"]; - optional uint32 Number = 2; - repeated string RepJson = 3 [(inline_test) = true]; - message TInner { - optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":2}"]; - } - optional TInner Inner = 4; -} + +message TInlineTestDefaultValues { + optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":1}"]; + optional uint32 Number = 2; + repeated string RepJson = 3 [(inline_test) = true]; + message TInner { + optional string OptJson = 1 [(inline_test) = true, default = "{\"default\":2}"]; + } + optional TInner Inner = 4; +} diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..9dbec774e2 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -20,8 +20,8 @@ #include <util/system/defaults.h> #include <util/system/yassert.h> -#include <limits> - +#include <limits> + using namespace NProtobufJson; using namespace NProtobufJsonTest; @@ -887,18 +887,18 @@ Y_UNIT_TEST(TestFieldNameMode) { /// TODO: test missing keys } // TestFieldNameMode - + Y_UNIT_TEST(TestNan) { TFlatOptional proto; proto.SetDouble(std::numeric_limits<double>::quiet_NaN()); - + UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception); } // TestNan - + Y_UNIT_TEST(TestInf) { TFlatOptional proto; proto.SetFloat(std::numeric_limits<float>::infinity()); - + UNIT_ASSERT_EXCEPTION(Proto2Json(proto, TProto2JsonConfig()), yexception); } // TestInf diff --git a/library/cpp/protobuf/json/ut/ya.make b/library/cpp/protobuf/json/ut/ya.make index b60a6d3c17..ac6aebeab8 100644 --- a/library/cpp/protobuf/json/ut/ya.make +++ b/library/cpp/protobuf/json/ut/ya.make @@ -3,14 +3,14 @@ UNITTEST_FOR(library/cpp/protobuf/json) OWNER(avitella) SRCS( - filter_ut.cpp + filter_ut.cpp json2proto_ut.cpp proto2json_ut.cpp inline_ut.proto inline_ut.cpp string_transform_ut.cpp - filter_ut.proto - test.proto + filter_ut.proto + test.proto util_ut.cpp ) diff --git a/library/cpp/protobuf/json/ya.make b/library/cpp/protobuf/json/ya.make index 2f2c75cfdb..b8e3f1a33c 100644 --- a/library/cpp/protobuf/json/ya.make +++ b/library/cpp/protobuf/json/ya.make @@ -4,9 +4,9 @@ OWNER(avitella) SRCS( json2proto.cpp - json_output_create.cpp - json_value_output.cpp - json_writer_output.cpp + json_output_create.cpp + json_value_output.cpp + json_writer_output.cpp name_generator.cpp proto2json.cpp proto2json_printer.cpp diff --git a/library/cpp/protobuf/util/cast.h b/library/cpp/protobuf/util/cast.h index 83749dfcee..d368d13766 100644 --- a/library/cpp/protobuf/util/cast.h +++ b/library/cpp/protobuf/util/cast.h @@ -1,11 +1,11 @@ #pragma once -#include "traits.h" +#include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/cast.h> +#include <util/generic/cast.h> namespace NProtoBuf { // C++ compatible conversions of FieldDescriptor::CppType's diff --git a/library/cpp/protobuf/util/path.cpp b/library/cpp/protobuf/util/path.cpp index efa2a42c8a..8a9c2ba7d7 100644 --- a/library/cpp/protobuf/util/path.cpp +++ b/library/cpp/protobuf/util/path.cpp @@ -1,20 +1,20 @@ -#include "path.h" - -#include <util/generic/yexception.h> - -namespace NProtoBuf { +#include "path.h" + +#include <util/generic/yexception.h> + +namespace NProtoBuf { TFieldPath::TFieldPath() { } - + TFieldPath::TFieldPath(const Descriptor* msgType, const TStringBuf& path) { Init(msgType, path); } - + TFieldPath::TFieldPath(const TVector<const FieldDescriptor*>& path) : Path(path) { } - + bool TFieldPath::InitUnsafe(const Descriptor* msgType, TStringBuf path) { Path.clear(); while (path) { @@ -23,10 +23,10 @@ namespace NProtoBuf { next = path.NextTok('/'); if (!next) return true; - + if (!msgType) // need field but no message type return false; - + TString nextStr(next); const FieldDescriptor* field = msgType->FindFieldByName(nextStr); if (!field) { @@ -41,21 +41,21 @@ namespace NProtoBuf { return false; // ambiguity field = ext; } - } - } - + } + } + if (!field) return false; - + Path.push_back(field); msgType = field->type() == FieldDescriptor::TYPE_MESSAGE ? field->message_type() : nullptr; } return true; - } - + } + void TFieldPath::Init(const Descriptor* msgType, const TStringBuf& path) { if (!InitUnsafe(msgType, path)) ythrow yexception() << "Failed to resolve path \"" << path << "\" relative to " << msgType->full_name(); } -} +} diff --git a/library/cpp/protobuf/util/path.h b/library/cpp/protobuf/util/path.h index 487f643a2d..4fbee86f26 100644 --- a/library/cpp/protobuf/util/path.h +++ b/library/cpp/protobuf/util/path.h @@ -1,11 +1,11 @@ -#pragma once - +#pragma once + #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> - -#include <util/generic/vector.h> - -namespace NProtoBuf { + +#include <util/generic/vector.h> + +namespace NProtoBuf { class TFieldPath { public: TFieldPath(); @@ -13,40 +13,40 @@ namespace NProtoBuf { TFieldPath(const TVector<const FieldDescriptor*>& path); TFieldPath(const TFieldPath&) = default; TFieldPath& operator=(const TFieldPath&) = default; - + bool InitUnsafe(const Descriptor* msgType, const TStringBuf path); // noexcept void Init(const Descriptor* msgType, const TStringBuf& path); // throws - + const TVector<const FieldDescriptor*>& Fields() const { return Path; } - + void AddField(const FieldDescriptor* field) { Path.push_back(field); } - + const Descriptor* ParentType() const { return Empty() ? nullptr : Path.front()->containing_type(); } - + const FieldDescriptor* FieldDescr() const { return Empty() ? nullptr : Path.back(); } - + bool Empty() const { return Path.empty(); } - + explicit operator bool() const { return !Empty(); } - + bool operator!() const { return Empty(); } - + private: TVector<const FieldDescriptor*> Path; }; - + } diff --git a/library/cpp/protobuf/util/proto/ya.make b/library/cpp/protobuf/util/proto/ya.make index 4d68047d8b..3a3d58e486 100644 --- a/library/cpp/protobuf/util/proto/ya.make +++ b/library/cpp/protobuf/util/proto/ya.make @@ -1,11 +1,11 @@ -PROTO_LIBRARY() - -OWNER(mowgli) - -SRCS( - merge.proto -) - +PROTO_LIBRARY() + +OWNER(mowgli) + +SRCS( + merge.proto +) + EXCLUDE_TAGS(GO_PROTO) -END() +END() diff --git a/library/cpp/protobuf/util/repeated_field_utils.h b/library/cpp/protobuf/util/repeated_field_utils.h index c07bd84647..8f7428b5dc 100644 --- a/library/cpp/protobuf/util/repeated_field_utils.h +++ b/library/cpp/protobuf/util/repeated_field_utils.h @@ -38,8 +38,8 @@ namespace NProtoBuf { T* ret = field->Add(); MoveRepeatedFieldItem(field, field->size() - 1, index); return ret; - } - + } + template <typename TRepeated> // suitable both for RepeatedField and RepeatedPtrField static void RemoveRepeatedFieldItem(TRepeated* field, size_t index) { if ((int)index >= field->size()) @@ -70,8 +70,8 @@ namespace NProtoBuf { for (int i = begIndex; i < endIndex; ++i, ++shiftIndex) field->SwapElements(shiftIndex, i); } - } - + } + // Remove several items at once, could be more efficient compared to calling RemoveRepeatedFieldItem several times template <typename TRepeated> static void RemoveRepeatedFieldItems(TRepeated* field, const TVector<size_t>& sortedIndices) { diff --git a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp index 58aaaa9e12..94a494e1a3 100644 --- a/library/cpp/protobuf/util/repeated_field_utils_ut.cpp +++ b/library/cpp/protobuf/util/repeated_field_utils_ut.cpp @@ -1,46 +1,46 @@ -#include "repeated_field_utils.h" +#include "repeated_field_utils.h" #include <library/cpp/protobuf/util/ut/common_ut.pb.h> - + #include <library/cpp/testing/unittest/registar.h> - -using namespace NProtoBuf; - + +using namespace NProtoBuf; + Y_UNIT_TEST_SUITE(RepeatedFieldUtils) { Y_UNIT_TEST(RemoveIf) { - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepInt(0); - msg.AddRepInt(1); - msg.AddRepInt(2); - msg.AddRepInt(3); - msg.AddRepInt(4); - msg.AddRepInt(5); - auto cond = [](ui32 val) { - return val % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); - } - - { - NProtobufUtilUt::TWalkTest msg; - msg.AddRepSub()->SetOptInt(0); - msg.AddRepSub()->SetOptInt(1); - msg.AddRepSub()->SetOptInt(2); - msg.AddRepSub()->SetOptInt(3); - msg.AddRepSub()->SetOptInt(4); - msg.AddRepSub()->SetOptInt(5); - auto cond = [](const NProtobufUtilUt::TWalkTest& val) { - return val.GetOptInt() % 2 == 0; - }; - RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); - UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); - } - } -} + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepInt(0); + msg.AddRepInt(1); + msg.AddRepInt(2); + msg.AddRepInt(3); + msg.AddRepInt(4); + msg.AddRepInt(5); + auto cond = [](ui32 val) { + return val % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepInt(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepIntSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepInt(1)); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepInt(2)); + } + + { + NProtobufUtilUt::TWalkTest msg; + msg.AddRepSub()->SetOptInt(0); + msg.AddRepSub()->SetOptInt(1); + msg.AddRepSub()->SetOptInt(2); + msg.AddRepSub()->SetOptInt(3); + msg.AddRepSub()->SetOptInt(4); + msg.AddRepSub()->SetOptInt(5); + auto cond = [](const NProtobufUtilUt::TWalkTest& val) { + return val.GetOptInt() % 2 == 0; + }; + RemoveRepeatedFieldItemIf(msg.MutableRepSub(), cond); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepSubSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepSub(0).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepSub(1).GetOptInt()); + UNIT_ASSERT_VALUES_EQUAL(5, msg.GetRepSub(2).GetOptInt()); + } + } +} diff --git a/library/cpp/protobuf/util/simple_reflection.cpp b/library/cpp/protobuf/util/simple_reflection.cpp index d842e9ee44..83afa0ce0c 100644 --- a/library/cpp/protobuf/util/simple_reflection.cpp +++ b/library/cpp/protobuf/util/simple_reflection.cpp @@ -1,10 +1,10 @@ -#include "simple_reflection.h" - -namespace NProtoBuf { +#include "simple_reflection.h" + +namespace NProtoBuf { const Message* GetMessageHelper(const TConstField& curField, bool) { return curField.HasValue() && curField.IsMessage() ? curField.Get<Message>() : nullptr; } - + Message* GetMessageHelper(TMutableField& curField, bool createPath) { if (curField.IsMessage()) { if (!curField.HasValue()) { @@ -13,14 +13,14 @@ namespace NProtoBuf { } else { return curField.MutableMessage(); } - } + } return nullptr; - } - + } + template <class TField, class TMsg> TMaybe<TField> ByPathImpl(TMsg& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { if (fieldsPath.empty()) - return TMaybe<TField>(); + return TMaybe<TField>(); TMsg* curParent = &msg; for (size_t i = 0, size = fieldsPath.size(); i < size; ++i) { const FieldDescriptor* field = fieldsPath[i]; @@ -35,36 +35,36 @@ namespace NProtoBuf { return TField(*curParent, fieldsPath.back()); else return TMaybe<TField>(); - } - + } + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TVector<const FieldDescriptor*>& fieldsPath) { return ByPathImpl<TConstField, const Message>(msg, fieldsPath, false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TStringBuf& path) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TConstField>(); return ByPathImpl<TConstField, const Message>(msg, fieldPath.Fields(), false); } - + TMaybe<TConstField> TConstField::ByPath(const Message& msg, const TFieldPath& path) { return ByPathImpl<TConstField, const Message>(msg, path.Fields(), false); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TVector<const FieldDescriptor*>& fieldsPath, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, fieldsPath, createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TStringBuf& path, bool createPath) { TFieldPath fieldPath; if (!fieldPath.InitUnsafe(msg.GetDescriptor(), path)) return TMaybe<TMutableField>(); return ByPathImpl<TMutableField, Message>(msg, fieldPath.Fields(), createPath); } - + TMaybe<TMutableField> TMutableField::ByPath(Message& msg, const TFieldPath& path, bool createPath) { return ByPathImpl<TMutableField, Message>(msg, path.Fields(), createPath); } -} +} diff --git a/library/cpp/protobuf/util/simple_reflection.h b/library/cpp/protobuf/util/simple_reflection.h index 61e877a787..a5dd46ac79 100644 --- a/library/cpp/protobuf/util/simple_reflection.h +++ b/library/cpp/protobuf/util/simple_reflection.h @@ -1,17 +1,17 @@ #pragma once -#include "cast.h" -#include "path.h" +#include "cast.h" +#include "path.h" #include "traits.h" #include <google/protobuf/descriptor.h> #include <google/protobuf/message.h> -#include <util/generic/maybe.h> -#include <util/generic/typetraits.h> -#include <util/generic/vector.h> -#include <util/system/defaults.h> - +#include <util/generic/maybe.h> +#include <util/generic/typetraits.h> +#include <util/generic/vector.h> +#include <util/system/defaults.h> + namespace NProtoBuf { class TConstField { public: @@ -29,7 +29,7 @@ namespace NProtoBuf { const Message& Parent() const { return Msg; } - + const FieldDescriptor* Field() const { return Fd; } @@ -80,7 +80,7 @@ namespace NProtoBuf { bool IsMessage() const { return CppType() == FieldDescriptor::CPPTYPE_MESSAGE; } - + bool HasSameType(const TConstField& other) const { if (CppType() != other.CppType()) return false; @@ -90,7 +90,7 @@ namespace NProtoBuf { return false; return true; } - + protected: bool IsRepeated() const { return Fd->is_repeated(); @@ -137,7 +137,7 @@ namespace NProtoBuf { template <typename T> inline void Add(T value); - + inline void MergeFrom(const TConstField& src); inline void Clear() { @@ -167,17 +167,17 @@ namespace NProtoBuf { return; Refl().SwapElements(Mut(), Fd, index1, index2); } - + inline void Remove(size_t index) { if (index >= Size()) return; - + // Move to the end for (size_t i = index, size = Size(); i < size - 1; ++i) SwapElements(i, i + 1); RemoveLast(); } - + Message* MutableMessage(size_t index = 0) { Y_ASSERT(IsMessage()); if (IsRepeated()) { @@ -193,12 +193,12 @@ namespace NProtoBuf { inline TMsg* AddMessage() { return CheckedCast<TMsg*>(AddMessage()); } - + inline Message* AddMessage() { Y_ASSERT(IsMessage() && IsRepeated()); return Refl().AddMessage(Mut(), Fd); } - + private: Message* Mut() { return const_cast<Message*>(&Msg); diff --git a/library/cpp/protobuf/util/simple_reflection_ut.cpp b/library/cpp/protobuf/util/simple_reflection_ut.cpp index 169d4703c9..e380991c02 100644 --- a/library/cpp/protobuf/util/simple_reflection_ut.cpp +++ b/library/cpp/protobuf/util/simple_reflection_ut.cpp @@ -7,8 +7,8 @@ using namespace NProtoBuf; Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { - static TSample GenSampleForMergeFrom() { - TSample smf; + static TSample GenSampleForMergeFrom() { + TSample smf; smf.SetOneStr("one str"); smf.MutableOneMsg()->AddRepInt(1); smf.AddRepMsg()->AddRepInt(2); @@ -20,8 +20,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromGeneric) { - const TSample src(GenSampleForMergeFrom()); - TSample dst; + const TSample src(GenSampleForMergeFrom()); + TSample dst; const Descriptor* descr = dst.GetDescriptor(); { @@ -52,8 +52,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromSelf) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(sample); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(sample); const Descriptor* descr = msg.GetDescriptor(); TMutableField oneStr(msg, descr->FindFieldByName("OneStr")); @@ -66,8 +66,8 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { } Y_UNIT_TEST(MergeFromAnotherFD) { - const TSample sample(GenSampleForMergeFrom()); - TSample msg(GenSampleForMergeFrom()); + const TSample sample(GenSampleForMergeFrom()); + TSample msg(GenSampleForMergeFrom()); const Descriptor* descr = msg.GetDescriptor(); { // string @@ -95,205 +95,205 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { UNIT_ASSERT_VALUES_EQUAL(msg.RepMsgSize(), sample.RepMsgSize() + 1); } } - + Y_UNIT_TEST(RemoveByIndex) { - TSample msg; - - const Descriptor* descr = msg.GetDescriptor(); - { - TMutableField fld(msg, descr->FindFieldByName("RepMsg")); - msg.AddRepMsg()->AddRepInt(1); - msg.AddRepMsg()->AddRepInt(2); - msg.AddRepMsg()->AddRepInt(3); - - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 + TSample msg; + + const Descriptor* descr = msg.GetDescriptor(); + { + TMutableField fld(msg, descr->FindFieldByName("RepMsg")); + msg.AddRepMsg()->AddRepInt(1); + msg.AddRepMsg()->AddRepInt(2); + msg.AddRepMsg()->AddRepInt(3); + + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 2, 3 fld.Remove(1); // from middle - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - - msg.AddRepMsg()->AddRepInt(5); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + + msg.AddRepMsg()->AddRepInt(5); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepMsgSize()); // 1, 3, 5 fld.Remove(2); // from end - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); - UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); - msg.ClearRepMsg(); - } - - { - TMutableField fld(msg, descr->FindFieldByName("RepStr")); - msg.AddRepStr("1"); - msg.AddRepStr("2"); - msg.AddRepStr("3"); - UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepMsgSize()); + UNIT_ASSERT_VALUES_EQUAL(1, msg.GetRepMsg(0).GetRepInt(0)); + UNIT_ASSERT_VALUES_EQUAL(3, msg.GetRepMsg(1).GetRepInt(0)); + msg.ClearRepMsg(); + } + + { + TMutableField fld(msg, descr->FindFieldByName("RepStr")); + msg.AddRepStr("1"); + msg.AddRepStr("2"); + msg.AddRepStr("3"); + UNIT_ASSERT_VALUES_EQUAL(3, msg.RepStrSize()); // "1", "2", "3" fld.Remove(0); // from begin - UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); - UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); - UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); - } - - { - TMutableField fld(msg, descr->FindFieldByName("OneStr")); - msg.SetOneStr("1"); - UNIT_ASSERT(msg.HasOneStr()); - fld.Remove(0); // not repeated - UNIT_ASSERT(!msg.HasOneStr()); - } - } - + UNIT_ASSERT_VALUES_EQUAL(2, msg.RepStrSize()); + UNIT_ASSERT_VALUES_EQUAL("2", msg.GetRepStr(0)); + UNIT_ASSERT_VALUES_EQUAL("3", msg.GetRepStr(1)); + } + + { + TMutableField fld(msg, descr->FindFieldByName("OneStr")); + msg.SetOneStr("1"); + UNIT_ASSERT(msg.HasOneStr()); + fld.Remove(0); // not repeated + UNIT_ASSERT(!msg.HasOneStr()); + } + } + Y_UNIT_TEST(GetFieldByPath) { - // Simple get by path - { - TSample msg; - msg.SetOneStr("1"); - msg.MutableOneMsg()->AddRepInt(2); - msg.MutableOneMsg()->AddRepInt(3); - msg.AddRepMsg()->AddRepInt(4); - msg.MutableRepMsg(0)->AddRepInt(5); - msg.AddRepMsg()->AddRepInt(6); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + // Simple get by path + { + TSample msg; + msg.SetOneStr("1"); + msg.MutableOneMsg()->AddRepInt(2); + msg.MutableOneMsg()->AddRepInt(3); + msg.AddRepMsg()->AddRepInt(4); + msg.MutableRepMsg(0)->AddRepInt(5); + msg.AddRepMsg()->AddRepInt(6); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("1", (field->Get<TString>())); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT(field->IsMessageInstance<TInnerSample>()); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "/OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(2, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(3, field->Get<int>(1)); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); - UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); - UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); - } - } - - // get of unset fields - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL(2, field->Size()); + UNIT_ASSERT_VALUES_EQUAL(4, field->Get<int>(0)); + UNIT_ASSERT_VALUES_EQUAL(5, field->Get<int>(1)); + } + } + + // get of unset fields + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable - { - TSample msg; - msg.MutableOneMsg(); - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + } + + { + TMaybe<TConstField> field = TConstField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable + { + TSample msg; + msg.MutableOneMsg(); + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); field->Set(TString("zz")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("zz", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr"); + UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); field->Set(TString("dd")); UNIT_ASSERT(field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); - UNIT_ASSERT(field); + UNIT_ASSERT_VALUES_EQUAL("dd", msg.GetOneStr()); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt"); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); - UNIT_ASSERT(!field); - } - } - - // mutable with path creation - { - TSample msg; - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); - UNIT_ASSERT(field); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt"); + UNIT_ASSERT(!field); + } + } + + // mutable with path creation + { + TSample msg; + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneStr", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); - UNIT_ASSERT(field); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "OneMsg/RepInt", true); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT(msg.HasOneMsg()); - field->Add(10); - UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); - } - - { - TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); - TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + UNIT_ASSERT(msg.HasOneMsg()); + field->Add(10); + UNIT_ASSERT_VALUES_EQUAL(10, msg.GetOneMsg().GetRepInt(0)); + } + + { + TMaybe<TMutableField> field = TMutableField::ByPath(msg, "RepMsg/RepInt", true); + TMaybe<TMutableField> fieldCopy = TMutableField::ByPath(msg, "RepMsg/RepInt", true); Y_UNUSED(fieldCopy); - UNIT_ASSERT(field); + UNIT_ASSERT(field); UNIT_ASSERT(!field->HasValue()); - UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); - field->Add(12); - UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); - } - } - - // error - { + UNIT_ASSERT_VALUES_EQUAL(1, msg.RepMsgSize()); + field->Add(12); + UNIT_ASSERT_VALUES_EQUAL(12, field->Get<int>()); + } + } + + // error + { {TSample msg; UNIT_ASSERT(!TConstField::ByPath(msg, "SomeField")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome")); } - + { TSample msg; UNIT_ASSERT(!TMutableField::ByPath(msg, "SomeField/FieldSome", true)); } } - + // extension { TSample msg; @@ -303,13 +303,13 @@ Y_UNIT_TEST_SUITE(ProtobufSimpleReflection) { TInnerSample* subMsg = msg.MutableExtension(NExt::SubMsgExt); subMsg->AddRepInt(20); subMsg->SetExtension(NExt::Ext3Field, 54); - + { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.TTestExt.ExtField"); UNIT_ASSERT(field); UNIT_ASSERT(field->HasValue()); UNIT_ASSERT_VALUES_EQUAL("ext", field->Get<TString>()); - } + } { TMaybe<TConstField> field = TConstField::ByPath(msg, "NExt.ExtField"); UNIT_ASSERT(field); diff --git a/library/cpp/protobuf/util/traits.h b/library/cpp/protobuf/util/traits.h index 50f036d0ea..01b49e4184 100644 --- a/library/cpp/protobuf/util/traits.h +++ b/library/cpp/protobuf/util/traits.h @@ -177,7 +177,7 @@ namespace NProtoBuf { static inline T GetDefault(const FieldDescriptor* field) { return TBaseTraits::GetDefault(field); } - + static inline bool Has(const Message& msg, const FieldDescriptor* field) { return TBaseTraits::Has(msg, field); } diff --git a/library/cpp/protobuf/util/ut/extensions.proto b/library/cpp/protobuf/util/ut/extensions.proto index 4944f0f5ca..0ef6a6fec2 100644 --- a/library/cpp/protobuf/util/ut/extensions.proto +++ b/library/cpp/protobuf/util/ut/extensions.proto @@ -1,22 +1,22 @@ -package NExt; - +package NExt; + import "library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto"; - -message TTestExt { - extend TSample { - optional string ExtField = 100; - } -} - -extend TSample { - optional uint64 ExtField = 150; // the same name, but another full name -} - -extend TSample { - repeated uint64 Ext2Field = 105; - optional TInnerSample SubMsgExt = 111; -} - -extend TInnerSample { - optional uint64 Ext3Field = 100; -} + +message TTestExt { + extend TSample { + optional string ExtField = 100; + } +} + +extend TSample { + optional uint64 ExtField = 150; // the same name, but another full name +} + +extend TSample { + repeated uint64 Ext2Field = 105; + optional TInnerSample SubMsgExt = 111; +} + +extend TInnerSample { + optional uint64 Ext3Field = 100; +} diff --git a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto index cca1dd869a..88e4f0f877 100644 --- a/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto +++ b/library/cpp/protobuf/util/ut/sample_for_simple_reflection.proto @@ -1,16 +1,16 @@ -message TInnerSample { +message TInnerSample { repeated int32 RepInt = 1; - - extensions 100 to 199; + + extensions 100 to 199; } -message TSample { +message TSample { optional string OneStr = 1; - optional TInnerSample OneMsg = 2; - repeated TInnerSample RepMsg = 3; + optional TInnerSample OneMsg = 2; + repeated TInnerSample RepMsg = 3; repeated string RepStr = 4; optional string AnotherOneStr = 5; - + optional int32 OneInt = 6; repeated int32 RepInt = 7; @@ -21,5 +21,5 @@ message TSample { optional EEnum OneEnum = 8; repeated EEnum RepEnum = 9; - extensions 100 to 199; + extensions 100 to 199; } diff --git a/library/cpp/protobuf/util/ut/ya.make b/library/cpp/protobuf/util/ut/ya.make index 701ba9a8c8..dd850af6cb 100644 --- a/library/cpp/protobuf/util/ut/ya.make +++ b/library/cpp/protobuf/util/ut/ya.make @@ -3,16 +3,16 @@ OWNER(nga) UNITTEST_FOR(library/cpp/protobuf/util) SRCS( - extensions.proto + extensions.proto sample_for_is_equal.proto sample_for_simple_reflection.proto common_ut.proto pb_io_ut.cpp - is_equal_ut.cpp + is_equal_ut.cpp iterators_ut.cpp - simple_reflection_ut.cpp - repeated_field_utils_ut.cpp - walk_ut.cpp + simple_reflection_ut.cpp + repeated_field_utils_ut.cpp + walk_ut.cpp merge_ut.cpp ) diff --git a/library/cpp/protobuf/util/walk.h b/library/cpp/protobuf/util/walk.h index d15d76562d..f5559fd907 100644 --- a/library/cpp/protobuf/util/walk.h +++ b/library/cpp/protobuf/util/walk.h @@ -30,4 +30,4 @@ namespace NProtoBuf { // Returned bool defines if we should walk down deeper to current node children (true), or not (false) void WalkSchema(const Descriptor* descriptor, std::function<bool(const FieldDescriptor*)> onField); -} +} diff --git a/library/cpp/protobuf/util/ya.make b/library/cpp/protobuf/util/ya.make index b62028af58..6f0299b76b 100644 --- a/library/cpp/protobuf/util/ya.make +++ b/library/cpp/protobuf/util/ya.make @@ -10,14 +10,14 @@ PEERDIR( ) SRCS( - is_equal.cpp + is_equal.cpp iterators.h merge.cpp - path.cpp - pb_io.cpp + path.cpp + pb_io.cpp pb_utils.h repeated_field_utils.h - simple_reflection.cpp + simple_reflection.cpp walk.cpp ) diff --git a/library/cpp/scheme/scheme.h b/library/cpp/scheme/scheme.h index 3d7c59f3c9..b9319c531e 100644 --- a/library/cpp/scheme/scheme.h +++ b/library/cpp/scheme/scheme.h @@ -294,7 +294,7 @@ namespace NSc { static const EJsonOpts JO_SAFE = TJsonOpts::JO_SAFE; // JO_SORT_KEYS | JO_SKIP_UNSAFE static const EJsonOpts JO_PARSER_STRICT_WITH_COMMENTS = TJsonOpts::JO_PARSER_STRICT_WITH_COMMENTS; // strict json + strict utf8 static const EJsonOpts JO_PARSER_STRICT = TJsonOpts::JO_PARSER_STRICT; // strict json + strict utf8 + comments are disallowed - static const EJsonOpts JO_PARSER_DISALLOW_DUPLICATE_KEYS = TJsonOpts::JO_PARSER_DISALLOW_DUPLICATE_KEYS; + static const EJsonOpts JO_PARSER_DISALLOW_DUPLICATE_KEYS = TJsonOpts::JO_PARSER_DISALLOW_DUPLICATE_KEYS; static TValue FromJson(TStringBuf, const TJsonOpts& = TJsonOpts()); static TValue FromJsonThrow(TStringBuf, const TJsonOpts& = TJsonOpts()); diff --git a/library/cpp/scheme/scimpl.h b/library/cpp/scheme/scimpl.h index 4f68f16290..7f0fc758ef 100644 --- a/library/cpp/scheme/scimpl.h +++ b/library/cpp/scheme/scimpl.h @@ -280,13 +280,13 @@ namespace NSc { TDict::const_iterator it = Dict.find(key); return it != Dict.end() ? it->second : TValue::DefaultValue(); - } - + } + TValue* GetNoAdd(TStringBuf key) { if (!IsDict()) { return nullptr; } - + return Dict.FindPtr(key); } @@ -582,7 +582,7 @@ namespace NSc { TValue& TValue::Back() { return CoreMutable().Back(); } - + const TValue& TValue::Back() const { const TArray& arr = GetArray(); return arr.empty() ? DefaultValue() : arr.back(); diff --git a/library/cpp/scheme/scimpl_defs.h b/library/cpp/scheme/scimpl_defs.h index f3dd66b437..2c02806fb9 100644 --- a/library/cpp/scheme/scimpl_defs.h +++ b/library/cpp/scheme/scimpl_defs.h @@ -74,7 +74,7 @@ namespace NSc { JO_PARSER_STRICT_JSON = 16, // strict standard json JO_PARSER_STRICT_UTF8 = 32, // strict utf8 JO_PARSER_DISALLOW_COMMENTS = 64, - JO_PARSER_DISALLOW_DUPLICATE_KEYS = 128, + JO_PARSER_DISALLOW_DUPLICATE_KEYS = 128, JO_PRETTY = JO_FORMAT | JO_SORT_KEYS, // pretty print json JO_SAFE = JO_SKIP_UNSAFE | JO_SORT_KEYS, // ensure standard parser-safe json @@ -85,8 +85,8 @@ namespace NSc { public: TJsonOpts(int opts = JO_SORT_KEYS) - : Opts(opts) - , SortKeys(opts & JO_SORT_KEYS) + : Opts(opts) + , SortKeys(opts & JO_SORT_KEYS) , FormatJson(opts & JO_FORMAT) , StringPolicy((opts & JO_SKIP_UNSAFE) ? StringPolicySafe : StringPolicyUnsafe) { @@ -97,7 +97,7 @@ namespace NSc { public: bool RelaxedJson = false; - int Opts = 0; + int Opts = 0; bool SortKeys = true; bool FormatJson = false; diff --git a/library/cpp/scheme/scimpl_json_read.cpp b/library/cpp/scheme/scimpl_json_read.cpp index 8a29cc7739..a0edc15d01 100644 --- a/library/cpp/scheme/scimpl_json_read.cpp +++ b/library/cpp/scheme/scimpl_json_read.cpp @@ -22,13 +22,13 @@ namespace NSc { { } - bool Add(TStringBuf v, bool allowDuplicated) { - if (!ExpectKey || Y_UNLIKELY(!Container->IsDict())) - return false; - - if (!allowDuplicated && Y_UNLIKELY(Container->Has(v))) + bool Add(TStringBuf v, bool allowDuplicated) { + if (!ExpectKey || Y_UNLIKELY(!Container->IsDict())) return false; + if (!allowDuplicated && Y_UNLIKELY(Container->Has(v))) + return false; + LastValue = &Container->GetOrAdd(v); ExpectKey = false; return true; @@ -62,16 +62,16 @@ namespace NSc { public: TValue& Root; TJsonError& Error; - const TJsonOpts& Cfg; + const TJsonOpts& Cfg; TStackType Stack; bool Virgin = true; public: - TJsonDeserializer(TValue& root, TJsonError& err, const TJsonOpts& cfg) + TJsonDeserializer(TValue& root, TJsonError& err, const TJsonOpts& cfg) : Root(root) , Error(err) - , Cfg(cfg) + , Cfg(cfg) { Root.SetNull(); Stack.reserve(10); @@ -143,7 +143,7 @@ namespace NSc { bool OnMapKey(const TStringBuf& k) override { if (Y_UNLIKELY(Stack.empty())) return false; - return Stack.back().Add(k, !(Cfg.Opts & TJsonOpts::JO_PARSER_DISALLOW_DUPLICATE_KEYS)); + return Stack.back().Add(k, !(Cfg.Opts & TJsonOpts::JO_PARSER_DISALLOW_DUPLICATE_KEYS)); } bool OnOpenMap() override { @@ -180,8 +180,8 @@ namespace NSc { } }; - static bool DoParseFromJson(TValue& res, TJsonError& err, TStringBuf json, const TJsonOpts& cfg) { - TJsonDeserializer d(res, err, cfg); + static bool DoParseFromJson(TValue& res, TJsonError& err, TStringBuf json, const TJsonOpts& cfg) { + TJsonDeserializer d(res, err, cfg); if (cfg.RelaxedJson) { return NJson::ReadJsonFast(json, &d); @@ -191,7 +191,7 @@ namespace NSc { } } - static bool DoParseFromJson(TValue& res, TStringBuf json, const TJsonOpts& cfg) { + static bool DoParseFromJson(TValue& res, TStringBuf json, const TJsonOpts& cfg) { TJsonError err; return DoParseFromJson(res, err, json, cfg); } diff --git a/library/cpp/scheme/tests/ut/scheme_json_ut.cpp b/library/cpp/scheme/tests/ut/scheme_json_ut.cpp index daeb2654f9..37d635238e 100644 --- a/library/cpp/scheme/tests/ut/scheme_json_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_json_ut.cpp @@ -137,7 +137,7 @@ Y_UNIT_TEST_SUITE(TSchemeJsonTest) { NSc::TValue b = NSc::TValue::FromJsonValue(a.ToJsonValue()); UNIT_ASSERT_JSON_EQ_JSON(a, b); } - + Y_UNIT_TEST(TestJsonEmptyContainers) { { NSc::TValue a = NSc::NUt::AssertFromJson("{a:[]}"); @@ -153,9 +153,9 @@ Y_UNIT_TEST_SUITE(TSchemeJsonTest) { Y_UNIT_TEST(TestDuplicateKeys) { const TStringBuf duplicatedKeys = "{\"a\":[{\"b\":1, \"b\":42}]}"; - UNIT_ASSERT_NO_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys)); - UNIT_ASSERT_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS), yexception); - UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys).IsDict()); - UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS).IsNull()); - } + UNIT_ASSERT_NO_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys)); + UNIT_ASSERT_EXCEPTION(NSc::TValue::FromJsonThrow(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS), yexception); + UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys).IsDict()); + UNIT_ASSERT(NSc::TValue::FromJson(duplicatedKeys, NSc::TValue::JO_PARSER_DISALLOW_DUPLICATE_KEYS).IsNull()); + } }; diff --git a/library/cpp/scheme/tests/ut/scheme_ut.cpp b/library/cpp/scheme/tests/ut/scheme_ut.cpp index 1a5d07c31b..20fe460948 100644 --- a/library/cpp/scheme/tests/ut/scheme_ut.cpp +++ b/library/cpp/scheme/tests/ut/scheme_ut.cpp @@ -863,17 +863,17 @@ Y_UNIT_TEST_SUITE(TSchemeTest) { } Y_UNIT_TEST(TestGetNoAdd) { - NSc::TValue v = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5}}"); + NSc::TValue v = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5}}"); UNIT_ASSERT(v.GetNoAdd("a") != nullptr); UNIT_ASSERT(v.GetNoAdd("b") != nullptr); UNIT_ASSERT(v.GetNoAdd("c") != nullptr); UNIT_ASSERT(v.GetNoAdd("d") == nullptr); UNIT_ASSERT(v.GetNoAdd("value") == nullptr); - + NSc::TValue* child = v.GetNoAdd("c"); UNIT_ASSERT(child != nullptr); (*child)["e"]["f"] = 42; - const NSc::TValue expectedResult = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5,e:{f:42}}}"); - UNIT_ASSERT_VALUES_EQUAL(v, expectedResult); - } + const NSc::TValue expectedResult = NSc::NUt::AssertFromJson("{a:[null,-1,2,3.4],b:3,c:{d:5,e:{f:42}}}"); + UNIT_ASSERT_VALUES_EQUAL(v, expectedResult); + } }; diff --git a/library/cpp/sliding_window/README.md b/library/cpp/sliding_window/README.md index 47692da7d5..b9c952ba8e 100644 --- a/library/cpp/sliding_window/README.md +++ b/library/cpp/sliding_window/README.md @@ -1,30 +1,30 @@ -# TSlidingWindow - скользящее окно - +# TSlidingWindow - скользящее окно + [TSlidingWindow](/arc/trunk/arcadia/library/cpp/sliding_window/sliding_window.h) - класс скользящего окна, позволяющий поддерживать и обновлять определённое значение (максимум, сумму) в промежутке времени определённой длины. Разбивает общий временной промежуток на маленькие бакеты (число задаётся в конструкторе) и ротирует их, поддерживая значение за окно. Есть возможность также указать мьютекс или спинлок для синхронизации (по умолчанию TFakeMutex). Использование: - -``` -// Создаём окно, вычисляющее максимум за последние пять минут, поддерживая 50 бакетов со значениями. -TSlidingWindow<TMaxOperation<int>> window(TDuration::Minutes(5), 50); - -// Загружаем значения в различные моменты времени -window.Update(42, TInstant::Now()); - -... // делаем какую-то работу -int currentMaximum = window.Update(50, TInstant::Now()); - -... // делаем ещё что-то -int currentMaximum = window.Update(25, TInstant::Now()); - -... -// Просто получаем значение максимума за последние 5 минут -int currentMaximum = window.Update(TInstant::Now()); - -... -int currentMaximum = window.GetValue(); // получение значения без обновления времени -``` - -# Поддерживаемые функции - -* `TMaxOperation` - максимум -* `TMinOperation` - минимум -* `TSumOperation` - сумма + +``` +// Создаём окно, вычисляющее максимум за последние пять минут, поддерживая 50 бакетов со значениями. +TSlidingWindow<TMaxOperation<int>> window(TDuration::Minutes(5), 50); + +// Загружаем значения в различные моменты времени +window.Update(42, TInstant::Now()); + +... // делаем какую-то работу +int currentMaximum = window.Update(50, TInstant::Now()); + +... // делаем ещё что-то +int currentMaximum = window.Update(25, TInstant::Now()); + +... +// Просто получаем значение максимума за последние 5 минут +int currentMaximum = window.Update(TInstant::Now()); + +... +int currentMaximum = window.GetValue(); // получение значения без обновления времени +``` + +# Поддерживаемые функции + +* `TMaxOperation` - максимум +* `TMinOperation` - минимум +* `TSumOperation` - сумма diff --git a/library/cpp/sliding_window/sliding_window.h b/library/cpp/sliding_window/sliding_window.h index 180bdf93d0..d140ec7f9c 100644 --- a/library/cpp/sliding_window/sliding_window.h +++ b/library/cpp/sliding_window/sliding_window.h @@ -1,16 +1,16 @@ #pragma once -#include <util/datetime/base.h> -#include <util/generic/vector.h> -#include <util/system/guard.h> -#include <util/system/mutex.h> -#include <util/system/types.h> -#include <util/system/yassert.h> +#include <util/datetime/base.h> +#include <util/generic/vector.h> +#include <util/system/guard.h> +#include <util/system/mutex.h> +#include <util/system/types.h> +#include <util/system/yassert.h> -#include <functional> -#include <limits> +#include <functional> +#include <limits> -namespace NSlidingWindow { +namespace NSlidingWindow { namespace NPrivate { template <class TValueType_, class TCmp, TValueType_ initialValue> // std::less for max, std::greater for min struct TMinMaxOperationImpl { @@ -33,14 +33,14 @@ namespace NSlidingWindow { } } return windowValue; - } + } static TValueType ClearBuckets(TValueType windowValue, TValueVector& buckets, const size_t firstElemIndex, const size_t bucketsToClear) { Y_ASSERT(!buckets.empty()); Y_ASSERT(firstElemIndex < buckets.size()); Y_ASSERT(bucketsToClear <= buckets.size()); TCmp cmp; - + bool needRecalc = false; size_t current = firstElemIndex; const size_t arraySize = buckets.size(); @@ -51,7 +51,7 @@ namespace NSlidingWindow { } curVal = InitialValue(); current = (current + 1) % arraySize; - } + } if (needRecalc) { windowValue = InitialValue(); for (size_t i = 0; i < firstElemIndex; ++i) { @@ -66,28 +66,28 @@ namespace NSlidingWindow { windowValue = val; } } - } + } return windowValue; - } + } }; - } + } template <class TValueType> using TMaxOperation = NPrivate::TMinMaxOperationImpl<TValueType, std::less<TValueType>, std::numeric_limits<TValueType>::min()>; - + template <class TValueType> using TMinOperation = NPrivate::TMinMaxOperationImpl<TValueType, std::greater<TValueType>, std::numeric_limits<TValueType>::max()>; - + template <class TValueType_> struct TSumOperation { using TValueType = TValueType_; using TValueVector = TVector<TValueType>; - + static constexpr TValueType InitialValue() { return TValueType(); // zero } - + // Updates value in current bucket and returns window value static TValueType UpdateBucket(TValueType windowValue, TValueVector& buckets, size_t index, TValueType newVal) { Y_ASSERT(index < buckets.size()); @@ -95,12 +95,12 @@ namespace NSlidingWindow { windowValue += newVal; return windowValue; } - + static TValueType ClearBuckets(TValueType windowValue, TValueVector& buckets, size_t firstElemIndex, size_t bucketsToClear) { Y_ASSERT(!buckets.empty()); Y_ASSERT(firstElemIndex < buckets.size()); Y_ASSERT(bucketsToClear <= buckets.size()); - + const size_t arraySize = buckets.size(); for (size_t i = 0; i < bucketsToClear; ++i) { TValueType& curVal = buckets[firstElemIndex]; @@ -145,17 +145,17 @@ namespace NSlidingWindow { Length = w.Length; MicroSecondsPerBucket = w.MicroSecondsPerBucket; } - + TSlidingWindow(TSlidingWindow&&) = default; - + TSlidingWindow& operator=(TSlidingWindow&&) = default; TSlidingWindow& operator=(const TSlidingWindow&) = delete; - + // Period of time const TDuration& GetDuration() const { return Length; } - + // Update window with new value and time TValueType Update(TValueType val, TInstant t) { TGuard<TMutexImpl> guard(&Mutex); @@ -163,14 +163,14 @@ namespace NSlidingWindow { UpdateCurrentBucket(val); return WindowValue; } - + // Update just time, without new values TValueType Update(TInstant t) { TGuard<TMutexImpl> guard(&Mutex); AdvanceTime(t); return WindowValue; } - + // Get current window value (without updating current time) TValueType GetValue() const { TGuard<TMutexImpl> guard(&Mutex); @@ -182,7 +182,7 @@ namespace NSlidingWindow { const TSizeType arraySize = Buckets.size(); const TSizeType pos = (FirstElem + arraySize - 1) % arraySize; WindowValue = TOperation::UpdateBucket(WindowValue, Buckets, pos, val); - } + } void AdvanceTime(const TInstant& time) { if (time < PeriodStart + Length) { @@ -193,24 +193,24 @@ namespace NSlidingWindow { PeriodStart = time - Length; return; } - + const TInstant& newPeriodStart = time - Length; const ui64 tmDiff = (newPeriodStart - PeriodStart).MicroSeconds(); const TSizeType bucketsDiff = tmDiff / MicroSecondsPerBucket; const TSizeType arraySize = Buckets.size(); const TSizeType buckets = Min(bucketsDiff, arraySize); - + WindowValue = TOperation::ClearBuckets(WindowValue, Buckets, FirstElem, buckets); FirstElem = (FirstElem + buckets) % arraySize; PeriodStart += TDuration::MicroSeconds(bucketsDiff * MicroSecondsPerBucket); - + // Check that PeriodStart lags behind newPeriodStart // (which is actual, uptodate, precise and equal to time - Length) not more // then MicroSecondsPerBucket Y_ASSERT(newPeriodStart >= PeriodStart); Y_ASSERT((newPeriodStart - PeriodStart).MicroSeconds() <= MicroSecondsPerBucket); } - + mutable TMutexImpl Mutex; TValueVector Buckets; diff --git a/library/cpp/sliding_window/sliding_window_ut.cpp b/library/cpp/sliding_window/sliding_window_ut.cpp index 1e7343a8d3..22814fadeb 100644 --- a/library/cpp/sliding_window/sliding_window_ut.cpp +++ b/library/cpp/sliding_window/sliding_window_ut.cpp @@ -2,131 +2,131 @@ #include <library/cpp/testing/unittest/registar.h> -using namespace NSlidingWindow; +using namespace NSlidingWindow; Y_UNIT_TEST_SUITE(TSlidingWindowTest) { Y_UNIT_TEST(TestSlidingWindowMax) { - TSlidingWindow<TMaxOperation<unsigned>> w(TDuration::Minutes(5), 5); + TSlidingWindow<TMaxOperation<unsigned>> w(TDuration::Minutes(5), 5); TInstant start = TInstant::MicroSeconds(TDuration::Hours(1).MicroSeconds()); - TInstant now = start; + TInstant now = start; w.Update(5, start); // ~ ~ ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1) + TDuration::Seconds(1); + now += TDuration::Minutes(1) + TDuration::Seconds(1); w.Update(5, now); // 5 ~ ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(3, now); // 5 3 ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(3); + now += TDuration::Minutes(3); w.Update(2, now); // 5 3 ~ ~ 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(2, now); // 2 3 ~ ~ 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 3); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(2, now); // 2 2 ~ ~ 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 2); // ^ - now += TDuration::Minutes(5); + now += TDuration::Minutes(5); w.Update(1, now); // ~ 1 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 1); // ^ - // update current bucket + // update current bucket w.Update(2, now); // ~ 2 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 2); // ^ - + w.Update(1, now + TDuration::Seconds(30)); // ~ 2 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 2); // ^ - - // test idle - now += TDuration::Minutes(1); + + // test idle + now += TDuration::Minutes(1); w.Update(now); // ~ 2 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 2); // ^ - + now += TDuration::Minutes(5); // ~ ~ ~ ~ ~ - UNIT_ASSERT_VALUES_EQUAL(w.Update(now), 0); + UNIT_ASSERT_VALUES_EQUAL(w.Update(now), 0); } Y_UNIT_TEST(TestSlidingWindowMin) { - TSlidingWindow<TMinOperation<unsigned>> w(TDuration::Minutes(5), 5); + TSlidingWindow<TMinOperation<unsigned>> w(TDuration::Minutes(5), 5); TInstant start = TInstant::MicroSeconds(TDuration::Hours(1).MicroSeconds()); - TInstant now = start; + TInstant now = start; w.Update(5, start); // ~ ~ ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1) + TDuration::Seconds(1); + now += TDuration::Minutes(1) + TDuration::Seconds(1); w.Update(5, now); // 5 ~ ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(7, now); // 5 7 ~ ~ 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(3); + now += TDuration::Minutes(3); w.Update(8, now); // 5 7 ~ ~ 8 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(8, now); // 8 7 ~ ~ 8 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 7); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(8, now); // 8 8 ~ ~ 8 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 8); // ^ - now += TDuration::Minutes(5); + now += TDuration::Minutes(5); w.Update(6, now); // ~ 6 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 6); // ^ - // update current bucket + // update current bucket w.Update(5, now); // ~ 5 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - + w.Update(6, now + TDuration::Seconds(30)); // ~ 5 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - - // test idle - now += TDuration::Minutes(1); + + // test idle + now += TDuration::Minutes(1); w.Update(now); // ~ 5 ~ ~ ~ UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - + now += TDuration::Minutes(5); // ~ ~ ~ ~ ~ - UNIT_ASSERT_VALUES_EQUAL(w.Update(now), std::numeric_limits<unsigned>::max()); - } - + UNIT_ASSERT_VALUES_EQUAL(w.Update(now), std::numeric_limits<unsigned>::max()); + } + Y_UNIT_TEST(TestSlidingWindowSum) { - TSlidingWindow<TSumOperation<unsigned>> w(TDuration::Minutes(5), 5); - UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 0); // current sum - + TSlidingWindow<TSumOperation<unsigned>> w(TDuration::Minutes(5), 5); + UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 0); // current sum + TInstant start = TInstant::MicroSeconds(TDuration::Hours(1).MicroSeconds()); - TInstant now = start; + TInstant now = start; w.Update(5, start); // 0 0 0 0 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 5); // ^ - now += TDuration::Minutes(1) + TDuration::Seconds(1); + now += TDuration::Minutes(1) + TDuration::Seconds(1); w.Update(5, now); // 5 0 0 0 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 10); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(3, now); // 5 3 0 0 5 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 13); // ^ - now += TDuration::Minutes(3); + now += TDuration::Minutes(3); w.Update(2, now); // 5 3 0 0 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 10); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(2, now); // 2 3 0 0 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 7); // ^ - now += TDuration::Minutes(1); + now += TDuration::Minutes(1); w.Update(2, now); // 2 2 0 0 2 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 6); // ^ - now += TDuration::Minutes(5); + now += TDuration::Minutes(5); w.Update(1, now); // 0 1 0 0 0 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 1); // ^ - - // update current bucket + + // update current bucket w.Update(2, now); // 0 3 0 0 0 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 3); // ^ - + w.Update(1, now + TDuration::Seconds(30)); // 0 4 0 0 0 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 4); // ^ - - // test idle - now += TDuration::Minutes(1); + + // test idle + now += TDuration::Minutes(1); w.Update(now); // 0 4 0 0 0 UNIT_ASSERT_VALUES_EQUAL(w.GetValue(), 4); // ^ - + now += TDuration::Minutes(5); // 0 0 0 0 0 - UNIT_ASSERT_VALUES_EQUAL(w.Update(now), 0); - } -} + UNIT_ASSERT_VALUES_EQUAL(w.Update(now), 0); + } +} diff --git a/library/cpp/sliding_window/ut/ya.make b/library/cpp/sliding_window/ut/ya.make index 3839a8dadc..a82a24e5b7 100644 --- a/library/cpp/sliding_window/ut/ya.make +++ b/library/cpp/sliding_window/ut/ya.make @@ -1,9 +1,9 @@ UNITTEST_FOR(library/cpp/sliding_window) - -OWNER(g:kikimr) - -SRCS( - sliding_window_ut.cpp -) - -END() + +OWNER(g:kikimr) + +SRCS( + sliding_window_ut.cpp +) + +END() diff --git a/library/cpp/sliding_window/ya.make b/library/cpp/sliding_window/ya.make index 79aeaa06bb..948ffdcb26 100644 --- a/library/cpp/sliding_window/ya.make +++ b/library/cpp/sliding_window/ya.make @@ -1,10 +1,10 @@ -LIBRARY() - -OWNER(g:kikimr) - -SRCS( - sliding_window.cpp - sliding_window.h -) - -END() +LIBRARY() + +OWNER(g:kikimr) + +SRCS( + sliding_window.cpp + sliding_window.h +) + +END() diff --git a/library/cpp/testing/common/env.h b/library/cpp/testing/common/env.h index 7b89aa1bed..c1247ec96a 100644 --- a/library/cpp/testing/common/env.h +++ b/library/cpp/testing/common/env.h @@ -23,16 +23,16 @@ TString BinaryPath(TStringBuf path); // @brief return true if environment is testenv otherwise false bool FromYaTest(); - + // @brief returns TestsData dir (from env:ARCADIA_TESTS_DATA_DIR or path to existing folder `arcadia_tests_data` within parent folders) TString GetArcadiaTestsData(); // @brief return current working dir (from env:TEST_WORK_PATH or cwd) TString GetWorkPath(); - + // @brief return tests output path (workdir + testing_out_stuff) TFsPath GetOutputPath(); - + // @brief return path from env:YA_TEST_RAM_DRIVE_PATH const TString& GetRamDrivePath(); diff --git a/library/cpp/testing/gmock_in_unittest/events.cpp b/library/cpp/testing/gmock_in_unittest/events.cpp index dbd65b727d..2155fb0d81 100644 --- a/library/cpp/testing/gmock_in_unittest/events.cpp +++ b/library/cpp/testing/gmock_in_unittest/events.cpp @@ -1,32 +1,32 @@ -#include "events.h" - +#include "events.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/strbuf.h> + +#include <util/generic/strbuf.h> #include <util/generic/string.h> -#include <util/string/builder.h> - -void TGMockTestEventListener::OnTestPartResult(const testing::TestPartResult& result) { - if (result.failed()) { +#include <util/string/builder.h> + +void TGMockTestEventListener::OnTestPartResult(const testing::TestPartResult& result) { + if (result.failed()) { const TString message = result.message(); const TString summary = result.summary(); - TStringBuilder msg; - if (result.file_name()) + TStringBuilder msg; + if (result.file_name()) msg << result.file_name() << TStringBuf(":"); - if (result.line_number() != -1) + if (result.line_number() != -1) msg << result.line_number() << TStringBuf(":"); - if (summary) { - if (msg) { + if (summary) { + if (msg) { msg << TStringBuf("\n"); - } - msg << summary; - } - if (message && summary != message) { - if (msg) { + } + msg << summary; + } + if (message && summary != message) { + if (msg) { msg << TStringBuf("\n"); - } - msg << message; - } - NUnitTest::NPrivate::RaiseError(result.summary(), msg, result.fatally_failed()); - } -} + } + msg << message; + } + NUnitTest::NPrivate::RaiseError(result.summary(), msg, result.fatally_failed()); + } +} diff --git a/library/cpp/testing/gmock_in_unittest/events.h b/library/cpp/testing/gmock_in_unittest/events.h index 84c10a93de..11af7dd64c 100644 --- a/library/cpp/testing/gmock_in_unittest/events.h +++ b/library/cpp/testing/gmock_in_unittest/events.h @@ -1,8 +1,8 @@ -#pragma once - -#include <gtest/gtest.h> - -class TGMockTestEventListener: public testing::EmptyTestEventListener { -public: - void OnTestPartResult(const testing::TestPartResult& result) override; -}; +#pragma once + +#include <gtest/gtest.h> + +class TGMockTestEventListener: public testing::EmptyTestEventListener { +public: + void OnTestPartResult(const testing::TestPartResult& result) override; +}; diff --git a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp index 97f19050e4..a6c12b3e5a 100644 --- a/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp +++ b/library/cpp/testing/gmock_in_unittest/example_ut/example_ut.cpp @@ -1,105 +1,105 @@ #include <library/cpp/testing/gmock_in_unittest/gmock.h> - + #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/string.h> - -// Set this variable to true if you want to see failures -///////////////////////////////////////////////////////// -static const bool fail = false; -///////////////////////////////////////////////////////// - -class ITestIface { -public: - virtual ~ITestIface() { - } - - virtual void Func1() = 0; - + +// Set this variable to true if you want to see failures +///////////////////////////////////////////////////////// +static const bool fail = false; +///////////////////////////////////////////////////////// + +class ITestIface { +public: + virtual ~ITestIface() { + } + + virtual void Func1() = 0; + virtual int Func2(const TString&) const = 0; -}; - -class TTestMock: public ITestIface { -public: +}; + +class TTestMock: public ITestIface { +public: MOCK_METHOD(void, Func1, (), (override)); MOCK_METHOD(int, Func2, (const TString&), (const, override)); -}; - -using namespace testing; - +}; + +using namespace testing; + Y_UNIT_TEST_SUITE(TExampleGMockTest) { Y_UNIT_TEST(TSimpleTest) { - TTestMock mock; - EXPECT_CALL(mock, Func1()) - .Times(AtLeast(1)); - - if (!fail) { - mock.Func1(); - } - } - + TTestMock mock; + EXPECT_CALL(mock, Func1()) + .Times(AtLeast(1)); + + if (!fail) { + mock.Func1(); + } + } + Y_UNIT_TEST(TNonExpectedCallTest) { - TTestMock mock; - EXPECT_CALL(mock, Func1()) - .Times(AtMost(1)); - mock.Func1(); - if (fail) { - mock.Func1(); - } - } - + TTestMock mock; + EXPECT_CALL(mock, Func1()) + .Times(AtMost(1)); + mock.Func1(); + if (fail) { + mock.Func1(); + } + } + Y_UNIT_TEST(TReturnValuesTest) { - TTestMock mock; + TTestMock mock; EXPECT_CALL(mock, Func2(TString("1"))) - .WillOnce(Return(1)) - .WillRepeatedly(Return(42)); - + .WillOnce(Return(1)) + .WillRepeatedly(Return(42)); + EXPECT_CALL(mock, Func2(TString("hello"))) - .WillOnce(Return(-1)); - - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); - - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 1); - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); - - if (fail) { - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); // expected to return -1 only once - } - } - + .WillOnce(Return(-1)); + + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); + + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 1); + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("1"), 42); + + if (fail) { + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("hello"), -1); // expected to return -1 only once + } + } + Y_UNIT_TEST(TStrictCallSequenceTest) { - TTestMock mock; - { - InSequence seq; - EXPECT_CALL(mock, Func1()) - .Times(1); - EXPECT_CALL(mock, Func2(_)) - .Times(2) - .WillOnce(Return(1)) - .WillOnce(Return(2)); - EXPECT_CALL(mock, Func1()); - } - mock.Func1(); - UNIT_ASSERT_VALUES_EQUAL(mock.Func2("sample"), 1); - if (fail) { - mock.Func1(); - } - UNIT_ASSERT_VALUES_EQUAL(mock.Func2(""), 2); - if (!fail) { - mock.Func1(); - } - } - + TTestMock mock; + { + InSequence seq; + EXPECT_CALL(mock, Func1()) + .Times(1); + EXPECT_CALL(mock, Func2(_)) + .Times(2) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_CALL(mock, Func1()); + } + mock.Func1(); + UNIT_ASSERT_VALUES_EQUAL(mock.Func2("sample"), 1); + if (fail) { + mock.Func1(); + } + UNIT_ASSERT_VALUES_EQUAL(mock.Func2(""), 2); + if (!fail) { + mock.Func1(); + } + } + Y_UNIT_TEST(TUninterestingMethodIsFailureTest) { - StrictMock<TTestMock> mock; - EXPECT_CALL(mock, Func1()) - .Times(1); - mock.Func1(); - if (fail) { - mock.Func1(); - } - } -} + StrictMock<TTestMock> mock; + EXPECT_CALL(mock, Func1()) + .Times(1); + mock.Func1(); + if (fail) { + mock.Func1(); + } + } +} diff --git a/library/cpp/testing/gmock_in_unittest/example_ut/ya.make b/library/cpp/testing/gmock_in_unittest/example_ut/ya.make index d2e5ee5d2a..81c9ee3fca 100644 --- a/library/cpp/testing/gmock_in_unittest/example_ut/ya.make +++ b/library/cpp/testing/gmock_in_unittest/example_ut/ya.make @@ -1,13 +1,13 @@ -UNITTEST() - -OWNER(galaxycrab) - -PEERDIR( +UNITTEST() + +OWNER(galaxycrab) + +PEERDIR( library/cpp/testing/gmock_in_unittest -) - -SRCS( - example_ut.cpp -) - -END() +) + +SRCS( + example_ut.cpp +) + +END() diff --git a/library/cpp/testing/gmock_in_unittest/gmock.h b/library/cpp/testing/gmock_in_unittest/gmock.h index 31f6aee1c3..65bfc5a5ed 100644 --- a/library/cpp/testing/gmock_in_unittest/gmock.h +++ b/library/cpp/testing/gmock_in_unittest/gmock.h @@ -1,5 +1,5 @@ -#pragma once +#pragma once #include <library/cpp/testing/gtest_extensions/gtest_extensions.h> - -#include <gmock/gmock.h> + +#include <gmock/gmock.h> diff --git a/library/cpp/testing/gmock_in_unittest/registration.cpp b/library/cpp/testing/gmock_in_unittest/registration.cpp index c2872a4c27..f6b9e516d1 100644 --- a/library/cpp/testing/gmock_in_unittest/registration.cpp +++ b/library/cpp/testing/gmock_in_unittest/registration.cpp @@ -1,20 +1,20 @@ -#include "events.h" - -#include <gmock/gmock.h> - +#include "events.h" + +#include <gmock/gmock.h> + #include <library/cpp/testing/unittest/plugin.h> - -namespace { - class TGMockUnittestPlugin: public NUnitTest::NPlugin::IPlugin { - public: - void OnStartMain(int argc, char* argv[]) override { - testing::InitGoogleMock(&argc, argv); - testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners(); - delete listeners.Release(listeners.default_result_printer()); - listeners.Append(new TGMockTestEventListener()); - } - }; - - NUnitTest::NPlugin::TPluginRegistrator registerGMock(new TGMockUnittestPlugin()); - + +namespace { + class TGMockUnittestPlugin: public NUnitTest::NPlugin::IPlugin { + public: + void OnStartMain(int argc, char* argv[]) override { + testing::InitGoogleMock(&argc, argv); + testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners(); + delete listeners.Release(listeners.default_result_printer()); + listeners.Append(new TGMockTestEventListener()); + } + }; + + NUnitTest::NPlugin::TPluginRegistrator registerGMock(new TGMockUnittestPlugin()); + } diff --git a/library/cpp/testing/gmock_in_unittest/ya.make b/library/cpp/testing/gmock_in_unittest/ya.make index 5de68ad98d..c8a6e33396 100644 --- a/library/cpp/testing/gmock_in_unittest/ya.make +++ b/library/cpp/testing/gmock_in_unittest/ya.make @@ -1,17 +1,17 @@ -LIBRARY() - -OWNER(galaxycrab) - -PEERDIR( +LIBRARY() + +OWNER(galaxycrab) + +PEERDIR( contrib/restricted/googletest/googlemock contrib/restricted/googletest/googletest library/cpp/testing/gtest_extensions library/cpp/testing/unittest -) - -SRCS( - events.cpp - GLOBAL registration.cpp -) - -END() +) + +SRCS( + events.cpp + GLOBAL registration.cpp +) + +END() diff --git a/library/cpp/testing/unittest/plugin.cpp b/library/cpp/testing/unittest/plugin.cpp index 543112f7ac..fd80eb48ac 100644 --- a/library/cpp/testing/unittest/plugin.cpp +++ b/library/cpp/testing/unittest/plugin.cpp @@ -1,50 +1,50 @@ -#include "plugin.h" - -#include <util/generic/singleton.h> -#include <util/generic/vector.h> -#include <util/generic/utility.h> - -namespace NUnitTest { - namespace NPlugin { - namespace { - class TPlugins { - public: - void OnStartMain(int argc, char* argv[]) const { - for (const auto& plugin : Plugins) { - plugin->OnStartMain(argc, argv); - } - } - - void OnStopMain(int argc, char* argv[]) const { - for (const auto& plugin : Plugins) { - plugin->OnStopMain(argc, argv); - } - } - - void Register(TSimpleSharedPtr<IPlugin> plugin) { - Plugins.emplace_back(std::move(plugin)); - } - - static TPlugins& Instance() { - return *Singleton<TPlugins>(); - } - - private: +#include "plugin.h" + +#include <util/generic/singleton.h> +#include <util/generic/vector.h> +#include <util/generic/utility.h> + +namespace NUnitTest { + namespace NPlugin { + namespace { + class TPlugins { + public: + void OnStartMain(int argc, char* argv[]) const { + for (const auto& plugin : Plugins) { + plugin->OnStartMain(argc, argv); + } + } + + void OnStopMain(int argc, char* argv[]) const { + for (const auto& plugin : Plugins) { + plugin->OnStopMain(argc, argv); + } + } + + void Register(TSimpleSharedPtr<IPlugin> plugin) { + Plugins.emplace_back(std::move(plugin)); + } + + static TPlugins& Instance() { + return *Singleton<TPlugins>(); + } + + private: TVector<TSimpleSharedPtr<IPlugin>> Plugins; - }; - } // anonymous namespace - - TPluginRegistrator::TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin) { - TPlugins::Instance().Register(std::move(plugin)); - } - - void OnStartMain(int argc, char* argv[]) { - TPlugins::Instance().OnStartMain(argc, argv); - } - - void OnStopMain(int argc, char* argv[]) { - TPlugins::Instance().OnStopMain(argc, argv); - } - + }; + } // anonymous namespace + + TPluginRegistrator::TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin) { + TPlugins::Instance().Register(std::move(plugin)); + } + + void OnStartMain(int argc, char* argv[]) { + TPlugins::Instance().OnStartMain(argc, argv); + } + + void OnStopMain(int argc, char* argv[]) { + TPlugins::Instance().OnStopMain(argc, argv); + } + } } diff --git a/library/cpp/testing/unittest/plugin.h b/library/cpp/testing/unittest/plugin.h index 102f2c1469..7cce973c78 100644 --- a/library/cpp/testing/unittest/plugin.h +++ b/library/cpp/testing/unittest/plugin.h @@ -1,29 +1,29 @@ -#pragma once - -#include <util/generic/ptr.h> - -namespace NUnitTest { +#pragma once + +#include <util/generic/ptr.h> + +namespace NUnitTest { // Plugins are deprecated, please use Y_TEST_HOOK_* from library/cpp/hook/hook.h - namespace NPlugin { - class IPlugin { - public: - virtual ~IPlugin() { - } - - virtual void OnStartMain(int /*argc*/, char* /*argv*/ []) { - } - - virtual void OnStopMain(int /*argc*/, char* /*argv*/ []) { - } - }; - - void OnStartMain(int argc, char* argv[]); - void OnStopMain(int argc, char* argv[]); - - class TPluginRegistrator { - public: - TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin); - }; - + namespace NPlugin { + class IPlugin { + public: + virtual ~IPlugin() { + } + + virtual void OnStartMain(int /*argc*/, char* /*argv*/ []) { + } + + virtual void OnStopMain(int /*argc*/, char* /*argv*/ []) { + } + }; + + void OnStartMain(int argc, char* argv[]); + void OnStopMain(int argc, char* argv[]); + + class TPluginRegistrator { + public: + TPluginRegistrator(TSimpleSharedPtr<IPlugin> plugin); + }; + } } diff --git a/library/cpp/testing/unittest/registar.cpp b/library/cpp/testing/unittest/registar.cpp index 3679b768ed..fea174a65e 100644 --- a/library/cpp/testing/unittest/registar.cpp +++ b/library/cpp/testing/unittest/registar.cpp @@ -1,13 +1,13 @@ -#include "registar.h" - +#include "registar.h" + #include <library/cpp/diff/diff.h> #include <library/cpp/colorizer/colors.h> -#include <util/generic/bt_exception.h> -#include <util/random/fast.h> +#include <util/generic/bt_exception.h> +#include <util/random/fast.h> #include <util/string/printf.h> -#include <util/system/backtrace.h> -#include <util/system/guard.h> +#include <util/system/backtrace.h> +#include <util/system/guard.h> #include <util/system/tls.h> #include <util/system/error.h> #include <util/string/cast.h> @@ -32,46 +32,46 @@ Y_POD_STATIC_THREAD(bool) UnittestThread; Y_POD_STATIC_THREAD(NUnitTest::TTestBase*) currentTest; -::NUnitTest::TRaiseErrorHandler RaiseErrorHandler; +::NUnitTest::TRaiseErrorHandler RaiseErrorHandler; void ::NUnitTest::NPrivate::RaiseError(const char* what, const TString& msg, bool fatalFailure) { Y_VERIFY(UnittestThread, "%s in non-unittest thread with message:\n%s", what, msg.data()); - Y_VERIFY(GetCurrentTest()); - - if (RaiseErrorHandler) { - RaiseErrorHandler(what, msg, fatalFailure); - return; - } - - // Default handler - TBackTrace bt; - bt.Capture(); + Y_VERIFY(GetCurrentTest()); + + if (RaiseErrorHandler) { + RaiseErrorHandler(what, msg, fatalFailure); + return; + } + + // Default handler + TBackTrace bt; + bt.Capture(); GetCurrentTest()->AddError(msg.data(), bt.PrintToString()); - if (::NUnitTest::ContinueOnFail || !fatalFailure) { - return; - } + if (::NUnitTest::ContinueOnFail || !fatalFailure) { + return; + } throw TAssertException(); } -void ::NUnitTest::SetRaiseErrorHandler(::NUnitTest::TRaiseErrorHandler handler) { - Y_VERIFY(UnittestThread); - RaiseErrorHandler = std::move(handler); -} - +void ::NUnitTest::SetRaiseErrorHandler(::NUnitTest::TRaiseErrorHandler handler) { + Y_VERIFY(UnittestThread); + RaiseErrorHandler = std::move(handler); +} + void ::NUnitTest::NPrivate::SetUnittestThread(bool unittestThread) { Y_VERIFY(UnittestThread != unittestThread, "state check"); UnittestThread = unittestThread; } -void ::NUnitTest::NPrivate::SetCurrentTest(TTestBase* test) { - Y_VERIFY(!test || !currentTest, "state check"); - currentTest = test; -} - -NUnitTest::TTestBase* ::NUnitTest::NPrivate::GetCurrentTest() { - return currentTest; -} - +void ::NUnitTest::NPrivate::SetCurrentTest(TTestBase* test) { + Y_VERIFY(!test || !currentTest, "state check"); + currentTest = test; +} + +NUnitTest::TTestBase* ::NUnitTest::NPrivate::GetCurrentTest() { + return currentTest; +} + struct TDiffColorizer { NColorizer::TColors Colors; bool Reverse = false; @@ -321,12 +321,12 @@ void NUnitTest::TTestBase::AddError(const char* msg, TTestContext* context) { AddError(msg, TString(), context); } -void NUnitTest::TTestBase::RunAfterTest(std::function<void()> f) { - with_lock (AfterTestFunctionsLock_) { - AfterTestFunctions_.emplace_back(std::move(f)); - } -} - +void NUnitTest::TTestBase::RunAfterTest(std::function<void()> f) { + with_lock (AfterTestFunctionsLock_) { + AfterTestFunctions_.emplace_back(std::move(f)); + } +} + bool NUnitTest::TTestBase::CheckAccessTest(const char* test) { return Processor()->CheckAccessTest(Name(), test); } @@ -376,18 +376,18 @@ void NUnitTest::TTestBase::BeforeTest() { void NUnitTest::TTestBase::AfterTest() { TearDown(); - - TVector<std::function<void()>> afterTestFunctions; - with_lock (AfterTestFunctionsLock_) { - afterTestFunctions.swap(AfterTestFunctions_); - } - - for (auto i = afterTestFunctions.rbegin(); i != afterTestFunctions.rend(); ++i) { - std::function<void()>& f = *i; - if (f) { - f(); - } - } + + TVector<std::function<void()>> afterTestFunctions; + with_lock (AfterTestFunctionsLock_) { + afterTestFunctions.swap(AfterTestFunctions_); + } + + for (auto i = afterTestFunctions.rbegin(); i != afterTestFunctions.rend(); ++i) { + std::function<void()>& f = *i; + if (f) { + f(); + } + } } bool NUnitTest::TTestBase::GetIsForked() const { diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index 44517a0092..be38300e1e 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -2,41 +2,41 @@ #include <library/cpp/dbg_output/dump.h> -#include <util/generic/bt_exception.h> +#include <util/generic/bt_exception.h> #include <util/generic/hash.h> -#include <util/generic/intrlist.h> +#include <util/generic/intrlist.h> #include <util/generic/map.h> #include <util/generic/ptr.h> #include <util/generic/set.h> #include <util/generic/typetraits.h> #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/string/builder.h> #include <util/string/cast.h> #include <util/string/printf.h> -#include <util/system/defaults.h> +#include <util/system/defaults.h> #include <util/system/type_name.h> -#include <util/system/spinlock.h> -#include <util/system/src_location.h> - +#include <util/system/spinlock.h> +#include <util/system/src_location.h> + #include <util/system/rusage.h> #include <cmath> #include <cstdio> -#include <functional> - +#include <functional> + extern bool CheckExceptionMessage(const char*, TString&); namespace NUnitTest { class TTestBase; - + namespace NPrivate { void RaiseError(const char* what, const TString& msg, bool fatalFailure); void SetUnittestThread(bool); - void SetCurrentTest(TTestBase*); - TTestBase* GetCurrentTest(); + void SetCurrentTest(TTestBase*); + TTestBase* GetCurrentTest(); } extern bool ShouldColorizeDiff; @@ -45,21 +45,21 @@ namespace NUnitTest { TString GetFormatTag(const char* name); TString GetResetTag(); - // Raise error handler + // Raise error handler // Used for testing library/cpp/testing/unittest macroses - // and unittest helpers. - // For all other unittests standard handler is used - using TRaiseErrorHandler = std::function<void(const char*, const TString&, bool)>; - - void SetRaiseErrorHandler(TRaiseErrorHandler handler); - - inline void ClearRaiseErrorHandler() { - SetRaiseErrorHandler(TRaiseErrorHandler()); - } - - class TAssertException: public yexception { - }; - + // and unittest helpers. + // For all other unittests standard handler is used + using TRaiseErrorHandler = std::function<void(const char*, const TString&, bool)>; + + void SetRaiseErrorHandler(TRaiseErrorHandler handler); + + inline void ClearRaiseErrorHandler() { + SetRaiseErrorHandler(TRaiseErrorHandler()); + } + + class TAssertException: public yexception { + }; + class ITestSuiteProcessor; struct TTestContext { @@ -97,10 +97,10 @@ namespace NUnitTest { TTestContext* Context; }; - struct TFinish { + struct TFinish { const TTest* test; TTestContext* Context; - bool Success; + bool Success; }; ITestSuiteProcessor(); @@ -159,9 +159,9 @@ namespace NUnitTest { virtual void OnBeforeTest(const TTest* /*test*/); void AddTestError(const TTest& test); - + void AddTestFinish(const TTest& test); - + private: TMap<TString, size_t> TestErrors_; TMap<TString, size_t> CurTestErrors_; @@ -203,11 +203,11 @@ namespace NUnitTest { virtual void TearDown(); void AddError(const char* msg, const TString& backtrace = TString(), TTestContext* context = nullptr); - + void AddError(const char* msg, TTestContext* context); - - void RunAfterTest(std::function<void()> f); // function like atexit to run after current unit test - + + void RunAfterTest(std::function<void()> f); // function like atexit to run after current unit test + protected: bool CheckAccessTest(const char* test); @@ -243,10 +243,10 @@ namespace NUnitTest { private: TTestFactory* Parent_; - size_t TestErrors_; - const char* CurrentSubtest_; - TAdaptiveLock AfterTestFunctionsLock_; - TVector<std::function<void()>> AfterTestFunctions_; + size_t TestErrors_; + const char* CurrentSubtest_; + TAdaptiveLock AfterTestFunctionsLock_; + TVector<std::function<void()>> AfterTestFunctions_; }; #define UNIT_TEST_SUITE(N) \ @@ -304,8 +304,8 @@ private: \ thiz->F(); \ } \ }; \ - this->TTestBase::Run(std::bind(&T##F##Caller::X, this, context), StaticName(), (#F), FF); \ - } + this->TTestBase::Run(std::bind(&T##F##Caller::X, this, context), StaticName(), (#F), FF); \ + } #define UNIT_TEST_IMPL(F, FF) \ UNIT_TEST_CHECK_TEST_IS_DECLARED_ONLY_ONCE(F) { \ @@ -336,20 +336,20 @@ private: \ /* forked process (or main without "--fork-tests") treats some exceptions as success - it's exception test! */ \ } else { \ NUnitTest::TTestContext context(this->TTestBase::Processor()); \ - if (this->CheckAccessTest((#F))) { \ - try { \ - UNIT_TEST_RUN(F, false, context) \ - this->AddError("exception expected", &context); \ - } catch (const ::NUnitTest::TAssertException&) { \ - } catch (const E& e) { \ + if (this->CheckAccessTest((#F))) { \ + try { \ + UNIT_TEST_RUN(F, false, context) \ + this->AddError("exception expected", &context); \ + } catch (const ::NUnitTest::TAssertException&) { \ + } catch (const E& e) { \ TString err; \ - if (!CheckExceptionMessage(e.what(), err)) \ - this->AddError(err.c_str(), &context); \ - } catch (const std::exception& e) { \ - this->AddError(e.what(), &context); \ - } catch (...) { \ - this->AddError("non-std exception!", &context); \ - } \ + if (!CheckExceptionMessage(e.what(), err)) \ + this->AddError(err.c_str(), &context); \ + } catch (const std::exception& e) { \ + this->AddError(e.what(), &context); \ + } catch (...) { \ + this->AddError("non-std exception!", &context); \ + } \ this->Finish((#F), &context); \ } \ } @@ -366,13 +366,13 @@ public: \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, true); \ } while (false) -#define UNIT_FAIL_NONFATAL_IMPL(R, M) \ - do { \ +#define UNIT_FAIL_NONFATAL_IMPL(R, M) \ + do { \ ::NUnitTest::NPrivate::RaiseError(R, ::TStringBuilder() << R << " at " << __LOCATION__ << ", " << __PRETTY_FUNCTION__ << ": " << M, false); \ - } while (false) - + } while (false) + #define UNIT_FAIL(M) UNIT_FAIL_IMPL("forced failure", M) -#define UNIT_FAIL_NONFATAL(M) UNIT_FAIL_NONFATAL_IMPL("forced failure", M) +#define UNIT_FAIL_NONFATAL(M) UNIT_FAIL_NONFATAL_IMPL("forced failure", M) //types #define UNIT_ASSERT_TYPES_EQUAL(A, B) \ @@ -536,20 +536,20 @@ public: \ #define UNIT_ASSERT_GE(A, B) UNIT_ASSERT_GE_C(A, B, "") -#define UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, C) \ +#define UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, C) \ do { \ - try { \ - (void)(A); \ - } catch (const ::NUnitTest::TAssertException&) { \ - throw; \ - } catch (const E&) { \ - break; \ - } \ - UNIT_ASSERT_C(0, "Exception hasn't been thrown, but it should have happened " << C); \ + try { \ + (void)(A); \ + } catch (const ::NUnitTest::TAssertException&) { \ + throw; \ + } catch (const E&) { \ + break; \ + } \ + 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_EXCEPTION(A, E) UNIT_CHECK_GENERATED_EXCEPTION_C(A, E, "") + #define UNIT_CHECK_GENERATED_NO_EXCEPTION_C(A, E, C) \ do { \ try { \ @@ -562,7 +562,7 @@ public: \ } 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 #define UNIT_ASSERT_EXCEPTION_SATISFIES_C(A, E, pred, C) \ do { \ @@ -588,7 +588,7 @@ public: \ #A << " did not throw any exception" \ << " (expected " << #E << ") " << C); \ } \ - } while (false) + } while (false) // Assert that a specific exception is thrown and satisfies predicate pred(e), where e is the exception instance. // Example: @@ -618,15 +618,15 @@ public: \ } while (false) // Assert that a specific exception is thrown and CurrentExceptionMessage() contains substr -#define UNIT_ASSERT_EXCEPTION_CONTAINS(A, E, substr) \ - UNIT_ASSERT_EXCEPTION_CONTAINS_C(A, E, substr, "") - +#define UNIT_ASSERT_EXCEPTION_CONTAINS(A, E, substr) \ + UNIT_ASSERT_EXCEPTION_CONTAINS_C(A, E, substr, "") + // Same as UNIT_ASSERT_EXCEPTION but prints additional string C when nothing was thrown #define UNIT_ASSERT_EXCEPTION_C(A, E, C) UNIT_ASSERT_EXCEPTION_SATISFIES_C(A, E, [](const E&){ return true; }, C) - + // Assert that a specific exception is thrown -#define UNIT_ASSERT_EXCEPTION(A, E) UNIT_ASSERT_EXCEPTION_C(A, E, "") - +#define UNIT_ASSERT_EXCEPTION(A, E) UNIT_ASSERT_EXCEPTION_C(A, E, "") + #define UNIT_ASSERT_NO_EXCEPTION_RESULT_C(A, C) \ [&] () mutable -> decltype(A) { \ static_assert(!std::is_void_v<decltype(A)>); \ @@ -653,10 +653,10 @@ public: \ } catch (...) { \ UNIT_FAIL_IMPL("exception-free assertion failed", Sprintf("%s throws %s\nException message: %s", #A, (::TStringBuilder() << C).data(), CurrentExceptionMessage().data())); \ } \ - } while (false) - -#define UNIT_ASSERT_NO_EXCEPTION(A) UNIT_ASSERT_NO_EXCEPTION_C(A, "") + } while (false) +#define UNIT_ASSERT_NO_EXCEPTION(A) UNIT_ASSERT_NO_EXCEPTION_C(A, "") + namespace NPrivate { template <class T, class U, bool Integers> struct TCompareValuesImpl { @@ -719,7 +719,7 @@ public: \ } \ UNIT_FAIL_IMPL("assertion failed", failMsg); \ } \ - } while (false) + } while (false) #define UNIT_ASSERT_VALUES_EQUAL_C(A, B, C) \ UNIT_ASSERT_VALUES_EQUAL_IMPL(A, B, C, true, "==", "!=") @@ -730,28 +730,28 @@ public: \ #define UNIT_ASSERT_VALUES_EQUAL(A, B) UNIT_ASSERT_VALUES_EQUAL_C(A, B, "") #define UNIT_ASSERT_VALUES_UNEQUAL(A, B) UNIT_ASSERT_VALUES_UNEQUAL_C(A, B, "") -// Checks that test will fail while executing given expression -// Macro for using in unitests for ut helpers -#define UNIT_ASSERT_TEST_FAILS_C(A, C) \ - do { \ - ::NUnitTest::TUnitTestFailChecker checker; \ - try { \ - auto guard = checker.InvokeGuard(); \ - (void)(A); \ - } catch (...) { \ - UNIT_FAIL_IMPL("fail test assertion failure", \ - "code is expected to generate test failure, " \ - "but it throws exception with message: " \ +// Checks that test will fail while executing given expression +// Macro for using in unitests for ut helpers +#define UNIT_ASSERT_TEST_FAILS_C(A, C) \ + do { \ + ::NUnitTest::TUnitTestFailChecker checker; \ + try { \ + auto guard = checker.InvokeGuard(); \ + (void)(A); \ + } catch (...) { \ + UNIT_FAIL_IMPL("fail test assertion failure", \ + "code is expected to generate test failure, " \ + "but it throws exception with message: " \ << CurrentExceptionMessage()); \ - } \ - if (!checker.Failed()) { \ - UNIT_FAIL_IMPL("fail test assertion failure", \ - "code is expected to generate test failure"); \ - } \ - } while (false) - -#define UNIT_ASSERT_TEST_FAILS(A) UNIT_ASSERT_TEST_FAILS_C(A, "") - + } \ + if (!checker.Failed()) { \ + UNIT_FAIL_IMPL("fail test assertion failure", \ + "code is expected to generate test failure"); \ + } \ + } while (false) + +#define UNIT_ASSERT_TEST_FAILS(A) UNIT_ASSERT_TEST_FAILS_C(A, "") + #define UNIT_ADD_METRIC(name, value) ut_context.Metrics[name] = value class TTestFactory { @@ -840,77 +840,77 @@ public: \ using TBaseFixture = TBaseTestCase; - // Class for checking that code raises unittest failure - class TUnitTestFailChecker { - public: - struct TInvokeGuard { - explicit TInvokeGuard(TUnitTestFailChecker& parent) - : Parent(&parent) - { - Parent->SetHandler(); - } - + // Class for checking that code raises unittest failure + class TUnitTestFailChecker { + public: + struct TInvokeGuard { + explicit TInvokeGuard(TUnitTestFailChecker& parent) + : Parent(&parent) + { + Parent->SetHandler(); + } + TInvokeGuard(TInvokeGuard&& guard) noexcept - : Parent(guard.Parent) - { - guard.Parent = nullptr; - } - - ~TInvokeGuard() { - if (Parent) { - ClearRaiseErrorHandler(); - } - } - - TUnitTestFailChecker* Parent; - }; - - TUnitTestFailChecker() = default; - TUnitTestFailChecker(const TUnitTestFailChecker&) = delete; - TUnitTestFailChecker(TUnitTestFailChecker&&) = delete; - - TInvokeGuard InvokeGuard() { - return TInvokeGuard(*this); - } - - const TString& What() const { - return What_; - } - - const TString& Msg() const { - return Msg_; - } - - bool FatalFailure() const { - return FatalFailure_; - } - - bool Failed() const { - return Failed_; - } - - private: - void Handler(const char* what, const TString& msg, bool fatalFailure) { - What_ = what; - Msg_ = msg; - FatalFailure_ = fatalFailure; - Failed_ = true; - } - - void SetHandler() { - TRaiseErrorHandler handler = [this](const char* what, const TString& msg, bool fatalFailure) { - Handler(what, msg, fatalFailure); - }; - SetRaiseErrorHandler(std::move(handler)); - } - - private: - TString What_; - TString Msg_; - bool FatalFailure_ = false; - bool Failed_ = false; - }; - + : Parent(guard.Parent) + { + guard.Parent = nullptr; + } + + ~TInvokeGuard() { + if (Parent) { + ClearRaiseErrorHandler(); + } + } + + TUnitTestFailChecker* Parent; + }; + + TUnitTestFailChecker() = default; + TUnitTestFailChecker(const TUnitTestFailChecker&) = delete; + TUnitTestFailChecker(TUnitTestFailChecker&&) = delete; + + TInvokeGuard InvokeGuard() { + return TInvokeGuard(*this); + } + + const TString& What() const { + return What_; + } + + const TString& Msg() const { + return Msg_; + } + + bool FatalFailure() const { + return FatalFailure_; + } + + bool Failed() const { + return Failed_; + } + + private: + void Handler(const char* what, const TString& msg, bool fatalFailure) { + What_ = what; + Msg_ = msg; + FatalFailure_ = fatalFailure; + Failed_ = true; + } + + void SetHandler() { + TRaiseErrorHandler handler = [this](const char* what, const TString& msg, bool fatalFailure) { + Handler(what, msg, fatalFailure); + }; + SetRaiseErrorHandler(std::move(handler)); + } + + private: + TString What_; + TString Msg_; + bool FatalFailure_ = false; + bool Failed_ = false; + }; + #define UNIT_TEST_SUITE_REGISTRATION(T) \ static const ::NUnitTest::TTestBaseFactory<T> Y_GENERATE_UNIQUE_ID(UTREG_); diff --git a/library/cpp/testing/unittest/registar_ut.cpp b/library/cpp/testing/unittest/registar_ut.cpp index 1f36d53abb..9442eb3d0a 100644 --- a/library/cpp/testing/unittest/registar_ut.cpp +++ b/library/cpp/testing/unittest/registar_ut.cpp @@ -1,132 +1,132 @@ #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { Y_UNIT_TEST(Assert) { - auto unitAssert = [] { - UNIT_ASSERT(false); - }; - UNIT_ASSERT_TEST_FAILS(unitAssert()); - - UNIT_ASSERT(true); - } - + auto unitAssert = [] { + UNIT_ASSERT(false); + }; + UNIT_ASSERT_TEST_FAILS(unitAssert()); + + UNIT_ASSERT(true); + } + Y_UNIT_TEST(TypesEqual) { - auto typesEqual = [] { - UNIT_ASSERT_TYPES_EQUAL(int, long); - }; - UNIT_ASSERT_TEST_FAILS(typesEqual()); - - UNIT_ASSERT_TYPES_EQUAL(TString, TString); - } - + auto typesEqual = [] { + UNIT_ASSERT_TYPES_EQUAL(int, long); + }; + UNIT_ASSERT_TEST_FAILS(typesEqual()); + + UNIT_ASSERT_TYPES_EQUAL(TString, TString); + } + Y_UNIT_TEST(DoublesEqual) { - auto doublesEqual = [](double d1, double d2, double precision) { - UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision); - }; - UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1)); - UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1)); - - UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1); - UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1); + auto doublesEqual = [](double d1, double d2, double precision) { + UNIT_ASSERT_DOUBLES_EQUAL(d1, d2, precision); + }; + UNIT_ASSERT_TEST_FAILS(doublesEqual(0.0, 0.5, 0.1)); + UNIT_ASSERT_TEST_FAILS(doublesEqual(0.1, -0.1, 0.1)); + + UNIT_ASSERT_DOUBLES_EQUAL(0.0, 0.01, 0.1); + UNIT_ASSERT_DOUBLES_EQUAL(0.01, 0.0, 0.1); constexpr auto nan = std::numeric_limits<double>::quiet_NaN(); UNIT_ASSERT_TEST_FAILS(doublesEqual(nan, 0.5, 0.1)); UNIT_ASSERT_TEST_FAILS(doublesEqual(0.5, nan, 0.1)); UNIT_ASSERT_DOUBLES_EQUAL(nan, nan, 0.1); - } - + } + Y_UNIT_TEST(StringsEqual) { - auto stringsEqual = [](auto s1, auto s2) { - UNIT_ASSERT_STRINGS_EQUAL(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w")); - UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w"))); - UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w")); - UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b"))); + auto stringsEqual = [](auto s1, auto s2) { + UNIT_ASSERT_STRINGS_EQUAL(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(stringsEqual("q", "w")); + UNIT_ASSERT_TEST_FAILS(stringsEqual("q", TString("w"))); + UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("q"), "w")); + UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TString("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual(TString("a"), TStringBuf("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual("a", TStringBuf("b"))); UNIT_ASSERT_TEST_FAILS(stringsEqual(TStringBuf("a"), "b")); - - TString empty; - TStringBuf emptyBuf; - UNIT_ASSERT_STRINGS_EQUAL("", empty); - UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf); - UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr)); - } - + + TString empty; + TStringBuf emptyBuf; + UNIT_ASSERT_STRINGS_EQUAL("", empty); + UNIT_ASSERT_STRINGS_EQUAL(empty, emptyBuf); + UNIT_ASSERT_STRINGS_EQUAL("", static_cast<const char*>(nullptr)); + } + Y_UNIT_TEST(StringContains) { - auto stringContains = [](auto s, auto substr) { - UNIT_ASSERT_STRING_CONTAINS(s, substr); - }; - UNIT_ASSERT_TEST_FAILS(stringContains("", "a")); - UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar")); - - UNIT_ASSERT_STRING_CONTAINS("", ""); - UNIT_ASSERT_STRING_CONTAINS("a", ""); - UNIT_ASSERT_STRING_CONTAINS("failure", "fail"); - UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more"); - } - + auto stringContains = [](auto s, auto substr) { + UNIT_ASSERT_STRING_CONTAINS(s, substr); + }; + UNIT_ASSERT_TEST_FAILS(stringContains("", "a")); + UNIT_ASSERT_TEST_FAILS(stringContains("lurkmore", "moar")); + + UNIT_ASSERT_STRING_CONTAINS("", ""); + UNIT_ASSERT_STRING_CONTAINS("a", ""); + UNIT_ASSERT_STRING_CONTAINS("failure", "fail"); + UNIT_ASSERT_STRING_CONTAINS("lurkmore", "more"); + } + Y_UNIT_TEST(NoDiff) { - auto noDiff = [](auto s1, auto s2) { - UNIT_ASSERT_NO_DIFF(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(noDiff("q", "w")); - UNIT_ASSERT_TEST_FAILS(noDiff("q", "")); - - UNIT_ASSERT_NO_DIFF("", ""); - UNIT_ASSERT_NO_DIFF("a", "a"); - } - + auto noDiff = [](auto s1, auto s2) { + UNIT_ASSERT_NO_DIFF(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(noDiff("q", "w")); + UNIT_ASSERT_TEST_FAILS(noDiff("q", "")); + + UNIT_ASSERT_NO_DIFF("", ""); + UNIT_ASSERT_NO_DIFF("a", "a"); + } + Y_UNIT_TEST(StringsUnequal) { - auto stringsUnequal = [](auto s1, auto s2) { - UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2); - }; - UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1")); - UNIT_ASSERT_TEST_FAILS(stringsUnequal("", "")); - UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42"))); - UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4")); + auto stringsUnequal = [](auto s1, auto s2) { + UNIT_ASSERT_STRINGS_UNEQUAL(s1, s2); + }; + UNIT_ASSERT_TEST_FAILS(stringsUnequal("1", "1")); + UNIT_ASSERT_TEST_FAILS(stringsUnequal("", "")); + UNIT_ASSERT_TEST_FAILS(stringsUnequal("42", TString("42"))); + UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("4"), "4")); UNIT_ASSERT_TEST_FAILS(stringsUnequal("d", TStringBuf("d"))); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("yandex"), "yandex")); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TStringBuf("index"), TString("index"))); UNIT_ASSERT_TEST_FAILS(stringsUnequal(TString("diff"), TStringBuf("diff"))); - - UNIT_ASSERT_STRINGS_UNEQUAL("1", "2"); - UNIT_ASSERT_STRINGS_UNEQUAL("", "3"); + + UNIT_ASSERT_STRINGS_UNEQUAL("1", "2"); + UNIT_ASSERT_STRINGS_UNEQUAL("", "3"); UNIT_ASSERT_STRINGS_UNEQUAL("green", TStringBuf("red")); UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("solomon"), "golovan"); - UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f")); - UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index"); + UNIT_ASSERT_STRINGS_UNEQUAL("d", TString("f")); + UNIT_ASSERT_STRINGS_UNEQUAL(TString("yandex"), "index"); UNIT_ASSERT_STRINGS_UNEQUAL(TString("mail"), TStringBuf("yandex")); UNIT_ASSERT_STRINGS_UNEQUAL(TStringBuf("C++"), TString("python")); - } - + } + Y_UNIT_TEST(Equal) { - auto equal = [](auto v1, auto v2) { - UNIT_ASSERT_EQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(equal("1", TString("2"))); - UNIT_ASSERT_TEST_FAILS(equal(1, 2)); - UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24))); - - UNIT_ASSERT_EQUAL("abc", TString("abc")); - UNIT_ASSERT_EQUAL(12l, 12); - UNIT_ASSERT_EQUAL(55, 55); - } - + auto equal = [](auto v1, auto v2) { + UNIT_ASSERT_EQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(equal("1", TString("2"))); + UNIT_ASSERT_TEST_FAILS(equal(1, 2)); + UNIT_ASSERT_TEST_FAILS(equal(42ul, static_cast<unsigned short>(24))); + + UNIT_ASSERT_EQUAL("abc", TString("abc")); + UNIT_ASSERT_EQUAL(12l, 12); + UNIT_ASSERT_EQUAL(55, 55); + } + Y_UNIT_TEST(Unequal) { - auto unequal = [](auto v1, auto v2) { - UNIT_ASSERT_UNEQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x"))); - UNIT_ASSERT_TEST_FAILS(unequal(1, 1)); - UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul)); - - UNIT_ASSERT_UNEQUAL("abc", TString("cba")); - UNIT_ASSERT_UNEQUAL(12l, 10); - UNIT_ASSERT_UNEQUAL(33, 50); - } - + auto unequal = [](auto v1, auto v2) { + UNIT_ASSERT_UNEQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(unequal("x", TString("x"))); + UNIT_ASSERT_TEST_FAILS(unequal(1, 1)); + UNIT_ASSERT_TEST_FAILS(unequal(static_cast<unsigned short>(42), 42ul)); + + UNIT_ASSERT_UNEQUAL("abc", TString("cba")); + UNIT_ASSERT_UNEQUAL(12l, 10); + UNIT_ASSERT_UNEQUAL(33, 50); + } + Y_UNIT_TEST(LessThan) { auto lt = [](auto v1, auto v2) { UNIT_ASSERT_LT(v1, v2); @@ -236,45 +236,45 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { } Y_UNIT_TEST(ValuesEqual) { - auto valuesEqual = [](auto v1, auto v2) { - UNIT_ASSERT_VALUES_EQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2)); - UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2))); - - UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex")); - UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0); - } - + auto valuesEqual = [](auto v1, auto v2) { + UNIT_ASSERT_VALUES_EQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(valuesEqual(1, 2)); + UNIT_ASSERT_TEST_FAILS(valuesEqual(1l, static_cast<short>(2))); + + UNIT_ASSERT_VALUES_EQUAL("yandex", TString("yandex")); + UNIT_ASSERT_VALUES_EQUAL(1.0, 1.0); + } + Y_UNIT_TEST(ValuesUnequal) { - auto valuesUnequal = [](auto v1, auto v2) { - UNIT_ASSERT_VALUES_UNEQUAL(v1, v2); - }; - UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5)); - UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l)); - TString test("test"); + auto valuesUnequal = [](auto v1, auto v2) { + UNIT_ASSERT_VALUES_UNEQUAL(v1, v2); + }; + UNIT_ASSERT_TEST_FAILS(valuesUnequal(5, 5)); + UNIT_ASSERT_TEST_FAILS(valuesUnequal(static_cast<char>(5), 5l)); + TString test("test"); UNIT_ASSERT_TEST_FAILS(valuesUnequal("test", test.data())); - - UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL"); - UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1); - } - - class TTestException: public yexception { - public: - TTestException(const TString& text = "test exception", bool throwMe = true) - : ThrowMe(throwMe) - { - *this << text; - } - - virtual ~TTestException() = default; - - virtual void Throw() { - if (ThrowMe) { - throw *this; - } - } - + + UNIT_ASSERT_VALUES_UNEQUAL("UNIT_ASSERT_VALUES_UNEQUAL", "UNIT_ASSERT_VALUES_EQUAL"); + UNIT_ASSERT_VALUES_UNEQUAL(1.0, 1.1); + } + + class TTestException: public yexception { + public: + TTestException(const TString& text = "test exception", bool throwMe = true) + : ThrowMe(throwMe) + { + *this << text; + } + + virtual ~TTestException() = default; + + virtual void Throw() { + if (ThrowMe) { + throw *this; + } + } + std::string ThrowStr() { if (ThrowMe) { throw *this; @@ -283,24 +283,24 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { return {}; } - void AssertNoException() { - UNIT_ASSERT_NO_EXCEPTION(Throw()); - } - + void AssertNoException() { + UNIT_ASSERT_NO_EXCEPTION(Throw()); + } + void AssertNoExceptionRet() { const TString res = UNIT_ASSERT_NO_EXCEPTION_RESULT(ThrowStr()); } - template <class TExpectedException> - void AssertException() { - UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException); - } - - template <class TExpectedException, class T> - void AssertExceptionContains(const T& substr) { - UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr); - } - + template <class TExpectedException> + void AssertException() { + UNIT_ASSERT_EXCEPTION(Throw(), TExpectedException); + } + + template <class TExpectedException, class T> + void AssertExceptionContains(const T& substr) { + UNIT_ASSERT_EXCEPTION_CONTAINS(Throw(), TExpectedException, substr); + } + template <class TExpectedException, class P> void AssertExceptionSatisfies(const P& predicate) { UNIT_ASSERT_EXCEPTION_SATISFIES(Throw(), TExpectedException, predicate); @@ -310,58 +310,58 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { return 5; // just some value for predicate testing } - bool ThrowMe; - }; - - class TOtherTestException: public TTestException { - public: - using TTestException::TTestException; - - // Throws other type of exception - void Throw() override { - if (ThrowMe) { - throw *this; - } - } - }; - + bool ThrowMe; + }; + + class TOtherTestException: public TTestException { + public: + using TTestException::TTestException; + + // Throws other type of exception + void Throw() override { + if (ThrowMe) { + throw *this; + } + } + }; + Y_UNIT_TEST(Exception) { - UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>()); - UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>()); - - UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException); - UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException); - } - + UNIT_ASSERT_TEST_FAILS(TTestException("", false).AssertException<TTestException>()); + UNIT_ASSERT_TEST_FAILS(TTestException().AssertException<TOtherTestException>()); + + UNIT_ASSERT_EXCEPTION(TOtherTestException().Throw(), TTestException); + UNIT_ASSERT_EXCEPTION(TTestException().Throw(), TTestException); + } + Y_UNIT_TEST(ExceptionAssertionContainsOtherExceptionMessage) { - NUnitTest::TUnitTestFailChecker checker; - { - auto guard = checker.InvokeGuard(); - TTestException("custom exception message").AssertException<TOtherTestException>(); - } - UNIT_ASSERT(checker.Failed()); - UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message"); - } - + NUnitTest::TUnitTestFailChecker checker; + { + auto guard = checker.InvokeGuard(); + TTestException("custom exception message").AssertException<TOtherTestException>(); + } + UNIT_ASSERT(checker.Failed()); + UNIT_ASSERT_STRING_CONTAINS(checker.Msg(), "custom exception message"); + } + Y_UNIT_TEST(NoException) { - UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException()); + UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoException()); UNIT_ASSERT_TEST_FAILS(TTestException().AssertNoExceptionRet()); - - UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw()); - } - + + UNIT_ASSERT_NO_EXCEPTION(TTestException("", false).Throw()); + } + Y_UNIT_TEST(ExceptionContains) { - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba")); + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>("cba")); UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuf("cba"))); - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba"))); - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba")); - - UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc")); - - UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b")); - - UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a"); - } + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TString("cba"))); + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TTestException>(TStringBuilder() << "cba")); + + UNIT_ASSERT_TEST_FAILS(TTestException("abc", false).AssertExceptionContains<TTestException>("bc")); + + UNIT_ASSERT_TEST_FAILS(TTestException("abc").AssertExceptionContains<TOtherTestException>("b")); + + UNIT_ASSERT_EXCEPTION_CONTAINS(TTestException("abc").Throw(), TTestException, "a"); + } Y_UNIT_TEST(ExceptionSatisfies) { const auto goodPredicate = [](const TTestException& e) { return e.GetValue() == 5; }; @@ -370,4 +370,4 @@ Y_UNIT_TEST_SUITE(TUnitTestMacroTest) { UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TTestException>(badPredicate)); UNIT_ASSERT_TEST_FAILS(TTestException().AssertExceptionSatisfies<TOtherTestException>(goodPredicate)); } -} +} diff --git a/library/cpp/testing/unittest/tests_data.cpp b/library/cpp/testing/unittest/tests_data.cpp index b51cbc4b87..a131a54ddf 100644 --- a/library/cpp/testing/unittest/tests_data.cpp +++ b/library/cpp/testing/unittest/tests_data.cpp @@ -1,5 +1,5 @@ #include "tests_data.h" -#include "registar.h" +#include "registar.h" #include <library/cpp/testing/common/network.h> @@ -35,10 +35,10 @@ public: return GetPort(port); } - ui16 GetTcpAndUdpPort(ui16 port) { + ui16 GetTcpAndUdpPort(ui16 port) { return GetPort(port); - } - + } + ui16 GetPortsRange(const ui16 startPort, const ui16 range) { Y_UNUSED(startPort); auto ports = NTesting::NLegacy::GetFreePortsRange(range); @@ -54,16 +54,16 @@ public: private: void ReservePortForCurrentTest(const TAtomicSharedPtr<NTesting::IPort>& portGuard) { if (EnableReservePortsForCurrentTest) { - TTestBase* currentTest = NUnitTest::NPrivate::GetCurrentTest(); - if (currentTest != nullptr) { - currentTest->RunAfterTest([guard = portGuard]() mutable { - guard = nullptr; // remove reference for allocated port - }); - } - } - } - -private: + TTestBase* currentTest = NUnitTest::NPrivate::GetCurrentTest(); + if (currentTest != nullptr) { + currentTest->RunAfterTest([guard = portGuard]() mutable { + guard = nullptr; // remove reference for allocated port + }); + } + } + } + +private: TMutex Lock; TVector<TAtomicSharedPtr<NTesting::IPort>> ReservedPorts; const bool EnableReservePortsForCurrentTest; @@ -81,18 +81,18 @@ TPortManager::~TPortManager() { ui16 TPortManager::GetPort(ui16 port) { return Impl_->GetTcpPort(port); } - -ui16 TPortManager::GetTcpPort(ui16 port) { + +ui16 TPortManager::GetTcpPort(ui16 port) { return Impl_->GetTcpPort(port); -} - -ui16 TPortManager::GetUdpPort(ui16 port) { +} + +ui16 TPortManager::GetUdpPort(ui16 port) { return Impl_->GetUdpPort(port); -} - -ui16 TPortManager::GetTcpAndUdpPort(ui16 port) { +} + +ui16 TPortManager::GetTcpAndUdpPort(ui16 port) { return Impl_->GetTcpAndUdpPort(port); -} +} ui16 TPortManager::GetPortsRange(const ui16 startPort, const ui16 range) { return Impl_->GetPortsRange(startPort, range); diff --git a/library/cpp/testing/unittest/tests_data.h b/library/cpp/testing/unittest/tests_data.h index 6536bc1ae6..fcc56520bd 100644 --- a/library/cpp/testing/unittest/tests_data.h +++ b/library/cpp/testing/unittest/tests_data.h @@ -31,19 +31,19 @@ class TPortManager: public TNonCopyable { public: TPortManager(bool reservePortsForCurrentTest = true); ~TPortManager(); - - // Gets free TCP port + + // Gets free TCP port ui16 GetPort(ui16 port = 0); - // Gets free TCP port - ui16 GetTcpPort(ui16 port = 0); - - // Gets free UDP port - ui16 GetUdpPort(ui16 port = 0); - - // Gets one free port for use in both TCP and UDP protocols - ui16 GetTcpAndUdpPort(ui16 port = 0); - + // Gets free TCP port + ui16 GetTcpPort(ui16 port = 0); + + // Gets free UDP port + ui16 GetUdpPort(ui16 port = 0); + + // Gets one free port for use in both TCP and UDP protocols + ui16 GetTcpAndUdpPort(ui16 port = 0); + ui16 GetPortsRange(const ui16 startPort, const ui16 range); private: diff --git a/library/cpp/testing/unittest/ut/ya.make b/library/cpp/testing/unittest/ut/ya.make index 6d4c0959cc..b5676b187c 100644 --- a/library/cpp/testing/unittest/ut/ya.make +++ b/library/cpp/testing/unittest/ut/ya.make @@ -4,7 +4,7 @@ OWNER(snowball) SRCS( main.cpp - registar_ut.cpp + registar_ut.cpp ) END() diff --git a/library/cpp/testing/unittest/utmain.cpp b/library/cpp/testing/unittest/utmain.cpp index 305bc6b40f..b7e1259cab 100644 --- a/library/cpp/testing/unittest/utmain.cpp +++ b/library/cpp/testing/unittest/utmain.cpp @@ -1,4 +1,4 @@ -#include "plugin.h" +#include "plugin.h" #include "registar.h" #include "utmain.h" @@ -8,27 +8,27 @@ #include <library/cpp/json/writer/json_value.h> #include <library/cpp/testing/common/env.h> #include <library/cpp/testing/hook/hook.h> - -#include <util/datetime/base.h> - + +#include <util/datetime/base.h> + #include <util/generic/hash.h> -#include <util/generic/hash_set.h> +#include <util/generic/hash_set.h> #include <util/generic/scope.h> #include <util/generic/string.h> #include <util/generic/yexception.h> - + #include <util/network/init.h> - + #include <util/stream/file.h> #include <util/stream/output.h> #include <util/string/join.h> -#include <util/string/util.h> - -#include <util/system/defaults.h> -#include <util/system/execpath.h> -#include <util/system/valgrind.h> -#include <util/system/shellcommand.h> +#include <util/string/util.h> +#include <util/system/defaults.h> +#include <util/system/execpath.h> +#include <util/system/valgrind.h> +#include <util/system/shellcommand.h> + #if defined(_win_) #include <fcntl.h> #include <io.h> @@ -141,26 +141,26 @@ private: void OnError(const TError* descr) override { const TString comment = BuildComment(descr->msg, descr->BackTrace.data()); - ErrorMessages.push_back(comment); - } - - void OnFinish(const TFinish* descr) override { - if (descr->Success) { + ErrorMessages.push_back(comment); + } + + void OnFinish(const TFinish* descr) override { + if (descr->Success) { TraceSubtestFinished(descr->test->unit->name.data(), descr->test->name, "good", "", descr->Context); - } else { - TStringBuilder msgs; + } else { + TStringBuilder msgs; for (const TString& m : ErrorMessages) { - if (msgs) { + if (msgs) { msgs << TStringBuf("\n"); - } - msgs << m; - } - if (msgs) { + } + msgs << m; + } + if (msgs) { msgs << TStringBuf("\n"); - } + } TraceSubtestFinished(descr->test->unit->name.data(), descr->test->name, "fail", msgs, descr->Context); - ErrorMessages.clear(); - } + ErrorMessages.clear(); + } } }; @@ -364,8 +364,8 @@ private: } } - void OnFinish(const TFinish* descr) override { - TraceProcessor->Finish(*descr); + void OnFinish(const TFinish* descr) override { + TraceProcessor->Finish(*descr); if (!IsForked && ForkExitedCorrectly) { return; } @@ -373,15 +373,15 @@ private: return; } - if (descr->Success) { + if (descr->Success) { fprintf(stderr, "[%sgood%s] %s::%s\n", LightGreenColor().data(), OldColor().data(), descr->test->unit->name.data(), - descr->test->name); - NOTE_IN_VALGRIND(descr->test); + descr->test->name); + NOTE_IN_VALGRIND(descr->test); PrintTimes(SaveTestDuration()); - if (IsForked) { - fprintf(stderr, "%s", ForkCorrectExitMsg); - } + if (IsForked) { + fprintf(stderr, "%s", ForkCorrectExitMsg); + } } } @@ -613,7 +613,7 @@ static int DoUsage(const char* progname) { << " -h, --help print this help message\n" << " -l, --list print a list of available tests\n" << " -A --list-verbose print a list of available subtests\n" - << " --print-before-test print each test name before running it\n" + << " --print-before-test print each test name before running it\n" << " --print-before-suite print each test suite name before running it\n" << " --show-fails print a list of all failed tests at the end\n" << " --dont-show-fails do not print a list of all failed tests at the end\n" @@ -661,9 +661,9 @@ int NUnitTest::RunMain(int argc, char** argv) { NTesting::THook::CallBeforeRun(); Y_DEFER { NTesting::THook::CallAfterRun(); }; - NPlugin::OnStartMain(argc, argv); + NPlugin::OnStartMain(argc, argv); Y_DEFER { NPlugin::OnStopMain(argc, argv); }; - + TColoredProcessor processor(GetExecPath()); IOutputStream* listStream = &Cout; THolder<IOutputStream> listFile; diff --git a/library/cpp/testing/unittest/ya.make b/library/cpp/testing/unittest/ya.make index aaa4f2ba85..93d23f396f 100644 --- a/library/cpp/testing/unittest/ya.make +++ b/library/cpp/testing/unittest/ya.make @@ -2,10 +2,10 @@ LIBRARY() PROVIDES(test_framework) -OWNER( - pg - galaxycrab -) +OWNER( + pg + galaxycrab +) PEERDIR( library/cpp/colorizer @@ -19,7 +19,7 @@ PEERDIR( SRCS( gtest.cpp checks.cpp - plugin.cpp + plugin.cpp registar.cpp tests_data.cpp utmain.cpp |