1 //===- llvm/unittest/OutputBufferTest.cpp - OutputStream unit tests -------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Demangle/MicrosoftDemangleNodes.h"
10 #include "llvm/Demangle/Utility.h"
11 #include "gtest/gtest.h"
13 #include <string_view>
16 using llvm::itanium_demangle::OutputBuffer
;
18 static std::string
toString(OutputBuffer
&OB
) {
19 std::string_view SV
= OB
;
20 return {SV
.begin(), SV
.end()};
23 template <typename T
> static std::string
printToString(const T
&Value
) {
26 std::string s
= toString(OB
);
27 std::free(OB
.getBuffer());
31 TEST(OutputBufferTest
, Format
) {
32 EXPECT_EQ("0", printToString(0));
33 EXPECT_EQ("1", printToString(1));
34 EXPECT_EQ("-1", printToString(-1));
35 EXPECT_EQ("-90", printToString(-90));
36 EXPECT_EQ("109", printToString(109));
37 EXPECT_EQ("400", printToString(400));
39 EXPECT_EQ("a", printToString('a'));
40 EXPECT_EQ("?", printToString('?'));
42 EXPECT_EQ("abc", printToString("abc"));
45 TEST(OutputBufferTest
, Insert
) {
49 EXPECT_EQ("", toString(OB
));
51 OB
.insert(0, "abcd", 4);
52 EXPECT_EQ("abcd", toString(OB
));
55 EXPECT_EQ("xabcd", toString(OB
));
58 EXPECT_EQ("xabcdy", toString(OB
));
60 OB
.insert(3, "defghi", 6);
61 EXPECT_EQ("xabdefghicdy", toString(OB
));
63 std::free(OB
.getBuffer());
66 TEST(OutputBufferTest
, Prepend
) {
70 EXPECT_EQ("n", toString(OB
));
74 EXPECT_EQ("defnabc", toString(OB
));
76 OB
.setCurrentPosition(3);
79 EXPECT_EQ("abcdef", toString(OB
));
81 std::free(OB
.getBuffer());
84 // Test when initial needed size is larger than the default.
85 TEST(OutputBufferTest
, Extend
) {
89 std::memset(Massive
, 'a', sizeof(Massive
));
90 Massive
[sizeof(Massive
) - 1] = 0;
92 EXPECT_EQ(Massive
, toString(OB
));
94 std::free(OB
.getBuffer());