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

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

Issue 9235063: Implementation of message reader for converting a message snapshot into a C structure (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 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/snapshot.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 "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/bigint_operations.h" 6 #include "vm/bigint_operations.h"
7 #include "vm/class_finalizer.h" 7 #include "vm/class_finalizer.h"
8 #include "vm/snapshot.h" 8 #include "vm/snapshot.h"
9 #include "vm/unit_test.h" 9 #include "vm/unit_test.h"
10 10
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 writer.WriteObject(null_object.raw()); 48 writer.WriteObject(null_object.raw());
49 writer.FinalizeBuffer(); 49 writer.FinalizeBuffer();
50 50
51 // Create a snapshot object using the buffer. 51 // Create a snapshot object using the buffer.
52 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 52 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
53 53
54 // Read object back from the snapshot. 54 // Read object back from the snapshot.
55 SnapshotReader reader(snapshot, Isolate::Current()); 55 SnapshotReader reader(snapshot, Isolate::Current());
56 const Object& serialized_object = Object::Handle(reader.ReadObject()); 56 const Object& serialized_object = Object::Handle(reader.ReadObject());
57 EXPECT(Equals(null_object, serialized_object)); 57 EXPECT(Equals(null_object, serialized_object));
58
59 // Read object back from the snapshot into a C structure.
60 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
61 Dart_Value* value = mreader.ReadObject();
62 EXPECT_EQ(Dart_Value::kNull, value->type);
63 free(value);
58 } 64 }
59 65
60 66
61 TEST_CASE(SerializeSmi1) { 67 TEST_CASE(SerializeSmi1) {
62 // Write snapshot with object content. 68 // Write snapshot with object content.
63 uint8_t* buffer; 69 uint8_t* buffer;
64 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 70 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
65 const Smi& smi = Smi::Handle(Smi::New(124)); 71 const Smi& smi = Smi::Handle(Smi::New(124));
66 writer.WriteObject(smi.raw()); 72 writer.WriteObject(smi.raw());
67 writer.FinalizeBuffer(); 73 writer.FinalizeBuffer();
68 74
69 // Create a snapshot object using the buffer. 75 // Create a snapshot object using the buffer.
70 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 76 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
71 77
72 // Read object back from the snapshot. 78 // Read object back from the snapshot.
73 SnapshotReader reader(snapshot, Isolate::Current()); 79 SnapshotReader reader(snapshot, Isolate::Current());
74 const Object& serialized_object = Object::Handle(reader.ReadObject()); 80 const Object& serialized_object = Object::Handle(reader.ReadObject());
75 EXPECT(Equals(smi, serialized_object)); 81 EXPECT(Equals(smi, serialized_object));
82
83 // Read object back from the snapshot into a C structure.
84 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
85 Dart_Value* value = mreader.ReadObject();
86 EXPECT_EQ(Dart_Value::kInt32, value->type);
87 EXPECT_EQ(smi.Value(), value->data.int32);
88 free(value);
76 } 89 }
77 90
78 91
79 TEST_CASE(SerializeSmi2) { 92 TEST_CASE(SerializeSmi2) {
80 // Write snapshot with object content. 93 // Write snapshot with object content.
81 uint8_t* buffer; 94 uint8_t* buffer;
82 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 95 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
83 const Smi& smi = Smi::Handle(Smi::New(-1)); 96 const Smi& smi = Smi::Handle(Smi::New(-1));
84 writer.WriteObject(smi.raw()); 97 writer.WriteObject(smi.raw());
85 writer.FinalizeBuffer(); 98 writer.FinalizeBuffer();
86 99
87 // Create a snapshot object using the buffer. 100 // Create a snapshot object using the buffer.
88 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 101 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
89 102
90 // Read object back from the snapshot. 103 // Read object back from the snapshot.
91 SnapshotReader reader(snapshot, Isolate::Current()); 104 SnapshotReader reader(snapshot, Isolate::Current());
92 const Object& serialized_object = Object::Handle(reader.ReadObject()); 105 const Object& serialized_object = Object::Handle(reader.ReadObject());
93 EXPECT(Equals(smi, serialized_object)); 106 EXPECT(Equals(smi, serialized_object));
107
108 // Read object back from the snapshot into a C structure.
109 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
110 Dart_Value* value = mreader.ReadObject();
111 EXPECT_EQ(Dart_Value::kInt32, value->type);
112 EXPECT_EQ(smi.Value(), value->data.int32);
113 free(value);
94 } 114 }
95 115
96 116
97 TEST_CASE(SerializeDouble) { 117 TEST_CASE(SerializeDouble) {
98 // Write snapshot with object content. 118 // Write snapshot with object content.
99 uint8_t* buffer; 119 uint8_t* buffer;
100 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 120 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
101 const Double& dbl = Double::Handle(Double::New(101.29)); 121 const Double& dbl = Double::Handle(Double::New(101.29));
102 writer.WriteObject(dbl.raw()); 122 writer.WriteObject(dbl.raw());
103 writer.FinalizeBuffer(); 123 writer.FinalizeBuffer();
104 124
105 // Create a snapshot object using the buffer. 125 // Create a snapshot object using the buffer.
106 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 126 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
107 127
108 // Read object back from the snapshot. 128 // Read object back from the snapshot.
109 SnapshotReader reader(snapshot, Isolate::Current()); 129 SnapshotReader reader(snapshot, Isolate::Current());
110 const Object& serialized_object = Object::Handle(reader.ReadObject()); 130 const Object& serialized_object = Object::Handle(reader.ReadObject());
111 EXPECT(Equals(dbl, serialized_object)); 131 EXPECT(Equals(dbl, serialized_object));
132
133 // Read object back from the snapshot into a C structure.
134 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
135 Dart_Value* value = mreader.ReadObject();
136 EXPECT_EQ(Dart_Value::kDouble, value->type);
137 EXPECT_EQ(dbl.value(), value->data.dbl);
138 free(value);
112 } 139 }
113 140
114 141
115 TEST_CASE(SerializeBool) { 142 TEST_CASE(SerializeBool) {
116 // Write snapshot with object content. 143 // Write snapshot with object content.
117 uint8_t* buffer; 144 uint8_t* buffer;
118 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 145 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
119 const Bool& bool1 = Bool::Handle(Bool::True()); 146 const Bool& bool1 = Bool::Handle(Bool::True());
120 const Bool& bool2 = Bool::Handle(Bool::False()); 147 const Bool& bool2 = Bool::Handle(Bool::False());
121 writer.WriteObject(bool1.raw()); 148 writer.WriteObject(bool1.raw());
122 writer.WriteObject(bool2.raw()); 149 writer.WriteObject(bool2.raw());
123 writer.FinalizeBuffer(); 150 writer.FinalizeBuffer();
124 151
125 // Create a snapshot object using the buffer. 152 // Create a snapshot object using the buffer.
126 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 153 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
127 154
128 // Read object back from the snapshot. 155 // Read object back from the snapshot.
129 SnapshotReader reader(snapshot, Isolate::Current()); 156 SnapshotReader reader(snapshot, Isolate::Current());
130 EXPECT(Bool::True() == reader.ReadObject()); 157 EXPECT(Bool::True() == reader.ReadObject());
131 EXPECT(Bool::False() == reader.ReadObject()); 158 EXPECT(Bool::False() == reader.ReadObject());
159
160 // Read object back from the snapshot into a C structure.
161 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
162 Dart_Value* value1 = mreader.ReadObject();
163 EXPECT_EQ(Dart_Value::kBool, value1->type);
164 EXPECT_EQ(true, value1->data.bl);
165 Dart_Value* value2 = mreader.ReadObject();
166 EXPECT_EQ(Dart_Value::kBool, value2->type);
167 EXPECT_EQ(false, value2->data.bl);
168 free(value1);
169 free(value2);
132 } 170 }
133 171
134 172
135 TEST_CASE(SerializeBigint) { 173 TEST_CASE(SerializeBigint) {
136 // Write snapshot with object content. 174 // Write snapshot with object content.
137 uint8_t* buffer; 175 uint8_t* buffer;
138 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 176 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
139 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); 177 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL));
140 writer.WriteObject(bigint.raw()); 178 writer.WriteObject(bigint.raw());
141 writer.FinalizeBuffer(); 179 writer.FinalizeBuffer();
142 180
143 // Create a snapshot object using the buffer. 181 // Create a snapshot object using the buffer.
144 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 182 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
145 183
146 // Read object back from the snapshot. 184 // Read object back from the snapshot.
147 SnapshotReader reader(snapshot, Isolate::Current()); 185 SnapshotReader reader(snapshot, Isolate::Current());
148 Bigint& obj = Bigint::Handle(); 186 Bigint& obj = Bigint::Handle();
149 obj ^= reader.ReadObject(); 187 obj ^= reader.ReadObject();
150 OS::Print("%lld", BigintOperations::ToInt64(obj)); 188 OS::Print("%lld", BigintOperations::ToInt64(obj));
151 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); 189 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj));
190
191 // Read object back from the snapshot into a C structure.
192 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
193 Dart_Value* value = mreader.ReadObject();
194 // Bigint not supported.
195 EXPECT(value == NULL);
152 } 196 }
153 197
154 198
155 TEST_CASE(SerializeSingletons) { 199 TEST_CASE(SerializeSingletons) {
156 // Write snapshot with object content. 200 // Write snapshot with object content.
157 uint8_t* buffer; 201 uint8_t* buffer;
158 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 202 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
159 writer.WriteObject(Object::class_class()); 203 writer.WriteObject(Object::class_class());
160 writer.WriteObject(Object::null_class()); 204 writer.WriteObject(Object::null_class());
161 writer.WriteObject(Object::type_class()); 205 writer.WriteObject(Object::type_class());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); 245 EXPECT(Object::exception_handlers_class() == reader.ReadObject());
202 EXPECT(Object::context_class() == reader.ReadObject()); 246 EXPECT(Object::context_class() == reader.ReadObject());
203 EXPECT(Object::context_scope_class() == reader.ReadObject()); 247 EXPECT(Object::context_scope_class() == reader.ReadObject());
204 } 248 }
205 249
206 250
207 TEST_CASE(SerializeString) { 251 TEST_CASE(SerializeString) {
208 // Write snapshot with object content. 252 // Write snapshot with object content.
209 uint8_t* buffer; 253 uint8_t* buffer;
210 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 254 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
211 String& str = String::Handle(String::New("This string shall be serialized")); 255 static const char* cstr = "This string shall be serialized";
256 String& str = String::Handle(String::New(cstr));
212 writer.WriteObject(str.raw()); 257 writer.WriteObject(str.raw());
213 writer.FinalizeBuffer(); 258 writer.FinalizeBuffer();
214 259
215 // Create a snapshot object using the buffer. 260 // Create a snapshot object using the buffer.
216 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 261 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
217 262
218 // Read object back from the snapshot. 263 // Read object back from the snapshot.
219 SnapshotReader reader(snapshot, Isolate::Current()); 264 SnapshotReader reader(snapshot, Isolate::Current());
220 String& serialized_str = String::Handle(); 265 String& serialized_str = String::Handle();
221 serialized_str ^= reader.ReadObject(); 266 serialized_str ^= reader.ReadObject();
222 EXPECT(str.Equals(serialized_str)); 267 EXPECT(str.Equals(serialized_str));
268
269 // Read object back from the snapshot into a C structure.
270 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
271 Dart_Value* value = mreader.ReadObject();
272 EXPECT_EQ(Dart_Value::kString, value->type);
273 EXPECT_EQ(0, strcmp(value->data.str, cstr));
siva 2012/01/27 01:56:52 I think we have an EXPECT_STREQ
Søren Gjesse 2012/01/27 14:40:03 Done.
274 free(value);
223 } 275 }
224 276
225 277
226 TEST_CASE(SerializeArray) { 278 TEST_CASE(SerializeArray) {
227 // Write snapshot with object content. 279 // Write snapshot with object content.
228 uint8_t* buffer; 280 uint8_t* buffer;
229 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); 281 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator);
230 const int kArrayLength = 10; 282 const int kArrayLength = 10;
231 Array& array = Array::Handle(Array::New(kArrayLength)); 283 Array& array = Array::Handle(Array::New(kArrayLength));
232 Smi& smi = Smi::Handle(); 284 Smi& smi = Smi::Handle();
233 for (int i = 0; i < kArrayLength; i++) { 285 for (int i = 0; i < kArrayLength; i++) {
234 smi ^= Smi::New(i); 286 smi ^= Smi::New(i);
235 array.SetAt(i, smi); 287 array.SetAt(i, smi);
236 } 288 }
237 writer.WriteObject(array.raw()); 289 writer.WriteObject(array.raw());
238 writer.FinalizeBuffer(); 290 writer.FinalizeBuffer();
239 291
240 // Create a snapshot object using the buffer. 292 // Create a snapshot object using the buffer.
241 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 293 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
242 294
243 // Read object back from the snapshot. 295 // Read object back from the snapshot.
244 SnapshotReader reader(snapshot, Isolate::Current()); 296 SnapshotReader reader(snapshot, Isolate::Current());
245 Array& serialized_array = Array::Handle(); 297 Array& serialized_array = Array::Handle();
246 serialized_array ^= reader.ReadObject(); 298 serialized_array ^= reader.ReadObject();
247 EXPECT(array.Equals(serialized_array)); 299 EXPECT(array.Equals(serialized_array));
300
301 // Read object back from the snapshot into a C structure.
302 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
303 Dart_Value* value = mreader.ReadObject();
304 EXPECT_EQ(Dart_Value::kArray, value->type);
305 EXPECT_EQ(kArrayLength, value->data.array.len);
306 for (int i = 0; i < kArrayLength; i++) {
307 Dart_Value* element = value->data.array.values[i];
308 EXPECT_EQ(Dart_Value::kInt32, element->type);
309 EXPECT_EQ(i, element->data.int32);
310 free(element);
311 }
312 free(value);
248 } 313 }
249 314
250 315
251 TEST_CASE(SerializeScript) { 316 TEST_CASE(SerializeScript) {
252 const char* kScriptChars = 317 const char* kScriptChars =
253 "class A {\n" 318 "class A {\n"
254 " static bar() { return 42; }\n" 319 " static bar() { return 42; }\n"
255 " static fly() { return 5; }\n" 320 " static fly() { return 5; }\n"
256 "}\n"; 321 "}\n";
257 322
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 0, 582 0,
518 NULL); 583 NULL);
519 EXPECT_VALID(result); 584 EXPECT_VALID(result);
520 Dart_ExitScope(); 585 Dart_ExitScope();
521 } 586 }
522 Dart_ShutdownIsolate(); 587 Dart_ShutdownIsolate();
523 free(full_snapshot); 588 free(full_snapshot);
524 free(script_snapshot); 589 free(script_snapshot);
525 } 590 }
526 591
592
593 TEST_CASE(IntArrayMessage) {
594 uint8_t* buffer = NULL;
595 MessageWriter writer(&buffer, &allocator);
596
597 static const int kArrayLength = 2;
598 intptr_t data[kArrayLength] = {1, 2};
599 int len = kArrayLength;
600 writer.WriteMessage(len, data);
601
602 // Read object back from the snapshot into a C structure.
603 MessageReader mreader(buffer + Snapshot::kHeaderSize, writer.BytesWritten());
604 Dart_Value* value = mreader.ReadObject();
605 EXPECT_EQ(Dart_Value::kArray, value->type);
606 EXPECT_EQ(kArrayLength, value->data.array.len);
607 for (int i = 0; i < kArrayLength; i++) {
608 Dart_Value* element = value->data.array.values[i];
609 EXPECT_EQ(Dart_Value::kInt32, element->type);
610 EXPECT_EQ(i + 1, element->data.int32);
611 free(element);
612 }
613 free(value);
614 }
615
527 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 616 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
528 617
529 } // namespace dart 618 } // namespace dart
OLDNEW
« runtime/vm/snapshot.cc ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698