1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/value_iterators.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/values.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
20 // Implementation of std::equal variant that is missing in C++11.
21 template <class BinaryPredicate, class InputIterator1, class InputIterator2>
22 bool are_equal(InputIterator1 first1,
24 InputIterator2 first2,
26 BinaryPredicate pred) {
27 for (; first1 != last1 && first2 != last2; ++first1, ++first2) {
28 if (!pred(*first1, *first2))
31 return first1 == last1 && first2 == last2;
36 TEST(ValueIteratorsTest, IsAssignable) {
38 !std::is_assignable<dict_iterator::reference::first_type, std::string>(),
39 "Can assign strings to dict_iterator");
42 std::is_assignable<dict_iterator::reference::second_type, Value>(),
43 "Can't assign Values to dict_iterator");
45 static_assert(!std::is_assignable<const_dict_iterator::reference::first_type,
47 "Can assign strings to const_dict_iterator");
50 !std::is_assignable<const_dict_iterator::reference::second_type, Value>(),
51 "Can assign Values to const_dict_iterator");
54 TEST(ValueIteratorsTest, DictIteratorOperatorStar) {
56 storage.emplace("0", std::make_unique<Value>(0));
58 using iterator = dict_iterator;
59 iterator iter(storage.begin());
60 EXPECT_EQ("0", (*iter).first);
61 EXPECT_EQ(Value(0), (*iter).second);
63 (*iter).second = Value(1);
64 EXPECT_EQ(Value(1), *storage["0"]);
67 TEST(ValueIteratorsTest, DictIteratorOperatorArrow) {
69 storage.emplace("0", std::make_unique<Value>(0));
71 using iterator = dict_iterator;
72 iterator iter(storage.begin());
73 EXPECT_EQ("0", iter->first);
74 EXPECT_EQ(Value(0), iter->second);
76 iter->second = Value(1);
77 EXPECT_EQ(Value(1), *storage["0"]);
80 TEST(ValueIteratorsTest, DictIteratorPreIncrement) {
82 storage.emplace("0", std::make_unique<Value>(0));
83 storage.emplace("1", std::make_unique<Value>(1));
85 using iterator = dict_iterator;
86 iterator iter(storage.begin());
87 EXPECT_EQ("0", iter->first);
88 EXPECT_EQ(Value(0), iter->second);
90 iterator& iter_ref = ++iter;
91 EXPECT_EQ(&iter, &iter_ref);
93 EXPECT_EQ("1", iter_ref->first);
94 EXPECT_EQ(Value(1), iter_ref->second);
97 TEST(ValueIteratorsTest, DictIteratorPostIncrement) {
99 storage.emplace("0", std::make_unique<Value>(0));
100 storage.emplace("1", std::make_unique<Value>(1));
102 using iterator = dict_iterator;
103 iterator iter(storage.begin());
104 iterator iter_old = iter++;
106 EXPECT_EQ("0", iter_old->first);
107 EXPECT_EQ(Value(0), iter_old->second);
109 EXPECT_EQ("1", iter->first);
110 EXPECT_EQ(Value(1), iter->second);
113 TEST(ValueIteratorsTest, DictIteratorPreDecrement) {
115 storage.emplace("0", std::make_unique<Value>(0));
116 storage.emplace("1", std::make_unique<Value>(1));
118 using iterator = dict_iterator;
119 iterator iter(++storage.begin());
120 EXPECT_EQ("1", iter->first);
121 EXPECT_EQ(Value(1), iter->second);
123 iterator& iter_ref = --iter;
124 EXPECT_EQ(&iter, &iter_ref);
126 EXPECT_EQ("0", iter_ref->first);
127 EXPECT_EQ(Value(0), iter_ref->second);
130 TEST(ValueIteratorsTest, DictIteratorPostDecrement) {
132 storage.emplace("0", std::make_unique<Value>(0));
133 storage.emplace("1", std::make_unique<Value>(1));
135 using iterator = dict_iterator;
136 iterator iter(++storage.begin());
137 iterator iter_old = iter--;
139 EXPECT_EQ("1", iter_old->first);
140 EXPECT_EQ(Value(1), iter_old->second);
142 EXPECT_EQ("0", iter->first);
143 EXPECT_EQ(Value(0), iter->second);
146 TEST(ValueIteratorsTest, DictIteratorOperatorEQ) {
148 using iterator = dict_iterator;
149 EXPECT_EQ(iterator(storage.begin()), iterator(storage.begin()));
150 EXPECT_EQ(iterator(storage.end()), iterator(storage.end()));
153 TEST(ValueIteratorsTest, DictIteratorOperatorNE) {
155 storage.emplace("0", std::make_unique<Value>(0));
157 using iterator = dict_iterator;
158 EXPECT_NE(iterator(storage.begin()), iterator(storage.end()));
161 TEST(ValueIteratorsTest, ConstDictIteratorOperatorStar) {
163 storage.emplace("0", std::make_unique<Value>(0));
165 using iterator = const_dict_iterator;
166 iterator iter(storage.begin());
167 EXPECT_EQ("0", (*iter).first);
168 EXPECT_EQ(Value(0), (*iter).second);
171 TEST(ValueIteratorsTest, ConstDictIteratorOperatorArrow) {
173 storage.emplace("0", std::make_unique<Value>(0));
175 using iterator = const_dict_iterator;
176 iterator iter(storage.begin());
177 EXPECT_EQ("0", iter->first);
178 EXPECT_EQ(Value(0), iter->second);
181 TEST(ValueIteratorsTest, ConstDictIteratorPreIncrement) {
183 storage.emplace("0", std::make_unique<Value>(0));
184 storage.emplace("1", std::make_unique<Value>(1));
186 using iterator = const_dict_iterator;
187 iterator iter(storage.begin());
188 EXPECT_EQ("0", iter->first);
189 EXPECT_EQ(Value(0), iter->second);
191 iterator& iter_ref = ++iter;
192 EXPECT_EQ(&iter, &iter_ref);
194 EXPECT_EQ("1", iter_ref->first);
195 EXPECT_EQ(Value(1), iter_ref->second);
198 TEST(ValueIteratorsTest, ConstDictIteratorPostIncrement) {
200 storage.emplace("0", std::make_unique<Value>(0));
201 storage.emplace("1", std::make_unique<Value>(1));
203 using iterator = const_dict_iterator;
204 iterator iter(storage.begin());
205 iterator iter_old = iter++;
207 EXPECT_EQ("0", iter_old->first);
208 EXPECT_EQ(Value(0), iter_old->second);
210 EXPECT_EQ("1", iter->first);
211 EXPECT_EQ(Value(1), iter->second);
214 TEST(ValueIteratorsTest, ConstDictIteratorPreDecrement) {
216 storage.emplace("0", std::make_unique<Value>(0));
217 storage.emplace("1", std::make_unique<Value>(1));
219 using iterator = const_dict_iterator;
220 iterator iter(++storage.begin());
221 EXPECT_EQ("1", iter->first);
222 EXPECT_EQ(Value(1), iter->second);
224 iterator& iter_ref = --iter;
225 EXPECT_EQ(&iter, &iter_ref);
227 EXPECT_EQ("0", iter_ref->first);
228 EXPECT_EQ(Value(0), iter_ref->second);
231 TEST(ValueIteratorsTest, ConstDictIteratorPostDecrement) {
233 storage.emplace("0", std::make_unique<Value>(0));
234 storage.emplace("1", std::make_unique<Value>(1));
236 using iterator = const_dict_iterator;
237 iterator iter(++storage.begin());
238 iterator iter_old = iter--;
240 EXPECT_EQ("1", iter_old->first);
241 EXPECT_EQ(Value(1), iter_old->second);
243 EXPECT_EQ("0", iter->first);
244 EXPECT_EQ(Value(0), iter->second);
247 TEST(ValueIteratorsTest, ConstDictIteratorOperatorEQ) {
249 using iterator = const_dict_iterator;
250 EXPECT_EQ(iterator(storage.begin()), iterator(storage.begin()));
251 EXPECT_EQ(iterator(storage.end()), iterator(storage.end()));
254 TEST(ValueIteratorsTest, ConstDictIteratorOperatorNE) {
256 storage.emplace("0", std::make_unique<Value>(0));
258 using iterator = const_dict_iterator;
259 EXPECT_NE(iterator(storage.begin()), iterator(storage.end()));
262 } // namespace detail