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

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

Issue 9363023: Add support for big integers to the native message format (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/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 "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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 switch (first->type) { 73 switch (first->type) {
74 case Dart_CObject::kNull: 74 case Dart_CObject::kNull:
75 // Nothing more to compare. 75 // Nothing more to compare.
76 break; 76 break;
77 case Dart_CObject::kBool: 77 case Dart_CObject::kBool:
78 EXPECT_EQ(first->value.as_bool, second->value.as_bool); 78 EXPECT_EQ(first->value.as_bool, second->value.as_bool);
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::kBigint:
84 EXPECT_STREQ(first->value.as_bigint, second->value.as_bigint);
85 break;
83 case Dart_CObject::kDouble: 86 case Dart_CObject::kDouble:
84 EXPECT_EQ(first->value.as_double, second->value.as_double); 87 EXPECT_EQ(first->value.as_double, second->value.as_double);
85 break; 88 break;
86 case Dart_CObject::kString: 89 case Dart_CObject::kString:
87 EXPECT_STREQ(first->value.as_string, second->value.as_string); 90 EXPECT_STREQ(first->value.as_string, second->value.as_string);
88 break; 91 break;
89 case Dart_CObject::kArray: 92 case Dart_CObject::kArray:
90 // Use invalid type as a visited marker to avoid infinite 93 // Use invalid type as a visited marker to avoid infinite
91 // recursion on graphs with cycles. 94 // recursion on graphs with cycles.
92 second->type = Dart_CObject::kNumberOfTypes; 95 second->type = Dart_CObject::kNumberOfTypes;
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 298 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
296 writer.BytesWritten(), 299 writer.BytesWritten(),
297 &zone_allocator); 300 &zone_allocator);
298 EXPECT_NOTNULL(root); 301 EXPECT_NOTNULL(root);
299 EXPECT_EQ(Dart_CObject::kBool, root->type); 302 EXPECT_EQ(Dart_CObject::kBool, root->type);
300 EXPECT_EQ(false, root->value.as_bool); 303 EXPECT_EQ(false, root->value.as_bool);
301 CheckEncodeDecodeMessage(root); 304 CheckEncodeDecodeMessage(root);
302 } 305 }
303 306
304 307
308 static uword allocator(intptr_t size) {
309 return reinterpret_cast<uword>(malloc(size));
310 }
311
312
305 TEST_CASE(SerializeBigint) { 313 TEST_CASE(SerializeBigint) {
306 Zone zone(Isolate::Current()); 314 Zone zone(Isolate::Current());
307 315
308 // Write snapshot with object content. 316 // Write snapshot with object content.
309 uint8_t* buffer; 317 uint8_t* buffer;
310 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 318 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
311 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); 319 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff)));
312 writer.WriteObject(bigint.raw()); 320 writer.WriteObject(bigint.raw());
313 writer.FinalizeBuffer(); 321 writer.FinalizeBuffer();
314 322
315 // Create a snapshot object using the buffer. 323 // Create a snapshot object using the buffer.
316 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 324 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
317 325
318 // Read object back from the snapshot. 326 // Read object back from the snapshot.
319 SnapshotReader reader(snapshot, Isolate::Current()); 327 SnapshotReader reader(snapshot, Isolate::Current());
320 Bigint& obj = Bigint::Handle(); 328 Bigint& obj = Bigint::Handle();
321 obj ^= reader.ReadObject(); 329 obj ^= reader.ReadObject();
322 OS::Print("%lld", BigintOperations::ToInt64(obj)); 330
323 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); 331 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj));
324 332
325 // Read object back from the snapshot into a C structure. 333 // Read object back from the snapshot into a C structure.
326 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 334 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
327 writer.BytesWritten(), 335 writer.BytesWritten(),
328 &zone_allocator); 336 &zone_allocator);
329 // Bigint not supported. 337 // Bigint not supported.
330 EXPECT(root == NULL); 338 EXPECT_NOTNULL(root);
339 EXPECT_EQ(Dart_CObject::kBigint, root->type);
340 EXPECT_STREQ("68719476735", root->value.as_bigint);
341 CheckEncodeDecodeMessage(root);
331 } 342 }
332 343
333 344
345 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
346 // Write snapshot with object content.
347 uint8_t* buffer;
348 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
349 writer.WriteObject(bigint.raw());
350 writer.FinalizeBuffer();
351
352 // Create a snapshot object using the buffer.
353 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
354
355 // Read object back from the snapshot.
356 SnapshotReader reader(snapshot, Isolate::Current());
357 Bigint& serialized_bigint = Bigint::Handle();
358 serialized_bigint ^= reader.ReadObject();
359 const char *str1 = BigintOperations::ToDecCString(bigint, allocator);
360 const char *str2 =
361 BigintOperations::ToDecCString(serialized_bigint, allocator);
362 EXPECT_STREQ(str1, str2);
363 free(const_cast<char*>(str1));
364 free(const_cast<char*>(str2));
365
366 // Read object back from the snapshot into a C structure.
367 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
368 writer.BytesWritten(),
369 &zone_allocator);
370 // Bigint not supported.
371 EXPECT_NOTNULL(root);
372 CheckEncodeDecodeMessage(root);
373 return root;
374 }
375
376
377 void CheckBigint(const char* bigint_value) {
378 Zone zone(Isolate::Current());
379
380 Bigint& bigint = Bigint::Handle();
381 bigint ^= BigintOperations::NewFromCString(bigint_value);
382 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint);
383 EXPECT_EQ(Dart_CObject::kBigint, bigint_cobject->type);
384 EXPECT_STREQ(bigint_value, bigint_cobject->value.as_bigint);
385 }
386
387
388 TEST_CASE(SerializeBigint2) {
389 CheckBigint("0");
390 CheckBigint("1");
391 CheckBigint("-1");
392 CheckBigint("11111111111111111111");
393 CheckBigint("-11111111111111111111");
394 CheckBigint("9876543210987654321098765432109876543210");
395 CheckBigint("-9876543210987654321098765432109876543210");
396 }
397
398
334 TEST_CASE(SerializeSingletons) { 399 TEST_CASE(SerializeSingletons) {
335 // Write snapshot with object content. 400 // Write snapshot with object content.
336 uint8_t* buffer; 401 uint8_t* buffer;
337 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); 402 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator);
338 writer.WriteObject(Object::class_class()); 403 writer.WriteObject(Object::class_class());
339 writer.WriteObject(Object::null_class()); 404 writer.WriteObject(Object::null_class());
340 writer.WriteObject(Object::type_class()); 405 writer.WriteObject(Object::type_class());
341 writer.WriteObject(Object::type_parameter_class()); 406 writer.WriteObject(Object::type_parameter_class());
342 writer.WriteObject(Object::instantiated_type_class()); 407 writer.WriteObject(Object::instantiated_type_class());
343 writer.WriteObject(Object::abstract_type_arguments_class()); 408 writer.WriteObject(Object::abstract_type_arguments_class());
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 writer.BytesWritten(), 907 writer.BytesWritten(),
843 &zone_allocator); 908 &zone_allocator);
844 } 909 }
845 910
846 911
847 UNIT_TEST_CASE(DartGeneratedMessages) { 912 UNIT_TEST_CASE(DartGeneratedMessages) {
848 static const char* kCustomIsolateScriptChars = 913 static const char* kCustomIsolateScriptChars =
849 "getSmi() {\n" 914 "getSmi() {\n"
850 " return 42;\n" 915 " return 42;\n"
851 "}\n" 916 "}\n"
917 "getBigint() {\n"
918 " return -424242424242424242424242424242424242;\n"
919 "}\n"
852 "getString() {\n" 920 "getString() {\n"
853 " return \"Hello, world!\";\n" 921 " return \"Hello, world!\";\n"
854 "}\n" 922 "}\n"
855 "getList() {\n" 923 "getList() {\n"
856 " return new List(kArrayLength);\n" 924 " return new List(kArrayLength);\n"
857 "}\n"; 925 "}\n";
858 926
859 TestCase::CreateTestIsolate(); 927 TestCase::CreateTestIsolate();
860 Isolate* isolate = Isolate::Current(); 928 Isolate* isolate = Isolate::Current();
861 EXPECT(isolate != NULL); 929 EXPECT(isolate != NULL);
862 Dart_EnterScope(); 930 Dart_EnterScope();
863 931
864 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, 932 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars,
865 NULL); 933 NULL);
866 EXPECT_VALID(lib); 934 EXPECT_VALID(lib);
867 Dart_Handle smi_result; 935 Dart_Handle smi_result;
868 smi_result = Dart_InvokeStatic(lib, 936 smi_result = Dart_InvokeStatic(lib,
869 Dart_NewString(""), 937 Dart_NewString(""),
870 Dart_NewString("getSmi"), 938 Dart_NewString("getSmi"),
871 0, 939 0,
872 NULL); 940 NULL);
873 EXPECT_VALID(smi_result); 941 EXPECT_VALID(smi_result);
942 Dart_Handle bigint_result;
943 bigint_result = Dart_InvokeStatic(lib,
944 Dart_NewString(""),
945 Dart_NewString("getBigint"),
946 0,
947 NULL);
948 EXPECT_VALID(bigint_result);
874 Dart_Handle string_result; 949 Dart_Handle string_result;
875 string_result = Dart_InvokeStatic(lib, 950 string_result = Dart_InvokeStatic(lib,
876 Dart_NewString(""), 951 Dart_NewString(""),
877 Dart_NewString("getString"), 952 Dart_NewString("getString"),
878 0, 953 0,
879 NULL); 954 NULL);
880 EXPECT_VALID(string_result); 955 EXPECT_VALID(string_result);
881 EXPECT(Dart_IsString(string_result)); 956 EXPECT(Dart_IsString(string_result));
882 957
883 { 958 {
(...skipping 14 matching lines...) Expand all
898 &zone_allocator); 973 &zone_allocator);
899 EXPECT_NOTNULL(root); 974 EXPECT_NOTNULL(root);
900 EXPECT_EQ(Dart_CObject::kInt32, root->type); 975 EXPECT_EQ(Dart_CObject::kInt32, root->type);
901 EXPECT_EQ(42, root->value.as_int32); 976 EXPECT_EQ(42, root->value.as_int32);
902 CheckEncodeDecodeMessage(root); 977 CheckEncodeDecodeMessage(root);
903 } 978 }
904 { 979 {
905 Zone zone(Isolate::Current()); 980 Zone zone(Isolate::Current());
906 uint8_t* buffer; 981 uint8_t* buffer;
907 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 982 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
983 Bigint& bigint = Bigint::Handle();
984 bigint ^= Api::UnwrapHandle(bigint_result);
985 writer.WriteObject(bigint.raw());
986 writer.FinalizeBuffer();
987
988 // Read object back from the snapshot into a C structure.
989 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
990 writer.BytesWritten(),
991 &zone_allocator);
992 EXPECT_NOTNULL(root);
993 EXPECT_EQ(Dart_CObject::kBigint, root->type);
994 EXPECT_STREQ("-424242424242424242424242424242424242",
995 root->value.as_bigint);
996 CheckEncodeDecodeMessage(root);
997 }
998 {
999 Zone zone(Isolate::Current());
1000 uint8_t* buffer;
1001 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
908 String& str = String::Handle(); 1002 String& str = String::Handle();
909 str ^= Api::UnwrapHandle(string_result); 1003 str ^= Api::UnwrapHandle(string_result);
910 writer.WriteObject(str.raw()); 1004 writer.WriteObject(str.raw());
911 writer.FinalizeBuffer(); 1005 writer.FinalizeBuffer();
912 1006
913 // Read object back from the snapshot into a C structure. 1007 // Read object back from the snapshot into a C structure.
914 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 1008 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
915 writer.BytesWritten(), 1009 writer.BytesWritten(),
916 &zone_allocator); 1010 &zone_allocator);
917 EXPECT_NOTNULL(root); 1011 EXPECT_NOTNULL(root);
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 EXPECT(Dart_ErrorHasException(result)); 1300 EXPECT(Dart_ErrorHasException(result));
1207 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", 1301 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
1208 Dart_GetError(result)); 1302 Dart_GetError(result));
1209 1303
1210 Dart_ExitScope(); 1304 Dart_ExitScope();
1211 } 1305 }
1212 1306
1213 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1307 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1214 1308
1215 } // namespace dart 1309 } // 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