[libc++][Android] BuildKite CI: update Clang and sysroot versions (#116151)
[llvm-project.git] / clang / unittests / Serialization / InMemoryModuleCacheTest.cpp
blobed5e1538eba74bda6bf65f246148c5e24f4c8901
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 //===----------------------------------------------------------------------===//
9 #include "clang/Serialization/InMemoryModuleCache.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "gtest/gtest.h"
13 using namespace llvm;
14 using namespace clang;
16 namespace {
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);
25 TEST(InMemoryModuleCacheTest, initialState) {
26 InMemoryModuleCache Cache;
27 EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
28 EXPECT_FALSE(Cache.isPCMFinal("B"));
29 EXPECT_FALSE(Cache.shouldBuildPCM("B"));
31 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
32 EXPECT_DEATH(Cache.tryToDropPCM("B"), "PCM to remove is unknown");
33 EXPECT_DEATH(Cache.finalizePCM("B"), "PCM to finalize is unknown");
34 #endif
37 TEST(InMemoryModuleCacheTest, addPCM) {
38 auto B = getBuffer(1);
39 auto *RawB = B.get();
41 InMemoryModuleCache Cache;
42 EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));
43 EXPECT_EQ(InMemoryModuleCache::Tentative, Cache.getPCMState("B"));
44 EXPECT_EQ(RawB, Cache.lookupPCM("B"));
45 EXPECT_FALSE(Cache.isPCMFinal("B"));
46 EXPECT_FALSE(Cache.shouldBuildPCM("B"));
48 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
49 EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
50 EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
51 "Trying to override tentative PCM");
52 #endif
55 TEST(InMemoryModuleCacheTest, addBuiltPCM) {
56 auto B = getBuffer(1);
57 auto *RawB = B.get();
59 InMemoryModuleCache Cache;
60 EXPECT_EQ(RawB, &Cache.addBuiltPCM("B", std::move(B)));
61 EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
62 EXPECT_EQ(RawB, Cache.lookupPCM("B"));
63 EXPECT_TRUE(Cache.isPCMFinal("B"));
64 EXPECT_FALSE(Cache.shouldBuildPCM("B"));
66 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
67 EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
68 EXPECT_DEATH(Cache.addBuiltPCM("B", getBuffer(2)),
69 "Trying to override finalized PCM");
70 #endif
73 TEST(InMemoryModuleCacheTest, tryToDropPCM) {
74 auto B1 = getBuffer(1);
75 auto B2 = getBuffer(2);
76 auto *RawB1 = B1.get();
77 auto *RawB2 = B2.get();
78 ASSERT_NE(RawB1, RawB2);
80 InMemoryModuleCache Cache;
81 EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
82 EXPECT_EQ(RawB1, &Cache.addPCM("B", std::move(B1)));
83 EXPECT_FALSE(Cache.tryToDropPCM("B"));
84 EXPECT_EQ(nullptr, Cache.lookupPCM("B"));
85 EXPECT_EQ(InMemoryModuleCache::ToBuild, Cache.getPCMState("B"));
86 EXPECT_FALSE(Cache.isPCMFinal("B"));
87 EXPECT_TRUE(Cache.shouldBuildPCM("B"));
89 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
90 EXPECT_DEATH(Cache.addPCM("B", getBuffer(2)), "Already has a PCM");
91 EXPECT_DEATH(Cache.tryToDropPCM("B"),
92 "PCM to remove is scheduled to be built");
93 EXPECT_DEATH(Cache.finalizePCM("B"), "Trying to finalize a dropped PCM");
94 #endif
96 // Add a new one.
97 EXPECT_EQ(RawB2, &Cache.addBuiltPCM("B", std::move(B2)));
98 EXPECT_TRUE(Cache.isPCMFinal("B"));
100 // Can try to drop again, but this should error and do nothing.
101 EXPECT_TRUE(Cache.tryToDropPCM("B"));
102 EXPECT_EQ(RawB2, Cache.lookupPCM("B"));
105 TEST(InMemoryModuleCacheTest, finalizePCM) {
106 auto B = getBuffer(1);
107 auto *RawB = B.get();
109 InMemoryModuleCache Cache;
110 EXPECT_EQ(InMemoryModuleCache::Unknown, Cache.getPCMState("B"));
111 EXPECT_EQ(RawB, &Cache.addPCM("B", std::move(B)));
113 // Call finalize.
114 Cache.finalizePCM("B");
115 EXPECT_EQ(InMemoryModuleCache::Final, Cache.getPCMState("B"));
116 EXPECT_TRUE(Cache.isPCMFinal("B"));
119 } // namespace