[LV] Add tests with dereferenceable assumptions.
[llvm-project.git] / llvm / unittests / Demangle / OutputBufferTest.cpp
blob76031e523d78101625b6fab967360758c1b9b5ec
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Demangle/MicrosoftDemangleNodes.h"
10 #include "llvm/Demangle/Utility.h"
11 #include "gtest/gtest.h"
12 #include <string>
13 #include <string_view>
15 using namespace llvm;
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) {
24 OutputBuffer OB;
25 OB << Value;
26 std::string s = toString(OB);
27 std::free(OB.getBuffer());
28 return s;
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) {
46 OutputBuffer OB;
48 OB.insert(0, "", 0);
49 EXPECT_EQ("", toString(OB));
51 OB.insert(0, "abcd", 4);
52 EXPECT_EQ("abcd", toString(OB));
54 OB.insert(0, "x", 1);
55 EXPECT_EQ("xabcd", toString(OB));
57 OB.insert(5, "y", 1);
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) {
67 OutputBuffer OB;
69 OB.prepend("n");
70 EXPECT_EQ("n", toString(OB));
72 OB << "abc";
73 OB.prepend("def");
74 EXPECT_EQ("defnabc", toString(OB));
76 OB.setCurrentPosition(3);
78 OB.prepend("abc");
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) {
86 OutputBuffer OB;
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));
94 std::free(OB.getBuffer());