|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <app/AttributeAccessInterface.h> |
| 20 | +#include <app/AttributeAccessInterfaceCache.h> |
| 21 | +#include <lib/support/UnitTestRegistration.h> |
| 22 | +#include <nlunit-test.h> |
| 23 | + |
| 24 | +using namespace chip; |
| 25 | +using namespace chip::app; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +void TestBasicLifecycle(nlTestSuite * inSuite, void * inContext) |
| 30 | +{ |
| 31 | + using CacheResult = AttributeAccessInterfaceCache::CacheResult; |
| 32 | + |
| 33 | + int data1 = 1; |
| 34 | + int data2 = 2; |
| 35 | + |
| 36 | + // We alias the pointers to given locations to avoid needing to implement anything |
| 37 | + // since the AttributeAccessInterfaceCache only deals in pointers, and never calls |
| 38 | + // the API itself. |
| 39 | + AttributeAccessInterface * accessor1 = reinterpret_cast<AttributeAccessInterface *>(&data1); |
| 40 | + AttributeAccessInterface * accessor2 = reinterpret_cast<AttributeAccessInterface *>(&data2); |
| 41 | + |
| 42 | + AttributeAccessInterfaceCache cache; |
| 43 | + |
| 44 | + // Cache can keep track of at least 1 entry, |
| 45 | + AttributeAccessInterface * entry = nullptr; |
| 46 | + |
| 47 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss); |
| 48 | + NL_TEST_ASSERT(inSuite, entry == nullptr); |
| 49 | + cache.MarkUsed(1, 1, accessor1); |
| 50 | + |
| 51 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kDefinitelyUsed); |
| 52 | + NL_TEST_ASSERT(inSuite, entry == accessor1); |
| 53 | + |
| 54 | + entry = nullptr; |
| 55 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss); |
| 56 | + NL_TEST_ASSERT(inSuite, entry == nullptr); |
| 57 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss); |
| 58 | + NL_TEST_ASSERT(inSuite, entry == nullptr); |
| 59 | + |
| 60 | + cache.MarkUsed(1, 2, accessor1); |
| 61 | + |
| 62 | + entry = nullptr; |
| 63 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed); |
| 64 | + NL_TEST_ASSERT(inSuite, entry == accessor1); |
| 65 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss); |
| 66 | + |
| 67 | + cache.MarkUsed(1, 2, accessor2); |
| 68 | + |
| 69 | + entry = nullptr; |
| 70 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed); |
| 71 | + NL_TEST_ASSERT(inSuite, entry == accessor2); |
| 72 | + // The following should not crash (e.g. output not used if nullptr). |
| 73 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, nullptr) == CacheResult::kDefinitelyUsed); |
| 74 | + |
| 75 | + // Setting used to nullptr == does not mark used. |
| 76 | + cache.MarkUsed(1, 2, nullptr); |
| 77 | + entry = nullptr; |
| 78 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss); |
| 79 | + NL_TEST_ASSERT(inSuite, entry == nullptr); |
| 80 | + |
| 81 | + cache.Invalidate(); |
| 82 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss); |
| 83 | + NL_TEST_ASSERT(inSuite, entry == nullptr); |
| 84 | + NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss); |
| 85 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss); |
| 86 | + |
| 87 | + // Marking unused works, keeps single entry, and is invalidated when invalidated fully. |
| 88 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused); |
| 89 | + NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused); |
| 90 | + cache.MarkUnused(2, 2); |
| 91 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) == CacheResult::kDefinitelyUnused); |
| 92 | + NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused); |
| 93 | + |
| 94 | + cache.MarkUnused(3, 3); |
| 95 | + NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused); |
| 96 | + NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) == CacheResult::kDefinitelyUnused); |
| 97 | + |
| 98 | + cache.Invalidate(); |
| 99 | + NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused); |
| 100 | +} |
| 101 | + |
| 102 | +// clang-format off |
| 103 | +const nlTest sTests[] = |
| 104 | +{ |
| 105 | + NL_TEST_DEF("Basic AttributeAccessInterfaceCache lifecycle works", TestBasicLifecycle), |
| 106 | + NL_TEST_SENTINEL() |
| 107 | +}; |
| 108 | +// clang-format on |
| 109 | + |
| 110 | +} // namespace |
| 111 | + |
| 112 | +int TestAttributeAccessInterfaceCache() |
| 113 | +{ |
| 114 | + // clang-format off |
| 115 | + nlTestSuite theSuite = |
| 116 | + { |
| 117 | + "Test for AttributeAccessInterface cache utility", |
| 118 | + &sTests[0], |
| 119 | + nullptr, |
| 120 | + nullptr |
| 121 | + }; |
| 122 | + // clang-format on |
| 123 | + |
| 124 | + nlTestRunner(&theSuite, nullptr); |
| 125 | + |
| 126 | + return (nlTestRunnerStats(&theSuite)); |
| 127 | +} |
| 128 | + |
| 129 | +CHIP_REGISTER_TEST_SUITE(TestAttributeAccessInterfaceCache) |
0 commit comments