|
| 1 | +//===- raw_ostream_proxy_test.cpp - Tests for raw ostream proxies ---------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/ADT/SmallString.h" |
| 10 | +#include "llvm/Support/WithColor.h" |
| 11 | +#include "llvm/Support/raw_ostream.h" |
| 12 | +#include "llvm/Support/raw_ostream_proxy.h" |
| 13 | +#include "gtest/gtest.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +/// Naive version of raw_svector_ostream that is buffered (by default) and |
| 20 | +/// doesn't support pwrite. |
| 21 | +class BufferedNoPwriteSmallVectorStream : public raw_ostream { |
| 22 | +public: |
| 23 | + // Choose a strange buffer size to ensure it doesn't collide with the default |
| 24 | + // on \a raw_ostream. |
| 25 | + static constexpr size_t PreferredBufferSize = 63; |
| 26 | + |
| 27 | + size_t preferred_buffer_size() const override { return PreferredBufferSize; } |
| 28 | + uint64_t current_pos() const override { return Vector.size(); } |
| 29 | + void write_impl(const char *Ptr, size_t Size) override { |
| 30 | + Vector.append(Ptr, Ptr + Size); |
| 31 | + } |
| 32 | + |
| 33 | + bool is_displayed() const override { return IsDisplayed; } |
| 34 | + |
| 35 | + explicit BufferedNoPwriteSmallVectorStream(SmallVectorImpl<char> &Vector) |
| 36 | + : Vector(Vector) {} |
| 37 | + ~BufferedNoPwriteSmallVectorStream() override { flush(); } |
| 38 | + |
| 39 | + SmallVectorImpl<char> &Vector; |
| 40 | + bool IsDisplayed = false; |
| 41 | +}; |
| 42 | + |
| 43 | +constexpr size_t BufferedNoPwriteSmallVectorStream::PreferredBufferSize; |
| 44 | + |
| 45 | +TEST(raw_ostream_proxyTest, write) { |
| 46 | + // Besides confirming that "write" works, this test confirms that the proxy |
| 47 | + // takes on the buffer from the stream it's proxying, such that writes to the |
| 48 | + // proxy are flushed to the underlying stream as if no proxy were present. |
| 49 | + SmallString<128> Dest; |
| 50 | + { |
| 51 | + // Confirm that BufferedNoPwriteSmallVectorStream is buffered by default, |
| 52 | + // and that setting up a proxy effectively transfers a buffer of the same |
| 53 | + // size to the proxy. |
| 54 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 55 | + EXPECT_EQ(BufferedNoPwriteSmallVectorStream::PreferredBufferSize, |
| 56 | + DestOS.GetBufferSize()); |
| 57 | + raw_ostream_proxy ProxyOS(DestOS); |
| 58 | + EXPECT_EQ(0u, DestOS.GetBufferSize()); |
| 59 | + EXPECT_EQ(BufferedNoPwriteSmallVectorStream::PreferredBufferSize, |
| 60 | + ProxyOS.GetBufferSize()); |
| 61 | + |
| 62 | + // Flushing should send through to Dest. |
| 63 | + ProxyOS << "abcd"; |
| 64 | + EXPECT_EQ("", Dest); |
| 65 | + ProxyOS.flush(); |
| 66 | + EXPECT_EQ("abcd", Dest); |
| 67 | + |
| 68 | + // Buffer should still work. |
| 69 | + ProxyOS << "e"; |
| 70 | + EXPECT_EQ("abcd", Dest); |
| 71 | + } |
| 72 | + |
| 73 | + // Destructing ProxyOS should flush (and not crash). |
| 74 | + EXPECT_EQ("abcde", Dest); |
| 75 | + |
| 76 | + { |
| 77 | + // Set up another stream, this time unbuffered. |
| 78 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 79 | + DestOS.SetUnbuffered(); |
| 80 | + EXPECT_EQ(0u, DestOS.GetBufferSize()); |
| 81 | + raw_ostream_proxy ProxyOS(DestOS); |
| 82 | + EXPECT_EQ(0u, DestOS.GetBufferSize()); |
| 83 | + EXPECT_EQ(0u, ProxyOS.GetBufferSize()); |
| 84 | + |
| 85 | + // Flushing should not be required. |
| 86 | + ProxyOS << "f"; |
| 87 | + EXPECT_EQ("abcdef", Dest); |
| 88 | + } |
| 89 | + EXPECT_EQ("abcdef", Dest); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(raw_ostream_proxyTest, pwrite) { |
| 93 | + // This test confirms that the proxy takes on the buffer from the stream it's |
| 94 | + // proxying, such that writes to the proxy are flushed to the underlying |
| 95 | + // stream as if no proxy were present. |
| 96 | + SmallString<128> Dest; |
| 97 | + raw_svector_ostream DestOS(Dest); |
| 98 | + raw_pwrite_stream_proxy ProxyOS(DestOS); |
| 99 | + EXPECT_EQ(0u, ProxyOS.GetBufferSize()); |
| 100 | + |
| 101 | + // Get some initial data. |
| 102 | + ProxyOS << "abcd"; |
| 103 | + EXPECT_EQ("abcd", Dest); |
| 104 | + |
| 105 | + // Confirm that pwrite works. |
| 106 | + ProxyOS.pwrite("BC", 2, 1); |
| 107 | + EXPECT_EQ("aBCd", Dest); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(raw_ostream_proxyTest, pwriteWithBuffer) { |
| 111 | + // This test confirms that when a buffer is configured, pwrite still works. |
| 112 | + SmallString<128> Dest; |
| 113 | + raw_svector_ostream DestOS(Dest); |
| 114 | + DestOS.SetBufferSize(256); |
| 115 | + EXPECT_EQ(256u, DestOS.GetBufferSize()); |
| 116 | + |
| 117 | + // Confirm that the proxy steals the buffer. |
| 118 | + raw_pwrite_stream_proxy ProxyOS(DestOS); |
| 119 | + EXPECT_EQ(0u, DestOS.GetBufferSize()); |
| 120 | + EXPECT_EQ(256u, ProxyOS.GetBufferSize()); |
| 121 | + |
| 122 | + // Check that the buffer is working. |
| 123 | + ProxyOS << "abcd"; |
| 124 | + EXPECT_EQ("", Dest); |
| 125 | + |
| 126 | + // Confirm that pwrite flushes. |
| 127 | + ProxyOS.pwrite("BC", 2, 1); |
| 128 | + EXPECT_EQ("aBCd", Dest); |
| 129 | +} |
| 130 | + |
| 131 | +class ProxyWithReset : public raw_ostream_proxy_adaptor<> { |
| 132 | +public: |
| 133 | + ProxyWithReset(raw_ostream &OS) : raw_ostream_proxy_adaptor<>(OS) {} |
| 134 | + |
| 135 | + // Allow this to be called outside the class. |
| 136 | + using raw_ostream_proxy_adaptor<>::hasProxiedOS; |
| 137 | + using raw_ostream_proxy_adaptor<>::getProxiedOS; |
| 138 | + using raw_ostream_proxy_adaptor<>::resetProxiedOS; |
| 139 | +}; |
| 140 | + |
| 141 | +TEST(raw_ostream_proxyTest, resetProxiedOS) { |
| 142 | + // Confirm that base classes can drop the proxied OS before destruction and |
| 143 | + // get consistent crashes. |
| 144 | + SmallString<128> Dest; |
| 145 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 146 | + ProxyWithReset ProxyOS(DestOS); |
| 147 | + EXPECT_TRUE(ProxyOS.hasProxiedOS()); |
| 148 | + EXPECT_EQ(&DestOS, &ProxyOS.getProxiedOS()); |
| 149 | + |
| 150 | + // Write some data. |
| 151 | + ProxyOS << "abcd"; |
| 152 | + EXPECT_EQ("", Dest); |
| 153 | + |
| 154 | + // Reset the underlying stream. |
| 155 | + ProxyOS.resetProxiedOS(); |
| 156 | + EXPECT_EQ("abcd", Dest); |
| 157 | + EXPECT_EQ(0u, ProxyOS.GetBufferSize()); |
| 158 | + EXPECT_FALSE(ProxyOS.hasProxiedOS()); |
| 159 | + |
| 160 | +#if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 161 | + EXPECT_DEATH(ProxyOS << "e", "use after reset"); |
| 162 | + EXPECT_DEATH(ProxyOS.getProxiedOS(), "use after reset"); |
| 163 | +#endif |
| 164 | +} |
| 165 | + |
| 166 | +TEST(raw_ostream_proxyTest, ColorMode) { |
| 167 | + { |
| 168 | + SmallString<128> Dest; |
| 169 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 170 | + DestOS.IsDisplayed = true; |
| 171 | + raw_ostream_proxy ProxyOS(DestOS); |
| 172 | + ProxyOS.enable_colors(true); |
| 173 | + |
| 174 | + WithColor(ProxyOS, raw_ostream::Colors::RED, /*Bold=*/true, /*BG=*/false, |
| 175 | + ColorMode::Disable) |
| 176 | + << "test"; |
| 177 | + EXPECT_EQ("", Dest); |
| 178 | + ProxyOS.flush(); |
| 179 | + EXPECT_EQ("test", Dest); |
| 180 | + } |
| 181 | + |
| 182 | + { |
| 183 | + SmallString<128> Dest; |
| 184 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 185 | + raw_ostream_proxy ProxyOS(DestOS); |
| 186 | + |
| 187 | + WithColor(ProxyOS, raw_ostream::Colors::RED, /*Bold=*/true, /*BG=*/false, |
| 188 | + ColorMode::Auto) |
| 189 | + << "test"; |
| 190 | + EXPECT_EQ("", Dest); |
| 191 | + ProxyOS.flush(); |
| 192 | + EXPECT_EQ("test", Dest); |
| 193 | + } |
| 194 | + |
| 195 | +#ifdef LLVM_ON_UNIX |
| 196 | + { |
| 197 | + SmallString<128> Dest; |
| 198 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 199 | + raw_ostream_proxy ProxyOS(DestOS); |
| 200 | + ProxyOS.enable_colors(true); |
| 201 | + |
| 202 | + WithColor(ProxyOS, raw_ostream::Colors::RED, /*Bold=*/true, /*BG=*/false, |
| 203 | + ColorMode::Enable) |
| 204 | + << "test"; |
| 205 | + EXPECT_EQ("", Dest); |
| 206 | + ProxyOS.flush(); |
| 207 | + EXPECT_EQ("\x1B[0;1;31mtest\x1B[0m", Dest); |
| 208 | + } |
| 209 | + |
| 210 | + { |
| 211 | + SmallString<128> Dest; |
| 212 | + BufferedNoPwriteSmallVectorStream DestOS(Dest); |
| 213 | + DestOS.IsDisplayed = true; |
| 214 | + raw_ostream_proxy ProxyOS(DestOS); |
| 215 | + ProxyOS.enable_colors(true); |
| 216 | + |
| 217 | + WithColor(ProxyOS, HighlightColor::Error, ColorMode::Auto) << "test"; |
| 218 | + EXPECT_EQ("", Dest); |
| 219 | + ProxyOS.flush(); |
| 220 | + EXPECT_EQ("\x1B[0;1;31mtest\x1B[0m", Dest); |
| 221 | + } |
| 222 | +#endif |
| 223 | +} |
| 224 | + |
| 225 | +} // end namespace |
0 commit comments