[flang] Fix length handling in character kind implicit conversion (#74586)
[llvm-project.git] / lldb / unittests / Target / MemoryTagMapTest.cpp
blobcadcecfbb2289a1a4139bebd31eed9154507a0f3
1 //===-- MemoryTagMapTest.cpp ----------------------------------------------===//
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 "lldb/Target/MemoryTagMap.h"
10 #include "Plugins/Process/Utility/MemoryTagManagerAArch64MTE.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 #include <optional>
15 using namespace lldb_private;
16 using namespace lldb;
18 // In these tests we use the AArch64 MTE tag manager because it is the only
19 // implementation of a memory tag manager. MemoryTagMap itself is generic.
21 TEST(MemoryTagMapTest, EmptyTagMap) {
22 MemoryTagManagerAArch64MTE manager;
23 MemoryTagMap tag_map(&manager);
25 tag_map.InsertTags(0, {});
26 ASSERT_TRUE(tag_map.Empty());
27 tag_map.InsertTags(0, {0});
28 ASSERT_FALSE(tag_map.Empty());
31 TEST(MemoryTagMapTest, GetTags) {
32 using TagsVec = std::vector<std::optional<lldb::addr_t>>;
34 MemoryTagManagerAArch64MTE manager;
35 MemoryTagMap tag_map(&manager);
37 // No tags for an address not in the map
38 ASSERT_TRUE(tag_map.GetTags(0, 16).empty());
40 tag_map.InsertTags(0, {0, 1});
42 // No tags if you read zero length
43 ASSERT_TRUE(tag_map.GetTags(0, 0).empty());
45 EXPECT_THAT(tag_map.GetTags(0, 16), ::testing::ContainerEq(TagsVec{0}));
47 EXPECT_THAT(tag_map.GetTags(0, 32), ::testing::ContainerEq(TagsVec{0, 1}));
49 // Last granule of the range is not tagged
50 EXPECT_THAT(tag_map.GetTags(0, 48),
51 ::testing::ContainerEq(TagsVec{0, 1, std::nullopt}));
53 EXPECT_THAT(tag_map.GetTags(16, 32),
54 ::testing::ContainerEq(TagsVec{1, std::nullopt}));
56 // Reading beyond that address gives you no tags at all
57 EXPECT_THAT(tag_map.GetTags(32, 16), ::testing::ContainerEq(TagsVec{}));
59 // Address is granule aligned for you
60 // The length here is set such that alignment doesn't produce a 2 granule
61 // range.
62 EXPECT_THAT(tag_map.GetTags(8, 8), ::testing::ContainerEq(TagsVec{0}));
64 EXPECT_THAT(tag_map.GetTags(30, 2), ::testing::ContainerEq(TagsVec{1}));
66 // Here the length pushes the range into the next granule. When aligned
67 // this produces 2 granules.
68 EXPECT_THAT(tag_map.GetTags(30, 4),
69 ::testing::ContainerEq(TagsVec{1, std::nullopt}));
71 // A range can also have gaps at the beginning or in the middle.
72 // Add more tags, 1 granule away from the first range.
73 tag_map.InsertTags(48, {3, 4});
75 // Untagged first granule
76 EXPECT_THAT(tag_map.GetTags(32, 32),
77 ::testing::ContainerEq(TagsVec{std::nullopt, 3}));
79 // Untagged middle granule
80 EXPECT_THAT(tag_map.GetTags(16, 48),
81 ::testing::ContainerEq(TagsVec{1, std::nullopt, 3}));