| 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/validate_params.h" | 10 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
| 10 #include "mojo/public/cpp/bindings/map.h" | 11 #include "mojo/public/cpp/bindings/map.h" |
| 11 #include "mojo/public/cpp/bindings/string.h" | 12 #include "mojo/public/cpp/bindings/string.h" |
| 12 #include "mojo/public/cpp/bindings/tests/container_test_util.h" | 13 #include "mojo/public/cpp/bindings/tests/container_test_util.h" |
| 13 #include "mojo/public/cpp/environment/environment.h" | 14 #include "mojo/public/cpp/environment/environment.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace mojo { | 17 namespace mojo { |
| 17 namespace test { | 18 namespace test { |
| 18 | 19 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 109 } |
| 109 | 110 |
| 110 Map<String, int> map(keys.Pass(), values.Pass()); | 111 Map<String, int> map(keys.Pass(), values.Pass()); |
| 111 | 112 |
| 112 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 113 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 113 EXPECT_EQ(kStringIntData[i].int_data, | 114 EXPECT_EQ(kStringIntData[i].int_data, |
| 114 map.at(mojo::String(kStringIntData[i].string_data))); | 115 map.at(mojo::String(kStringIntData[i].string_data))); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 TEST_F(MapTest, DecomposeMapTo) { | |
| 119 Array<String> keys(kStringIntDataSize); | |
| 120 Array<int> values(kStringIntDataSize); | |
| 121 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 122 keys[i] = kStringIntData[i].string_data; | |
| 123 values[i] = kStringIntData[i].int_data; | |
| 124 } | |
| 125 | |
| 126 Map<String, int> map(keys.Pass(), values.Pass()); | |
| 127 EXPECT_EQ(kStringIntDataSize, map.size()); | |
| 128 | |
| 129 Array<String> keys2; | |
| 130 Array<int> values2; | |
| 131 map.DecomposeMapTo(&keys2, &values2); | |
| 132 EXPECT_EQ(0u, map.size()); | |
| 133 | |
| 134 EXPECT_EQ(kStringIntDataSize, keys2.size()); | |
| 135 EXPECT_EQ(kStringIntDataSize, values2.size()); | |
| 136 | |
| 137 for (size_t i = 0; i < kStringIntDataSize; ++i) { | |
| 138 // We are not guaranteed that the copies have the same sorting as the | |
| 139 // originals. | |
| 140 String key = kStringIntData[i].string_data; | |
| 141 int value = kStringIntData[i].int_data; | |
| 142 | |
| 143 bool found = false; | |
| 144 for (size_t j = 0; j < keys2.size(); ++j) { | |
| 145 if (keys2[j] == key) { | |
| 146 EXPECT_EQ(value, values2[j]); | |
| 147 found = true; | |
| 148 break; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 EXPECT_TRUE(found); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 TEST_F(MapTest, Insert_Copyable) { | 119 TEST_F(MapTest, Insert_Copyable) { |
| 157 ASSERT_EQ(0u, CopyableType::num_instances()); | 120 ASSERT_EQ(0u, CopyableType::num_instances()); |
| 158 mojo::Map<mojo::String, CopyableType> map; | 121 mojo::Map<mojo::String, CopyableType> map; |
| 159 std::vector<CopyableType*> value_ptrs; | 122 std::vector<CopyableType*> value_ptrs; |
| 160 | 123 |
| 161 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 124 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 162 const char* key = kStringIntData[i].string_data; | 125 const char* key = kStringIntData[i].string_data; |
| 163 CopyableType value; | 126 CopyableType value; |
| 164 value_ptrs.push_back(value.ptr()); | 127 value_ptrs.push_back(value.ptr()); |
| 165 map.insert(key, value); | 128 map.insert(key, value); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 TEST_F(MapTest, ArrayOfMap) { | 239 TEST_F(MapTest, ArrayOfMap) { |
| 277 { | 240 { |
| 278 Array<Map<int32_t, int8_t>> array(1); | 241 Array<Map<int32_t, int8_t>> array(1); |
| 279 array[0].insert(1, 42); | 242 array[0].insert(1, 42); |
| 280 | 243 |
| 281 size_t size = GetSerializedSize_(array); | 244 size_t size = GetSerializedSize_(array); |
| 282 FixedBufferForTesting buf(size); | 245 FixedBufferForTesting buf(size); |
| 283 Array_Data<Map_Data<int32_t, int8_t>*>* data; | 246 Array_Data<Map_Data<int32_t, int8_t>*>* data; |
| 284 ArrayValidateParams validate_params( | 247 ArrayValidateParams validate_params( |
| 285 0, false, new ArrayValidateParams(0, false, nullptr)); | 248 0, false, new ArrayValidateParams(0, false, nullptr)); |
| 286 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 249 SerializeArray_(&array, &buf, &data, &validate_params); |
| 287 | 250 |
| 288 Array<Map<int32_t, int8_t>> deserialized_array; | 251 Array<Map<int32_t, int8_t>> deserialized_array; |
| 289 Deserialize_(data, &deserialized_array); | 252 Deserialize_(data, &deserialized_array); |
| 290 | 253 |
| 291 ASSERT_EQ(1u, deserialized_array.size()); | 254 ASSERT_EQ(1u, deserialized_array.size()); |
| 292 ASSERT_EQ(1u, deserialized_array[0].size()); | 255 ASSERT_EQ(1u, deserialized_array[0].size()); |
| 293 ASSERT_EQ(42, deserialized_array[0].at(1)); | 256 ASSERT_EQ(42, deserialized_array[0].at(1)); |
| 294 } | 257 } |
| 295 | 258 |
| 296 { | 259 { |
| 297 Array<Map<String, Array<bool>>> array(1); | 260 Array<Map<String, Array<bool>>> array(1); |
| 298 Array<bool> map_value(2); | 261 Array<bool> map_value(2); |
| 299 map_value[0] = false; | 262 map_value[0] = false; |
| 300 map_value[1] = true; | 263 map_value[1] = true; |
| 301 array[0].insert("hello world", map_value.Pass()); | 264 array[0].insert("hello world", map_value.Pass()); |
| 302 | 265 |
| 303 size_t size = GetSerializedSize_(array); | 266 size_t size = GetSerializedSize_(array); |
| 304 FixedBufferForTesting buf(size); | 267 FixedBufferForTesting buf(size); |
| 305 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; | 268 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data; |
| 306 ArrayValidateParams validate_params( | 269 ArrayValidateParams validate_params( |
| 307 0, false, new ArrayValidateParams( | 270 0, false, new ArrayValidateParams( |
| 308 0, false, new ArrayValidateParams(0, false, nullptr))); | 271 0, false, new ArrayValidateParams(0, false, nullptr))); |
| 309 SerializeArray_(array.Pass(), &buf, &data, &validate_params); | 272 SerializeArray_(&array, &buf, &data, &validate_params); |
| 310 | 273 |
| 311 Array<Map<String, Array<bool>>> deserialized_array; | 274 Array<Map<String, Array<bool>>> deserialized_array; |
| 312 Deserialize_(data, &deserialized_array); | 275 Deserialize_(data, &deserialized_array); |
| 313 | 276 |
| 314 ASSERT_EQ(1u, deserialized_array.size()); | 277 ASSERT_EQ(1u, deserialized_array.size()); |
| 315 ASSERT_EQ(1u, deserialized_array[0].size()); | 278 ASSERT_EQ(1u, deserialized_array[0].size()); |
| 316 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); | 279 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]); |
| 317 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); | 280 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]); |
| 318 } | 281 } |
| 319 } | 282 } |
| 320 | 283 |
| 321 } // namespace | 284 } // namespace |
| 322 } // namespace test | 285 } // namespace test |
| 323 } // namespace mojo | 286 } // namespace mojo |
| OLD | NEW |