1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/common/bounded_inspect_list_node.h"
16
17 #include <gmock/gmock.h>
18
19 #include "pw_bluetooth_sapphire/internal/host/testing/inspect.h"
20 #include "pw_unit_test/framework.h"
21
22 #ifndef NINSPECT
23
24 namespace bt {
25
26 namespace {
27
28 using namespace ::inspect::testing;
29 using Item = BoundedInspectListNode::Item;
30
TEST(BoundedInspectListNodeTest,ListEviction)31 TEST(BoundedInspectListNodeTest, ListEviction) {
32 const size_t kCapacity = 2;
33 inspect::Inspector inspector;
34 BoundedInspectListNode list(kCapacity);
35
36 list.AttachInspect(inspector.GetRoot(), "list_name");
37 Item& item_0 = list.CreateItem();
38 item_0.node.RecordInt("item_0", 0);
39 Item& item_1 = list.CreateItem();
40 item_1.node.RecordInt("item_1", 1);
41
42 auto hierarchy = ::inspect::ReadFromVmo(inspector.DuplicateVmo());
43 ASSERT_TRUE(hierarchy.is_ok());
44 EXPECT_THAT(
45 hierarchy.take_value(),
46 ChildrenMatch(ElementsAre(AllOf(
47 // list node
48 NodeMatches(NameMatches("list_name")),
49 ChildrenMatch(UnorderedElementsAre(
50 // item_0
51 NodeMatches(AllOf(NameMatches("0"),
52 PropertyList(ElementsAre(IntIs("item_0", 0))))),
53 // item_1
54 NodeMatches(
55 AllOf(NameMatches("1"),
56 PropertyList(ElementsAre(IntIs("item_1", 1)))))))))));
57
58 // Exceed capacity. item_0 should be evicted.
59 Item& item_2 = list.CreateItem();
60 item_2.node.RecordInt("item_2", 2);
61
62 hierarchy = ::inspect::ReadFromVmo(inspector.DuplicateVmo());
63 ASSERT_TRUE(hierarchy.is_ok());
64 EXPECT_THAT(
65 hierarchy.take_value(),
66 ChildrenMatch(ElementsAre(AllOf(
67 // list node
68 NodeMatches(NameMatches("list_name")),
69 ChildrenMatch(UnorderedElementsAre(
70 // item_1
71 NodeMatches(AllOf(NameMatches("1"),
72 PropertyList(ElementsAre(IntIs("item_1", 1))))),
73 // item_2
74 NodeMatches(
75 AllOf(NameMatches("2"),
76 PropertyList(ElementsAre(IntIs("item_2", 2)))))))))));
77 }
78
79 } // namespace
80 } // namespace bt
81
82 #endif // NINSPECT
83