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

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: Addressed review comments from asiva@ 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
« no previous file with comments | « 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 496 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
489 writer.BytesWritten(), 497 writer.BytesWritten(),
490 &zone_allocator); 498 &zone_allocator);
491 EXPECT_EQ(Dart_CObject::kArray, root->type); 499 EXPECT_EQ(Dart_CObject::kArray, root->type);
492 EXPECT_EQ(kArrayLength, root->value.as_array.length); 500 EXPECT_EQ(kArrayLength, root->value.as_array.length);
493 EXPECT(root->value.as_array.values == NULL); 501 EXPECT(root->value.as_array.values == NULL);
494 CheckEncodeDecodeMessage(root); 502 CheckEncodeDecodeMessage(root);
495 } 503 }
496 504
497 505
506 TEST_CASE(SerializeByteArray) {
507 Zone zone(Isolate::Current());
508
509 // Write snapshot with object content.
510 uint8_t* buffer;
511 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
512 const int kByteArrayLength = 256;
513 InternalByteArray& byte_array =
514 InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
515 for (int i = 0; i < kByteArrayLength; i++) {
516 byte_array.SetAt<uint8_t>(i, i);
517 }
518 writer.WriteObject(byte_array.raw());
519 writer.FinalizeBuffer();
520
521 // Create a snapshot object using the buffer.
522 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
523
524 // Read object back from the snapshot.
525 SnapshotReader reader(snapshot, Isolate::Current());
526 ByteArray& serialized_byte_array = ByteArray::Handle();
527 serialized_byte_array ^= reader.ReadObject();
528 EXPECT(serialized_byte_array.IsByteArray());
529
530 // Read object back from the snapshot into a C structure.
531 ApiNativeScope scope;
532 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
533 writer.BytesWritten(),
534 &zone_allocator);
535 EXPECT_EQ(Dart_CObject::kByteArray, root->type);
536 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
537 for (int i = 0; i < kByteArrayLength; i++) {
538 EXPECT(root->value.as_byte_array.values[i] == i);
539 }
540 CheckEncodeDecodeMessage(root);
541 }
542
543
544 TEST_CASE(SerializeEmptyByteArray) {
545 Zone zone(Isolate::Current());
546
547 // Write snapshot with object content.
548 uint8_t* buffer;
549 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
550 const int kByteArrayLength = 0;
551 InternalByteArray& byte_array =
552 InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
553 writer.WriteObject(byte_array.raw());
554 writer.FinalizeBuffer();
555
556 // Create a snapshot object using the buffer.
557 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
558
559 // Read object back from the snapshot.
560 SnapshotReader reader(snapshot, Isolate::Current());
561 ByteArray& serialized_byte_array = ByteArray::Handle();
562 serialized_byte_array ^= reader.ReadObject();
563 EXPECT(serialized_byte_array.IsByteArray());
564
565 // Read object back from the snapshot into a C structure.
566 ApiNativeScope scope;
567 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
568 writer.BytesWritten(),
569 &zone_allocator);
570 EXPECT_EQ(Dart_CObject::kByteArray, root->type);
571 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
572 EXPECT(root->value.as_byte_array.values == NULL);
573 CheckEncodeDecodeMessage(root);
574 }
575
576
498 TEST_CASE(SerializeScript) { 577 TEST_CASE(SerializeScript) {
499 const char* kScriptChars = 578 const char* kScriptChars =
500 "class A {\n" 579 "class A {\n"
501 " static bar() { return 42; }\n" 580 " static bar() { return 42; }\n"
502 " static fly() { return 5; }\n" 581 " static fly() { return 5; }\n"
503 "}\n"; 582 "}\n";
504 583
505 String& url = String::Handle(String::New("dart-test:SerializeScript")); 584 String& url = String::Handle(String::New("dart-test:SerializeScript"));
506 String& source = String::Handle(String::New(kScriptChars)); 585 String& source = String::Handle(String::New(kScriptChars));
507 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); 586 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource));
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
1050 " var list = new List<String>(kArrayLength);\n" 1129 " var list = new List<String>(kArrayLength);\n"
1051 " for (var i = 0; i < kArrayLength; i++) list[i] = s;\n" 1130 " for (var i = 0; i < kArrayLength; i++) list[i] = s;\n"
1052 " return list;\n" 1131 " return list;\n"
1053 "}\n" 1132 "}\n"
1054 "getDoubleList() {\n" 1133 "getDoubleList() {\n"
1055 " var d = 3.14;\n" 1134 " var d = 3.14;\n"
1056 " var list = new List<double>(kArrayLength);\n" 1135 " var list = new List<double>(kArrayLength);\n"
1057 " for (var i = 0; i < kArrayLength; i++) list[i] = d;\n" 1136 " for (var i = 0; i < kArrayLength; i++) list[i] = d;\n"
1058 " return list;\n" 1137 " return list;\n"
1059 "}\n" 1138 "}\n"
1139 "getByteArrayList() {\n"
1140 " var byte_array = new ByteArray(256);\n"
1141 " var list = new List<ByteArray>(kArrayLength);\n"
1142 " for (var i = 0; i < kArrayLength; i++) list[i] = byte_array;\n"
1143 " return list;\n"
1144 "}\n"
1060 "getMixedList() {\n" 1145 "getMixedList() {\n"
1061 " var list = new List(kArrayLength);\n" 1146 " var list = new List(kArrayLength);\n"
1062 " for (var i = 0; i < kArrayLength; i++) {\n" 1147 " for (var i = 0; i < kArrayLength; i++) {\n"
1063 " list[i] = ((i % 2) == 0) ? 'A' : 2.72;\n" 1148 " list[i] = ((i % 2) == 0) ? 'A' : 2.72;\n"
1064 " }\n" 1149 " }\n"
1065 " return list;\n" 1150 " return list;\n"
1066 "}\n" 1151 "}\n"
1067 "getSelfRefList() {\n" 1152 "getSelfRefList() {\n"
1068 " var list = new List(kArrayLength);\n" 1153 " var list = new List(kArrayLength);\n"
1069 " for (var i = 0; i < kArrayLength; i++) {\n" 1154 " for (var i = 0; i < kArrayLength; i++) {\n"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 EXPECT_EQ(Dart_CObject::kArray, root->type); 1190 EXPECT_EQ(Dart_CObject::kArray, root->type);
1106 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1191 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1107 for (int i = 0; i < kArrayLength; i++) { 1192 for (int i = 0; i < kArrayLength; i++) {
1108 Dart_CObject* element = root->value.as_array.values[i]; 1193 Dart_CObject* element = root->value.as_array.values[i];
1109 EXPECT_EQ(root->value.as_array.values[0], element); 1194 EXPECT_EQ(root->value.as_array.values[0], element);
1110 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1195 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1111 EXPECT_EQ(3.14, element->value.as_double); 1196 EXPECT_EQ(3.14, element->value.as_double);
1112 } 1197 }
1113 } 1198 }
1114 { 1199 {
1200 // Generate a list of doubles from Dart code.
1201 ApiNativeScope scope;
1202 Dart_CObject* root = GetDeserializedDartMessage(lib, "getByteArrayList");
1203 EXPECT_NOTNULL(root);
1204 EXPECT_EQ(Dart_CObject::kArray, root->type);
1205 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1206 for (int i = 0; i < kArrayLength; i++) {
1207 Dart_CObject* element = root->value.as_array.values[i];
1208 EXPECT_EQ(root->value.as_array.values[0], element);
1209 EXPECT_EQ(Dart_CObject::kByteArray, element->type);
1210 EXPECT_EQ(256, element->value.as_byte_array.length);
1211 }
1212 }
1213 {
1115 // Generate a list of objects of different types from Dart code. 1214 // Generate a list of objects of different types from Dart code.
1116 ApiNativeScope scope; 1215 ApiNativeScope scope;
1117 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList"); 1216 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
1118 EXPECT_NOTNULL(root); 1217 EXPECT_NOTNULL(root);
1119 EXPECT_EQ(Dart_CObject::kArray, root->type); 1218 EXPECT_EQ(Dart_CObject::kArray, root->type);
1120 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1219 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1121 for (int i = 0; i < kArrayLength; i++) { 1220 for (int i = 0; i < kArrayLength; i++) {
1122 Dart_CObject* element = root->value.as_array.values[i]; 1221 Dart_CObject* element = root->value.as_array.values[i];
1123 if ((i % 2) == 0) { 1222 if ((i % 2) == 0) {
1124 EXPECT_EQ(root->value.as_array.values[0], element); 1223 EXPECT_EQ(root->value.as_array.values[0], element);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 EXPECT(Dart_ErrorHasException(result)); 1317 EXPECT(Dart_ErrorHasException(result));
1219 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", 1318 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
1220 Dart_GetError(result)); 1319 Dart_GetError(result));
1221 1320
1222 Dart_ExitScope(); 1321 Dart_ExitScope();
1223 } 1322 }
1224 1323
1225 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1324 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1226 1325
1227 } // namespace dart 1326 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698