aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/input_ut.cpp
blob: 4a93f5458e68607cd2ecd5863cbca4685c50d935 (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
#include "input.h"
#include "output.h"

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

#include <util/system/file.h>
#include <util/system/yassert.h>

#ifdef _win_
    #include <io.h>
#endif

class TMockStdIn {
public:
    TMockStdIn()
        : StdInCopy_(dup(0))
    {
    }
    ~TMockStdIn() {
        close(StdInCopy_);
    }

    template <typename FuncType>
    void ForInput(const TStringBuf text, const FuncType& func) {
        TFile tempFile(TFile::Temporary("input_ut"));
        tempFile.Write(text.data(), text.size());
        tempFile.FlushData();
        tempFile.Seek(0, sSet);

        TFileHandle tempFh(tempFile.GetHandle());
        tempFh.Duplicate2Posix(0);
        tempFh.Release();

        func();
        Cin.ReadAll();
        dup2(StdInCopy_, 0);
        clearerr(stdin);
    }

private:
    int StdInCopy_;
};

class TNoInput: public IInputStream {
public:
    TNoInput(ui64 size)
        : Size_(size)
    {
    }

protected:
    size_t DoRead(void*, size_t len) override {
        len = Min(static_cast<ui64>(len), Size_);
        Size_ -= len;
        return len;
    }

private:
    ui64 Size_;
};

class TNoOutput: public IOutputStream {
public:
    TNoOutput() = default;

protected:
    void DoWrite(const void*, size_t) override {
    }
};

class TSimpleStringInput: public IInputStream {
public:
    TSimpleStringInput(const TString& string)
        : String_(string)
    {
    }

protected:
    size_t DoRead(void* buf, size_t len) override {
        Y_ASSERT(len != 0);

        if (String_.empty()) {
            return 0;
        }

        *static_cast<char*>(buf) = String_[0];
        String_.remove(0, 1);
        return 1;
    }

private:
    TString String_;
};

Y_UNIT_TEST_SUITE(TInputTest) {
    Y_UNIT_TEST(BigTransfer) {
        ui64 size = 1024ull * 1024ull * 1024ull * 5;
        TNoInput input(size);
        TNoOutput output;

        ui64 transferred = TransferData(&input, &output);

        UNIT_ASSERT_VALUES_EQUAL(transferred, size);
    }

    Y_UNIT_TEST(TestReadTo) {
        /* This one tests default implementation of ReadTo. */

        TSimpleStringInput in("0123456789abc");

        TString t;
        UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8);
        UNIT_ASSERT_VALUES_EQUAL(t, "0123456");
        UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5);
        UNIT_ASSERT_VALUES_EQUAL(t, "89abc");
        UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 0);
        UNIT_ASSERT_VALUES_EQUAL(t, "89abc");
    }

    Y_UNIT_TEST(TestReadLine) {
        TSimpleStringInput in("1\n22\n333");

        TString t;
        UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 2);
        UNIT_ASSERT_VALUES_EQUAL(t, "1");
        UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3);
        UNIT_ASSERT_VALUES_EQUAL(t, "22");
        UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 3);
        UNIT_ASSERT_VALUES_EQUAL(t, "333");
        UNIT_ASSERT_VALUES_EQUAL(in.ReadLine(t), 0);
        UNIT_ASSERT_VALUES_EQUAL(t, "333");
    }

    Y_UNIT_TEST(TestStdInReadTo) {
        std::pair<std::pair<TStringBuf, char>, TStringBuf> testPairs[] = {
            {{"", '\n'}, ""},
            {{"\n", '\n'}, ""},
            {{"\n\t", '\t'}, "\n"},
            {{"\t\n", '\n'}, "\t"},
            {{"a\tb\n", '\t'}, "a"}};

        TMockStdIn stdIn;

        for (const auto& testPair : testPairs) {
            const TStringBuf text = testPair.first.first;
            const char delim = testPair.first.second;
            const TStringBuf expectedValue = testPair.second;

            stdIn.ForInput(text,
                           [=] {
                               TString value;
                               Cin.ReadTo(value, delim);
                               UNIT_ASSERT_VALUES_EQUAL(value, expectedValue);
                           });
        }
    }
}