1 //===- MemoryBufferRefTest.cpp - MemoryBufferRef 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 // This file implements unit tests for the MemoryBufferRef support class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/MemoryBufferRef.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 
19 namespace {
20 
TEST(MemoryBufferRefTest,construct)21 TEST(MemoryBufferRefTest, construct) {
22   std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer("data", "id"));
23   MemoryBufferRef MBR(*MB);
24 
25   EXPECT_EQ(MB->getBufferStart(), MBR.getBufferStart());
26   EXPECT_EQ(MB->getBufferIdentifier(), MBR.getBufferIdentifier());
27 }
28 
TEST(MemoryBufferRefTest,compareEquals)29 TEST(MemoryBufferRefTest, compareEquals) {
30   std::string Data = "data";
31   std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(Data, "id"));
32   MemoryBufferRef Ref(*MB);
33   MemoryBufferRef Empty;
34   MemoryBufferRef NoIdentifier(MB->getBuffer(), "");
35   MemoryBufferRef NoData("", MB->getBufferIdentifier());
36   MemoryBufferRef Same(MB->getBuffer(), MB->getBufferIdentifier());
37 
38   EXPECT_NE(Empty, Ref);
39   EXPECT_NE(NoIdentifier, Ref);
40   EXPECT_NE(NoData, Ref);
41   EXPECT_EQ(Same, Ref);
42 
43   // Confirm NE when content matches but pointer identity does not.
44   std::unique_ptr<MemoryBuffer> Copy(
45       MemoryBuffer::getMemBufferCopy(Data, "id"));
46   MemoryBufferRef CopyRef(*Copy);
47   EXPECT_EQ(Ref.getBuffer(), CopyRef.getBuffer());
48   EXPECT_NE(Ref, CopyRef);
49 }
50 
51 } // end namespace
52