aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/archive/yarchive_ut.cpp
blob: 602a1cdbbde88f2f45872885d55d3c1043b7fbdf (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
#include "yarchive.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/string/cast.h>
#include <util/stream/file.h>
#include <util/system/tempfile.h>
#include <util/memory/blob.h>

class TArchiveTest: public TTestBase {
    UNIT_TEST_SUITE(TArchiveTest)
    UNIT_TEST(TestCreate);
    UNIT_TEST(TestRead);
    UNIT_TEST(TestOffsetOrder);
    UNIT_TEST_SUITE_END();

private:
    void CreateArchive();
    void TestCreate();
    void TestRead();
    void TestOffsetOrder();
};

UNIT_TEST_SUITE_REGISTRATION(TArchiveTest);

#define ARCHIVE "./test.ar"

void TArchiveTest::CreateArchive() {
    TFixedBufferFileOutput out(ARCHIVE);
    TArchiveWriter w(&out);

    for (size_t i = 0; i < 1000; ++i) {
        const TString path = "/" + ToString(i);
        const TString data = "data" + ToString(i * 1000) + "dataend";
        TStringInput si(data);

        w.Add(path, &si);
    }

    w.Finish();
    out.Finish();
}

void TArchiveTest::TestCreate() {
    CreateArchive();
    TTempFile tmpFile(ARCHIVE);
}

void TArchiveTest::TestRead() {
    CreateArchive();
    TTempFile tmpFile(ARCHIVE);
    TBlob blob = TBlob::FromFileSingleThreaded(ARCHIVE);
    TArchiveReader r(blob);

    UNIT_ASSERT_EQUAL(r.Count(), 1000);

    for (size_t i = 0; i < 1000; ++i) {
        const TString key = "/" + ToString(i);
        TAutoPtr<IInputStream> is = r.ObjectByKey(key);
        const TString data = is->ReadAll();

        UNIT_ASSERT_EQUAL(data, "data" + ToString(i * 1000) + "dataend");
    }
}

void TArchiveTest::TestOffsetOrder() {
    CreateArchive();
    TTempFile tmpFile(ARCHIVE);
    TBlob blob1 = TBlob::FromFileSingleThreaded(ARCHIVE);
    TArchiveReader r(blob1);

    const void* prevOffset = nullptr;

    for (size_t i = 0; i < r.Count(); ++i) {
        const TString key = r.KeyByIndex(i);
        TBlob blob2 = r.BlobByKey(key);
        const void* offset = blob2.Data();

        if (i) {
            UNIT_ASSERT(prevOffset < offset);
        }
        prevOffset = offset;
    }
}