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

source code of llvm/unittests/Demangle/OutputBufferTest.cpp