1 /*
2  * Copyright (c) 2024 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 #include <limits>
18 #include <test_header.h>
19 
20 #include "hgm_lru_cache.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 class HgmLRUCacheTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase() {}
TearDownTestCase()30     static void TearDownTestCase() {}
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 };
34 
35 /**
36  * @tc.name: Put
37  * @tc.desc: Verify the result of Put function
38  * @tc.type: FUNC
39  * @tc.require:
40  */
41 HWTEST_F(HgmLRUCacheTest, Put, Function | SmallTest | Level1)
42 {
43     constexpr int32_t cacheSize = 3;
44     constexpr int32_t val0 = 0;
45     constexpr int32_t val1 = 1;
46     constexpr int32_t val2 = 2;
47     constexpr int32_t val3 = 3;
48     constexpr int32_t val4 = 4;
49 
50     auto cache = HgmLRUCache<int32_t>(cacheSize);
51     cache.Put(val0);
52     ASSERT_TRUE(cache.Existed(val0));
53 
54     cache.Put(val1);
55     ASSERT_TRUE(cache.Existed(val0));
56     ASSERT_TRUE(cache.Existed(val1));
57 
58     cache.Put(val2);
59     ASSERT_TRUE(cache.Existed(val0));
60     ASSERT_TRUE(cache.Existed(val1));
61     ASSERT_TRUE(cache.Existed(val2));
62 
63     cache.Put(val3);
64     ASSERT_FALSE(cache.Existed(val0));
65     ASSERT_TRUE(cache.Existed(val1));
66     ASSERT_TRUE(cache.Existed(val2));
67     ASSERT_TRUE(cache.Existed(val3));
68 
69     cache.Put(val2);
70     ASSERT_TRUE(cache.Existed(val1));
71     ASSERT_TRUE(cache.Existed(val2));
72     ASSERT_TRUE(cache.Existed(val3));
73 
74     cache.Put(val1);
75     ASSERT_TRUE(cache.Existed(val1));
76     ASSERT_TRUE(cache.Existed(val2));
77     ASSERT_TRUE(cache.Existed(val3));
78 
79     cache.Put(val4);
80     ASSERT_FALSE(cache.Existed(val0));
81     ASSERT_TRUE(cache.Existed(val1));
82     ASSERT_TRUE(cache.Existed(val2));
83     ASSERT_FALSE(cache.Existed(val3));
84     ASSERT_TRUE(cache.Existed(val4));
85 }
86 } // namespace Rosen
87 } // namespace OHOS