1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_containers/intrusive_multimap.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include "pw_unit_test/framework.h"
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker namespace examples {
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_containers-intrusive_multimap]
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker struct Book : public pw::IntrusiveMultiMap<uint32_t, Book>::Pair {
24*61c4878aSAndroid Build Coastguard Worker private:
25*61c4878aSAndroid Build Coastguard Worker using Pair = pw::IntrusiveMultiMap<uint32_t, Book>::Pair;
26*61c4878aSAndroid Build Coastguard Worker
27*61c4878aSAndroid Build Coastguard Worker public:
Bookexamples::Book28*61c4878aSAndroid Build Coastguard Worker Book(const char* name, uint32_t oclc) : Pair(oclc), name_(name) {}
nameexamples::Book29*61c4878aSAndroid Build Coastguard Worker const char* name() const { return name_; }
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker private:
32*61c4878aSAndroid Build Coastguard Worker const char* name_;
33*61c4878aSAndroid Build Coastguard Worker };
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker std::array<Book, 12> books = {{
36*61c4878aSAndroid Build Coastguard Worker {"The Little Prince", 182537909u},
37*61c4878aSAndroid Build Coastguard Worker {"Harry Potter and the Philosopher's Stone", 44795766u},
38*61c4878aSAndroid Build Coastguard Worker {"Harry Potter and the Philosopher's Stone", 44795766u},
39*61c4878aSAndroid Build Coastguard Worker {"Harry Potter and the Philosopher's Stone", 44795766u},
40*61c4878aSAndroid Build Coastguard Worker {"Harry Potter and the Philosopher's Stone", 44795766u},
41*61c4878aSAndroid Build Coastguard Worker {"Harry Potter and the Philosopher's Stone", 44795766u},
42*61c4878aSAndroid Build Coastguard Worker {"The Hobbit", 1827184u},
43*61c4878aSAndroid Build Coastguard Worker {"The Hobbit", 1827184u},
44*61c4878aSAndroid Build Coastguard Worker {"The Hobbit", 1827184u},
45*61c4878aSAndroid Build Coastguard Worker {"The Hobbit", 1827184u},
46*61c4878aSAndroid Build Coastguard Worker {"Alice's Adventures in Wonderland", 5635965u},
47*61c4878aSAndroid Build Coastguard Worker {"Alice's Adventures in Wonderland", 5635965u},
48*61c4878aSAndroid Build Coastguard Worker }};
49*61c4878aSAndroid Build Coastguard Worker pw::IntrusiveMultiMap<uint32_t, Book> library(books.begin(), books.end());
50*61c4878aSAndroid Build Coastguard Worker
VisitLibrary(pw::IntrusiveMultiMap<uint32_t,Book> & book_bag)51*61c4878aSAndroid Build Coastguard Worker void VisitLibrary(pw::IntrusiveMultiMap<uint32_t, Book>& book_bag) {
52*61c4878aSAndroid Build Coastguard Worker // Pick out some new books to read to the kids, but only if they're available.
53*61c4878aSAndroid Build Coastguard Worker std::array<uint32_t, 3> oclcs = {
54*61c4878aSAndroid Build Coastguard Worker 1827184u, // The Hobbit
55*61c4878aSAndroid Build Coastguard Worker 5635965u, // Alice's Adventures in Wonderland
56*61c4878aSAndroid Build Coastguard Worker 182537909u, // The Little Prince
57*61c4878aSAndroid Build Coastguard Worker };
58*61c4878aSAndroid Build Coastguard Worker for (uint32_t oclc : oclcs) {
59*61c4878aSAndroid Build Coastguard Worker auto iter = library.find(oclc);
60*61c4878aSAndroid Build Coastguard Worker if (iter != library.end()) {
61*61c4878aSAndroid Build Coastguard Worker Book& book = *iter;
62*61c4878aSAndroid Build Coastguard Worker library.erase(iter);
63*61c4878aSAndroid Build Coastguard Worker book_bag.insert(book);
64*61c4878aSAndroid Build Coastguard Worker }
65*61c4878aSAndroid Build Coastguard Worker }
66*61c4878aSAndroid Build Coastguard Worker }
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_containers-intrusive_multimap]
69*61c4878aSAndroid Build Coastguard Worker
70*61c4878aSAndroid Build Coastguard Worker } // namespace examples
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker namespace {
73*61c4878aSAndroid Build Coastguard Worker
TEST(IntrusiveMultiMapExampleTest,VisitLibrary)74*61c4878aSAndroid Build Coastguard Worker TEST(IntrusiveMultiMapExampleTest, VisitLibrary) {
75*61c4878aSAndroid Build Coastguard Worker pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag1;
76*61c4878aSAndroid Build Coastguard Worker examples::VisitLibrary(book_bag1);
77*61c4878aSAndroid Build Coastguard Worker
78*61c4878aSAndroid Build Coastguard Worker pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag2;
79*61c4878aSAndroid Build Coastguard Worker examples::VisitLibrary(book_bag2);
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag3;
82*61c4878aSAndroid Build Coastguard Worker examples::VisitLibrary(book_bag3);
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker auto iter = book_bag1.begin();
85*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag1.end());
86*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "The Hobbit");
87*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag1.end());
88*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
89*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag1.end());
90*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "The Little Prince");
91*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(iter, book_bag1.end());
92*61c4878aSAndroid Build Coastguard Worker book_bag1.clear();
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker iter = book_bag2.begin();
95*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag2.end());
96*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "The Hobbit");
97*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag2.end());
98*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
99*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(iter, book_bag2.end());
100*61c4878aSAndroid Build Coastguard Worker book_bag2.clear();
101*61c4878aSAndroid Build Coastguard Worker
102*61c4878aSAndroid Build Coastguard Worker iter = book_bag3.begin();
103*61c4878aSAndroid Build Coastguard Worker ASSERT_NE(iter, book_bag3.end());
104*61c4878aSAndroid Build Coastguard Worker EXPECT_STREQ((iter++)->name(), "The Hobbit");
105*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(iter, book_bag3.end());
106*61c4878aSAndroid Build Coastguard Worker book_bag3.clear();
107*61c4878aSAndroid Build Coastguard Worker
108*61c4878aSAndroid Build Coastguard Worker examples::library.clear();
109*61c4878aSAndroid Build Coastguard Worker }
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard Worker } // namespace
112