aboutsummaryrefslogtreecommitdiffstats
path: root/util/memory/tempbuf_ut.cpp
blob: d6bcf9d54611d65c19e4813fcd9a31cd659fb8ec (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
#include "tempbuf.h"

#include <utility>

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

class TTempBufTest: public TTestBase {
    UNIT_TEST_SUITE(TTempBufTest);
    UNIT_TEST(TestCreate);
    UNIT_TEST(TestOps);
    UNIT_TEST(TestMoveCtor);
    UNIT_TEST(TestAppend);
    UNIT_TEST(TestProceed);
    UNIT_TEST_SUITE_END();

public:
    void TestCreate();
    void TestOps();
    void TestMoveCtor();
    void TestProceed();

    void TestAppend() {
        TTempBuf tmp;

        tmp.Append("a", 1);
        tmp.Append("bc", 2);
        tmp.Append("def", 3);

        UNIT_ASSERT_EQUAL(tmp.Filled(), 6);
        UNIT_ASSERT_EQUAL(TString(tmp.Data(), tmp.Filled()), "abcdef");
    }
};

UNIT_TEST_SUITE_REGISTRATION(TTempBufTest);

void TTempBufTest::TestCreate() {
    const size_t num = 1000000;
    size_t tmp = 0;
    const size_t len = 4096;

    for (size_t i = 0; i < num; ++i) {
        TTempBuf buf(len);

        tmp += (size_t)buf.Data();
    }

    UNIT_ASSERT(tmp != 0);
}

void TTempBufTest::TestOps() {
    TTempBuf tmp(201);

    tmp.Proceed(100);

    UNIT_ASSERT_EQUAL(tmp.Current() - tmp.Data(), 100);
    UNIT_ASSERT(tmp.Left() >= 101);
    UNIT_ASSERT(tmp.Size() >= 201);
    UNIT_ASSERT_EQUAL(tmp.Filled(), 100);

    tmp.Reset();

    UNIT_ASSERT_EQUAL(tmp.Current(), tmp.Data());
    UNIT_ASSERT(tmp.Left() >= 201);
    UNIT_ASSERT(tmp.Size() >= 201);
    UNIT_ASSERT_EQUAL(tmp.Filled(), 0);
}

void TTempBufTest::TestMoveCtor() {
    TTempBuf src;
    UNIT_ASSERT(!src.IsNull());

    src.Proceed(10);

    TTempBuf dst(std::move(src));

    UNIT_ASSERT(src.IsNull());
    UNIT_ASSERT(!dst.IsNull());
    UNIT_ASSERT_EQUAL(dst.Filled(), 10);
}

void TTempBufTest::TestProceed() {
    TTempBuf src;

    char* data = src.Proceed(100);
    UNIT_ASSERT_EQUAL(data, src.Data());
    UNIT_ASSERT_EQUAL(data + 100, src.Current());
    UNIT_ASSERT_EQUAL(100, src.Filled());

    char* second = src.Proceed(100);
    UNIT_ASSERT_EQUAL(data + 100, second);
    UNIT_ASSERT_EQUAL(data + 200, src.Current());
    UNIT_ASSERT_EQUAL(200, src.Filled());
}