| 1 | //===-- UniqueCStringMapTest.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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Core/UniqueCStringMap.h" |
| 10 | #include "gmock/gmock.h" |
| 11 | |
| 12 | using namespace lldb_private; |
| 13 | |
| 14 | namespace { |
| 15 | struct NoDefault { |
| 16 | int x; |
| 17 | |
| 18 | NoDefault(int x) : x(x) {} |
| 19 | NoDefault() = delete; |
| 20 | |
| 21 | friend bool operator==(NoDefault lhs, NoDefault rhs) { |
| 22 | return lhs.x == rhs.x; |
| 23 | } |
| 24 | |
| 25 | friend llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 26 | NoDefault x) { |
| 27 | return OS << "NoDefault{" << x.x << "}" ; |
| 28 | } |
| 29 | }; |
| 30 | } // namespace |
| 31 | |
| 32 | TEST(UniqueCStringMap, NoDefaultConstructor) { |
| 33 | using MapT = UniqueCStringMap<NoDefault>; |
| 34 | using EntryT = MapT::Entry; |
| 35 | |
| 36 | MapT Map; |
| 37 | ConstString Foo("foo" ), Bar("bar" ); |
| 38 | |
| 39 | Map.Append(unique_cstr: Foo, value: NoDefault(42)); |
| 40 | EXPECT_THAT(Map.Find(Foo, NoDefault(47)), NoDefault(42)); |
| 41 | EXPECT_THAT(Map.Find(Bar, NoDefault(47)), NoDefault(47)); |
| 42 | EXPECT_THAT(Map.FindFirstValueForName(Foo), |
| 43 | testing::Pointee(testing::Field(&EntryT::value, NoDefault(42)))); |
| 44 | EXPECT_THAT(Map.FindFirstValueForName(Bar), nullptr); |
| 45 | |
| 46 | std::vector<NoDefault> Values; |
| 47 | EXPECT_THAT(Map.GetValues(Foo, Values), 1); |
| 48 | EXPECT_THAT(Values, testing::ElementsAre(NoDefault(42))); |
| 49 | |
| 50 | Values.clear(); |
| 51 | EXPECT_THAT(Map.GetValues(Bar, Values), 0); |
| 52 | EXPECT_THAT(Values, testing::IsEmpty()); |
| 53 | } |
| 54 | |
| 55 | TEST(UniqueCStringMap, ValueCompare) { |
| 56 | UniqueCStringMap<int> Map; |
| 57 | |
| 58 | ConstString Foo("foo" ); |
| 59 | |
| 60 | Map.Append(unique_cstr: Foo, value: 0); |
| 61 | Map.Append(unique_cstr: Foo, value: 5); |
| 62 | Map.Append(unique_cstr: Foo, value: -5); |
| 63 | |
| 64 | Map.Sort(tc: std::less<int>()); |
| 65 | std::vector<int> Values; |
| 66 | EXPECT_THAT(Map.GetValues(Foo, Values), 3); |
| 67 | EXPECT_THAT(Values, testing::ElementsAre(-5, 0, 5)); |
| 68 | } |
| 69 | |