133 lines · cpp
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-exception6//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(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(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 66TEST(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.85TEST(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 97TEST(OutputBufferTest, Notifications) {98 struct MyOutputBuffer : public OutputBuffer {99 size_t Inserted = 0;100 size_t LatestPos = 0;101 102 void notifyDeletion(size_t OldPos, size_t NewPos) override {103 LatestPos = NewPos;104 }105 106 void notifyInsertion(size_t Position, size_t Count) override {107 Inserted += Count;108 LatestPos = Position;109 }110 } OB;111 112 OB.prepend("n");113 EXPECT_EQ(OB.Inserted, 1U);114 EXPECT_EQ(OB.LatestPos, 0U);115 116 OB.prepend("");117 EXPECT_EQ(OB.Inserted, 1U);118 EXPECT_EQ(OB.LatestPos, 0U);119 120 OB.prepend("abc");121 EXPECT_EQ(OB.Inserted, 4U);122 EXPECT_EQ(OB.LatestPos, 0U);123 124 OB.insert(2, "abc", 3U);125 EXPECT_EQ(OB.Inserted, 7U);126 EXPECT_EQ(OB.LatestPos, 2U);127 128 OB.setCurrentPosition(3U);129 EXPECT_EQ(OB.LatestPos, 3U);130 131 std::free(OB.getBuffer());132}133