Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Unified Diff: mojo/public/cpp/bindings/tests/array_unittest.cc

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix windows bustage Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/tests/array_unittest.cc
diff --git a/mojo/public/cpp/bindings/tests/array_unittest.cc b/mojo/public/cpp/bindings/tests/array_unittest.cc
index 4bef4cd57001916be97b21a5f12540a47f7578aa..b0c1f6b65347228c9ef5a9e9974ad56b1da6d7c6 100644
--- a/mojo/public/cpp/bindings/tests/array_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/array_unittest.cc
@@ -2,10 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/cpp/bindings/array.h"
+#include "mojo/public/cpp/bindings/lib/array_serialization.h"
#include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
-#include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -16,97 +15,57 @@ namespace {
// Tests that basic Array operations work.
TEST(ArrayTest, Basic) {
- Environment env;
-
- // 8 bytes for the array, with 8 bytes left over for elements.
- internal::FixedBuffer buf(8 + 8*sizeof(char));
- EXPECT_EQ(16u, buf.size());
-
- Array<char>::Builder builder(8);
- EXPECT_EQ(8u, builder.size());
- for (size_t i = 0; i < builder.size(); ++i) {
- char val = static_cast<char>(i*2);
- builder[i] = val;
- EXPECT_EQ(val, builder.at(i));
- }
- Array<char> array = builder.Finish();
+ Array<char> array(8);
for (size_t i = 0; i < array.size(); ++i) {
- char val = static_cast<char>(i) * 2;
- EXPECT_EQ(val, array[i]);
+ char val = static_cast<char>(i*2);
+ array[i] = val;
+ EXPECT_EQ(val, array.at(i));
}
}
-// Tests that basic Array<bool> operations work, and that it's packed into 1
-// bit per element.
+// Tests that basic Array<bool> operations work.
TEST(ArrayTest, Bool) {
- Environment env;
-
- // 8 bytes for the array header, with 8 bytes left over for elements.
- internal::FixedBuffer buf(8 + 3);
- EXPECT_EQ(16u, buf.size());
-
- // An array of 64 bools can fit into 8 bytes.
- Array<bool>::Builder builder(64);
- EXPECT_EQ(64u, builder.size());
- for (size_t i = 0; i < builder.size(); ++i) {
- bool val = i % 3 == 0;
- builder[i] = val;
- EXPECT_EQ(val, builder.at(i));
- }
- Array<bool> array = builder.Finish();
+ Array<bool> array(64);
for (size_t i = 0; i < array.size(); ++i) {
bool val = i % 3 == 0;
- EXPECT_EQ(val, array[i]);
+ array[i] = val;
+ EXPECT_EQ(val, array.at(i));
}
}
-// Tests that Array<Handle> supports transferring handles.
+// Tests that Array<ScopedMessagePipeHandle> supports transferring handles.
TEST(ArrayTest, Handle) {
- Environment env;
-
- AllocationScope scope;
-
ScopedMessagePipeHandle pipe0, pipe1;
CreateMessagePipe(&pipe0, &pipe1);
- Array<MessagePipeHandle>::Builder handles_builder(2);
- handles_builder[0] = pipe0.Pass();
- handles_builder[1].reset(pipe1.release());
+ Array<ScopedMessagePipeHandle> handles(2);
+ handles[0] = pipe0.Pass();
+ handles[1].reset(pipe1.release());
EXPECT_FALSE(pipe0.is_valid());
EXPECT_FALSE(pipe1.is_valid());
- Array<MessagePipeHandle> handles = handles_builder.Finish();
- EXPECT_TRUE(handles[0].is_valid());
- EXPECT_TRUE(handles[1].is_valid());
+ Array<ScopedMessagePipeHandle> handles2 = handles.Pass();
+ EXPECT_TRUE(handles2[0].is_valid());
+ EXPECT_TRUE(handles2[1].is_valid());
- pipe0 = handles[0].Pass();
+ pipe0 = handles2[0].Pass();
EXPECT_TRUE(pipe0.is_valid());
- EXPECT_FALSE(handles[0].is_valid());
+ EXPECT_FALSE(handles2[0].is_valid());
}
-// Tests that Array<Handle> supports closing handles.
+// Tests that Array<ScopedMessagePipeHandle> supports closing handles.
TEST(ArrayTest, HandlesAreClosed) {
- Environment env;
-
- ScopedMessagePipeHandle msg_pipe0, msg_pipe1;
- CreateMessagePipe(&msg_pipe0, &msg_pipe1);
-
- ScopedHandle pipe0 = ScopedHandle::From(msg_pipe0.Pass());
- ScopedHandle pipe1 = ScopedHandle::From(msg_pipe1.Pass());
+ ScopedMessagePipeHandle pipe0, pipe1;
+ CreateMessagePipe(&pipe0, &pipe1);
MojoHandle pipe0_value = pipe0.get().value();
MojoHandle pipe1_value = pipe1.get().value();
{
- AllocationScope scope;
-
- Array<Handle>::Builder handles_builder(2);
- handles_builder[0] = pipe0.Pass();
- handles_builder[1].reset(pipe1.release());
-
- MOJO_ALLOW_UNUSED Array<Handle> handles =
- handles_builder.Finish();
+ Array<ScopedMessagePipeHandle> handles(2);
+ handles[0] = pipe0.Pass();
+ handles[1].reset(pipe1.release());
}
// We expect the pipes to have been closed.
@@ -114,30 +73,100 @@ TEST(ArrayTest, HandlesAreClosed) {
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value));
}
-// Tests that Array<MessagePipeHandle> supports closing handles.
-TEST(ArrayTest, MessagePipeHandlesAreClosed) {
- Environment env;
+TEST(ArrayTest, Serialization_ArrayOfPOD) {
+ Array<int32_t> array(4);
+ for (size_t i = 0; i < array.size(); ++i)
+ array[i] = static_cast<int32_t>(i);
- ScopedMessagePipeHandle pipe0, pipe1;
- CreateMessagePipe(&pipe0, &pipe1);
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(8U + 4*4U, size);
- MojoHandle pipe0_value = pipe0.get().value();
- MojoHandle pipe1_value = pipe1.get().value();
+ internal::FixedBuffer buf(size);
+ internal::Array_Data<int32_t>* data;
+ Serialize_(array.Pass(), &buf, &data);
- {
- AllocationScope scope;
+ Array<int32_t> array2;
+ Deserialize_(data, &array2);
- Array<MessagePipeHandle>::Builder handles_builder(2);
- handles_builder[0] = pipe0.Pass();
- handles_builder[1].reset(pipe1.release());
+ EXPECT_EQ(4U, array2.size());
+ for (size_t i = 0; i < array2.size(); ++i)
+ EXPECT_EQ(static_cast<int32_t>(i), array2[i]);
+}
- MOJO_ALLOW_UNUSED Array<MessagePipeHandle> handles =
- handles_builder.Finish();
+TEST(ArrayTest, Serialization_ArrayOfArrayOfPOD) {
+ Array<Array<int32_t> > array(2);
+ for (size_t j = 0; j < array.size(); ++j) {
+ Array<int32_t> inner(4);
+ for (size_t i = 0; i < inner.size(); ++i)
+ inner[i] = static_cast<int32_t>(i + (j * 10));
+ array[j] = inner.Pass();
}
- // We expect the pipes to have been closed.
- EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe0_value));
- EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(pipe1_value));
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(8U + 2*8U + 2*(8U + 4*4U), size);
+
+ internal::FixedBuffer buf(size);
+ internal::Array_Data<internal::Array_Data<int32_t>*>* data;
+ Serialize_(array.Pass(), &buf, &data);
+
+ Array<Array<int32_t> > array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(2U, array2.size());
+ for (size_t j = 0; j < array2.size(); ++j) {
+ const Array<int32_t>& inner = array2[j];
+ EXPECT_EQ(4U, inner.size());
+ for (size_t i = 0; i < inner.size(); ++i)
+ EXPECT_EQ(static_cast<int32_t>(i + (j * 10)), inner[i]);
+ }
+}
+
+TEST(ArrayTest, Serialization_ArrayOfBool) {
+ Array<bool> array(10);
+ for (size_t i = 0; i < array.size(); ++i)
+ array[i] = i % 2 ? true : false;
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(8U + 8U, size);
+
+ internal::FixedBuffer buf(size);
+ internal::Array_Data<bool>* data;
+ Serialize_(array.Pass(), &buf, &data);
+
+ Array<bool> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(10U, array2.size());
+ for (size_t i = 0; i < array2.size(); ++i)
+ EXPECT_EQ(i % 2 ? true : false, array2[i]);
+}
+
+TEST(ArrayTest, Serialization_ArrayOfString) {
+ Array<String> array(10);
+ for (size_t i = 0; i < array.size(); ++i) {
+ char c = 'A' + 1;
+ array[i] = String(&c, 1);
+ }
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(8U + // array header
+ 10*8U + // array payload (10 pointers)
+ 10*(8U + // string header
+ 8U), // string length of 1 padded to 8
+ size);
+
+ internal::FixedBuffer buf(size);
+ internal::Array_Data<internal::String_Data*>* data;
+ Serialize_(array.Pass(), &buf, &data);
+
+ Array<String> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(10U, array2.size());
+ for (size_t i = 0; i < array2.size(); ++i) {
+ char c = 'A' + 1;
+ EXPECT_EQ(String(&c, 1), array2[i]);
+ }
}
} // namespace

Powered by Google App Engine
This is Rietveld 408576698