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
17 #ifndef LRU_CACHE_TEST
18 #define LRU_CACHE_TEST
19
20 #include "gtest/gtest.h"
21 #include "socperf_lru_cache.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace SOCPERF {
27 class SocPerfLRUCacheTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 private:
34 SocPerfLRUCache<int, std::string>* cache;
35 };
36
SetUpTestCase()37 void SocPerfLRUCacheTest::SetUpTestCase() {}
38
TearDownTestCase()39 void SocPerfLRUCacheTest::TearDownTestCase() {}
40
SetUp()41 void SocPerfLRUCacheTest::SetUp()
42 {
43 static const int maxSize = 2;
44 cache = new SocPerfLRUCache<int, std::string>(maxSize);
45 }
46
TearDown()47 void SocPerfLRUCacheTest::TearDown()
48 {
49 delete cache;
50 cache = nullptr;
51 }
52
53 /**
54 * @tc.name: SocPerfLRUCacheTest BasicPutGet
55 * @tc.desc: test get
56 * @tc.type: FUNC
57 * @tc.require: issueICMIEN
58 */
59 HWTEST_F(SocPerfLRUCacheTest, BasicPutGet, Function | MediumTest | Level0)
60 {
61 cache->put(1, "one");
62 std::string val = "";
63 EXPECT_TRUE(cache->get(1, val));
64 EXPECT_EQ(val, "one");
65 }
66
67 /**
68 * @tc.name: SocPerfLRUCacheTest CacheEviction
69 * @tc.desc: test CacheEviction
70 * @tc.type: FUNC
71 * @tc.require: issueICMIEN
72 */
73 HWTEST_F(SocPerfLRUCacheTest, CacheEviction, Function | MediumTest | Level0)
74 {
75 cache->put(1, "one");
76 cache->put(2, "two");
77 cache->put(3, "three");
78 std::string val = "";
79 EXPECT_FALSE(cache->get(1, val));
80 EXPECT_TRUE(cache->get(2, val));
81 EXPECT_TRUE(cache->get(3, val));
82 }
83
84 /**
85 * @tc.name: SocPerfLRUCacheTest UpdateExistingKey
86 * @tc.desc: test UpdateExistingKey
87 * @tc.type: FUNC
88 * @tc.require: issueICMIEN
89 */
90 HWTEST_F(SocPerfLRUCacheTest, UpdateExistingKey, Function | MediumTest | Level0)
91 {
92 cache->put(1, "one");
93 cache->put(1, "new_one");
94 std::string val = "";
95 EXPECT_TRUE(cache->get(1, val));
96 EXPECT_EQ(val, "new_one");
97 }
98
99 /**
100 * @tc.name: SocPerfLRUCacheTest GetUpdatesLRU
101 * @tc.desc: test GetUpdatesLRU
102 * @tc.type: FUNC
103 * @tc.require: issueICMIEN
104 */
105 HWTEST_F(SocPerfLRUCacheTest, GetUpdatesLRU, Function | MediumTest | Level0)
106 {
107 cache->put(1, "one");
108 cache->put(2, "two");
109 std::string val = "";
110 cache->get(1, val);
111 cache->put(3, "three");
112 EXPECT_TRUE(cache->get(1, val));
113 EXPECT_FALSE(cache->get(2, val));
114 EXPECT_TRUE(cache->get(3, val));
115 }
116
117 } // namespace SOCPERF
118 } // namespace OHOS
119
120 #endif // LRU_CACHE_TEST
121