xref: /aosp_15_r20/external/pigweed/pw_containers/examples/intrusive_multimap.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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_containers/intrusive_multimap.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace examples {
20 
21 // DOCSTAG: [pw_containers-intrusive_multimap]
22 
23 struct Book : public pw::IntrusiveMultiMap<uint32_t, Book>::Pair {
24  private:
25   using Pair = pw::IntrusiveMultiMap<uint32_t, Book>::Pair;
26 
27  public:
Bookexamples::Book28   Book(const char* name, uint32_t oclc) : Pair(oclc), name_(name) {}
nameexamples::Book29   const char* name() const { return name_; }
30 
31  private:
32   const char* name_;
33 };
34 
35 std::array<Book, 12> books = {{
36     {"The Little Prince", 182537909u},
37     {"Harry Potter and the Philosopher's Stone", 44795766u},
38     {"Harry Potter and the Philosopher's Stone", 44795766u},
39     {"Harry Potter and the Philosopher's Stone", 44795766u},
40     {"Harry Potter and the Philosopher's Stone", 44795766u},
41     {"Harry Potter and the Philosopher's Stone", 44795766u},
42     {"The Hobbit", 1827184u},
43     {"The Hobbit", 1827184u},
44     {"The Hobbit", 1827184u},
45     {"The Hobbit", 1827184u},
46     {"Alice's Adventures in Wonderland", 5635965u},
47     {"Alice's Adventures in Wonderland", 5635965u},
48 }};
49 pw::IntrusiveMultiMap<uint32_t, Book> library(books.begin(), books.end());
50 
VisitLibrary(pw::IntrusiveMultiMap<uint32_t,Book> & book_bag)51 void VisitLibrary(pw::IntrusiveMultiMap<uint32_t, Book>& book_bag) {
52   // Pick out some new books to read to the kids, but only if they're available.
53   std::array<uint32_t, 3> oclcs = {
54       1827184u,    // The Hobbit
55       5635965u,    // Alice's Adventures in Wonderland
56       182537909u,  // The Little Prince
57   };
58   for (uint32_t oclc : oclcs) {
59     auto iter = library.find(oclc);
60     if (iter != library.end()) {
61       Book& book = *iter;
62       library.erase(iter);
63       book_bag.insert(book);
64     }
65   }
66 }
67 
68 // DOCSTAG: [pw_containers-intrusive_multimap]
69 
70 }  // namespace examples
71 
72 namespace {
73 
TEST(IntrusiveMultiMapExampleTest,VisitLibrary)74 TEST(IntrusiveMultiMapExampleTest, VisitLibrary) {
75   pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag1;
76   examples::VisitLibrary(book_bag1);
77 
78   pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag2;
79   examples::VisitLibrary(book_bag2);
80 
81   pw::IntrusiveMultiMap<uint32_t, examples::Book> book_bag3;
82   examples::VisitLibrary(book_bag3);
83 
84   auto iter = book_bag1.begin();
85   ASSERT_NE(iter, book_bag1.end());
86   EXPECT_STREQ((iter++)->name(), "The Hobbit");
87   ASSERT_NE(iter, book_bag1.end());
88   EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
89   ASSERT_NE(iter, book_bag1.end());
90   EXPECT_STREQ((iter++)->name(), "The Little Prince");
91   EXPECT_EQ(iter, book_bag1.end());
92   book_bag1.clear();
93 
94   iter = book_bag2.begin();
95   ASSERT_NE(iter, book_bag2.end());
96   EXPECT_STREQ((iter++)->name(), "The Hobbit");
97   ASSERT_NE(iter, book_bag2.end());
98   EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
99   EXPECT_EQ(iter, book_bag2.end());
100   book_bag2.clear();
101 
102   iter = book_bag3.begin();
103   ASSERT_NE(iter, book_bag3.end());
104   EXPECT_STREQ((iter++)->name(), "The Hobbit");
105   EXPECT_EQ(iter, book_bag3.end());
106   book_bag3.clear();
107 
108   examples::library.clear();
109 }
110 
111 }  // namespace
112