OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "mojo/public/cpp/bindings/map.h" |
| 6 |
5 #include <stddef.h> | 7 #include <stddef.h> |
6 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
7 | 10 |
8 #include "mojo/public/cpp/bindings/array.h" | 11 #include "mojo/public/cpp/bindings/array.h" |
9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 12 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 13 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
11 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 14 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
12 #include "mojo/public/cpp/bindings/lib/validate_params.h" | 15 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
13 #include "mojo/public/cpp/bindings/map.h" | |
14 #include "mojo/public/cpp/bindings/string.h" | 16 #include "mojo/public/cpp/bindings/string.h" |
15 #include "mojo/public/cpp/bindings/tests/container_test_util.h" | 17 #include "mojo/public/cpp/bindings/tests/container_test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace mojo { | 20 namespace mojo { |
19 namespace test { | 21 namespace test { |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 using mojo::internal::Array_Data; | 25 using mojo::internal::Array_Data; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 77 |
76 TEST_F(MapTest, TestIndexOperatorMoveOnly) { | 78 TEST_F(MapTest, TestIndexOperatorMoveOnly) { |
77 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 79 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
78 mojo::Map<mojo::String, mojo::Array<int32_t>> map; | 80 mojo::Map<mojo::String, mojo::Array<int32_t>> map; |
79 std::vector<MoveOnlyType*> value_ptrs; | 81 std::vector<MoveOnlyType*> value_ptrs; |
80 | 82 |
81 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 83 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
82 const char* key = kStringIntData[i].string_data; | 84 const char* key = kStringIntData[i].string_data; |
83 Array<int32_t> array(1); | 85 Array<int32_t> array(1); |
84 array[0] = kStringIntData[i].int_data; | 86 array[0] = kStringIntData[i].int_data; |
85 map[key] = array.Pass(); | 87 map[key] = std::move(array); |
86 EXPECT_TRUE(map); | 88 EXPECT_TRUE(map); |
87 } | 89 } |
88 | 90 |
89 // We now read back that data, to test the behavior of operator[]. | 91 // We now read back that data, to test the behavior of operator[]. |
90 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 92 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
91 auto it = map.find(kStringIntData[i].string_data); | 93 auto it = map.find(kStringIntData[i].string_data); |
92 ASSERT_TRUE(it != map.end()); | 94 ASSERT_TRUE(it != map.end()); |
93 ASSERT_EQ(1u, it.GetValue().size()); | 95 ASSERT_EQ(1u, it.GetValue().size()); |
94 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); | 96 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); |
95 } | 97 } |
96 } | 98 } |
97 | 99 |
98 TEST_F(MapTest, ConstructedFromArray) { | 100 TEST_F(MapTest, ConstructedFromArray) { |
99 Array<String> keys(kStringIntDataSize); | 101 Array<String> keys(kStringIntDataSize); |
100 Array<int> values(kStringIntDataSize); | 102 Array<int> values(kStringIntDataSize); |
101 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 103 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
102 keys[i] = kStringIntData[i].string_data; | 104 keys[i] = kStringIntData[i].string_data; |
103 values[i] = kStringIntData[i].int_data; | 105 values[i] = kStringIntData[i].int_data; |
104 } | 106 } |
105 | 107 |
106 Map<String, int> map(keys.Pass(), values.Pass()); | 108 Map<String, int> map(std::move(keys), std::move(values)); |
107 | 109 |
108 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 110 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
109 EXPECT_EQ(kStringIntData[i].int_data, | 111 EXPECT_EQ(kStringIntData[i].int_data, |
110 map.at(mojo::String(kStringIntData[i].string_data))); | 112 map.at(mojo::String(kStringIntData[i].string_data))); |
111 } | 113 } |
112 } | 114 } |
113 | 115 |
114 TEST_F(MapTest, DecomposeMapTo) { | 116 TEST_F(MapTest, DecomposeMapTo) { |
115 Array<String> keys(kStringIntDataSize); | 117 Array<String> keys(kStringIntDataSize); |
116 Array<int> values(kStringIntDataSize); | 118 Array<int> values(kStringIntDataSize); |
117 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 119 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
118 keys[i] = kStringIntData[i].string_data; | 120 keys[i] = kStringIntData[i].string_data; |
119 values[i] = kStringIntData[i].int_data; | 121 values[i] = kStringIntData[i].int_data; |
120 } | 122 } |
121 | 123 |
122 Map<String, int> map(keys.Pass(), values.Pass()); | 124 Map<String, int> map(std::move(keys), std::move(values)); |
123 EXPECT_EQ(kStringIntDataSize, map.size()); | 125 EXPECT_EQ(kStringIntDataSize, map.size()); |
124 | 126 |
125 Array<String> keys2; | 127 Array<String> keys2; |
126 Array<int> values2; | 128 Array<int> values2; |
127 map.DecomposeMapTo(&keys2, &values2); | 129 map.DecomposeMapTo(&keys2, &values2); |
128 EXPECT_EQ(0u, map.size()); | 130 EXPECT_EQ(0u, map.size()); |
129 | 131 |
130 EXPECT_EQ(kStringIntDataSize, keys2.size()); | 132 EXPECT_EQ(kStringIntDataSize, keys2.size()); |
131 EXPECT_EQ(kStringIntDataSize, values2.size()); | 133 EXPECT_EQ(kStringIntDataSize, values2.size()); |
132 | 134 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 179 |
178 TEST_F(MapTest, Insert_MoveOnly) { | 180 TEST_F(MapTest, Insert_MoveOnly) { |
179 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 181 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
180 mojo::Map<mojo::String, MoveOnlyType> map; | 182 mojo::Map<mojo::String, MoveOnlyType> map; |
181 std::vector<MoveOnlyType*> value_ptrs; | 183 std::vector<MoveOnlyType*> value_ptrs; |
182 | 184 |
183 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 185 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
184 const char* key = kStringIntData[i].string_data; | 186 const char* key = kStringIntData[i].string_data; |
185 MoveOnlyType value; | 187 MoveOnlyType value; |
186 value_ptrs.push_back(value.ptr()); | 188 value_ptrs.push_back(value.ptr()); |
187 map.insert(key, value.Pass()); | 189 map.insert(key, std::move(value)); |
188 ASSERT_EQ(i + 1, map.size()); | 190 ASSERT_EQ(i + 1, map.size()); |
189 ASSERT_EQ(i + 1, value_ptrs.size()); | 191 ASSERT_EQ(i + 1, value_ptrs.size()); |
190 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances()); | 192 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances()); |
191 EXPECT_TRUE(map.at(key).moved()); | 193 EXPECT_TRUE(map.at(key).moved()); |
192 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); | 194 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); |
193 map.at(key).ResetMoved(); | 195 map.at(key).ResetMoved(); |
194 EXPECT_TRUE(map); | 196 EXPECT_TRUE(map); |
195 } | 197 } |
196 | 198 |
197 // std::map doesn't have a capacity() method like std::vector so this test is | 199 // std::map doesn't have a capacity() method like std::vector so this test is |
198 // a lot more boring. | 200 // a lot more boring. |
199 | 201 |
200 map.reset(); | 202 map.reset(); |
201 EXPECT_EQ(0u, MoveOnlyType::num_instances()); | 203 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
202 } | 204 } |
203 | 205 |
204 TEST_F(MapTest, IndexOperator_MoveOnly) { | 206 TEST_F(MapTest, IndexOperator_MoveOnly) { |
205 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 207 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
206 mojo::Map<mojo::String, MoveOnlyType> map; | 208 mojo::Map<mojo::String, MoveOnlyType> map; |
207 std::vector<MoveOnlyType*> value_ptrs; | 209 std::vector<MoveOnlyType*> value_ptrs; |
208 | 210 |
209 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 211 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
210 const char* key = kStringIntData[i].string_data; | 212 const char* key = kStringIntData[i].string_data; |
211 MoveOnlyType value; | 213 MoveOnlyType value; |
212 value_ptrs.push_back(value.ptr()); | 214 value_ptrs.push_back(value.ptr()); |
213 map[key] = value.Pass(); | 215 map[key] = std::move(value); |
214 ASSERT_EQ(i + 1, map.size()); | 216 ASSERT_EQ(i + 1, map.size()); |
215 ASSERT_EQ(i + 1, value_ptrs.size()); | 217 ASSERT_EQ(i + 1, value_ptrs.size()); |
216 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances()); | 218 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances()); |
217 EXPECT_TRUE(map.at(key).moved()); | 219 EXPECT_TRUE(map.at(key).moved()); |
218 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); | 220 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); |
219 map.at(key).ResetMoved(); | 221 map.at(key).ResetMoved(); |
220 EXPECT_TRUE(map); | 222 EXPECT_TRUE(map); |
221 } | 223 } |
222 | 224 |
223 // std::map doesn't have a capacity() method like std::vector so this test is | 225 // std::map doesn't have a capacity() method like std::vector so this test is |
(...skipping 27 matching lines...) Expand all Loading... |
251 ASSERT_TRUE(it != stl_map.end()); | 253 ASSERT_TRUE(it != stl_map.end()); |
252 EXPECT_EQ(kStringIntData[i].int_data, it->second); | 254 EXPECT_EQ(kStringIntData[i].int_data, it->second); |
253 } | 255 } |
254 } | 256 } |
255 | 257 |
256 TEST_F(MapTest, MapArrayClone) { | 258 TEST_F(MapTest, MapArrayClone) { |
257 Map<String, Array<String>> m; | 259 Map<String, Array<String>> m; |
258 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 260 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
259 Array<String> s; | 261 Array<String> s; |
260 s.push_back(kStringIntData[i].string_data); | 262 s.push_back(kStringIntData[i].string_data); |
261 m.insert(kStringIntData[i].string_data, s.Pass()); | 263 m.insert(kStringIntData[i].string_data, std::move(s)); |
262 } | 264 } |
263 | 265 |
264 Map<String, Array<String>> m2 = m.Clone(); | 266 Map<String, Array<String>> m2 = m.Clone(); |
265 | 267 |
266 for (auto it = m2.begin(); it != m2.end(); ++it) { | 268 for (auto it = m2.begin(); it != m2.end(); ++it) { |
267 ASSERT_EQ(1u, it.GetValue().size()); | 269 ASSERT_EQ(1u, it.GetValue().size()); |
268 EXPECT_EQ(it.GetKey(), it.GetValue().at(0)); | 270 EXPECT_EQ(it.GetKey(), it.GetValue().at(0)); |
269 } | 271 } |
270 } | 272 } |
271 | 273 |
272 TEST_F(MapTest, ArrayOfMap) { | 274 TEST_F(MapTest, ArrayOfMap) { |
273 { | 275 { |
274 Array<Map<int32_t, int8_t>> array(1); | 276 Array<Map<int32_t, int8_t>> array(1); |
275 array[0].insert(1, 42); | 277 array[0].insert(1, 42); |
276 | 278 |
277 size_t size = GetSerializedSize_(array); | 279 size_t size = GetSerializedSize_(array); |
278 FixedBufferForTesting buf(size); | 280 FixedBufferForTesting buf(size); |
279 Array_Data<Map_Data<int32_t, int8_t>*>* data; | 281 Array_Data<Map_Data<int32_t, int8_t>*>* data; |
280 ArrayValidateParams validate_params( | 282 ArrayValidateParams validate_params( |
281 0, false, new ArrayValidateParams(0, false, nullptr)); | 283 0, false, new ArrayValidateParams(0, false, nullptr)); |
282 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 284 SerializeArray_(std::move(array), &buf, &data, &validate_params); |
283 | 285 |
284 Array<Map<int32_t, int8_t>> deserialized_array; | 286 Array<Map<int32_t, int8_t>> deserialized_array; |
285 Deserialize_(data, &deserialized_array, nullptr); | 287 Deserialize_(data, &deserialized_array, nullptr); |
286 | 288 |
287 ASSERT_EQ(1u, deserialized_array.size()); | 289 ASSERT_EQ(1u, deserialized_array.size()); |
288 ASSERT_EQ(1u, deserialized_array[0].size()); | 290 ASSERT_EQ(1u, deserialized_array[0].size()); |
289 ASSERT_EQ(42, deserialized_array[0].at(1)); | 291 ASSERT_EQ(42, deserialized_array[0].at(1)); |
290 } | 292 } |
291 | 293 |
292 { | 294 { |
293 Array<Map<String, Array<bool>>> array(1); | 295 Array<Map<String, Array<bool>>> array(1); |
294 Array<bool> map_value(2); | 296 Array<bool> map_value(2); |
295 map_value[0] = false; | 297 map_value[0] = false; |
296 map_value[1] = true; | 298 map_value[1] = true; |
297 array[0].insert("hello world", map_value.Pass()); | 299 array[0].insert("hello world", std::move(map_value)); |
298 | 300 |
299 size_t size = GetSerializedSize_(array); | 301 size_t size = GetSerializedSize_(array); |
300 FixedBufferForTesting buf(size); | 302 FixedBufferForTesting buf(size); |
301 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; | 303 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; |
302 ArrayValidateParams validate_params( | 304 ArrayValidateParams validate_params( |
303 0, false, new ArrayValidateParams( | 305 0, false, new ArrayValidateParams( |
304 0, false, new ArrayValidateParams(0, false, nullptr))); | 306 0, false, new ArrayValidateParams(0, false, nullptr))); |
305 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 307 SerializeArray_(std::move(array), &buf, &data, &validate_params); |
306 | 308 |
307 Array<Map<String, Array<bool>>> deserialized_array; | 309 Array<Map<String, Array<bool>>> deserialized_array; |
308 Deserialize_(data, &deserialized_array, nullptr); | 310 Deserialize_(data, &deserialized_array, nullptr); |
309 | 311 |
310 ASSERT_EQ(1u, deserialized_array.size()); | 312 ASSERT_EQ(1u, deserialized_array.size()); |
311 ASSERT_EQ(1u, deserialized_array[0].size()); | 313 ASSERT_EQ(1u, deserialized_array[0].size()); |
312 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); | 314 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); |
313 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); | 315 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); |
314 } | 316 } |
315 } | 317 } |
316 | 318 |
317 } // namespace | 319 } // namespace |
318 } // namespace test | 320 } // namespace test |
319 } // namespace mojo | 321 } // namespace mojo |
OLD | NEW |