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/array.h" | 5 #include "mojo/public/cpp/bindings/array.h" |
6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
9 #include "mojo/public/cpp/bindings/lib/map_serialization.h" | 9 #include "mojo/public/cpp/bindings/lib/map_serialization.h" |
10 #include "mojo/public/cpp/bindings/lib/validate_params.h" | 10 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 TEST_F(MapTest, TestIndexOperatorMoveOnly) { | 81 TEST_F(MapTest, TestIndexOperatorMoveOnly) { |
82 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 82 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
83 mojo::Map<mojo::String, mojo::Array<int32_t>> map; | 83 mojo::Map<mojo::String, mojo::Array<int32_t>> map; |
84 std::vector<MoveOnlyType*> value_ptrs; | 84 std::vector<MoveOnlyType*> value_ptrs; |
85 | 85 |
86 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 86 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
87 const char* key = kStringIntData[i].string_data; | 87 const char* key = kStringIntData[i].string_data; |
88 Array<int32_t> array(1); | 88 auto array = Array<int32_t>::New(1); |
89 array[0] = kStringIntData[i].int_data; | 89 array[0] = kStringIntData[i].int_data; |
90 map[key] = array.Pass(); | 90 map[key] = array.Pass(); |
91 EXPECT_TRUE(map); | 91 EXPECT_TRUE(map); |
92 } | 92 } |
93 | 93 |
94 // We now read back that data, to test the behavior of operator[]. | 94 // We now read back that data, to test the behavior of operator[]. |
95 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 95 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
96 auto it = map.find(kStringIntData[i].string_data); | 96 auto it = map.find(kStringIntData[i].string_data); |
97 ASSERT_TRUE(it != map.end()); | 97 ASSERT_TRUE(it != map.end()); |
98 ASSERT_EQ(1u, it.GetValue().size()); | 98 ASSERT_EQ(1u, it.GetValue().size()); |
99 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); | 99 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 TEST_F(MapTest, ConstructedFromArray) { | 103 TEST_F(MapTest, ConstructedFromArray) { |
104 Array<String> keys(kStringIntDataSize); | 104 auto keys = Array<String>::New(kStringIntDataSize); |
105 Array<int> values(kStringIntDataSize); | 105 auto values = Array<int>::New(kStringIntDataSize); |
106 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 106 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
107 keys[i] = kStringIntData[i].string_data; | 107 keys[i] = kStringIntData[i].string_data; |
108 values[i] = kStringIntData[i].int_data; | 108 values[i] = kStringIntData[i].int_data; |
109 } | 109 } |
110 | 110 |
111 Map<String, int> map(keys.Pass(), values.Pass()); | 111 Map<String, int> map(keys.Pass(), values.Pass()); |
112 | 112 |
113 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 113 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
114 EXPECT_EQ(kStringIntData[i].int_data, | 114 EXPECT_EQ(kStringIntData[i].int_data, |
115 map.at(mojo::String(kStringIntData[i].string_data))); | 115 map.at(mojo::String(kStringIntData[i].string_data))); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 Map<String, Array<String>> m2 = m.Clone(); | 231 Map<String, Array<String>> m2 = m.Clone(); |
232 | 232 |
233 for (auto it = m2.begin(); it != m2.end(); ++it) { | 233 for (auto it = m2.begin(); it != m2.end(); ++it) { |
234 ASSERT_EQ(1u, it.GetValue().size()); | 234 ASSERT_EQ(1u, it.GetValue().size()); |
235 EXPECT_EQ(it.GetKey(), it.GetValue().at(0)); | 235 EXPECT_EQ(it.GetKey(), it.GetValue().at(0)); |
236 } | 236 } |
237 } | 237 } |
238 | 238 |
239 TEST_F(MapTest, ArrayOfMap) { | 239 TEST_F(MapTest, ArrayOfMap) { |
240 { | 240 { |
241 Array<Map<int32_t, int8_t>> array(1); | 241 auto array = Array<Map<int32_t, int8_t>>::New(1); |
242 array[0].insert(1, 42); | 242 array[0].insert(1, 42); |
243 | 243 |
244 size_t size = GetSerializedSize_(array); | 244 size_t size = GetSerializedSize_(array); |
245 FixedBufferForTesting buf(size); | 245 FixedBufferForTesting buf(size); |
246 Array_Data<Map_Data<int32_t, int8_t>*>* data; | 246 Array_Data<Map_Data<int32_t, int8_t>*>* data; |
247 ArrayValidateParams validate_params( | 247 ArrayValidateParams validate_params( |
248 0, false, new ArrayValidateParams(0, false, nullptr)); | 248 0, false, new ArrayValidateParams(0, false, nullptr)); |
249 SerializeArray_(&array, &buf, &data, &validate_params); | 249 SerializeArray_(&array, &buf, &data, &validate_params); |
250 | 250 |
251 Array<Map<int32_t, int8_t>> deserialized_array; | 251 Array<Map<int32_t, int8_t>> deserialized_array; |
252 Deserialize_(data, &deserialized_array); | 252 Deserialize_(data, &deserialized_array); |
253 | 253 |
254 ASSERT_EQ(1u, deserialized_array.size()); | 254 ASSERT_EQ(1u, deserialized_array.size()); |
255 ASSERT_EQ(1u, deserialized_array[0].size()); | 255 ASSERT_EQ(1u, deserialized_array[0].size()); |
256 ASSERT_EQ(42, deserialized_array[0].at(1)); | 256 ASSERT_EQ(42, deserialized_array[0].at(1)); |
257 } | 257 } |
258 | 258 |
259 { | 259 { |
260 Array<Map<String, Array<bool>>> array(1); | 260 auto array = Array<Map<String, Array<bool>>>::New(1); |
261 Array<bool> map_value(2); | 261 auto map_value = Array<bool>::New(2); |
262 map_value[0] = false; | 262 map_value[0] = false; |
263 map_value[1] = true; | 263 map_value[1] = true; |
264 array[0].insert("hello world", map_value.Pass()); | 264 array[0].insert("hello world", map_value.Pass()); |
265 | 265 |
266 size_t size = GetSerializedSize_(array); | 266 size_t size = GetSerializedSize_(array); |
267 FixedBufferForTesting buf(size); | 267 FixedBufferForTesting buf(size); |
268 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; | 268 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; |
269 ArrayValidateParams validate_params( | 269 ArrayValidateParams validate_params( |
270 0, false, new ArrayValidateParams( | 270 0, false, new ArrayValidateParams( |
271 0, false, new ArrayValidateParams(0, false, nullptr))); | 271 0, false, new ArrayValidateParams(0, false, nullptr))); |
272 SerializeArray_(&array, &buf, &data, &validate_params); | 272 SerializeArray_(&array, &buf, &data, &validate_params); |
273 | 273 |
274 Array<Map<String, Array<bool>>> deserialized_array; | 274 Array<Map<String, Array<bool>>> deserialized_array; |
275 Deserialize_(data, &deserialized_array); | 275 Deserialize_(data, &deserialized_array); |
276 | 276 |
277 ASSERT_EQ(1u, deserialized_array.size()); | 277 ASSERT_EQ(1u, deserialized_array.size()); |
278 ASSERT_EQ(1u, deserialized_array[0].size()); | 278 ASSERT_EQ(1u, deserialized_array[0].size()); |
279 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); | 279 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); |
280 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); | 280 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 } // namespace | 284 } // namespace |
285 } // namespace test | 285 } // namespace test |
286 } // namespace mojo | 286 } // namespace mojo |
OLD | NEW |