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