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