123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #include <library/cpp/testing/unittest/registar.h>
- #include <util/stream/file.h>
- #include <util/system/filemap.h>
- #include <util/system/tempfile.h>
- #include "chunked_helpers.h"
- /// Data for TChunkedHelpersTest::TestGeneralVector
- struct TPodStruct {
- int x;
- float y;
- TPodStruct(int _x = 0, float _y = 0)
- : x(_x)
- , y(_y)
- {
- }
- };
- /// And its serialization
- template <>
- struct TSaveLoadVectorNonPodElement<TPodStruct> {
- typedef TPodStruct TItem;
- static inline void Save(IOutputStream* out, const TItem& item) {
- TSerializer<int>::Save(out, item.x);
- TSerializer<float>::Save(out, item.y);
- }
- static inline void Load(IInputStream* in, TItem& item, size_t elementSize) {
- Y_ASSERT(elementSize == sizeof(TItem));
- TSerializer<int>::Load(in, item.x);
- TSerializer<float>::Load(in, item.y);
- }
- };
- class TChunkedHelpersTest: public TTestBase {
- UNIT_TEST_SUITE(TChunkedHelpersTest);
- UNIT_TEST(TestHash)
- UNIT_TEST(TestGeneralVector)
- UNIT_TEST(TestStrings);
- UNIT_TEST(TestNamedChunkedData);
- UNIT_TEST_SUITE_END();
- public:
- void TestHash() {
- {
- TBufferStream stream;
- {
- TPlainHashWriter<ui64, ui16> writer;
- writer.Add(5, 7);
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TPlainHash<ui64, ui16> reader(temp);
- ui16 value = 0;
- UNIT_ASSERT(reader.Find(5, &value));
- UNIT_ASSERT_EQUAL(7, value);
- UNIT_ASSERT(!reader.Find(6, &value));
- }
- }
- {
- TBufferStream stream;
- int v = 1;
- wchar16 k = 'a';
- {
- TPlainHashWriter<wchar16, void*> writer;
- writer.Add(k, &v);
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TPlainHash<wchar16, void*> reader(temp);
- void* value = nullptr;
- UNIT_ASSERT(reader.Find(k, &value));
- UNIT_ASSERT_EQUAL((int*)value, &v);
- }
- }
- }
- void TestGeneralVector() {
- { /// ui32
- const size_t N = 3;
- TBufferStream stream;
- {
- TGeneralVectorWriter<ui32> writer;
- for (size_t i = 0; i < N; ++i)
- writer.PushBack(i);
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TGeneralVector<ui32> reader(temp);
- UNIT_ASSERT_EQUAL(reader.GetSize(), N);
- for (size_t i = 0; i < N; ++i) {
- ui32 value;
- reader.Get(i, value);
- UNIT_ASSERT_EQUAL(value, i);
- UNIT_ASSERT_EQUAL(reader.At(i), i);
- }
- UNIT_ASSERT_EQUAL(reader.RealSize(), sizeof(ui64) + N * sizeof(ui32));
- }
- }
- { /// TString
- const size_t N = 4;
- TBufferStream stream;
- {
- TGeneralVectorWriter<TString> writer;
- for (size_t i = 0; i < N; ++i)
- writer.PushBack(ToString(i));
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TGeneralVector<TString> reader(temp);
- UNIT_ASSERT_EQUAL(reader.GetSize(), N);
- for (size_t i = 0; i < N; ++i) {
- TString value;
- reader.Get(i, value);
- UNIT_ASSERT_EQUAL(value, ToString(i));
- UNIT_ASSERT_EQUAL(reader.Get(i), ToString(i));
- }
- UNIT_ASSERT_EQUAL(reader.RealSize(), sizeof(ui64) * (N + 2) + N * 2);
- }
- }
- { /// some other struct
- typedef TPodStruct TItem;
- const size_t N = 2;
- TBufferStream stream;
- {
- TGeneralVectorWriter<TItem> writer;
- writer.PushBack(TItem(1, 2));
- writer.PushBack(TItem(3, 4));
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TGeneralVector<TItem> reader(temp);
- UNIT_ASSERT_EQUAL(reader.GetSize(), N);
- TItem value;
- reader.Get(0, value);
- UNIT_ASSERT(value.x == 1 && value.y == 2.0);
- reader.Get(1, value);
- UNIT_ASSERT(value.x == 3 && value.y == 4.0);
- UNIT_ASSERT_EQUAL(reader.RealSize(), sizeof(ui64) * (N + 2) + N * sizeof(TItem));
- }
- }
- { /// pointer
- const size_t N = 3;
- TVector<int> data_holder(N);
- int* a = &(data_holder[0]);
- TBufferStream stream;
- {
- TGeneralVectorWriter<int*> writer;
- for (size_t i = 0; i < N; ++i) {
- a[i] = i;
- writer.PushBack(a + i);
- }
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TGeneralVector<int*> reader(temp);
- UNIT_ASSERT_EQUAL(reader.GetSize(), N);
- for (size_t i = 0; i < N; ++i) {
- int* value;
- reader.Get(i, value);
- UNIT_ASSERT_EQUAL(value, a + i);
- UNIT_ASSERT_EQUAL(reader.At(i), a + i);
- }
- UNIT_ASSERT_EQUAL(reader.RealSize(), sizeof(ui64) + N * sizeof(int*));
- }
- }
- { /// std::pair<int, int>
- typedef std::pair<int, int> TItem;
- const size_t N = 3;
- TBufferStream stream;
- {
- TGeneralVectorWriter<TItem> writer;
- for (size_t i = 0; i < N; ++i)
- writer.PushBack(TItem(i, i));
- writer.Save(stream);
- }
- {
- TBlob temp = TBlob::FromStreamSingleThreaded(stream);
- TGeneralVector<TItem> reader(temp);
- UNIT_ASSERT_EQUAL(reader.GetSize(), N);
- for (size_t i = 0; i < N; ++i) {
- TItem value;
- reader.Get(i, value);
- UNIT_ASSERT_EQUAL(value, TItem(i, i));
- }
- UNIT_ASSERT_EQUAL(reader.RealSize(), sizeof(ui64) + N * sizeof(TItem));
- }
- }
- }
- void TestStrings() {
- const TString FILENAME = "chunked_helpers_test.bin";
- TTempFileHandle file(FILENAME.c_str());
- {
- TFixedBufferFileOutput fOut(FILENAME);
- TStringsVectorWriter stringsWriter;
- stringsWriter.PushBack("");
- stringsWriter.PushBack("test");
- TChunkedDataWriter writer(fOut);
- WriteBlock(writer, stringsWriter);
- writer.WriteFooter();
- }
- {
- TBlob fIn = TBlob::FromFileSingleThreaded(FILENAME);
- TStringsVector vct(GetBlock(fIn, 0));
- UNIT_ASSERT_EQUAL(vct.Get(0), "");
- UNIT_ASSERT_EQUAL(vct.Get(1), "test");
- bool wasException = false;
- try {
- vct.Get(2);
- } catch (...) {
- wasException = true;
- }
- UNIT_ASSERT(wasException);
- }
- }
- void TestNamedChunkedData() {
- const TString filename = MakeTempName(nullptr, "named_chunked_data_test");
- TTempFile file(filename);
- {
- TFixedBufferFileOutput fOut(filename);
- TNamedChunkedDataWriter writer(fOut);
- writer.NewBlock("alpha");
- writer.Write("123456");
- writer.NewBlock();
- writer.Write("anonymous");
- writer.NewBlock("omega");
- writer.Write("12345678901234567");
- writer.WriteFooter();
- }
- {
- TBlob mf = TBlob::FromFileSingleThreaded(filename);
- TNamedChunkedDataReader reader(mf);
- UNIT_ASSERT(reader.GetBlocksCount() == 3);
- UNIT_ASSERT(reader.HasBlock("alpha"));
- UNIT_ASSERT(reader.HasBlock("omega"));
- UNIT_ASSERT_STRINGS_EQUAL(reader.GetBlockName(0), "alpha");
- UNIT_ASSERT_STRINGS_EQUAL(reader.GetBlockName(1), "");
- UNIT_ASSERT_STRINGS_EQUAL(reader.GetBlockName(2), "omega");
- UNIT_ASSERT_EQUAL(reader.GetBlockLenByName("alpha"), 6); // padding not included
- UNIT_ASSERT_EQUAL(reader.GetBlockLenByName("omega"), 17);
- UNIT_ASSERT(memcmp(reader.GetBlockByName("alpha"), "123456", 6) == 0);
- UNIT_ASSERT(memcmp(reader.GetBlock(1), "anonymous", 9) == 0);
- UNIT_ASSERT(memcmp(reader.GetBlockByName("omega"), "12345678901234567", 17) == 0);
- }
- }
- };
- UNIT_TEST_SUITE_REGISTRATION(TChunkedHelpersTest);
- class TChunkedDataTest: public TTestBase {
- private:
- UNIT_TEST_SUITE(TChunkedDataTest);
- UNIT_TEST(Test)
- UNIT_TEST(TestEmpty)
- UNIT_TEST_SUITE_END();
- void Test() {
- TBuffer buffer;
- {
- TBufferOutput out(buffer);
- TChunkedDataWriter writer(out);
- writer.NewBlock();
- writer << "test";
- writer.NewBlock();
- writer << 4;
- writer.NewBlock();
- writer.NewBlock();
- writer << 1;
- writer << 2;
- writer.WriteFooter();
- }
- {
- TBlob blob = TBlob::FromBufferSingleThreaded(buffer);
- TChunkedDataReader data(blob);
- // printf("%d\n", (int)data.GetBlockLen(3));
- UNIT_ASSERT_EQUAL(4, data.GetBlockLen(0));
- UNIT_ASSERT_EQUAL(1, data.GetBlockLen(1));
- UNIT_ASSERT_EQUAL(0, data.GetBlockLen(2));
- UNIT_ASSERT_EQUAL(2, data.GetBlockLen(3));
- }
- }
- void TestEmpty() {
- TBuffer buffer;
- {
- TBufferOutput out(buffer);
- TChunkedDataWriter writer(out);
- writer.NewBlock();
- writer.NewBlock();
- writer.WriteFooter();
- }
- {
- TBlob blob = TBlob::FromBufferSingleThreaded(buffer);
- TChunkedDataReader data(blob);
- // printf("%d\n", (int)data.GetBlockLen(1));
- UNIT_ASSERT_EQUAL(0, data.GetBlockLen(0));
- UNIT_ASSERT_EQUAL(0, data.GetBlockLen(1));
- }
- }
- };
- UNIT_TEST_SUITE_REGISTRATION(TChunkedDataTest);
|