xref: /llvm-project/llvm/unittests/Demangle/OutputBufferTest.cpp (revision 7c59e8001a3b66be545a890a26ad8a24a6c9fe4b)
1 //===- llvm/unittest/OutputBufferTest.cpp - OutputStream unit tests -------===//
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/Demangle/MicrosoftDemangleNodes.h"
10 #include "llvm/Demangle/Utility.h"
11 #include "gtest/gtest.h"
12 #include <string>
13 #include <string_view>
14 
15 using namespace llvm;
16 using llvm::itanium_demangle::OutputBuffer;
17 
toString(OutputBuffer & OB)18 static std::string toString(OutputBuffer &OB) {
19   std::string_view SV = OB;
20   return {SV.begin(), SV.end()};
21 }
22 
printToString(const T & Value)23 template <typename T> static std::string printToString(const T &Value) {
24   OutputBuffer OB;
25   OB << Value;
26   std::string s = toString(OB);
27   std::free(OB.getBuffer());
28   return s;
29 }
30 
TEST(OutputBufferTest,Format)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));
38 
39   EXPECT_EQ("a", printToString('a'));
40   EXPECT_EQ("?", printToString('?'));
41 
42   EXPECT_EQ("abc", printToString("abc"));
43 }
44 
TEST(OutputBufferTest,Insert)45 TEST(OutputBufferTest, Insert) {
46   OutputBuffer OB;
47 
48   OB.insert(0, "", 0);
49   EXPECT_EQ("", toString(OB));
50 
51   OB.insert(0, "abcd", 4);
52   EXPECT_EQ("abcd", toString(OB));
53 
54   OB.insert(0, "x", 1);
55   EXPECT_EQ("xabcd", toString(OB));
56 
57   OB.insert(5, "y", 1);
58   EXPECT_EQ("xabcdy", toString(OB));
59 
60   OB.insert(3, "defghi", 6);
61   EXPECT_EQ("xabdefghicdy", toString(OB));
62 
63   std::free(OB.getBuffer());
64 }
65 
TEST(OutputBufferTest,Prepend)66 TEST(OutputBufferTest, Prepend) {
67   OutputBuffer OB;
68 
69   OB.prepend("n");
70   EXPECT_EQ("n", toString(OB));
71 
72   OB << "abc";
73   OB.prepend("def");
74   EXPECT_EQ("defnabc", toString(OB));
75 
76   OB.setCurrentPosition(3);
77 
78   OB.prepend("abc");
79   EXPECT_EQ("abcdef", toString(OB));
80 
81   std::free(OB.getBuffer());
82 }
83 
84 // Test when initial needed size is larger than the default.
TEST(OutputBufferTest,Extend)85 TEST(OutputBufferTest, Extend) {
86   OutputBuffer OB;
87 
88   char Massive[2000];
89   std::memset(Massive, 'a', sizeof(Massive));
90   Massive[sizeof(Massive) - 1] = 0;
91   OB << Massive;
92   EXPECT_EQ(Massive, toString(OB));
93 
94   std::free(OB.getBuffer());
95 }
96