summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/unittests/stream_ut.cpp
blob: 5f46105373ce5e08168eeeb7f799f357f24f8022 (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
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/string/stream.h>

#include <util/stream/str.h>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

TEST(TStdStringOutputTest, AppendsToReferencedString)
{
    std::string buffer = "abc";
    {
        TStdStringOutput output(buffer);
        output << "de" << 'f' << 42;
    }
    EXPECT_EQ(buffer, "abcdef42");
}

TEST(TStdStringOutputTest, MatchesTStringOutput)
{
    std::string stdBuffer;
    TString utilBuffer;
    TStdStringOutput stdOutput(stdBuffer);
    TStringOutput utilOutput(utilBuffer);
    for (int i = 0; i < 1000; ++i) {
        stdOutput << i << ',';
        utilOutput << i << ',';
    }
    EXPECT_EQ(stdBuffer, std::string(utilBuffer));
}

TEST(TStdStringStreamTest, WriteAndStr)
{
    TStdStringStream stream;
    EXPECT_FALSE(static_cast<bool>(stream));
    stream << "value=" << 123;
    EXPECT_TRUE(static_cast<bool>(stream));
    EXPECT_EQ(stream.Str(), "value=123");
    EXPECT_EQ(stream.Size(), 9u);
}

TEST(TStdStringStreamTest, ClearAndReuse)
{
    TStdStringStream stream("seed");
    EXPECT_EQ(stream.Str(), "seed");
    stream.Clear();
    EXPECT_TRUE(stream.Empty());
    stream << "again";
    EXPECT_EQ(stream.Str(), "again");
}

TEST(TStdStringStreamTest, MoveOutBuffer)
{
    TStdStringStream stream;
    stream << "movable";
    std::string extracted = std::move(stream).Str();
    EXPECT_EQ(extracted, "movable");
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT