aboutsummaryrefslogtreecommitdiff
blob: c55006bfb856df3a187f8b0b2c9e6ceb089b8585 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//===-- StreamTeeTest.cpp ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Utility/StreamTee.h"
#include "lldb/Utility/StreamString.h"
#include "gtest/gtest.h"

using namespace lldb_private;

TEST(StreamTeeTest, DefaultConstructor) {
  // Test the default constructor.
  StreamTee tee;
  ASSERT_EQ(0U, tee.GetNumStreams());
}

TEST(StreamTeeTest, Constructor1Stream) {
  // Test the constructor for a single stream.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  StreamTee tee(s1);

  ASSERT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
}

TEST(StreamTeeTest, Constructor2Streams) {
  // Test the constructor for two streams.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  lldb::StreamSP s2(std::make_shared<StreamString>());
  StreamTee tee(s1, s2);

  ASSERT_EQ(2U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
  EXPECT_EQ(s2, tee.GetStreamAtIndex(1U));
}

TEST(StreamTeeTest, CopyConstructor) {
  // Test the copy constructor.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  lldb::StreamSP s2(std::make_shared<StreamString>());
  StreamTee tee1(s1, s2);
  StreamTee tee2(tee1);

  ASSERT_EQ(2U, tee2.GetNumStreams());
  EXPECT_EQ(s1, tee2.GetStreamAtIndex(0U));
  EXPECT_EQ(s2, tee2.GetStreamAtIndex(1U));
}

TEST(StreamTeeTest, Assignment) {
  // Test the assignment of StreamTee.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  lldb::StreamSP s2(std::make_shared<StreamString>());
  StreamTee tee1(s1, s2);
  StreamTee tee2 = tee1;

  ASSERT_EQ(2U, tee2.GetNumStreams());
  EXPECT_EQ(s1, tee2.GetStreamAtIndex(0U));
  EXPECT_EQ(s2, tee2.GetStreamAtIndex(1U));
}

TEST(StreamTeeTest, Write) {
  // Test that write is sent out to all children.
  auto ss1 = new StreamString();
  auto ss2 = new StreamString();
  lldb::StreamSP s1(ss1);
  lldb::StreamSP s2(ss2);
  StreamTee tee(s1, s2);

  tee << "foo";
  tee.Flush();

  ASSERT_EQ(2U, tee.GetNumStreams());
  EXPECT_EQ("foo", ss1->GetString().str());
  EXPECT_EQ("foo", ss2->GetString().str());

  tee << "bar";
  tee.Flush();
  EXPECT_EQ("foobar", ss1->GetString().str());
  EXPECT_EQ("foobar", ss2->GetString().str());
}

namespace {
  struct FlushTestStream : public Stream {
    unsigned m_flush_count = false;
    void Flush() override {
      ++m_flush_count;
    }
    size_t WriteImpl(const void *src, size_t src_len) override {
      return src_len;
    }
  };
}

TEST(StreamTeeTest, Flush) {
  // Check that Flush is distributed to all streams.
  auto fs1 = new FlushTestStream();
  auto fs2 = new FlushTestStream();
  lldb::StreamSP s1(fs1);
  lldb::StreamSP s2(fs2);
  StreamTee tee(s1, s2);

  tee << "foo";
  tee.Flush();

  ASSERT_EQ(2U, tee.GetNumStreams());
  EXPECT_EQ(1U, fs1->m_flush_count);
  EXPECT_EQ(1U, fs2->m_flush_count);

  tee << "bar";
  tee.Flush();
  EXPECT_EQ(2U, fs1->m_flush_count);
  EXPECT_EQ(2U, fs2->m_flush_count);
}

TEST(StreamTeeTest, AppendStream) {
  // Append new streams to our StreamTee.
  auto ss1 = new StreamString();
  auto ss2 = new StreamString();
  lldb::StreamSP s1(ss1);
  lldb::StreamSP s2(ss2);

  StreamTee tee;

  ASSERT_EQ(0U, tee.GetNumStreams());

  tee.AppendStream(s1);
  ASSERT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));

  tee.AppendStream(s2);
  ASSERT_EQ(2U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
  EXPECT_EQ(s2, tee.GetStreamAtIndex(1U));
}

TEST(StreamTeeTest, GetStreamAtIndexOutOfBounds) {
  // The index we check for is not in the bounds of the StreamTee.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  StreamTee tee(s1);

  ASSERT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1));
}

TEST(StreamTeeTest, GetStreamAtIndexOutOfBoundsEmpty) {
  // Same as above, but with an empty StreamTee.
  StreamTee tee;
  ASSERT_EQ(0U, tee.GetNumStreams());
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(0U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1U));
}

TEST(StreamTeeTest, SetStreamAtIndexOverwrite) {
  // We overwrite an existing stream at a given index.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  StreamTee tee(s1);

  ASSERT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1U));

  lldb::StreamSP s2(std::make_shared<StreamString>());
  tee.SetStreamAtIndex(0U, s2);
  EXPECT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(s2, tee.GetStreamAtIndex(0U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1));
}

TEST(StreamTeeTest, SetStreamAtIndexOutOfBounds) {
  // We place a new stream out of the bounds of the current StreamTee.
  lldb::StreamSP s1(std::make_shared<StreamString>());
  StreamTee tee(s1);

  ASSERT_EQ(1U, tee.GetNumStreams());
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1U));

  // Place a new stream out of bounds of the current array. The StreamTee should
  // resize itself until it can contain this index.
  lldb::StreamSP s2(std::make_shared<StreamString>());
  tee.SetStreamAtIndex(4U, s2);
  // Check that the vector has been resized.
  EXPECT_EQ(5U, tee.GetNumStreams());
  // Is our stream at the right place?
  EXPECT_EQ(s2, tee.GetStreamAtIndex(4U));

  // Existing stream should still be there.
  EXPECT_EQ(s1, tee.GetStreamAtIndex(0U));
  // Other elements are all invalid StreamSPs.
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(1U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(2U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(3U));
  EXPECT_EQ(lldb::StreamSP(), tee.GetStreamAtIndex(5U));
}