| Index: runtime/vm/snapshot_test.cc
|
| diff --git a/runtime/vm/snapshot_test.cc b/runtime/vm/snapshot_test.cc
|
| index bb8fe8f453b77df7d54c81a3a0224886e960173a..c766a53bcded1532292eeb3bdcf962b52f90749b 100644
|
| --- a/runtime/vm/snapshot_test.cc
|
| +++ b/runtime/vm/snapshot_test.cc
|
| @@ -60,6 +60,62 @@ static Dart_CMessage* DecodeMessage(uint8_t* message,
|
| }
|
|
|
|
|
| +// Compare two Dart_CObject object graphs rooted in first and
|
| +// second. The second graph will be destroyed by this operation no matter
|
| +// whether the graphs are equal or not.
|
| +static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) {
|
| + // Return immediately if entering a cycle.
|
| + if (second->type == Dart_CObject::kNumberOfTypes) return;
|
| +
|
| + EXPECT_NE(first, second);
|
| + EXPECT_EQ(first->type, second->type);
|
| + switch (first->type) {
|
| + case Dart_CObject::kNull:
|
| + // Nothing more to compare.
|
| + break;
|
| + case Dart_CObject::kBool:
|
| + EXPECT_EQ(first->value.as_bool, second->value.as_bool);
|
| + break;
|
| + case Dart_CObject::kInt32:
|
| + EXPECT_EQ(first->value.as_int32, second->value.as_int32);
|
| + break;
|
| + case Dart_CObject::kDouble:
|
| + EXPECT_EQ(first->value.as_double, second->value.as_double);
|
| + break;
|
| + case Dart_CObject::kString:
|
| + EXPECT_STREQ(first->value.as_string, second->value.as_string);
|
| + break;
|
| + case Dart_CObject::kArray:
|
| + // Use invalid type as a visited marker to avoid infinite
|
| + // recursion on graphs with cycles.
|
| + second->type = Dart_CObject::kNumberOfTypes;
|
| + EXPECT_EQ(first->value.as_array.length, second->value.as_array.length);
|
| + for (int i = 0; i < first->value.as_array.length; i++) {
|
| + CompareDartCObjects(first->value.as_array.values[i],
|
| + second->value.as_array.values[i]);
|
| + }
|
| + break;
|
| + default:
|
| + EXPECT(false);
|
| + }
|
| +}
|
| +
|
| +
|
| +static void CheckEncodeDecodeMessage(Dart_CMessage* cmessage) {
|
| + // Encode and decode the message.
|
| + uint8_t* buffer = NULL;
|
| + MessageWriter writer(&buffer, &malloc_allocator);
|
| + writer.WriteCMessage(cmessage->root);
|
| +
|
| + Zone zone(Isolate::Current());
|
| + Dart_CMessage* new_cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize,
|
| + writer.BytesWritten(),
|
| + &zone_allocator);
|
| +
|
| + // Check that the two messages are the same.
|
| + CompareDartCObjects(cmessage->root, new_cmessage->root);
|
| +}
|
| +
|
| TEST_CASE(SerializeNull) {
|
| Zone zone(Isolate::Current());
|
|
|
| @@ -85,6 +141,7 @@ TEST_CASE(SerializeNull) {
|
| Dart_CObject* cobject = cmessage->root;
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kNull, cobject->type);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -114,6 +171,7 @@ TEST_CASE(SerializeSmi1) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kInt32, cobject->type);
|
| EXPECT_EQ(smi.Value(), cobject->value.as_int32);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -143,6 +201,7 @@ TEST_CASE(SerializeSmi2) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kInt32, cobject->type);
|
| EXPECT_EQ(smi.Value(), cobject->value.as_int32);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -172,6 +231,7 @@ TEST_CASE(SerializeDouble) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kDouble, cobject->type);
|
| EXPECT_EQ(dbl.value(), cobject->value.as_double);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -218,6 +278,7 @@ TEST_CASE(SerializeTrue) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kBool, cobject->type);
|
| EXPECT_EQ(true, cobject->value.as_bool);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -242,6 +303,7 @@ TEST_CASE(SerializeFalse) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kBool, cobject->type);
|
| EXPECT_EQ(false, cobject->value.as_bool);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -356,6 +418,7 @@ TEST_CASE(SerializeString) {
|
| Dart_CObject* cobject = cmessage->root;
|
| EXPECT_EQ(Dart_CObject::kString, cobject->type);
|
| EXPECT_STREQ(cstr, cobject->value.as_string);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -396,6 +459,7 @@ TEST_CASE(SerializeArray) {
|
| EXPECT_EQ(Dart_CObject::kInt32, element->type);
|
| EXPECT_EQ(i, element->value.as_int32);
|
| }
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -427,6 +491,7 @@ TEST_CASE(SerializeEmptyArray) {
|
| EXPECT_EQ(Dart_CObject::kArray, cobject->type);
|
| EXPECT_EQ(kArrayLength, cobject->value.as_array.length);
|
| EXPECT(cobject->value.as_array.values == NULL);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -759,6 +824,7 @@ TEST_CASE(IntArrayMessage) {
|
| EXPECT_EQ(Dart_CObject::kInt32, element->type);
|
| EXPECT_EQ(i + 1, element->value.as_int32);
|
| }
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
|
|
|
|
| @@ -845,6 +911,7 @@ UNIT_TEST_CASE(DartGeneratedMessages) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kInt32, cobject->type);
|
| EXPECT_EQ(42, cobject->value.as_int32);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
| {
|
| Zone zone(Isolate::Current());
|
| @@ -863,6 +930,7 @@ UNIT_TEST_CASE(DartGeneratedMessages) {
|
| EXPECT_NOTNULL(cobject);
|
| EXPECT_EQ(Dart_CObject::kString, cobject->type);
|
| EXPECT_STREQ("Hello, world!", cobject->value.as_string);
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
| }
|
| Dart_ExitScope();
|
| @@ -906,7 +974,6 @@ UNIT_TEST_CASE(DartGeneratedListMessages) {
|
|
|
| {
|
| DARTSCOPE_NOCHECKS(isolate);
|
| -
|
| {
|
| // Generate a list of nulls from Dart code.
|
| Zone zone(Isolate::Current());
|
| @@ -918,6 +985,7 @@ UNIT_TEST_CASE(DartGeneratedListMessages) {
|
| for (int i = 0; i < kArrayLength; i++) {
|
| EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type);
|
| }
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
| {
|
| // Generate a list of ints from Dart code.
|
| @@ -931,6 +999,7 @@ UNIT_TEST_CASE(DartGeneratedListMessages) {
|
| EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type);
|
| EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32);
|
| }
|
| + CheckEncodeDecodeMessage(cmessage);
|
| }
|
| {
|
| // Generate a list of strings from Dart code.
|
| @@ -1093,6 +1162,81 @@ UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) {
|
| Dart_ShutdownIsolate();
|
| }
|
|
|
| +
|
| +UNIT_TEST_CASE(PostCMessage) {
|
| + // Create a native port for posting from C to Dart
|
| + TestIsolateScope __test_isolate__;
|
| + const char* kScriptChars =
|
| + "void main() {\n"
|
| + " var messageCount = 0;\n"
|
| + " var exception = '';\n"
|
| + " var port = new ReceivePort();\n"
|
| + " port.receive((message, replyTo) {\n"
|
| + " exception += message.toString();\n"
|
| + " messageCount++;\n"
|
| + " if (messageCount == 7) throw new Exception(exception);\n"
|
| + " });\n"
|
| + " return port.toSendPort();\n"
|
| + "}\n";
|
| + Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
|
| + Dart_EnterScope();
|
| +
|
| + // xxx
|
| + Dart_Handle send_port = Dart_InvokeStatic(lib,
|
| + Dart_NewString(""),
|
| + Dart_NewString("main"),
|
| + 0,
|
| + NULL);
|
| + EXPECT_VALID(send_port);
|
| + Dart_Handle result =
|
| + Dart_GetInstanceField(send_port, Dart_NewString("_id"));
|
| + ASSERT(!Dart_IsError(result));
|
| + ASSERT(Dart_IsInteger(result));
|
| + int64_t send_port_id;
|
| + Dart_Handle result2 = Dart_IntegerToInt64(result, &send_port_id);
|
| + ASSERT(!Dart_IsError(result2));
|
| +
|
| + // Setup single object message.
|
| + Dart_CObject object;
|
| + Dart_CMessage message;
|
| + message.root = &object;
|
| +
|
| + object.type = Dart_CObject::kNull;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kBool;
|
| + object.value.as_bool = true;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kBool;
|
| + object.value.as_bool = false;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kInt32;
|
| + object.value.as_int32 = 123;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kString;
|
| + object.value.as_string = const_cast<char*>("456");
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kDouble;
|
| + object.value.as_double = 3.14;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + object.type = Dart_CObject::kArray;
|
| + object.value.as_array.length = 0;
|
| + EXPECT(Dart_PostCMessage(send_port_id, &message));
|
| +
|
| + result = Dart_RunLoop();
|
| + EXPECT(Dart_IsError(result));
|
| + EXPECT(Dart_ErrorHasException(result));
|
| + EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
|
| + Dart_GetError(result));
|
| +
|
| + Dart_ExitScope();
|
| +}
|
| +
|
| #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
|
|
|
| } // namespace dart
|
|
|