aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/buffer_ut.cpp
blob: 349469619076d0726a4af1163286c22b64f773aa (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
#include "buffer.h"

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

#include <util/generic/buffer.h>

#include <cstring>

#include "str.h"

Y_UNIT_TEST_SUITE(TBufferTest) {
    Y_UNIT_TEST(Transfer) {
        TBuffer buffer("razrazraz", 9);
        TBufferInput input(buffer);

        input.Skip(3);

        TStringStream output;
        TransferData(&input, &output);

        UNIT_ASSERT_VALUES_EQUAL(output.Str(), "razraz");
    }

    Y_UNIT_TEST(ReadTo) {
        TBuffer buffer("1234567890", 10);
        TBufferInput input(buffer);

        TString tmp;
        UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, '3'), 3);
        UNIT_ASSERT_VALUES_EQUAL(tmp, "12");

        UNIT_ASSERT_VALUES_EQUAL(input.ReadTo(tmp, 'z'), 7);
        UNIT_ASSERT_VALUES_EQUAL(tmp, "4567890");
    }

    Y_UNIT_TEST(WriteViaNextAndUndo) {
        TBuffer buffer;
        TBufferOutput output(buffer);
        TString str;

        for (size_t i = 0; i < 10000; ++i) {
            str.push_back('a' + (i % 20));
        }

        size_t written = 0;
        void* ptr = nullptr;
        while (written < str.size()) {
            size_t bufferSize = output.Next(&ptr);
            UNIT_ASSERT(ptr && bufferSize > 0);
            size_t toWrite = Min(bufferSize, str.size() - written);
            memcpy(ptr, str.begin() + written, toWrite);
            written += toWrite;
            if (toWrite < bufferSize) {
                output.Undo(bufferSize - toWrite);
            }
        }

        UNIT_ASSERT(0 == memcmp(buffer.data(), str.begin(), buffer.size()));
    }

    Y_UNIT_TEST(Write) {
        TBuffer buffer;
        TBufferOutput output(buffer);
        output << "1"
               << "22"
               << "333"
               << "4444"
               << "55555";

        UNIT_ASSERT(0 == memcmp(buffer.data(), "1"
                                               "22"
                                               "333"
                                               "4444"
                                               "55555",
                                buffer.size()));
    }

    Y_UNIT_TEST(WriteChars) {
        TBuffer buffer;
        TBufferOutput output(buffer);
        output << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << '0';

        UNIT_ASSERT(0 == memcmp(buffer.data(), "1234567890", buffer.size()));
    }
}