1 //===- MemoryBufferRefTest.cpp - MemoryBufferRef tests --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements unit tests for the MemoryBufferRef support class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/MemoryBufferRef.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "gtest/gtest.h"
21 TEST(MemoryBufferRefTest
, construct
) {
22 std::unique_ptr
<MemoryBuffer
> MB(MemoryBuffer::getMemBuffer("data", "id"));
23 MemoryBufferRef
MBR(*MB
);
25 EXPECT_EQ(MB
->getBufferStart(), MBR
.getBufferStart());
26 EXPECT_EQ(MB
->getBufferIdentifier(), MBR
.getBufferIdentifier());
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());
38 EXPECT_NE(Empty
, Ref
);
39 EXPECT_NE(NoIdentifier
, Ref
);
40 EXPECT_NE(NoData
, Ref
);
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
);