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_multiset.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace examples {
20
21 // DOCSTAG: [pw_containers-intrusive_multiset]
22
23 class Book : public pw::IntrusiveMultiSet<Book>::Item {
24 private:
25 using Item = pw::IntrusiveMultiSet<Book>::Item;
26
27 public:
Book(const char * name)28 explicit Book(const char* name) : name_(name) {}
name() const29 const char* name() const { return name_; }
operator <(const Book & rhs) const30 bool operator<(const Book& rhs) const {
31 return strcmp(name_, rhs.name()) < 0;
32 }
33
34 private:
35 const char* name_;
36 };
37
38 std::array<Book, 12> books = {
39 Book("The Little Prince"),
40 Book("Harry Potter and the Philosopher's Stone"),
41 Book("Harry Potter and the Philosopher's Stone"),
42 Book("Harry Potter and the Philosopher's Stone"),
43 Book("Harry Potter and the Philosopher's Stone"),
44 Book("Harry Potter and the Philosopher's Stone"),
45 Book("The Hobbit"),
46 Book("The Hobbit"),
47 Book("The Hobbit"),
48 Book("The Hobbit"),
49 Book("Alice's Adventures in Wonderland"),
50 Book("Alice's Adventures in Wonderland"),
51 };
52 pw::IntrusiveMultiSet<Book> library(books.begin(), books.end());
53
VisitLibrary(pw::IntrusiveMultiSet<Book> & book_bag)54 void VisitLibrary(pw::IntrusiveMultiSet<Book>& book_bag) {
55 // Pick out some new books to read to the kids, but only if they're available.
56 std::array<const char*, 3> titles = {
57 "The Hobbit",
58 "Alice's Adventures in Wonderland",
59 "The Little Prince",
60 };
61 for (const char* title : titles) {
62 Book requested(title);
63 auto iter = library.find(requested);
64 if (iter != library.end()) {
65 Book& book = *iter;
66 library.erase(iter);
67 book_bag.insert(book);
68 }
69 }
70 }
71
72 // DOCSTAG: [pw_containers-intrusive_multiset]
73
74 } // namespace examples
75
76 namespace {
77
TEST(IntrusiveMultiSetExampleTest,VisitLibrary)78 TEST(IntrusiveMultiSetExampleTest, VisitLibrary) {
79 pw::IntrusiveMultiSet<examples::Book> book_bag1;
80 examples::VisitLibrary(book_bag1);
81
82 pw::IntrusiveMultiSet<examples::Book> book_bag2;
83 examples::VisitLibrary(book_bag2);
84
85 pw::IntrusiveMultiSet<examples::Book> book_bag3;
86 examples::VisitLibrary(book_bag3);
87
88 auto iter = book_bag1.begin();
89 ASSERT_NE(iter, book_bag1.end());
90 EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
91 ASSERT_NE(iter, book_bag1.end());
92 EXPECT_STREQ((iter++)->name(), "The Hobbit");
93 ASSERT_NE(iter, book_bag1.end());
94 EXPECT_STREQ((iter++)->name(), "The Little Prince");
95 EXPECT_EQ(iter, book_bag1.end());
96 book_bag1.clear();
97
98 iter = book_bag2.begin();
99 ASSERT_NE(iter, book_bag2.end());
100 EXPECT_STREQ((iter++)->name(), "Alice's Adventures in Wonderland");
101 ASSERT_NE(iter, book_bag2.end());
102 EXPECT_STREQ((iter++)->name(), "The Hobbit");
103 EXPECT_EQ(iter, book_bag2.end());
104 book_bag2.clear();
105
106 iter = book_bag3.begin();
107 ASSERT_NE(iter, book_bag3.end());
108 EXPECT_STREQ((iter++)->name(), "The Hobbit");
109 EXPECT_EQ(iter, book_bag3.end());
110 book_bag3.clear();
111
112 examples::library.clear();
113 }
114
115 } // namespace
116