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

Side by Side Diff: runtime/vm/snapshot_test.cc

Issue 9348019: Add support for byte arrays to native messages (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 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 unified diff | Download patch | Annotate | Revision Log
« runtime/vm/dart_api_message.cc ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_debugger_api.h" 5 #include "include/dart_debugger_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_message.h" 10 #include "vm/dart_api_message.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 break; 79 break;
80 case Dart_CObject::kInt32: 80 case Dart_CObject::kInt32:
81 EXPECT_EQ(first->value.as_int32, second->value.as_int32); 81 EXPECT_EQ(first->value.as_int32, second->value.as_int32);
82 break; 82 break;
83 case Dart_CObject::kDouble: 83 case Dart_CObject::kDouble:
84 EXPECT_EQ(first->value.as_double, second->value.as_double); 84 EXPECT_EQ(first->value.as_double, second->value.as_double);
85 break; 85 break;
86 case Dart_CObject::kString: 86 case Dart_CObject::kString:
87 EXPECT_STREQ(first->value.as_string, second->value.as_string); 87 EXPECT_STREQ(first->value.as_string, second->value.as_string);
88 break; 88 break;
89 case Dart_CObject::kByteArray:
90 EXPECT_EQ(first->value.as_byte_array.length,
91 second->value.as_byte_array.length);
92 for (int i = 0; i < first->value.as_byte_array.length; i++) {
93 EXPECT_EQ(first->value.as_byte_array.values[i],
94 second->value.as_byte_array.values[i]);
95 }
96 break;
89 case Dart_CObject::kArray: 97 case Dart_CObject::kArray:
90 // Use invalid type as a visited marker to avoid infinite 98 // Use invalid type as a visited marker to avoid infinite
91 // recursion on graphs with cycles. 99 // recursion on graphs with cycles.
92 second->type = Dart_CObject::kNumberOfTypes; 100 second->type = Dart_CObject::kNumberOfTypes;
93 EXPECT_EQ(first->value.as_array.length, second->value.as_array.length); 101 EXPECT_EQ(first->value.as_array.length, second->value.as_array.length);
94 for (int i = 0; i < first->value.as_array.length; i++) { 102 for (int i = 0; i < first->value.as_array.length; i++) {
95 CompareDartCObjects(first->value.as_array.values[i], 103 CompareDartCObjects(first->value.as_array.values[i],
96 second->value.as_array.values[i]); 104 second->value.as_array.values[i]);
97 } 105 }
98 break; 106 break;
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 487 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
480 writer.BytesWritten(), 488 writer.BytesWritten(),
481 &zone_allocator); 489 &zone_allocator);
482 EXPECT_EQ(Dart_CObject::kArray, root->type); 490 EXPECT_EQ(Dart_CObject::kArray, root->type);
483 EXPECT_EQ(kArrayLength, root->value.as_array.length); 491 EXPECT_EQ(kArrayLength, root->value.as_array.length);
484 EXPECT(root->value.as_array.values == NULL); 492 EXPECT(root->value.as_array.values == NULL);
485 CheckEncodeDecodeMessage(root); 493 CheckEncodeDecodeMessage(root);
486 } 494 }
487 495
488 496
497 TEST_CASE(SerializeByteArray) {
498 Zone zone(Isolate::Current());
499
500 // Write snapshot with object content.
501 uint8_t* buffer;
502 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
503 const int kByteArrayLength = 256;
504 InternalByteArray& byte_array =
505 InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
506 for (int i = 0; i < kByteArrayLength; i++) {
507 byte_array.SetAt<uint8_t>(i, i);
508 }
509 writer.WriteObject(byte_array.raw());
510 writer.FinalizeBuffer();
511
512 // Create a snapshot object using the buffer.
513 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
514
515 // Read object back from the snapshot.
516 SnapshotReader reader(snapshot, Isolate::Current());
517 ByteArray& serialized_byte_array = ByteArray::Handle();
518 serialized_byte_array ^= reader.ReadObject();
519 EXPECT(serialized_byte_array.IsByteArray());
520
521 // Read object back from the snapshot into a C structure.
522 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
523 writer.BytesWritten(),
524 &zone_allocator);
525 EXPECT_EQ(Dart_CObject::kByteArray, root->type);
526 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
527 for (int i = 0; i < kByteArrayLength; i++) {
528 EXPECT(root->value.as_byte_array.values[i] == i);
529 }
530 CheckEncodeDecodeMessage(root);
531 }
532
533
534 TEST_CASE(SerializeEmptyByteArray) {
535 Zone zone(Isolate::Current());
536
537 // Write snapshot with object content.
538 uint8_t* buffer;
539 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
540 const int kByteArrayLength = 0;
541 InternalByteArray& byte_array =
542 InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
543 writer.WriteObject(byte_array.raw());
544 writer.FinalizeBuffer();
545
546 // Create a snapshot object using the buffer.
547 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
548
549 // Read object back from the snapshot.
550 SnapshotReader reader(snapshot, Isolate::Current());
551 ByteArray& serialized_byte_array = ByteArray::Handle();
552 serialized_byte_array ^= reader.ReadObject();
553 EXPECT(serialized_byte_array.IsByteArray());
554
555 // Read object back from the snapshot into a C structure.
556 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
557 writer.BytesWritten(),
558 &zone_allocator);
559 EXPECT_EQ(Dart_CObject::kByteArray, root->type);
560 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
561 EXPECT(root->value.as_byte_array.values == NULL);
562 CheckEncodeDecodeMessage(root);
563 }
564
565
489 TEST_CASE(SerializeScript) { 566 TEST_CASE(SerializeScript) {
490 const char* kScriptChars = 567 const char* kScriptChars =
491 "class A {\n" 568 "class A {\n"
492 " static bar() { return 42; }\n" 569 " static bar() { return 42; }\n"
493 " static fly() { return 5; }\n" 570 " static fly() { return 5; }\n"
494 "}\n"; 571 "}\n";
495 572
496 String& url = String::Handle(String::New("dart-test:SerializeScript")); 573 String& url = String::Handle(String::New("dart-test:SerializeScript"));
497 String& source = String::Handle(String::New(kScriptChars)); 574 String& source = String::Handle(String::New(kScriptChars));
498 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); 575 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource));
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
1038 " var list = new List<String>(kArrayLength);\n" 1115 " var list = new List<String>(kArrayLength);\n"
1039 " for (var i = 0; i < kArrayLength; i++) list[i] = s;\n" 1116 " for (var i = 0; i < kArrayLength; i++) list[i] = s;\n"
1040 " return list;\n" 1117 " return list;\n"
1041 "}\n" 1118 "}\n"
1042 "getDoubleList() {\n" 1119 "getDoubleList() {\n"
1043 " var d = 3.14;\n" 1120 " var d = 3.14;\n"
1044 " var list = new List<double>(kArrayLength);\n" 1121 " var list = new List<double>(kArrayLength);\n"
1045 " for (var i = 0; i < kArrayLength; i++) list[i] = d;\n" 1122 " for (var i = 0; i < kArrayLength; i++) list[i] = d;\n"
1046 " return list;\n" 1123 " return list;\n"
1047 "}\n" 1124 "}\n"
1125 "getByteArrayList() {\n"
1126 " var byte_array = new ByteArray(256);\n"
1127 " var list = new List<ByteArray>(kArrayLength);\n"
1128 " for (var i = 0; i < kArrayLength; i++) list[i] = byte_array;\n"
1129 " return list;\n"
1130 "}\n"
1048 "getMixedList() {\n" 1131 "getMixedList() {\n"
1049 " var list = new List(kArrayLength);\n" 1132 " var list = new List(kArrayLength);\n"
1050 " for (var i = 0; i < kArrayLength; i++) {\n" 1133 " for (var i = 0; i < kArrayLength; i++) {\n"
1051 " list[i] = ((i % 2) == 0) ? 'A' : 2.72;\n" 1134 " list[i] = ((i % 2) == 0) ? 'A' : 2.72;\n"
1052 " }\n" 1135 " }\n"
1053 " return list;\n" 1136 " return list;\n"
1054 "}\n" 1137 "}\n"
1055 "getSelfRefList() {\n" 1138 "getSelfRefList() {\n"
1056 " var list = new List(kArrayLength);\n" 1139 " var list = new List(kArrayLength);\n"
1057 " for (var i = 0; i < kArrayLength; i++) {\n" 1140 " for (var i = 0; i < kArrayLength; i++) {\n"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 EXPECT_EQ(Dart_CObject::kArray, root->type); 1176 EXPECT_EQ(Dart_CObject::kArray, root->type);
1094 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1177 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1095 for (int i = 0; i < kArrayLength; i++) { 1178 for (int i = 0; i < kArrayLength; i++) {
1096 Dart_CObject* element = root->value.as_array.values[i]; 1179 Dart_CObject* element = root->value.as_array.values[i];
1097 EXPECT_EQ(root->value.as_array.values[0], element); 1180 EXPECT_EQ(root->value.as_array.values[0], element);
1098 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1181 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1099 EXPECT_EQ(3.14, element->value.as_double); 1182 EXPECT_EQ(3.14, element->value.as_double);
1100 } 1183 }
1101 } 1184 }
1102 { 1185 {
1186 // Generate a list of doubles from Dart code.
1187 Zone zone(Isolate::Current());
1188 Dart_CObject* root = GetDeserializedDartMessage(lib, "getByteArrayList");
1189 EXPECT_NOTNULL(root);
1190 EXPECT_EQ(Dart_CObject::kArray, root->type);
1191 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1192 for (int i = 0; i < kArrayLength; i++) {
1193 Dart_CObject* element = root->value.as_array.values[i];
1194 EXPECT_EQ(root->value.as_array.values[0], element);
1195 EXPECT_EQ(Dart_CObject::kByteArray, element->type);
1196 EXPECT_EQ(256, element->value.as_byte_array.length);
1197 }
1198 }
1199 {
1103 // Generate a list of objects of different types from Dart code. 1200 // Generate a list of objects of different types from Dart code.
1104 Zone zone(Isolate::Current()); 1201 Zone zone(Isolate::Current());
1105 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList"); 1202 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
1106 EXPECT_NOTNULL(root); 1203 EXPECT_NOTNULL(root);
1107 EXPECT_EQ(Dart_CObject::kArray, root->type); 1204 EXPECT_EQ(Dart_CObject::kArray, root->type);
1108 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1205 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1109 for (int i = 0; i < kArrayLength; i++) { 1206 for (int i = 0; i < kArrayLength; i++) {
1110 Dart_CObject* element = root->value.as_array.values[i]; 1207 Dart_CObject* element = root->value.as_array.values[i];
1111 if ((i % 2) == 0) { 1208 if ((i % 2) == 0) {
1112 EXPECT_EQ(root->value.as_array.values[0], element); 1209 EXPECT_EQ(root->value.as_array.values[0], element);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 EXPECT(Dart_ErrorHasException(result)); 1303 EXPECT(Dart_ErrorHasException(result));
1207 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", 1304 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
1208 Dart_GetError(result)); 1305 Dart_GetError(result));
1209 1306
1210 Dart_ExitScope(); 1307 Dart_ExitScope();
1211 } 1308 }
1212 1309
1213 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1310 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1214 1311
1215 } // namespace dart 1312 } // namespace dart
OLDNEW
« runtime/vm/dart_api_message.cc ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698