| 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_internal.h" | 6 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 7 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 7 #include "mojo/public/cpp/bindings/lib/array_serialization.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/tests/container_test_util.h" | 9 #include "mojo/public/cpp/bindings/tests/container_test_util.h" |
| 10 #include "mojo/public/cpp/bindings/tests/iterator_test_util.h" | 10 #include "mojo/public/cpp/bindings/tests/iterator_test_util.h" |
| 11 #include "mojo/public/cpp/environment/environment.h" | |
| 12 #include "mojo/public/interfaces/bindings/tests/test_arrays.mojom.h" | 11 #include "mojo/public/interfaces/bindings/tests/test_arrays.mojom.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" | 12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace mojo { | 15 namespace mojo { |
| 17 namespace test { | 16 namespace test { |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 using mojo::internal::Array_Data; | 19 using mojo::internal::Array_Data; |
| 21 using mojo::internal::ArrayValidateParams; | 20 using mojo::internal::ArrayValidateParams; |
| 22 using mojo::internal::FixedBufferForTesting; | 21 using mojo::internal::FixedBufferForTesting; |
| 23 using mojo::internal::String_Data; | 22 using mojo::internal::String_Data; |
| 24 | 23 |
| 25 class ArrayTest : public testing::Test { | |
| 26 public: | |
| 27 ~ArrayTest() override {} | |
| 28 | |
| 29 private: | |
| 30 Environment env_; | |
| 31 }; | |
| 32 | |
| 33 // Tests that basic Array operations work. | 24 // Tests that basic Array operations work. |
| 34 TEST_F(ArrayTest, Basic) { | 25 TEST(ArrayTest, Basic) { |
| 35 auto array = Array<uint8_t>::New(8); | 26 auto array = Array<uint8_t>::New(8); |
| 36 for (size_t i = 0u; i < array.size(); ++i) { | 27 for (size_t i = 0u; i < array.size(); ++i) { |
| 37 uint8_t val = static_cast<uint8_t>(i * 2); | 28 uint8_t val = static_cast<uint8_t>(i * 2); |
| 38 array[i] = val; | 29 array[i] = val; |
| 39 EXPECT_EQ(val, array.at(i)); | 30 EXPECT_EQ(val, array.at(i)); |
| 40 } | 31 } |
| 41 | 32 |
| 42 EXPECT_EQ(0u, *array.data()); | 33 EXPECT_EQ(0u, *array.data()); |
| 43 EXPECT_EQ(2u, *(array.data() + 1)); | 34 EXPECT_EQ(2u, *(array.data() + 1)); |
| 44 EXPECT_EQ(4u, *(array.data() + 2)); | 35 EXPECT_EQ(4u, *(array.data() + 2)); |
| 45 } | 36 } |
| 46 | 37 |
| 47 TEST_F(ArrayTest, Testability) { | 38 TEST(ArrayTest, Testability) { |
| 48 Array<int32_t> array; | 39 Array<int32_t> array; |
| 49 EXPECT_FALSE(array); | 40 EXPECT_FALSE(array); |
| 50 EXPECT_TRUE(array.is_null()); | 41 EXPECT_TRUE(array.is_null()); |
| 51 | 42 |
| 52 array.push_back(123); | 43 array.push_back(123); |
| 53 EXPECT_TRUE(array); | 44 EXPECT_TRUE(array); |
| 54 EXPECT_FALSE(array.is_null()); | 45 EXPECT_FALSE(array.is_null()); |
| 55 } | 46 } |
| 56 | 47 |
| 57 void NullptrConstructorTestHelper(Array<int32_t> array) { | 48 void NullptrConstructorTestHelper(Array<int32_t> array) { |
| 58 EXPECT_FALSE(array); | 49 EXPECT_FALSE(array); |
| 59 EXPECT_TRUE(array.is_null()); | 50 EXPECT_TRUE(array.is_null()); |
| 60 EXPECT_EQ(0u, array.size()); | 51 EXPECT_EQ(0u, array.size()); |
| 61 } | 52 } |
| 62 | 53 |
| 63 TEST_F(ArrayTest, NullptrConstructor) { | 54 TEST(ArrayTest, NullptrConstructor) { |
| 64 Array<int32_t> array(nullptr); | 55 Array<int32_t> array(nullptr); |
| 65 EXPECT_FALSE(array); | 56 EXPECT_FALSE(array); |
| 66 EXPECT_TRUE(array.is_null()); | 57 EXPECT_TRUE(array.is_null()); |
| 67 EXPECT_EQ(0u, array.size()); | 58 EXPECT_EQ(0u, array.size()); |
| 68 | 59 |
| 69 array.push_back(123); | 60 array.push_back(123); |
| 70 EXPECT_TRUE(array); | 61 EXPECT_TRUE(array); |
| 71 EXPECT_FALSE(array.is_null()); | 62 EXPECT_FALSE(array.is_null()); |
| 72 EXPECT_EQ(1u, array.size()); | 63 EXPECT_EQ(1u, array.size()); |
| 73 | 64 |
| 74 // Test some implicit constructions of |Array<int32_t>| from a |nullptr|. | 65 // Test some implicit constructions of |Array<int32_t>| from a |nullptr|. |
| 75 array = nullptr; | 66 array = nullptr; |
| 76 NullptrConstructorTestHelper(nullptr); | 67 NullptrConstructorTestHelper(nullptr); |
| 77 } | 68 } |
| 78 | 69 |
| 79 // Tests that basic Array<bool> operations work. | 70 // Tests that basic Array<bool> operations work. |
| 80 TEST_F(ArrayTest, Bool) { | 71 TEST(ArrayTest, Bool) { |
| 81 auto array = Array<bool>::New(64); | 72 auto array = Array<bool>::New(64); |
| 82 for (size_t i = 0; i < array.size(); ++i) { | 73 for (size_t i = 0; i < array.size(); ++i) { |
| 83 bool val = i % 3 == 0; | 74 bool val = i % 3 == 0; |
| 84 array[i] = val; | 75 array[i] = val; |
| 85 EXPECT_EQ(val, array.at(i)); | 76 EXPECT_EQ(val, array.at(i)); |
| 86 } | 77 } |
| 87 } | 78 } |
| 88 | 79 |
| 89 // Tests that Array<ScopedMessagePipeHandle> supports transferring handles. | 80 // Tests that Array<ScopedMessagePipeHandle> supports transferring handles. |
| 90 TEST_F(ArrayTest, Handle) { | 81 TEST(ArrayTest, Handle) { |
| 91 MessagePipe pipe; | 82 MessagePipe pipe; |
| 92 auto handles = Array<ScopedMessagePipeHandle>::New(2); | 83 auto handles = Array<ScopedMessagePipeHandle>::New(2); |
| 93 handles[0] = pipe.handle0.Pass(); | 84 handles[0] = pipe.handle0.Pass(); |
| 94 handles[1].reset(pipe.handle1.release()); | 85 handles[1].reset(pipe.handle1.release()); |
| 95 | 86 |
| 96 EXPECT_FALSE(pipe.handle0.is_valid()); | 87 EXPECT_FALSE(pipe.handle0.is_valid()); |
| 97 EXPECT_FALSE(pipe.handle1.is_valid()); | 88 EXPECT_FALSE(pipe.handle1.is_valid()); |
| 98 | 89 |
| 99 Array<ScopedMessagePipeHandle> handles2 = handles.Pass(); | 90 Array<ScopedMessagePipeHandle> handles2 = handles.Pass(); |
| 100 EXPECT_TRUE(handles2[0].is_valid()); | 91 EXPECT_TRUE(handles2[0].is_valid()); |
| 101 EXPECT_TRUE(handles2[1].is_valid()); | 92 EXPECT_TRUE(handles2[1].is_valid()); |
| 102 | 93 |
| 103 ScopedMessagePipeHandle pipe_handle = handles2[0].Pass(); | 94 ScopedMessagePipeHandle pipe_handle = handles2[0].Pass(); |
| 104 EXPECT_TRUE(pipe_handle.is_valid()); | 95 EXPECT_TRUE(pipe_handle.is_valid()); |
| 105 EXPECT_FALSE(handles2[0].is_valid()); | 96 EXPECT_FALSE(handles2[0].is_valid()); |
| 106 } | 97 } |
| 107 | 98 |
| 108 // Tests that Array<ScopedMessagePipeHandle> supports closing handles. | 99 // Tests that Array<ScopedMessagePipeHandle> supports closing handles. |
| 109 TEST_F(ArrayTest, HandlesAreClosed) { | 100 TEST(ArrayTest, HandlesAreClosed) { |
| 110 MessagePipe pipe; | 101 MessagePipe pipe; |
| 111 MojoHandle pipe0_value = pipe.handle0.get().value(); | 102 MojoHandle pipe0_value = pipe.handle0.get().value(); |
| 112 MojoHandle pipe1_value = pipe.handle0.get().value(); | 103 MojoHandle pipe1_value = pipe.handle0.get().value(); |
| 113 | 104 |
| 114 { | 105 { |
| 115 auto handles = Array<ScopedMessagePipeHandle>::New(2); | 106 auto handles = Array<ScopedMessagePipeHandle>::New(2); |
| 116 handles[0] = pipe.handle0.Pass(); | 107 handles[0] = pipe.handle0.Pass(); |
| 117 handles[1].reset(pipe.handle0.release()); | 108 handles[1].reset(pipe.handle0.release()); |
| 118 } | 109 } |
| 119 | 110 |
| 120 // We expect the pipes to have been closed. | 111 // We expect the pipes to have been closed. |
| 121 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe0_value)); | 112 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe0_value)); |
| 122 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value)); | 113 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value)); |
| 123 } | 114 } |
| 124 | 115 |
| 125 TEST_F(ArrayTest, Clone) { | 116 TEST(ArrayTest, Clone) { |
| 126 { | 117 { |
| 127 // Test POD. | 118 // Test POD. |
| 128 auto array = Array<int32_t>::New(3); | 119 auto array = Array<int32_t>::New(3); |
| 129 for (size_t i = 0; i < array.size(); ++i) | 120 for (size_t i = 0; i < array.size(); ++i) |
| 130 array[i] = static_cast<int32_t>(i); | 121 array[i] = static_cast<int32_t>(i); |
| 131 | 122 |
| 132 Array<int32_t> clone_array = array.Clone(); | 123 Array<int32_t> clone_array = array.Clone(); |
| 133 EXPECT_EQ(array.size(), clone_array.size()); | 124 EXPECT_EQ(array.size(), clone_array.size()); |
| 134 for (size_t i = 0; i < array.size(); ++i) | 125 for (size_t i = 0; i < array.size(); ++i) |
| 135 EXPECT_EQ(array[i], clone_array[i]); | 126 EXPECT_EQ(array[i], clone_array[i]); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 EXPECT_EQ(array[1][1], clone_array[1][1]); | 171 EXPECT_EQ(array[1][1], clone_array[1][1]); |
| 181 } | 172 } |
| 182 | 173 |
| 183 { | 174 { |
| 184 // Test that array of handles still works although Clone() is not available. | 175 // Test that array of handles still works although Clone() is not available. |
| 185 auto array = Array<ScopedMessagePipeHandle>::New(10); | 176 auto array = Array<ScopedMessagePipeHandle>::New(10); |
| 186 EXPECT_FALSE(array[0].is_valid()); | 177 EXPECT_FALSE(array[0].is_valid()); |
| 187 } | 178 } |
| 188 } | 179 } |
| 189 | 180 |
| 190 TEST_F(ArrayTest, Serialization_ArrayOfPOD) { | 181 TEST(ArrayTest, Serialization_ArrayOfPOD) { |
| 191 auto array = Array<int32_t>::New(4); | 182 auto array = Array<int32_t>::New(4); |
| 192 for (size_t i = 0; i < array.size(); ++i) | 183 for (size_t i = 0; i < array.size(); ++i) |
| 193 array[i] = static_cast<int32_t>(i); | 184 array[i] = static_cast<int32_t>(i); |
| 194 | 185 |
| 195 size_t size = GetSerializedSize_(array); | 186 size_t size = GetSerializedSize_(array); |
| 196 EXPECT_EQ(8U + 4 * 4U, size); | 187 EXPECT_EQ(8U + 4 * 4U, size); |
| 197 | 188 |
| 198 FixedBufferForTesting buf(size); | 189 FixedBufferForTesting buf(size); |
| 199 Array_Data<int32_t>* data = nullptr; | 190 Array_Data<int32_t>* data = nullptr; |
| 200 ArrayValidateParams validate_params(0, false, nullptr); | 191 ArrayValidateParams validate_params(0, false, nullptr); |
| 201 EXPECT_EQ(mojo::internal::ValidationError::NONE, | 192 EXPECT_EQ(mojo::internal::ValidationError::NONE, |
| 202 SerializeArray_(&array, &buf, &data, &validate_params)); | 193 SerializeArray_(&array, &buf, &data, &validate_params)); |
| 203 | 194 |
| 204 Array<int32_t> array2; | 195 Array<int32_t> array2; |
| 205 Deserialize_(data, &array2); | 196 Deserialize_(data, &array2); |
| 206 | 197 |
| 207 EXPECT_EQ(4U, array2.size()); | 198 EXPECT_EQ(4U, array2.size()); |
| 208 for (size_t i = 0; i < array2.size(); ++i) | 199 for (size_t i = 0; i < array2.size(); ++i) |
| 209 EXPECT_EQ(static_cast<int32_t>(i), array2[i]); | 200 EXPECT_EQ(static_cast<int32_t>(i), array2[i]); |
| 210 } | 201 } |
| 211 | 202 |
| 212 TEST_F(ArrayTest, Serialization_EmptyArrayOfPOD) { | 203 TEST(ArrayTest, Serialization_EmptyArrayOfPOD) { |
| 213 auto array = Array<int32_t>::New(0); | 204 auto array = Array<int32_t>::New(0); |
| 214 size_t size = GetSerializedSize_(array); | 205 size_t size = GetSerializedSize_(array); |
| 215 EXPECT_EQ(8U, size); | 206 EXPECT_EQ(8U, size); |
| 216 | 207 |
| 217 FixedBufferForTesting buf(size); | 208 FixedBufferForTesting buf(size); |
| 218 Array_Data<int32_t>* data = nullptr; | 209 Array_Data<int32_t>* data = nullptr; |
| 219 ArrayValidateParams validate_params(0, false, nullptr); | 210 ArrayValidateParams validate_params(0, false, nullptr); |
| 220 EXPECT_EQ(mojo::internal::ValidationError::NONE, | 211 EXPECT_EQ(mojo::internal::ValidationError::NONE, |
| 221 SerializeArray_(&array, &buf, &data, &validate_params)); | 212 SerializeArray_(&array, &buf, &data, &validate_params)); |
| 222 | 213 |
| 223 Array<int32_t> array2; | 214 Array<int32_t> array2; |
| 224 Deserialize_(data, &array2); | 215 Deserialize_(data, &array2); |
| 225 EXPECT_EQ(0U, array2.size()); | 216 EXPECT_EQ(0U, array2.size()); |
| 226 } | 217 } |
| 227 | 218 |
| 228 TEST_F(ArrayTest, Serialization_ArrayOfArrayOfPOD) { | 219 TEST(ArrayTest, Serialization_ArrayOfArrayOfPOD) { |
| 229 auto array = Array<Array<int32_t>>::New(2); | 220 auto array = Array<Array<int32_t>>::New(2); |
| 230 for (size_t j = 0; j < array.size(); ++j) { | 221 for (size_t j = 0; j < array.size(); ++j) { |
| 231 auto inner = Array<int32_t>::New(4); | 222 auto inner = Array<int32_t>::New(4); |
| 232 for (size_t i = 0; i < inner.size(); ++i) | 223 for (size_t i = 0; i < inner.size(); ++i) |
| 233 inner[i] = static_cast<int32_t>(i + (j * 10)); | 224 inner[i] = static_cast<int32_t>(i + (j * 10)); |
| 234 array[j] = inner.Pass(); | 225 array[j] = inner.Pass(); |
| 235 } | 226 } |
| 236 | 227 |
| 237 size_t size = GetSerializedSize_(array); | 228 size_t size = GetSerializedSize_(array); |
| 238 EXPECT_EQ(8U + 2 * 8U + 2 * (8U + 4 * 4U), size); | 229 EXPECT_EQ(8U + 2 * 8U + 2 * (8U + 4 * 4U), size); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 249 | 240 |
| 250 EXPECT_EQ(2U, array2.size()); | 241 EXPECT_EQ(2U, array2.size()); |
| 251 for (size_t j = 0; j < array2.size(); ++j) { | 242 for (size_t j = 0; j < array2.size(); ++j) { |
| 252 const Array<int32_t>& inner = array2[j]; | 243 const Array<int32_t>& inner = array2[j]; |
| 253 EXPECT_EQ(4U, inner.size()); | 244 EXPECT_EQ(4U, inner.size()); |
| 254 for (size_t i = 0; i < inner.size(); ++i) | 245 for (size_t i = 0; i < inner.size(); ++i) |
| 255 EXPECT_EQ(static_cast<int32_t>(i + (j * 10)), inner[i]); | 246 EXPECT_EQ(static_cast<int32_t>(i + (j * 10)), inner[i]); |
| 256 } | 247 } |
| 257 } | 248 } |
| 258 | 249 |
| 259 TEST_F(ArrayTest, Serialization_ArrayOfScopedEnum) { | 250 TEST(ArrayTest, Serialization_ArrayOfScopedEnum) { |
| 260 enum class TestEnum : int32_t { | 251 enum class TestEnum : int32_t { |
| 261 E0, | 252 E0, |
| 262 E1, | 253 E1, |
| 263 E2, | 254 E2, |
| 264 E3, | 255 E3, |
| 265 }; | 256 }; |
| 266 static const TestEnum TEST_VALS[] = { | 257 static const TestEnum TEST_VALS[] = { |
| 267 TestEnum::E0, TestEnum::E2, TestEnum::E1, TestEnum::E3, | 258 TestEnum::E0, TestEnum::E2, TestEnum::E1, TestEnum::E3, |
| 268 TestEnum::E2, TestEnum::E2, TestEnum::E2, TestEnum::E0, | 259 TestEnum::E2, TestEnum::E2, TestEnum::E2, TestEnum::E0, |
| 269 }; | 260 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 281 SerializeArray_(&array, &buf, &data, &validate_params); | 272 SerializeArray_(&array, &buf, &data, &validate_params); |
| 282 | 273 |
| 283 Array<TestEnum> array2; | 274 Array<TestEnum> array2; |
| 284 Deserialize_(data, &array2); | 275 Deserialize_(data, &array2); |
| 285 | 276 |
| 286 EXPECT_EQ(MOJO_ARRAYSIZE(TEST_VALS), array2.size()); | 277 EXPECT_EQ(MOJO_ARRAYSIZE(TEST_VALS), array2.size()); |
| 287 for (size_t i = 0; i < array2.size(); ++i) | 278 for (size_t i = 0; i < array2.size(); ++i) |
| 288 EXPECT_EQ(TEST_VALS[i], array2[i]); | 279 EXPECT_EQ(TEST_VALS[i], array2[i]); |
| 289 } | 280 } |
| 290 | 281 |
| 291 TEST_F(ArrayTest, Serialization_ArrayOfBool) { | 282 TEST(ArrayTest, Serialization_ArrayOfBool) { |
| 292 auto array = Array<bool>::New(10); | 283 auto array = Array<bool>::New(10); |
| 293 for (size_t i = 0; i < array.size(); ++i) | 284 for (size_t i = 0; i < array.size(); ++i) |
| 294 array[i] = i % 2 ? true : false; | 285 array[i] = i % 2 ? true : false; |
| 295 | 286 |
| 296 size_t size = GetSerializedSize_(array); | 287 size_t size = GetSerializedSize_(array); |
| 297 EXPECT_EQ(8U + 8U, size); | 288 EXPECT_EQ(8U + 8U, size); |
| 298 | 289 |
| 299 FixedBufferForTesting buf(size); | 290 FixedBufferForTesting buf(size); |
| 300 Array_Data<bool>* data = nullptr; | 291 Array_Data<bool>* data = nullptr; |
| 301 ArrayValidateParams validate_params(0, false, nullptr); | 292 ArrayValidateParams validate_params(0, false, nullptr); |
| 302 EXPECT_EQ(mojo::internal::ValidationError::NONE, | 293 EXPECT_EQ(mojo::internal::ValidationError::NONE, |
| 303 SerializeArray_(&array, &buf, &data, &validate_params)); | 294 SerializeArray_(&array, &buf, &data, &validate_params)); |
| 304 | 295 |
| 305 Array<bool> array2; | 296 Array<bool> array2; |
| 306 Deserialize_(data, &array2); | 297 Deserialize_(data, &array2); |
| 307 | 298 |
| 308 EXPECT_EQ(10U, array2.size()); | 299 EXPECT_EQ(10U, array2.size()); |
| 309 for (size_t i = 0; i < array2.size(); ++i) | 300 for (size_t i = 0; i < array2.size(); ++i) |
| 310 EXPECT_EQ(i % 2 ? true : false, array2[i]); | 301 EXPECT_EQ(i % 2 ? true : false, array2[i]); |
| 311 } | 302 } |
| 312 | 303 |
| 313 TEST_F(ArrayTest, Serialization_ArrayOfString) { | 304 TEST(ArrayTest, Serialization_ArrayOfString) { |
| 314 auto array = Array<String>::New(10); | 305 auto array = Array<String>::New(10); |
| 315 for (size_t i = 0; i < array.size(); ++i) { | 306 for (size_t i = 0; i < array.size(); ++i) { |
| 316 char c = 'A' + static_cast<char>(i); | 307 char c = 'A' + static_cast<char>(i); |
| 317 array[i] = String(&c, 1); | 308 array[i] = String(&c, 1); |
| 318 } | 309 } |
| 319 | 310 |
| 320 size_t size = GetSerializedSize_(array); | 311 size_t size = GetSerializedSize_(array); |
| 321 EXPECT_EQ(8U + // array header | 312 EXPECT_EQ(8U + // array header |
| 322 10 * 8U + // array payload (10 pointers) | 313 10 * 8U + // array payload (10 pointers) |
| 323 10 * (8U + // string header | 314 10 * (8U + // string header |
| (...skipping 11 matching lines...) Expand all Loading... |
| 335 Deserialize_(data, &array2); | 326 Deserialize_(data, &array2); |
| 336 | 327 |
| 337 EXPECT_EQ(10U, array2.size()); | 328 EXPECT_EQ(10U, array2.size()); |
| 338 for (size_t i = 0; i < array2.size(); ++i) { | 329 for (size_t i = 0; i < array2.size(); ++i) { |
| 339 char c = 'A' + static_cast<char>(i); | 330 char c = 'A' + static_cast<char>(i); |
| 340 EXPECT_EQ(String(&c, 1), array2[i]); | 331 EXPECT_EQ(String(&c, 1), array2[i]); |
| 341 } | 332 } |
| 342 } | 333 } |
| 343 | 334 |
| 344 // Tests serializing and deserializing an Array<Handle>. | 335 // Tests serializing and deserializing an Array<Handle>. |
| 345 TEST_F(ArrayTest, Serialization_ArrayOfHandle) { | 336 TEST(ArrayTest, Serialization_ArrayOfHandle) { |
| 346 auto array = Array<ScopedHandleBase<MessagePipeHandle>>::New(4); | 337 auto array = Array<ScopedHandleBase<MessagePipeHandle>>::New(4); |
| 347 MessagePipe p0; | 338 MessagePipe p0; |
| 348 MessagePipe p1; | 339 MessagePipe p1; |
| 349 // array[0] is left invalid. | 340 // array[0] is left invalid. |
| 350 array[1] = p0.handle1.Pass(); | 341 array[1] = p0.handle1.Pass(); |
| 351 array[2] = p1.handle0.Pass(); | 342 array[2] = p1.handle0.Pass(); |
| 352 array[3] = p1.handle1.Pass(); | 343 array[3] = p1.handle1.Pass(); |
| 353 | 344 |
| 354 size_t size = GetSerializedSize_(array); | 345 size_t size = GetSerializedSize_(array); |
| 355 EXPECT_EQ(8U // array header | 346 EXPECT_EQ(8U // array header |
| (...skipping 25 matching lines...) Expand all Loading... |
| 381 EXPECT_FALSE(array[2].is_valid()); | 372 EXPECT_FALSE(array[2].is_valid()); |
| 382 EXPECT_FALSE(array[3].is_valid()); | 373 EXPECT_FALSE(array[3].is_valid()); |
| 383 | 374 |
| 384 Deserialize_(data, &array); | 375 Deserialize_(data, &array); |
| 385 EXPECT_FALSE(array[0].is_valid()); | 376 EXPECT_FALSE(array[0].is_valid()); |
| 386 EXPECT_TRUE(array[1].is_valid()); | 377 EXPECT_TRUE(array[1].is_valid()); |
| 387 EXPECT_TRUE(array[2].is_valid()); | 378 EXPECT_TRUE(array[2].is_valid()); |
| 388 EXPECT_TRUE(array[3].is_valid()); | 379 EXPECT_TRUE(array[3].is_valid()); |
| 389 } | 380 } |
| 390 | 381 |
| 391 TEST_F(ArrayTest, Serialization_StructWithArraysOfHandles) { | 382 TEST(ArrayTest, Serialization_StructWithArraysOfHandles) { |
| 392 StructWithHandles handles_struct; | 383 StructWithHandles handles_struct; |
| 393 MessagePipe handle_pair_0; | 384 MessagePipe handle_pair_0; |
| 394 } | 385 } |
| 395 | 386 |
| 396 // Test serializing and deserializing an Array<InterfacePtr>. | 387 // Test serializing and deserializing an Array<InterfacePtr>. |
| 397 TEST_F(ArrayTest, Serialization_ArrayOfInterfacePtr) { | 388 TEST(ArrayTest, Serialization_ArrayOfInterfacePtr) { |
| 398 auto iface_array = Array<mojo::InterfaceHandle<TestInterface>>::New(1); | 389 auto iface_array = Array<mojo::InterfaceHandle<TestInterface>>::New(1); |
| 399 size_t size = GetSerializedSize_(iface_array); | 390 size_t size = GetSerializedSize_(iface_array); |
| 400 EXPECT_EQ(8U // array header | 391 EXPECT_EQ(8U // array header |
| 401 + (8U * 1), // Interface_Data * number of elements | 392 + (8U * 1), // Interface_Data * number of elements |
| 402 size); | 393 size); |
| 403 | 394 |
| 404 FixedBufferForTesting buf(size * 3); | 395 FixedBufferForTesting buf(size * 3); |
| 405 Array_Data<mojo::internal::Interface_Data>* output = nullptr; | 396 Array_Data<mojo::internal::Interface_Data>* output = nullptr; |
| 406 | 397 |
| 407 // 1. Invalid InterfacePtr should fail serialization. | 398 // 1. Invalid InterfacePtr should fail serialization. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 428 mojo::internal::ValidationError::NONE, | 419 mojo::internal::ValidationError::NONE, |
| 429 SerializeArray_(&iface_array, &buf, &output, &validate_non_nullable)); | 420 SerializeArray_(&iface_array, &buf, &output, &validate_non_nullable)); |
| 430 EXPECT_FALSE(iface_array[0].is_valid()); | 421 EXPECT_FALSE(iface_array[0].is_valid()); |
| 431 | 422 |
| 432 Deserialize_(output, &iface_array); | 423 Deserialize_(output, &iface_array); |
| 433 EXPECT_TRUE(iface_array[0].is_valid()); | 424 EXPECT_TRUE(iface_array[0].is_valid()); |
| 434 } | 425 } |
| 435 | 426 |
| 436 // Test serializing and deserializing a struct with an Array<> of another struct | 427 // Test serializing and deserializing a struct with an Array<> of another struct |
| 437 // which has an InterfacePtr. | 428 // which has an InterfacePtr. |
| 438 TEST_F(ArrayTest, Serialization_StructWithArrayOfInterfacePtr) { | 429 TEST(ArrayTest, Serialization_StructWithArrayOfInterfacePtr) { |
| 439 StructWithInterfaceArray struct_arr_iface; | 430 StructWithInterfaceArray struct_arr_iface; |
| 440 struct_arr_iface.structs_array = Array<StructWithInterfacePtr>::New(1); | 431 struct_arr_iface.structs_array = Array<StructWithInterfacePtr>::New(1); |
| 441 struct_arr_iface.nullable_structs_array = | 432 struct_arr_iface.nullable_structs_array = |
| 442 Array<StructWithInterfacePtr>::New(1); | 433 Array<StructWithInterfacePtr>::New(1); |
| 443 | 434 |
| 444 size_t size = GetSerializedSize_(struct_arr_iface); | 435 size_t size = GetSerializedSize_(struct_arr_iface); |
| 445 EXPECT_EQ(8U // struct header | 436 EXPECT_EQ(8U // struct header |
| 446 + 8U // offset to |structs_array| | 437 + 8U // offset to |structs_array| |
| 447 + (8U // array header | 438 + (8U // array header |
| 448 + 8U) // offset to StructWithInterface (nullptr) | 439 + 8U) // offset to StructWithInterface (nullptr) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 473 Serialize_(&struct_arr_iface, &buf, &struct_arr_iface_data)); | 464 Serialize_(&struct_arr_iface, &buf, &struct_arr_iface_data)); |
| 474 | 465 |
| 475 EXPECT_FALSE(struct_arr_iface.structs_array[0]->iptr.is_valid()); | 466 EXPECT_FALSE(struct_arr_iface.structs_array[0]->iptr.is_valid()); |
| 476 | 467 |
| 477 Deserialize_(struct_arr_iface_data, &struct_arr_iface); | 468 Deserialize_(struct_arr_iface_data, &struct_arr_iface); |
| 478 EXPECT_TRUE(struct_arr_iface.structs_array[0]->iptr.is_valid()); | 469 EXPECT_TRUE(struct_arr_iface.structs_array[0]->iptr.is_valid()); |
| 479 } | 470 } |
| 480 | 471 |
| 481 // Test serializing and deserializing a struct with an Array<> of interface | 472 // Test serializing and deserializing a struct with an Array<> of interface |
| 482 // requests. | 473 // requests. |
| 483 TEST_F(ArrayTest, Serialization_StructWithArrayOfIntefaceRequest) { | 474 TEST(ArrayTest, Serialization_StructWithArrayOfIntefaceRequest) { |
| 484 StructWithInterfaceRequests struct_arr_iface_req; | 475 StructWithInterfaceRequests struct_arr_iface_req; |
| 485 struct_arr_iface_req.req_array = | 476 struct_arr_iface_req.req_array = |
| 486 Array<InterfaceRequest<TestInterface>>::New(1); | 477 Array<InterfaceRequest<TestInterface>>::New(1); |
| 487 struct_arr_iface_req.nullable_req_array = | 478 struct_arr_iface_req.nullable_req_array = |
| 488 Array<InterfaceRequest<TestInterface>>::New(1); | 479 Array<InterfaceRequest<TestInterface>>::New(1); |
| 489 | 480 |
| 490 size_t size = GetSerializedSize_(struct_arr_iface_req); | 481 size_t size = GetSerializedSize_(struct_arr_iface_req); |
| 491 EXPECT_EQ(8U // struct header | 482 EXPECT_EQ(8U // struct header |
| 492 + 8U // offset to |req_array| | 483 + 8U // offset to |req_array| |
| 493 + (8U // array header for |req_array| | 484 + (8U // array header for |req_array| |
| (...skipping 23 matching lines...) Expand all Loading... |
| 517 EXPECT_EQ( | 508 EXPECT_EQ( |
| 518 mojo::internal::ValidationError::NONE, | 509 mojo::internal::ValidationError::NONE, |
| 519 Serialize_(&struct_arr_iface_req, &buf, &struct_arr_iface_req_data)); | 510 Serialize_(&struct_arr_iface_req, &buf, &struct_arr_iface_req_data)); |
| 520 | 511 |
| 521 EXPECT_FALSE(struct_arr_iface_req.req_array[0].is_pending()); | 512 EXPECT_FALSE(struct_arr_iface_req.req_array[0].is_pending()); |
| 522 | 513 |
| 523 Deserialize_(struct_arr_iface_req_data, &struct_arr_iface_req); | 514 Deserialize_(struct_arr_iface_req_data, &struct_arr_iface_req); |
| 524 EXPECT_TRUE(struct_arr_iface_req.req_array[0].is_pending()); | 515 EXPECT_TRUE(struct_arr_iface_req.req_array[0].is_pending()); |
| 525 } | 516 } |
| 526 | 517 |
| 527 TEST_F(ArrayTest, Resize_Copyable) { | 518 TEST(ArrayTest, Resize_Copyable) { |
| 528 ASSERT_EQ(0u, CopyableType::num_instances()); | 519 ASSERT_EQ(0u, CopyableType::num_instances()); |
| 529 auto array = mojo::Array<CopyableType>::New(3); | 520 auto array = mojo::Array<CopyableType>::New(3); |
| 530 std::vector<CopyableType*> value_ptrs; | 521 std::vector<CopyableType*> value_ptrs; |
| 531 value_ptrs.push_back(array[0].ptr()); | 522 value_ptrs.push_back(array[0].ptr()); |
| 532 value_ptrs.push_back(array[1].ptr()); | 523 value_ptrs.push_back(array[1].ptr()); |
| 533 | 524 |
| 534 for (size_t i = 0; i < array.size(); i++) | 525 for (size_t i = 0; i < array.size(); i++) |
| 535 array[i].ResetCopied(); | 526 array[i].ResetCopied(); |
| 536 | 527 |
| 537 array.resize(2); | 528 array.resize(2); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 EXPECT_EQ(value_ptrs[i], array[i].ptr()); | 560 EXPECT_EQ(value_ptrs[i], array[i].ptr()); |
| 570 } | 561 } |
| 571 array.reset(); | 562 array.reset(); |
| 572 EXPECT_EQ(0u, CopyableType::num_instances()); | 563 EXPECT_EQ(0u, CopyableType::num_instances()); |
| 573 EXPECT_FALSE(array); | 564 EXPECT_FALSE(array); |
| 574 array.resize(0); | 565 array.resize(0); |
| 575 EXPECT_EQ(0u, CopyableType::num_instances()); | 566 EXPECT_EQ(0u, CopyableType::num_instances()); |
| 576 EXPECT_TRUE(array); | 567 EXPECT_TRUE(array); |
| 577 } | 568 } |
| 578 | 569 |
| 579 TEST_F(ArrayTest, Resize_MoveOnly) { | 570 TEST(ArrayTest, Resize_MoveOnly) { |
| 580 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 571 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
| 581 auto array = mojo::Array<MoveOnlyType>::New(3); | 572 auto array = mojo::Array<MoveOnlyType>::New(3); |
| 582 std::vector<MoveOnlyType*> value_ptrs; | 573 std::vector<MoveOnlyType*> value_ptrs; |
| 583 value_ptrs.push_back(array[0].ptr()); | 574 value_ptrs.push_back(array[0].ptr()); |
| 584 value_ptrs.push_back(array[1].ptr()); | 575 value_ptrs.push_back(array[1].ptr()); |
| 585 | 576 |
| 586 for (size_t i = 0; i < array.size(); i++) | 577 for (size_t i = 0; i < array.size(); i++) |
| 587 EXPECT_FALSE(array[i].moved()); | 578 EXPECT_FALSE(array[i].moved()); |
| 588 | 579 |
| 589 array.resize(2); | 580 array.resize(2); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 EXPECT_FALSE(array[i].moved()); | 612 EXPECT_FALSE(array[i].moved()); |
| 622 | 613 |
| 623 array.reset(); | 614 array.reset(); |
| 624 EXPECT_EQ(0u, MoveOnlyType::num_instances()); | 615 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
| 625 EXPECT_FALSE(array); | 616 EXPECT_FALSE(array); |
| 626 array.resize(0); | 617 array.resize(0); |
| 627 EXPECT_EQ(0u, MoveOnlyType::num_instances()); | 618 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
| 628 EXPECT_TRUE(array); | 619 EXPECT_TRUE(array); |
| 629 } | 620 } |
| 630 | 621 |
| 631 TEST_F(ArrayTest, PushBack_Copyable) { | 622 TEST(ArrayTest, PushBack_Copyable) { |
| 632 ASSERT_EQ(0u, CopyableType::num_instances()); | 623 ASSERT_EQ(0u, CopyableType::num_instances()); |
| 633 auto array = mojo::Array<CopyableType>::New(2); | 624 auto array = mojo::Array<CopyableType>::New(2); |
| 634 array.reset(); | 625 array.reset(); |
| 635 std::vector<CopyableType*> value_ptrs; | 626 std::vector<CopyableType*> value_ptrs; |
| 636 size_t capacity = array.storage().capacity(); | 627 size_t capacity = array.storage().capacity(); |
| 637 for (size_t i = 0; i < capacity; i++) { | 628 for (size_t i = 0; i < capacity; i++) { |
| 638 CopyableType value; | 629 CopyableType value; |
| 639 value_ptrs.push_back(value.ptr()); | 630 value_ptrs.push_back(value.ptr()); |
| 640 array.push_back(value); | 631 array.push_back(value); |
| 641 ASSERT_EQ(i + 1, array.size()); | 632 ASSERT_EQ(i + 1, array.size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 656 EXPECT_EQ(array.size(), CopyableType::num_instances()); | 647 EXPECT_EQ(array.size(), CopyableType::num_instances()); |
| 657 | 648 |
| 658 for (size_t i = 0; i < array.size(); i++) { | 649 for (size_t i = 0; i < array.size(); i++) { |
| 659 EXPECT_TRUE(array[i].copied()); | 650 EXPECT_TRUE(array[i].copied()); |
| 660 EXPECT_EQ(value_ptrs[i], array[i].ptr()); | 651 EXPECT_EQ(value_ptrs[i], array[i].ptr()); |
| 661 } | 652 } |
| 662 array.reset(); | 653 array.reset(); |
| 663 EXPECT_EQ(0u, CopyableType::num_instances()); | 654 EXPECT_EQ(0u, CopyableType::num_instances()); |
| 664 } | 655 } |
| 665 | 656 |
| 666 TEST_F(ArrayTest, PushBack_MoveOnly) { | 657 TEST(ArrayTest, PushBack_MoveOnly) { |
| 667 ASSERT_EQ(0u, MoveOnlyType::num_instances()); | 658 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
| 668 auto array = mojo::Array<MoveOnlyType>::New(2); | 659 auto array = mojo::Array<MoveOnlyType>::New(2); |
| 669 array.reset(); | 660 array.reset(); |
| 670 std::vector<MoveOnlyType*> value_ptrs; | 661 std::vector<MoveOnlyType*> value_ptrs; |
| 671 size_t capacity = array.storage().capacity(); | 662 size_t capacity = array.storage().capacity(); |
| 672 for (size_t i = 0; i < capacity; i++) { | 663 for (size_t i = 0; i < capacity; i++) { |
| 673 MoveOnlyType value; | 664 MoveOnlyType value; |
| 674 value_ptrs.push_back(value.ptr()); | 665 value_ptrs.push_back(value.ptr()); |
| 675 array.push_back(value.Pass()); | 666 array.push_back(value.Pass()); |
| 676 ASSERT_EQ(i + 1, array.size()); | 667 ASSERT_EQ(i + 1, array.size()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 691 EXPECT_EQ(array.size(), MoveOnlyType::num_instances()); | 682 EXPECT_EQ(array.size(), MoveOnlyType::num_instances()); |
| 692 | 683 |
| 693 for (size_t i = 0; i < array.size(); i++) { | 684 for (size_t i = 0; i < array.size(); i++) { |
| 694 EXPECT_TRUE(array[i].moved()); | 685 EXPECT_TRUE(array[i].moved()); |
| 695 EXPECT_EQ(value_ptrs[i], array[i].ptr()); | 686 EXPECT_EQ(value_ptrs[i], array[i].ptr()); |
| 696 } | 687 } |
| 697 array.reset(); | 688 array.reset(); |
| 698 EXPECT_EQ(0u, MoveOnlyType::num_instances()); | 689 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
| 699 } | 690 } |
| 700 | 691 |
| 701 TEST_F(ArrayTest, Iterator) { | 692 TEST(ArrayTest, Iterator) { |
| 702 std::vector<int> values; | 693 std::vector<int> values; |
| 703 values.push_back(0); | 694 values.push_back(0); |
| 704 values.push_back(1); | 695 values.push_back(1); |
| 705 values.push_back(2); | 696 values.push_back(2); |
| 706 values.push_back(3); | 697 values.push_back(3); |
| 707 Array<int> arr = Array<int>::From(values); | 698 Array<int> arr = Array<int>::From(values); |
| 708 | 699 |
| 709 // Test RandomAcessIterator traits. | 700 // Test RandomAcessIterator traits. |
| 710 { | 701 { |
| 711 // Test +,-,+=,-=. | 702 // Test +,-,+=,-=. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 741 } | 732 } |
| 742 | 733 |
| 743 { | 734 { |
| 744 SCOPED_TRACE("Array iterator bidirectionality test."); | 735 SCOPED_TRACE("Array iterator bidirectionality test."); |
| 745 ExpectBidiIteratorConcept(arr.begin(), arr.end(), values); | 736 ExpectBidiIteratorConcept(arr.begin(), arr.end(), values); |
| 746 ExpectBidiMutableIteratorConcept(arr.begin(), arr.end(), values); | 737 ExpectBidiMutableIteratorConcept(arr.begin(), arr.end(), values); |
| 747 } | 738 } |
| 748 } | 739 } |
| 749 | 740 |
| 750 // Test serializing and deserializing of an array with null elements. | 741 // Test serializing and deserializing of an array with null elements. |
| 751 TEST_F(ArrayTest, Serialization_ArrayOfStructPtr) { | 742 TEST(ArrayTest, Serialization_ArrayOfStructPtr) { |
| 752 ArrayValidateParams validate_nullable(2, true, nullptr); | 743 ArrayValidateParams validate_nullable(2, true, nullptr); |
| 753 ArrayValidateParams validate_non_nullable(2, false, nullptr); | 744 ArrayValidateParams validate_non_nullable(2, false, nullptr); |
| 754 | 745 |
| 755 Array<RectPtr> array = Array<RectPtr>::New(2); | 746 Array<RectPtr> array = Array<RectPtr>::New(2); |
| 756 array[1] = Rect::New(); | 747 array[1] = Rect::New(); |
| 757 array[1]->x = 1; | 748 array[1]->x = 1; |
| 758 array[1]->y = 2; | 749 array[1]->y = 2; |
| 759 array[1]->width = 3; | 750 array[1]->width = 3; |
| 760 array[1]->height = 4; | 751 array[1]->height = 4; |
| 761 | 752 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 EXPECT_EQ(1, array3[1]->x); | 815 EXPECT_EQ(1, array3[1]->x); |
| 825 EXPECT_EQ(2, array3[1]->y); | 816 EXPECT_EQ(2, array3[1]->y); |
| 826 EXPECT_EQ(3, array3[1]->width); | 817 EXPECT_EQ(3, array3[1]->width); |
| 827 EXPECT_EQ(4, array3[1]->height); | 818 EXPECT_EQ(4, array3[1]->height); |
| 828 } | 819 } |
| 829 } | 820 } |
| 830 | 821 |
| 831 } // namespace | 822 } // namespace |
| 832 } // namespace test | 823 } // namespace test |
| 833 } // namespace mojo | 824 } // namespace mojo |
| OLD | NEW |