diff options
author | mvel <mvel@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
commit | bd30392c4cc92487950adc375c07adf52da1d592 (patch) | |
tree | e8d1a3f19b7fc890bcef6e4cc5de41f1d88c9ac3 /library/cpp/compproto/compproto_ut.cpp | |
parent | 5d50718e66d9c037dc587a0211110b7d25a66185 (diff) | |
download | ydb-bd30392c4cc92487950adc375c07adf52da1d592.tar.gz |
Restoring authorship annotation for <mvel@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/compproto/compproto_ut.cpp')
-rw-r--r-- | library/cpp/compproto/compproto_ut.cpp | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/library/cpp/compproto/compproto_ut.cpp b/library/cpp/compproto/compproto_ut.cpp index 9393be967a..0845beecc3 100644 --- a/library/cpp/compproto/compproto_ut.cpp +++ b/library/cpp/compproto/compproto_ut.cpp @@ -1,37 +1,37 @@ -#include "huff.h" -#include "metainfo.h" -#include "bit.h" - +#include "huff.h" +#include "metainfo.h" +#include "bit.h" + #include <util/generic/vector.h> #include <util/generic/map.h> -#include <util/system/protect.h> - +#include <util/system/protect.h> + #include <library/cpp/testing/unittest/registar.h> -static ui64 gSeed = 42; - -static void FlushPseudoRandom() { - gSeed = 42; -} - -static ui32 PseudoRandom(ui32 max) { - // stupid and non-threadsafe, but very predictable chaos generator - gSeed += 1; - gSeed *= 419; - gSeed = gSeed ^ (ui64(max) << 17); - return gSeed % max; -}; - -enum ECompMode { - CM_SINGLEPASS, - CM_TWOPASS -}; - -struct TTestParams { - size_t DataSize; - ui32 ValueArraySize; +static ui64 gSeed = 42; + +static void FlushPseudoRandom() { + gSeed = 42; +} + +static ui32 PseudoRandom(ui32 max) { + // stupid and non-threadsafe, but very predictable chaos generator + gSeed += 1; + gSeed *= 419; + gSeed = gSeed ^ (ui64(max) << 17); + return gSeed % max; }; +enum ECompMode { + CM_SINGLEPASS, + CM_TWOPASS +}; + +struct TTestParams { + size_t DataSize; + ui32 ValueArraySize; +}; + template <typename X> void TestSaveLoadMeta(NCompProto::TMetaInfo<X>& src) { TStringStream ss; @@ -45,16 +45,16 @@ void TestSaveLoadMeta(NCompProto::TMetaInfo<X>& src) { template <typename TDecompressor, template <typename, typename> class TSerialize> void TestWithParams(const TString& metainfo, const ECompMode mode, const TTestParams& params) { - using namespace NCompProto; - FlushPseudoRandom(); - - TStringInput stream(metainfo); + using namespace NCompProto; + FlushPseudoRandom(); + TStringInput stream(metainfo); + THolder<TMetaInfo<THuff>> meta; - if (mode == CM_TWOPASS) { + if (mode == CM_TWOPASS) { TMetaInfo<THist> hist(stream); TEmpty empty; - TSerialize<THist, TEmpty>::Serialize(hist, empty, params); + TSerialize<THist, TEmpty>::Serialize(hist, empty, params); meta.Reset(new TMetaInfo<THuff>(hist, THistToHuff::Instance())); } else { meta.Reset(new TMetaInfo<THuff>(stream)); @@ -62,55 +62,55 @@ void TestWithParams(const TString& metainfo, const ECompMode mode, const TTestPa TestSaveLoadMeta(*meta.Get()); TBitBuffer buffer; - TSerialize<THuff, TBitBuffer>::Serialize(*meta, buffer, params); + TSerialize<THuff, TBitBuffer>::Serialize(*meta, buffer, params); ui64 codedSize = buffer.Position; - + TMetaInfo<TTable> decompressor(*meta, THuffToTable::Instance()); - - // verify that no memory read beyond buffer occurs - const size_t byteSize = buffer.ByteLength(); + + // verify that no memory read beyond buffer occurs + const size_t byteSize = buffer.ByteLength(); const size_t PAGESIZEX = 4096; const size_t busyPages = (byteSize + (PAGESIZEX - 1)) / PAGESIZEX; - const size_t allPages = busyPages + 1; + const size_t allPages = busyPages + 1; const size_t allocSize = (allPages + 1) * PAGESIZEX; TVector<ui8> readBuffer(allocSize); - ui8* start = &readBuffer[0]; + ui8* start = &readBuffer[0]; ui8* pageStart = reinterpret_cast<ui8*>((size_t(start) + PAGESIZEX) & ~(PAGESIZEX - 1)); - // XX DATA DATA DATA DATA PROT - // | | | | | pages - // calculate dataStart so that data ends exactly at the page end + // XX DATA DATA DATA DATA PROT + // | | | | | pages + // calculate dataStart so that data ends exactly at the page end ui8* dataStart = pageStart + busyPages * PAGESIZEX - byteSize; ui8* dataEnd = pageStart + busyPages * PAGESIZEX; ProtectMemory(dataEnd, PAGESIZEX, PM_NONE); - // memory copying should be performed without any problems + // memory copying should be performed without any problems memcpy(dataStart, buffer.Out.data(), byteSize); - + ui64 position = 0; TMetaIterator<TDecompressor> instance; - // we should not read beyond dataEnd here - instance.Decompress(&decompressor, dataStart, position); - const ui64 decodedSize = position; + // we should not read beyond dataEnd here + instance.Decompress(&decompressor, dataStart, position); + const ui64 decodedSize = position; UNIT_ASSERT_EQUAL(codedSize, decodedSize); - // unprotect memory + // unprotect memory ProtectMemory(dataEnd, PAGESIZEX, PM_READ | PM_WRITE | PM_EXEC); } -template <typename TDecompressor, template <typename, typename> class TSerialize> +template <typename TDecompressor, template <typename, typename> class TSerialize> void Test(const TString& metainfo, const ECompMode mode) { - for (size_t ds = 3; ds < 42; ds += (3 + PseudoRandom(5))) { - for (size_t vas = 5; vas < 42; vas += (4 + PseudoRandom(10))) { - TTestParams params; - params.DataSize = ds; - params.ValueArraySize = vas; - TestWithParams<TDecompressor, TSerialize>(metainfo, mode, params); - } - } -} + for (size_t ds = 3; ds < 42; ds += (3 + PseudoRandom(5))) { + for (size_t vas = 5; vas < 42; vas += (4 + PseudoRandom(10))) { + TTestParams params; + params.DataSize = ds; + params.ValueArraySize = vas; + TestWithParams<TDecompressor, TSerialize>(metainfo, mode, params); + } + } +} Y_UNIT_TEST_SUITE(CompProtoTestBasic) { - using namespace NCompProto; - + using namespace NCompProto; + const TString metainfo = "\n\ repeated data id 0\n\ @@ -123,14 +123,14 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { scalar extra id 31 default const 0\n\ end\n"; - struct TRegInfo { - ui32 Clicks; - ui32 Shows; + struct TRegInfo { + ui32 Clicks; + ui32 Shows; }; - struct TData { - ui32 Clicks; - ui32 Shows; + struct TData { + ui32 Clicks; + ui32 Shows; ui32 Extra; TMap<ui32, TRegInfo> RegClicks; }; @@ -138,30 +138,30 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { TVector<TData> data; template <class TMeta, class TFunctor> - struct TSerialize { + struct TSerialize { static void Serialize(TMetaInfo<TMeta>& meta, TFunctor& functor, const TTestParams& params) { - FlushPseudoRandom(); + FlushPseudoRandom(); meta.BeginSelf(functor); data.clear(); - data.resize(params.DataSize); - for (ui32 i = 0; i < params.DataSize; ++i) { + data.resize(params.DataSize); + for (ui32 i = 0; i < params.DataSize; ++i) { meta.BeginElement(i, functor); - data[i].Clicks = PseudoRandom(16) + 100; - data[i].Shows = PseudoRandom(500) * PseudoRandom(16); + data[i].Clicks = PseudoRandom(16) + 100; + data[i].Shows = PseudoRandom(500) * PseudoRandom(16); data[i].Extra = PseudoRandom(500) + (1UL << 31); // test also saving of big values - meta.SetScalar(0, data[i].Clicks, functor); - meta.SetScalar(1, data[i].Shows, functor); + meta.SetScalar(0, data[i].Clicks, functor); + meta.SetScalar(1, data[i].Shows, functor); TMetaInfo<TMeta>& regClicks = meta.BeginRepeated(2, functor); - for (ui32 j = 0; j < PseudoRandom(200); j += 1 + PseudoRandom(10)) { + for (ui32 j = 0; j < PseudoRandom(200); j += 1 + PseudoRandom(10)) { regClicks.BeginElement(j, functor); - TRegInfo& r = data[i].RegClicks[j]; + TRegInfo& r = data[i].RegClicks[j]; - r.Clicks = PseudoRandom(2); - r.Shows = PseudoRandom(800) * PseudoRandom(8) + 56; - regClicks.SetScalar(0, r.Clicks, functor); - regClicks.SetScalar(1, r.Shows, functor); + r.Clicks = PseudoRandom(2); + r.Shows = PseudoRandom(800) * PseudoRandom(8) + 56; + regClicks.SetScalar(0, r.Clicks, functor); + regClicks.SetScalar(1, r.Shows, functor); regClicks.EndElement(functor); } @@ -274,7 +274,7 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { case InDataElem: UNIT_ASSERT_EQUAL(id, 2); State = InRegClicks; - RegIter = data[DataInd].RegClicks.begin(); + RegIter = data[DataInd].RegClicks.begin(); break; case InRegClicks: UNIT_ASSERT(0); @@ -292,7 +292,7 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { case InDataElem: UNIT_ASSERT(0); case InRegClicks: - UNIT_ASSERT_EQUAL(RegIter, data[DataInd].RegClicks.end()); + UNIT_ASSERT_EQUAL(RegIter, data[DataInd].RegClicks.end()); State = InDataElem; break; default: @@ -336,17 +336,17 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { UNIT_ASSERT(0); case InDataElem: if (index == 0) - UNIT_ASSERT_EQUAL(val, data[DataInd].Clicks); + UNIT_ASSERT_EQUAL(val, data[DataInd].Clicks); if (index == 1) - UNIT_ASSERT_EQUAL(val, data[DataInd].Shows); + UNIT_ASSERT_EQUAL(val, data[DataInd].Shows); if (index == 31) UNIT_ASSERT_EQUAL(val, data[DataInd].Extra); break; case InRegClicks: if (index == 0) - UNIT_ASSERT_EQUAL(val, RegIter->second.Clicks); + UNIT_ASSERT_EQUAL(val, RegIter->second.Clicks); if (index == 1) - UNIT_ASSERT_EQUAL(val, RegIter->second.Shows); + UNIT_ASSERT_EQUAL(val, RegIter->second.Shows); break; default: UNIT_ASSERT(0); @@ -355,11 +355,11 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { }; Y_UNIT_TEST(VerifyDecompression) { - Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_SINGLEPASS); + Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_SINGLEPASS); } Y_UNIT_TEST(VerifyHistDecompression) { - Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_TWOPASS); + Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_TWOPASS); } Y_UNIT_TEST(VerifyDecompressionMulti) { @@ -372,7 +372,7 @@ Y_UNIT_TEST_SUITE(CompProtoTestBasic) { } Y_UNIT_TEST_SUITE(CompProtoTestExtended) { - using namespace NCompProto; + using namespace NCompProto; const TString metainfo = "\n\ repeated data id 0\n\ @@ -386,17 +386,17 @@ Y_UNIT_TEST_SUITE(CompProtoTestExtended) { TVector<std::pair<TVector<ui32>, TVector<ui32>>> data; template <class TMeta, class TFunctor> - struct TSerialize { + struct TSerialize { static void Serialize(TMetaInfo<TMeta>& meta, TFunctor& functor, const TTestParams& params) { - FlushPseudoRandom(); + FlushPseudoRandom(); meta.BeginSelf(functor); data.clear(); - data.resize(params.DataSize); - for (size_t i = 0; i < params.DataSize; ++i) { + data.resize(params.DataSize); + for (size_t i = 0; i < params.DataSize; ++i) { meta.BeginElement(i, functor); TMetaInfo<TMeta>& first = meta.BeginRepeated(2, functor); data[i].first.resize(params.ValueArraySize); - for (ui32 j = 0; j < params.ValueArraySize; j++) { + for (ui32 j = 0; j < params.ValueArraySize; j++) { first.BeginElement(j, functor); ui32 val = PseudoRandom(42 * 42 * 42); @@ -409,7 +409,7 @@ Y_UNIT_TEST_SUITE(CompProtoTestExtended) { TMetaInfo<TMeta>& second = meta.BeginRepeated(3, functor); data[i].second.resize(params.ValueArraySize); - for (ui32 j = 0; j < params.ValueArraySize; j++) { + for (ui32 j = 0; j < params.ValueArraySize; j++) { second.BeginElement(j, functor); ui32 val = PseudoRandom(42 * 42 * 42); @@ -534,10 +534,10 @@ Y_UNIT_TEST_SUITE(CompProtoTestExtended) { } }; Y_UNIT_TEST(VerifyDecompression) { - Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_SINGLEPASS); + Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_SINGLEPASS); } Y_UNIT_TEST(VerifyHistDecompression) { - Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_TWOPASS); + Test<TVerifyingDecompressor, TSerialize>(metainfo, CM_TWOPASS); } } |