aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/on_disk/chunks/chunks_ut.cpp
blob: 2be71e91084df76cc3c15686c9956bd34cc3f260 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#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);