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