1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
//===- MemoryBufferCacheTest.cpp - MemoryBufferCache tests ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/MemoryBufferCache.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
std::unique_ptr<MemoryBuffer> getBuffer(int I) {
SmallVector<char, 8> Bytes;
raw_svector_ostream(Bytes) << "data:" << I;
return MemoryBuffer::getMemBuffer(StringRef(Bytes.data(), Bytes.size()), "",
/* RequiresNullTerminator = */ false);
}
TEST(MemoryBufferCacheTest, addBuffer) {
auto B1 = getBuffer(1);
auto B2 = getBuffer(2);
auto B3 = getBuffer(3);
auto *RawB1 = B1.get();
auto *RawB2 = B2.get();
auto *RawB3 = B3.get();
// Add a few buffers.
MemoryBufferCache Cache;
EXPECT_EQ(RawB1, &Cache.addBuffer("1", std::move(B1)));
EXPECT_EQ(RawB2, &Cache.addBuffer("2", std::move(B2)));
EXPECT_EQ(RawB3, &Cache.addBuffer("3", std::move(B3)));
EXPECT_EQ(RawB1, Cache.lookupBuffer("1"));
EXPECT_EQ(RawB2, Cache.lookupBuffer("2"));
EXPECT_EQ(RawB3, Cache.lookupBuffer("3"));
EXPECT_FALSE(Cache.isBufferFinal("1"));
EXPECT_FALSE(Cache.isBufferFinal("2"));
EXPECT_FALSE(Cache.isBufferFinal("3"));
// Remove the middle buffer.
EXPECT_FALSE(Cache.tryToRemoveBuffer("2"));
EXPECT_EQ(nullptr, Cache.lookupBuffer("2"));
EXPECT_FALSE(Cache.isBufferFinal("2"));
// Replace the middle buffer.
B2 = getBuffer(2);
RawB2 = B2.get();
EXPECT_EQ(RawB2, &Cache.addBuffer("2", std::move(B2)));
// Check that nothing is final.
EXPECT_FALSE(Cache.isBufferFinal("1"));
EXPECT_FALSE(Cache.isBufferFinal("2"));
EXPECT_FALSE(Cache.isBufferFinal("3"));
}
TEST(MemoryBufferCacheTest, finalizeCurrentBuffers) {
// Add a buffer.
MemoryBufferCache Cache;
auto B1 = getBuffer(1);
auto *RawB1 = B1.get();
Cache.addBuffer("1", std::move(B1));
ASSERT_FALSE(Cache.isBufferFinal("1"));
// Finalize it.
Cache.finalizeCurrentBuffers();
EXPECT_TRUE(Cache.isBufferFinal("1"));
EXPECT_TRUE(Cache.tryToRemoveBuffer("1"));
EXPECT_EQ(RawB1, Cache.lookupBuffer("1"));
EXPECT_TRUE(Cache.isBufferFinal("1"));
// Repeat.
auto B2 = getBuffer(2);
auto *RawB2 = B2.get();
Cache.addBuffer("2", std::move(B2));
EXPECT_FALSE(Cache.isBufferFinal("2"));
Cache.finalizeCurrentBuffers();
EXPECT_TRUE(Cache.isBufferFinal("1"));
EXPECT_TRUE(Cache.isBufferFinal("2"));
EXPECT_TRUE(Cache.tryToRemoveBuffer("1"));
EXPECT_TRUE(Cache.tryToRemoveBuffer("2"));
EXPECT_EQ(RawB1, Cache.lookupBuffer("1"));
EXPECT_EQ(RawB2, Cache.lookupBuffer("2"));
EXPECT_TRUE(Cache.isBufferFinal("1"));
EXPECT_TRUE(Cache.isBufferFinal("2"));
}
} // namespace
|