1 //===- InMemoryModuleCacheTest.cpp - InMemoryModuleCache 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 "clang/Serialization/InMemoryModuleCache.h" 10 #include "llvm/Support/MemoryBuffer.h" 11 #include "gtest/gtest.h" 12 13 using namespace llvm; 14 using namespace clang; 15 16 namespace { 17 18 std::unique_ptr<MemoryBuffer> getBuffer(int I) { 19 SmallVector<char, 8> Bytes; 20 raw_svector_ostream(Bytes) << "data:" << I; 21 return MemoryBuffer::getMemBuffer(StringRef(Bytes.data(), Bytes.size()), "", 22 /* RequiresNullTerminator = */ false); 23 } 24 25 TEST(InMemoryModuleCacheTest, initialState) { 26 InMemoryModuleCache Cache; 27 EXPECT_EQ(nullptr, Cache.lookupPCM("B")); 28 EXPECT_FALSE(Cache.isPCMFinal("B")); 29 30 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST 31 EXPECT_DEATH(Cache.tryToRemovePCM("B"), "PCM to remove is unknown"); 32 EXPECT_DEATH(Cache.finalizePCM("B"), "PCM to finalize is unknown"); 33 #endif 34 } 35 36 TEST(InMemoryModuleCacheTest, addPCM) { 37 auto B = getBuffer(1); 38 auto *RawB = B.get(); 39 40 InMemoryModuleCache Cache; 41 EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B))); 42 EXPECT_EQ(RawB, Cache.lookupPCM("B")); 43 EXPECT_FALSE(Cache.isPCMFinal("B")); 44 45 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST 46 EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM"); 47 EXPECT_DEATH(Cache.addFinalPCM("B", getBuffer(2)), 48 "Already has a non-final PCM"); 49 #endif 50 } 51 52 TEST(InMemoryModuleCacheTest, addFinalPCM) { 53 auto B = getBuffer(1); 54 auto *RawB = B.get(); 55 56 InMemoryModuleCache Cache; 57 EXPECT_EQ(RawB, &Cache.addFinalPCM("B", std::move(B))); 58 EXPECT_EQ(RawB, Cache.lookupPCM("B")); 59 EXPECT_TRUE(Cache.isPCMFinal("B")); 60 61 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST 62 EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM"); 63 EXPECT_DEATH(Cache.addFinalPCM("B", getBuffer(2)), 64 "Trying to override finalized PCM"); 65 #endif 66 } 67 68 TEST(InMemoryModuleCacheTest, tryToRemovePCM) { 69 auto B1 = getBuffer(1); 70 auto B2 = getBuffer(2); 71 auto *RawB1 = B1.get(); 72 auto *RawB2 = B2.get(); 73 ASSERT_NE(RawB1, RawB2); 74 75 InMemoryModuleCache Cache; 76 EXPECT_EQ(RawB1, &Cache.addPCM("B", std::move(B1))); 77 EXPECT_FALSE(Cache.tryToRemovePCM("B")); 78 EXPECT_EQ(nullptr, Cache.lookupPCM("B")); 79 EXPECT_FALSE(Cache.isPCMFinal("B")); 80 81 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST 82 EXPECT_DEATH(Cache.tryToRemovePCM("B"), "PCM to remove is unknown"); 83 EXPECT_DEATH(Cache.finalizePCM("B"), "PCM to finalize is unknown"); 84 #endif 85 86 // Add a new one. 87 EXPECT_EQ(RawB2, &Cache.addFinalPCM("B", std::move(B2))); 88 EXPECT_TRUE(Cache.isPCMFinal("B")); 89 90 // Can try to drop again, but this should error and do nothing. 91 EXPECT_TRUE(Cache.tryToRemovePCM("B")); 92 EXPECT_EQ(RawB2, Cache.lookupPCM("B")); 93 } 94 95 TEST(InMemoryModuleCacheTest, finalizePCM) { 96 auto B = getBuffer(1); 97 auto *RawB = B.get(); 98 99 InMemoryModuleCache Cache; 100 EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B))); 101 102 // Call finalize. 103 Cache.finalizePCM("B"); 104 EXPECT_TRUE(Cache.isPCMFinal("B")); 105 } 106 107 } // namespace 108