1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 18 #include "base/utils/unique_valued_map.h" 19 20 using namespace testing; 21 using namespace testing::ext; 22 23 namespace OHOS::Ace { 24 class UniqueValuedMapTest : public testing::Test { 25 protected: 26 UniqueValuedMap<int, std::string> map; 27 }; 28 29 HWTEST_F(UniqueValuedMapTest, PutAndGet, TestSize.Level1) 30 { 31 map.Put(1, "one"); 32 map.Put(2, "two"); 33 34 EXPECT_EQ(map.Get(1).value(), "one"); 35 EXPECT_EQ(map.Get(2).value(), "two"); 36 EXPECT_EQ(map.Get(3), std::nullopt); 37 } 38 39 HWTEST_F(UniqueValuedMapTest, OverwriteKey, TestSize.Level1) 40 { 41 map.Put(1, "one"); 42 map.Put(1, "uno"); 43 44 EXPECT_EQ(map.Get(1).value(), "uno"); 45 EXPECT_EQ(map.ContainsValue("one"), false); 46 } 47 48 HWTEST_F(UniqueValuedMapTest, OverwriteValue, TestSize.Level1) 49 { 50 map.Put(1, "one"); 51 map.Put(2, "one"); 52 53 EXPECT_EQ(map.Get(2).value(), "one"); 54 EXPECT_EQ(map.ContainsKey(1), false); 55 } 56 57 HWTEST_F(UniqueValuedMapTest, RemoveByKey, TestSize.Level1) 58 { 59 map.Put(1, "one"); 60 map.Put(2, "two"); 61 62 map.Remove(1); 63 64 EXPECT_EQ(map.Get(1), std::nullopt); 65 EXPECT_EQ(map.ContainsValue("one"), false); 66 EXPECT_EQ(map.Get(2).value(), "two"); 67 } 68 69 HWTEST_F(UniqueValuedMapTest, RemoveByValue, TestSize.Level1) 70 { 71 map.Put(1, "one"); 72 map.Put(2, "two"); 73 74 map.RemoveValue("one"); 75 76 EXPECT_EQ(map.Get(1), std::nullopt); 77 EXPECT_EQ(map.ContainsKey(1), false); 78 EXPECT_EQ(map.Get(2).value(), "two"); 79 } 80 81 HWTEST_F(UniqueValuedMapTest, ContainsKeyAndValue, TestSize.Level1) 82 { 83 map.Put(1, "one"); 84 85 EXPECT_TRUE(map.ContainsKey(1)); 86 EXPECT_TRUE(map.ContainsValue("one")); 87 EXPECT_FALSE(map.ContainsKey(2)); 88 EXPECT_FALSE(map.ContainsValue("two")); 89 } 90 91 HWTEST_F(UniqueValuedMapTest, Clear, TestSize.Level1) 92 { 93 map.Put(1, "one"); 94 map.Put(2, "two"); 95 96 map.Clear(); 97 98 EXPECT_EQ(map.Size(), 0); 99 EXPECT_FALSE(map.ContainsKey(1)); 100 EXPECT_FALSE(map.ContainsValue("one")); 101 } 102 103 HWTEST_F(UniqueValuedMapTest, Size, TestSize.Level1) 104 { 105 EXPECT_EQ(map.Size(), 0); 106 107 map.Put(1, "one"); 108 EXPECT_EQ(map.Size(), 1); 109 110 map.Put(2, "two"); 111 EXPECT_EQ(map.Size(), 2); 112 113 map.Remove(1); 114 EXPECT_EQ(map.Size(), 1); 115 } 116 117 /** 118 * @tc.name: RemoveIfFunction 119 * @tc.desc: Test the RemoveIf functionality of UniqueValuedMap 120 * @tc.type: FUNC 121 */ 122 HWTEST_F(UniqueValuedMapTest, RemoveIfFunction, TestSize.Level1) 123 { 124 map.Put(1, "one"); 125 map.Put(2, "two"); 126 map.Put(3, "three"); 127 map.Put(4, "four"); 128 129 // Remove elements where key is even __anon747c14550102(const int& key, const std::string&) 130 map.RemoveIf([](const int& key, const std::string&) { 131 return key % 2 == 0; 132 }); 133 134 EXPECT_EQ(map.Size(), 2); 135 EXPECT_TRUE(map.ContainsKey(1)); 136 EXPECT_TRUE(map.ContainsKey(3)); 137 EXPECT_FALSE(map.ContainsKey(2)); 138 EXPECT_FALSE(map.ContainsKey(4)); 139 EXPECT_TRUE(map.ContainsValue("one")); 140 EXPECT_TRUE(map.ContainsValue("three")); 141 EXPECT_FALSE(map.ContainsValue("two")); 142 EXPECT_FALSE(map.ContainsValue("four")); 143 144 // Remove elements where value contains 'e' __anon747c14550202(const int&, const std::string& value) 145 map.RemoveIf([](const int&, const std::string& value) { 146 return value.find('e') != std::string::npos; 147 }); 148 149 EXPECT_EQ(map.Size(), 0); 150 EXPECT_FALSE(map.ContainsKey(1)); 151 EXPECT_FALSE(map.ContainsKey(3)); 152 EXPECT_FALSE(map.ContainsValue("one")); 153 EXPECT_FALSE(map.ContainsValue("three")); 154 155 // Test with empty predicate 156 map.Put(1, "one"); 157 map.RemoveIf(nullptr); 158 EXPECT_EQ(map.Size(), 1); 159 EXPECT_TRUE(map.ContainsKey(1)); 160 EXPECT_TRUE(map.ContainsValue("one")); 161 } 162 } // namespace OHOS::Ace 163