xref: /aosp_15_r20/external/pigweed/pw_containers/examples/flat_map.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/flat_map.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace examples {
20 
21 // DOCSTAG: [pw_containers-flat_map]
22 
23 using pw::containers::FlatMap;
24 using pw::containers::Pair;
25 
26 // Initialized by an initializer list.
27 FlatMap<int, char, 2> my_flat_map1({{
28     {1, 'a'},
29     {-3, 'b'},
30 }});
31 
32 // Initialized by a std::array of Pair<K, V> objects.
33 std::array<Pair<int, char>, 2> my_array{{
34     {1, 'a'},
35     {-3, 'b'},
36 }};
37 FlatMap my_flat_map2(my_array);
38 
39 // Initialized by Pair<K, V> objects.
40 FlatMap my_flat_map3 = {
41     Pair<int, char>{1, 'a'},
42     Pair<int, char>{-3, 'b'},
43 };
44 
45 // DOCSTAG: [pw_containers-flat_map]
46 
47 }  // namespace examples
48 
49 namespace {
50 
TEST(FlapMapExampleTest,CheckValues)51 TEST(FlapMapExampleTest, CheckValues) {
52   EXPECT_EQ(examples::my_flat_map1.at(1), 'a');
53   EXPECT_EQ(examples::my_flat_map1.at(-3), 'b');
54   EXPECT_EQ(examples::my_flat_map2.at(1), 'a');
55   EXPECT_EQ(examples::my_flat_map2.at(-3), 'b');
56   EXPECT_EQ(examples::my_flat_map3.at(1), 'a');
57   EXPECT_EQ(examples::my_flat_map3.at(-3), 'b');
58 }
59 
60 }  // namespace
61