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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/snapshot_test.cc
diff --git a/runtime/vm/snapshot_test.cc b/runtime/vm/snapshot_test.cc
index 6d444bdf84d0df4a81ffe79605ada551959797f3..6a829883b6799a56d4433f38f42c8fb72f078558 100644
--- a/runtime/vm/snapshot_test.cc
+++ b/runtime/vm/snapshot_test.cc
@@ -86,6 +86,14 @@ static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) {
case Dart_CObject::kString:
EXPECT_STREQ(first->value.as_string, second->value.as_string);
break;
+ case Dart_CObject::kByteArray:
+ EXPECT_EQ(first->value.as_byte_array.length,
+ second->value.as_byte_array.length);
+ for (int i = 0; i < first->value.as_byte_array.length; i++) {
+ EXPECT_EQ(first->value.as_byte_array.values[i],
+ second->value.as_byte_array.values[i]);
+ }
+ break;
case Dart_CObject::kArray:
// Use invalid type as a visited marker to avoid infinite
// recursion on graphs with cycles.
@@ -495,6 +503,77 @@ TEST_CASE(SerializeEmptyArray) {
}
+TEST_CASE(SerializeByteArray) {
+ Zone zone(Isolate::Current());
+
+ // Write snapshot with object content.
+ uint8_t* buffer;
+ SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
+ const int kByteArrayLength = 256;
+ InternalByteArray& byte_array =
+ InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
+ for (int i = 0; i < kByteArrayLength; i++) {
+ byte_array.SetAt<uint8_t>(i, i);
+ }
+ writer.WriteObject(byte_array.raw());
+ writer.FinalizeBuffer();
+
+ // Create a snapshot object using the buffer.
+ const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
+
+ // Read object back from the snapshot.
+ SnapshotReader reader(snapshot, Isolate::Current());
+ ByteArray& serialized_byte_array = ByteArray::Handle();
+ serialized_byte_array ^= reader.ReadObject();
+ EXPECT(serialized_byte_array.IsByteArray());
+
+ // Read object back from the snapshot into a C structure.
+ ApiNativeScope scope;
+ Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
+ writer.BytesWritten(),
+ &zone_allocator);
+ EXPECT_EQ(Dart_CObject::kByteArray, root->type);
+ EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
+ for (int i = 0; i < kByteArrayLength; i++) {
+ EXPECT(root->value.as_byte_array.values[i] == i);
+ }
+ CheckEncodeDecodeMessage(root);
+}
+
+
+TEST_CASE(SerializeEmptyByteArray) {
+ Zone zone(Isolate::Current());
+
+ // Write snapshot with object content.
+ uint8_t* buffer;
+ SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
+ const int kByteArrayLength = 0;
+ InternalByteArray& byte_array =
+ InternalByteArray::Handle(InternalByteArray::New(kByteArrayLength));
+ writer.WriteObject(byte_array.raw());
+ writer.FinalizeBuffer();
+
+ // Create a snapshot object using the buffer.
+ const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
+
+ // Read object back from the snapshot.
+ SnapshotReader reader(snapshot, Isolate::Current());
+ ByteArray& serialized_byte_array = ByteArray::Handle();
+ serialized_byte_array ^= reader.ReadObject();
+ EXPECT(serialized_byte_array.IsByteArray());
+
+ // Read object back from the snapshot into a C structure.
+ ApiNativeScope scope;
+ Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
+ writer.BytesWritten(),
+ &zone_allocator);
+ EXPECT_EQ(Dart_CObject::kByteArray, root->type);
+ EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length);
+ EXPECT(root->value.as_byte_array.values == NULL);
+ CheckEncodeDecodeMessage(root);
+}
+
+
TEST_CASE(SerializeScript) {
const char* kScriptChars =
"class A {\n"
@@ -1057,6 +1136,12 @@ UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) {
" for (var i = 0; i < kArrayLength; i++) list[i] = d;\n"
" return list;\n"
"}\n"
+ "getByteArrayList() {\n"
+ " var byte_array = new ByteArray(256);\n"
+ " var list = new List<ByteArray>(kArrayLength);\n"
+ " for (var i = 0; i < kArrayLength; i++) list[i] = byte_array;\n"
+ " return list;\n"
+ "}\n"
"getMixedList() {\n"
" var list = new List(kArrayLength);\n"
" for (var i = 0; i < kArrayLength; i++) {\n"
@@ -1112,6 +1197,20 @@ UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) {
}
}
{
+ // Generate a list of doubles from Dart code.
+ ApiNativeScope scope;
+ Dart_CObject* root = GetDeserializedDartMessage(lib, "getByteArrayList");
+ EXPECT_NOTNULL(root);
+ EXPECT_EQ(Dart_CObject::kArray, root->type);
+ EXPECT_EQ(kArrayLength, root->value.as_array.length);
+ for (int i = 0; i < kArrayLength; i++) {
+ Dart_CObject* element = root->value.as_array.values[i];
+ EXPECT_EQ(root->value.as_array.values[0], element);
+ EXPECT_EQ(Dart_CObject::kByteArray, element->type);
+ EXPECT_EQ(256, element->value.as_byte_array.length);
+ }
+ }
+ {
// Generate a list of objects of different types from Dart code.
ApiNativeScope scope;
Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
« 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