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