Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : #include "estd/ordered_map.h"
4 :
5 : #include <gtest/gtest.h>
6 :
7 : namespace
8 : {
9 : struct Value
10 : {
11 11 : Value() : _v() {}
12 :
13 1 : Value(int32_t v) : _v(v) {}
14 :
15 : int32_t _v;
16 : };
17 :
18 : using IntValueMap = ::estd::ordered_map<int32_t, Value>;
19 : #define MAKE_MAP(K, T, N, Name) \
20 : ::estd::declare::ordered_map<K, T, N> Name##_; \
21 : ::estd::ordered_map<K, T>& Name = Name##_
22 :
23 : struct IntCompare
24 : {
25 1 : IntCompare(int32_t i) : _i(i) {}
26 : #if 0 /* not used */
27 : bool operator()(int32_t const& x, int32_t const& y) const
28 : {
29 : return x < _i;
30 : }
31 : #endif
32 : int32_t _i;
33 : };
34 :
35 1 : void example_construction()
36 : {
37 : // [EXAMPLE_ORDERED_MAP_DECLARATION_START]
38 : // instantiation of the map that stores up to 10 elements in ascending order
39 1 : ::estd::declare::ordered_map<int32_t, int32_t, 10> m;
40 : // check the lower and upper bound
41 1 : ASSERT_EQ(m.end(), m.lower_bound(0));
42 1 : ASSERT_EQ(m.end(), m.upper_bound(0));
43 : // [EXAMPLE_ORDERED_MAP_DECLARATION_END]
44 : // [EXAMPLE_ORDERED_MAP_MEMBER_FUNCTION_START]
45 1 : MAKE_MAP(int32_t, Value, 10, testMap);
46 : // check whether the key is in the testMap or not
47 1 : ASSERT_EQ(0U, testMap.count(1));
48 : // assign value of 100 to key 1 for the ordered map named testMap
49 1 : testMap[1] = 100;
50 : // check whether the key is in the testMap or not
51 1 : ASSERT_EQ(1U, testMap.count(1));
52 : // clear the ordered map
53 1 : testMap.clear();
54 : // assign key-value pair to testMap
55 1 : testMap[1] = 100;
56 1 : testMap[2] = 200;
57 1 : testMap.insert(std::make_pair(3, 300));
58 1 : testMap.emplace(4); // key is 4 and value is 0
59 : // erase options in testMap with position
60 1 : testMap.erase(testMap.cend());
61 : // erase options in testMap with key
62 1 : testMap.erase(1);
63 : // erase options in testMap for range of positions
64 1 : testMap.erase(testMap.cbegin(), testMap.cend());
65 : // check the capacity of testMap
66 1 : testMap.empty(); // returns TRUE
67 1 : testMap.size(); // size is 0
68 1 : testMap.max_size(); // max_size is 10
69 : // [EXAMPLE_ORDERED_MAP_MEMBER_FUNCTION_END]
70 : // [EXAMPLE_ORDERED_MAP_OPERATIONS_START]
71 1 : std::pair<
72 : ::estd::ordered_map<int32_t, int32_t>::iterator,
73 : ::estd::ordered_map<int32_t, int32_t>::iterator>
74 1 : r = m.equal_range(0);
75 1 : ASSERT_EQ(m.end(), r.first);
76 1 : m[1] = 2;
77 1 : m[3] = 4;
78 1 : ::estd::ordered_map<int32_t, int32_t>::value_compare comp = m.value_comp();
79 1 : ASSERT_TRUE(comp(*m.find(1), *m.find(3)));
80 1 : MAKE_MAP(int32_t, Value, 10, m1);
81 1 : m1[1] = 100;
82 1 : IntValueMap const& cm = m1;
83 : // Find whether the key is in the testMap or not
84 1 : ASSERT_EQ(m1.begin(), m1.find(1));
85 1 : ASSERT_EQ(m1.cbegin(), cm.find(1));
86 1 : ASSERT_EQ(1, m1.find(1)->first);
87 1 : ASSERT_EQ(1, cm.find(1)->first);
88 1 : ASSERT_EQ(1, (*m1.find(1)).first);
89 1 : ASSERT_EQ(1, (*cm.find(1)).first);
90 1 : ASSERT_EQ(100, m1.find(1)->second._v);
91 1 : ASSERT_EQ(100, cm.find(1)->second._v);
92 1 : ASSERT_EQ(100, (*m1.find(1)).second._v);
93 1 : ASSERT_EQ(100, (*cm.find(1)).second._v);
94 : // check whether testMap is full or not
95 1 : ASSERT_FALSE(m1.full());
96 : // fill the map with some data
97 6 : for (auto i = 0U; i < 5; ++i)
98 : {
99 5 : testMap[2 * i] = 10 * i;
100 : }
101 : // iterator to the end of the map
102 1 : auto const endIter = testMap.end();
103 : // key *3* does not exist in the ordered_map, so it performs insertion.
104 1 : (void)testMap[3];
105 : // insertion invalidates all the references and iterators referring
106 : // to the reallocated elements after an added item.
107 1 : (void)(endIter == testMap.end()); // undefined behavior, endIter is invalidated
108 : // use key_comp to compare the temp value assigned
109 1 : static_assert(
110 : std::is_same<IntCompare, ::estd::ordered_map<int32_t, int32_t, IntCompare>::key_compare>::
111 : value,
112 : "");
113 2 : ::estd::declare::ordered_map<int32_t, int32_t, 10, IntCompare> _m(IntCompare(17));
114 1 : ::estd::ordered_map<int32_t, int32_t, IntCompare>& m2 = _m;
115 2 : ASSERT_EQ(17, m2.key_comp()._i);
116 : // [EXAMPLE_ORDERED_MAP_OPERATIONS_END]
117 1 : }
118 : } // namespace
119 :
120 3 : TEST(OrderedMapExample, run_examples) { example_construction(); }
|