1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "ConcurrentMap"
17 
18 #include "concurrent_map.h"
19 #include "gtest/gtest.h"
20 using namespace testing::ext;
21 namespace OHOS::Test {
22 template<typename _Key, typename _Tp> using ConcurrentMap = OHOS::ConcurrentMap<_Key, _Tp>;
23 class ConcurrentMapTest : public testing::Test {
24 public:
25     struct TestValue {
26         std::string id;
27         std::string name;
28         std::string testCase;
29     };
SetUpTestCase(void)30     static void SetUpTestCase(void) {}
31 
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
33 
34 protected:
SetUp()35     void SetUp()
36     {
37         values_.Clear();
38     }
39 
TearDown()40     void TearDown()
41     {
42         values_.Clear();
43     }
44 
45     ConcurrentMap<std::string, TestValue> values_;
46 };
47 
48 /**
49 * @tc.name: EmplaceWithNone
50 * @tc.desc: test the bool Emplace() noexcept function.
51 * @tc.type: FUNC
52 * @tc.require:
53 * @tc.author: Sven Wang
54 */
55 HWTEST_F(ConcurrentMapTest, EmplaceWithNone, TestSize.Level0)
56 {
57     values_.Emplace();
58     auto it = values_.Find("");
59     ASSERT_TRUE(it.first);
60 }
61 
62 /**
63 * @tc.name: EmplaceWithFilter
64 * @tc.desc: test the function:
65  * template<typename _Filter, typename... _Args>
66  * typename std::enable_if<std::is_convertible_v<_Filter, filter_type>, bool>::type
67  * Emplace(const _Filter &filter, _Args &&...args) noexcept
68 * @tc.type: FUNC
69 * @tc.require:
70 * @tc.author: Sven Wang
71 */
72 HWTEST_F(ConcurrentMapTest, EmplaceWithFilter, TestSize.Level0)
73 {
74     auto success = values_.Emplace(
__anona9ad0bdc0102(const decltype(values_)::map_type &entries) 75         [](const decltype(values_)::map_type &entries) {
76             return (entries.find("test") == entries.end());
77         },
__anona9ad0bdc0202(const decltype(values_)::map_type &entries) 78         "test", TestValue{ "id", "name", "test case" });
79     ASSERT_TRUE(success);
80     success = values_.Emplace(
__anona9ad0bdc0302(const decltype(values_)::map_type &entries) 81         [](const decltype(values_)::map_type &entries) {
82             return (entries.find("test") == entries.end());
83         },
__anona9ad0bdc0402(const decltype(values_)::map_type &entries) 84         "test", TestValue{ "id", "name", "test case" });
85     ASSERT_TRUE(!success);
86     success = values_.Emplace(
__anona9ad0bdc0502(decltype(values_)::map_type &entries) 87         [](decltype(values_)::map_type &entries) {
88             auto it = entries.find("test");
89             if (it != entries.end()) {
90                 entries.erase(it);
91                 it = entries.end();
92             }
93             return (it == entries.end());
94         },
__anona9ad0bdc0602(decltype(values_)::map_type &entries) 95         "test", TestValue{ "id", "name", "test case" });
96     ASSERT_TRUE(success);
97 }
98 
99 /**
100 * @tc.name: EmplaceWithArgs
101 * @tc.desc: test the function:
102  * template<typename... _Args>
103  * typename std::enable_if<!std::is_convertible_v<decltype(First<_Args...>()), filter_type>, bool>::type
104  * Emplace(_Args &&...args) noexcept
105 * @tc.type: FUNC
106 * @tc.require:
107 * @tc.author: Sven Wang
108 */
109 HWTEST_F(ConcurrentMapTest, EmplaceWithArgs, TestSize.Level0)
110 {
111     TestValue value = { "id", "name", "test case" };
112     auto success = values_.Emplace("test", value);
113     ASSERT_TRUE(success);
114     auto it = values_.Find("test");
115     ASSERT_TRUE(it.first);
116     ASSERT_EQ(it.second.id, value.id);
117     ASSERT_EQ(it.second.name, value.name);
118     ASSERT_EQ(it.second.testCase, value.testCase);
119     values_.Clear();
120     success = values_.Emplace(std::piecewise_construct, std::forward_as_tuple("test"), std::forward_as_tuple(value));
121     ASSERT_TRUE(success);
122     it = values_.Find("test");
123     ASSERT_TRUE(it.first);
124     ASSERT_EQ(it.second.id, value.id);
125     ASSERT_EQ(it.second.name, value.name);
126     ASSERT_EQ(it.second.testCase, value.testCase);
127 }
128 } // namespace OHOS::Test