diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/packers/ut | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/packers/ut')
-rw-r--r-- | library/cpp/packers/ut/packers_ut.cpp | 110 | ||||
-rw-r--r-- | library/cpp/packers/ut/proto_packer_ut.cpp | 104 | ||||
-rw-r--r-- | library/cpp/packers/ut/region_packer_ut.cpp | 40 | ||||
-rw-r--r-- | library/cpp/packers/ut/test.proto | 11 | ||||
-rw-r--r-- | library/cpp/packers/ut/ya.make | 12 |
5 files changed, 277 insertions, 0 deletions
diff --git a/library/cpp/packers/ut/packers_ut.cpp b/library/cpp/packers/ut/packers_ut.cpp new file mode 100644 index 0000000000..18ce2150d1 --- /dev/null +++ b/library/cpp/packers/ut/packers_ut.cpp @@ -0,0 +1,110 @@ +#include <library/cpp/testing/unittest/registar.h> + +#include <util/stream/output.h> +#include <utility> + +#include <util/charset/wide.h> +#include <util/generic/algorithm.h> +#include <util/generic/buffer.h> +#include <util/generic/map.h> +#include <util/generic/vector.h> +#include <util/generic/ptr.h> +#include <util/generic/ylimits.h> + +#include <util/folder/dirut.h> + +#include <util/random/random.h> + +#include <util/string/hex.h> + +#include "packers.h" + +#include <array> +#include <iterator> + +class TPackersTest: public TTestBase { +private: + UNIT_TEST_SUITE(TPackersTest); + UNIT_TEST(TestPackers); + UNIT_TEST_SUITE_END(); + + template <class TData, class TPacker> + void TestPacker(const TData& data); + + template <class TData, class TPacker> + void TestPacker(const TData* test, size_t size); + +public: + void TestPackers(); +}; + +UNIT_TEST_SUITE_REGISTRATION(TPackersTest); + +template <class TData, class TPacker> +void TPackersTest::TestPacker(const TData& data) { + size_t len = TPacker().MeasureLeaf(data); + size_t bufLen = len * 3; + + TArrayHolder<char> buf(new char[bufLen]); + memset(buf.Get(), -1, bufLen); + + TPacker().PackLeaf(buf.Get(), data, len); + + UNIT_ASSERT(TPacker().SkipLeaf(buf.Get()) == len); + + TData dataTmp; + TPacker().UnpackLeaf(buf.Get(), dataTmp); + UNIT_ASSERT(data == dataTmp); +} + +template <class TData, class TPacker> +void TPackersTest::TestPacker(const TData* test, size_t size) { + for (size_t i = 0; i < size; ++i) { + TestPacker<TData, TPacker>(test[i]); + } +} + +void TPackersTest::TestPackers() { + { + const TString test[] = {"", + "a", "b", "c", "d", + "aa", "ab", "ac", "ad", + "aaa", "aab", "aac", "aad", + "aba", "abb", "abc", "abd", + "asdfjjmk.gjilsjgilsjilgjildsajgfilsjdfilgjm ldsa8oq43u 583uq4905 -q435 jiores u893q 5oiju fd-KE 89536 9Q2URE 12AI894T3 89 Q*(re43"}; + + TestPacker<TString, NPackers::TPacker<TString>>(test, Y_ARRAY_SIZE(test)); + + for (size_t i = 0; i != Y_ARRAY_SIZE(test); ++i) { + TestPacker<TUtf16String, NPackers::TPacker<TUtf16String>>(UTF8ToWide(test[i])); + } + } + { + const ui64 test[] = { + 0, 1, 2, 3, 4, 5, 6, 76, 100000, Max<ui64>()}; + + TestPacker<ui64, NPackers::TPacker<ui64>>(test, Y_ARRAY_SIZE(test)); + } + { + const int test[] = { + 0, 1, 2, 3, 4, 5, 6, 76, 100000, -1, -2, -3, -4, -5, -6, -76, -10000, Min<int>(), Max<int>()}; + + TestPacker<int, NPackers::TPacker<int>>(test, Y_ARRAY_SIZE(test)); + } + { + const float test[] = { + 2.f, 3.f, 4.f, 0.f, -0.f, 1.f, -1.f, 1.1f, -1.1f, + std::numeric_limits<float>::min(), -std::numeric_limits<float>::min(), + std::numeric_limits<float>::max(), -std::numeric_limits<float>::max()}; + + TestPacker<float, NPackers::TFloatPacker>(test, Y_ARRAY_SIZE(test)); + } + { + const double test[] = { + 0., -0., 1., -1., 1.1, -1.1, + std::numeric_limits<double>::min(), -std::numeric_limits<double>::min(), + std::numeric_limits<double>::max(), -std::numeric_limits<double>::max()}; + + TestPacker<double, NPackers::TDoublePacker>(test, Y_ARRAY_SIZE(test)); + } +} diff --git a/library/cpp/packers/ut/proto_packer_ut.cpp b/library/cpp/packers/ut/proto_packer_ut.cpp new file mode 100644 index 0000000000..e4151ba68c --- /dev/null +++ b/library/cpp/packers/ut/proto_packer_ut.cpp @@ -0,0 +1,104 @@ +#include "proto_packer.h" + +#include <library/cpp/packers/ut/test.pb.h> +#include <library/cpp/testing/unittest/registar.h> + +#include <util/generic/string.h> + +using namespace NPackers; +using namespace NProtoPackerTest; + +void FillRequiredFields(TTestMessage& msg) { + msg.SetRequiredString("required_string"); + msg.SetRequiredInt32(42); +} + +void FillOptionalFields(TTestMessage& msg) { + msg.SetOptionalString("optional_string"); + msg.SetOptionalInt32(43); +} + +void FillRepeatedFields(TTestMessage& msg) { + msg.ClearRepeatedStrings(); + for (ui32 idx = 0; idx < 5; ++idx) { + msg.AddRepeatedStrings("repeated_string" + ToString(idx)); + } +} + +// do not want to use google/protobuf/util/message_differencer because of warnings +bool operator==(const TTestMessage& lhs, const TTestMessage& rhs) { + if (lhs.GetRequiredString() != rhs.GetRequiredString() || + lhs.GetRequiredInt32() != rhs.GetRequiredInt32() || + lhs.HasOptionalString() != rhs.HasOptionalString() || + (lhs.HasOptionalString() && lhs.GetOptionalString() != rhs.GetOptionalString()) || + lhs.HasOptionalInt32() != rhs.HasOptionalInt32() || + (lhs.HasOptionalInt32() && lhs.GetOptionalInt32() != rhs.GetOptionalInt32()) || + lhs.RepeatedStringsSize() != rhs.RepeatedStringsSize()) + { + return false; + } + for (ui32 idx = 0; idx < lhs.RepeatedStringsSize(); ++idx) { + if (lhs.GetRepeatedStrings(idx) != rhs.GetRepeatedStrings(idx)) { + return false; + } + } + return true; +} + +Y_UNIT_TEST_SUITE(ProtoPackerTestSuite) { + TProtoMessagePacker<TTestMessage> Packer; + TString Buffer; + + void DoPackUnpackTest(const TTestMessage& msg) { + const ui32 msgByteSize = Packer.MeasureLeaf(msg); + Buffer.resize(msgByteSize); + + Packer.PackLeaf(Buffer.begin(), msg, msgByteSize); + + TTestMessage checkMsg; + Packer.UnpackLeaf(Buffer.begin(), checkMsg); + + UNIT_ASSERT_EQUAL(msg, checkMsg); + } + + Y_UNIT_TEST(TestPackUnpackOnlyRequired) { + TTestMessage msg; + FillRequiredFields(msg); + DoPackUnpackTest(msg); + } + + Y_UNIT_TEST(TestPackUnpackRequiredAndOptional) { + TTestMessage msg; + FillRequiredFields(msg); + FillOptionalFields(msg); + DoPackUnpackTest(msg); + } + + Y_UNIT_TEST(TestPackUnpackAll) { + TTestMessage msg; + FillRequiredFields(msg); + FillOptionalFields(msg); + FillRepeatedFields(msg); + DoPackUnpackTest(msg); + } + + Y_UNIT_TEST(TestSkipLeaf) { + TTestMessage msgFirst; + FillRequiredFields(msgFirst); + TTestMessage msgSecond; + FillRequiredFields(msgSecond); + FillOptionalFields(msgSecond); + + const ui32 msgFirstByteSize = Packer.MeasureLeaf(msgFirst); + const ui32 msgSecondByteSize = Packer.MeasureLeaf(msgSecond); + + Buffer.resize(msgFirstByteSize + msgSecondByteSize); + Packer.PackLeaf(Buffer.begin(), msgFirst, msgFirstByteSize); + Packer.PackLeaf(Buffer.begin() + msgFirstByteSize, msgSecond, msgSecondByteSize); + + TTestMessage checkMsg; + Packer.UnpackLeaf(Buffer.begin() + Packer.SkipLeaf(Buffer.begin()), checkMsg); + + UNIT_ASSERT_EQUAL(msgSecond, checkMsg); + } +} diff --git a/library/cpp/packers/ut/region_packer_ut.cpp b/library/cpp/packers/ut/region_packer_ut.cpp new file mode 100644 index 0000000000..0cb08ccf65 --- /dev/null +++ b/library/cpp/packers/ut/region_packer_ut.cpp @@ -0,0 +1,40 @@ +#include "region_packer.h" +#include <library/cpp/testing/unittest/registar.h> + +template <typename TValue> +void TestPacker() { + TValue values[] = {1, 2, 3, 42}; + TString buffer; + + TRegionPacker<TValue> p; + + using TValues = TArrayRef<TValue>; + TValues valueRegion = TValues(values, Y_ARRAY_SIZE(values)); + size_t sz = p.MeasureLeaf(valueRegion); + UNIT_ASSERT_VALUES_EQUAL(sz, 1 + sizeof(values)); + + buffer.resize(sz); + p.PackLeaf(buffer.begin(), valueRegion, sz); + UNIT_ASSERT_VALUES_EQUAL(buffer[0], 4); + + p.UnpackLeaf(buffer.data(), valueRegion); + UNIT_ASSERT_EQUAL(valueRegion.data(), (const TValue*)(buffer.begin() + 1)); + UNIT_ASSERT_EQUAL(valueRegion.size(), Y_ARRAY_SIZE(values)); + UNIT_ASSERT_EQUAL(0, memcmp(values, valueRegion.data(), sizeof(values))); +} + +Y_UNIT_TEST_SUITE(RegionPacker) { + Y_UNIT_TEST(Test0) { + TestPacker<char>(); + TestPacker<signed char>(); + TestPacker<unsigned char>(); + TestPacker<i8>(); + TestPacker<ui8>(); + TestPacker<i16>(); + TestPacker<ui16>(); + TestPacker<i32>(); + TestPacker<ui32>(); + TestPacker<i64>(); + TestPacker<ui64>(); + } +} diff --git a/library/cpp/packers/ut/test.proto b/library/cpp/packers/ut/test.proto new file mode 100644 index 0000000000..c872616bcc --- /dev/null +++ b/library/cpp/packers/ut/test.proto @@ -0,0 +1,11 @@ +package NProtoPackerTest; + +message TTestMessage { + required string RequiredString = 1; + optional string OptionalString = 2; + + required int32 RequiredInt32 = 3; + optional int32 OptionalInt32 = 4; + + repeated string RepeatedStrings = 5; +} diff --git a/library/cpp/packers/ut/ya.make b/library/cpp/packers/ut/ya.make new file mode 100644 index 0000000000..1c024ffd94 --- /dev/null +++ b/library/cpp/packers/ut/ya.make @@ -0,0 +1,12 @@ +UNITTEST_FOR(library/cpp/packers) + +OWNER(velavokr) + +SRCS( + packers_ut.cpp + proto_packer_ut.cpp + region_packer_ut.cpp + test.proto +) + +END() |