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 
14 using namespace llvm;
15 using llvm::itanium_demangle::OutputBuffer;
16 
17 static std::string toString(OutputBuffer &OB) {
18   return {OB.getBuffer(), OB.getCurrentPosition()};
19 }
20 
21 template <typename T> static std::string printToString(const T &Value) {
22   OutputBuffer OB;
23   OB << Value;
24   std::string s = toString(OB);
25   std::free(OB.getBuffer());
26   return s;
27 }
28 
29 TEST(OutputBufferTest, Format) {
30   EXPECT_EQ("0", printToString(0));
31   EXPECT_EQ("1", printToString(1));
32   EXPECT_EQ("-1", printToString(-1));
33   EXPECT_EQ("-90", printToString(-90));
34   EXPECT_EQ("109", printToString(109));
35   EXPECT_EQ("400", printToString(400));
36 
37   EXPECT_EQ("a", printToString('a'));
38   EXPECT_EQ("?", printToString('?'));
39 
40   EXPECT_EQ("abc", printToString("abc"));
41 }
42 
43 TEST(OutputBufferTest, Insert) {
44   OutputBuffer OB;
45 
46   OB.insert(0, "", 0);
47   EXPECT_EQ("", toString(OB));
48 
49   OB.insert(0, "abcd", 4);
50   EXPECT_EQ("abcd", toString(OB));
51 
52   OB.insert(0, "x", 1);
53   EXPECT_EQ("xabcd", toString(OB));
54 
55   OB.insert(5, "y", 1);
56   EXPECT_EQ("xabcdy", toString(OB));
57 
58   OB.insert(3, "defghi", 6);
59   EXPECT_EQ("xabdefghicdy", toString(OB));
60 
61   std::free(OB.getBuffer());
62 }
63 
64 TEST(OutputBufferTest, Prepend) {
65   OutputBuffer OB;
66 
67   OB.prepend("n");
68   EXPECT_EQ("n", toString(OB));
69 
70   OB << "abc";
71   OB.prepend("def");
72   EXPECT_EQ("defnabc", toString(OB));
73 
74   OB.setCurrentPosition(3);
75 
76   OB.prepend("abc");
77   EXPECT_EQ("abcdef", toString(OB));
78 
79   std::free(OB.getBuffer());
80 }
81