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

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

Issue 509153003: New bigint implementation in the vm. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 3 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
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/globals.h" 5 #include "platform/globals.h"
6 6
7 #include "include/dart_debugger_api.h" 7 #include "include/dart_debugger_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/bigint_operations.h"
10 #include "vm/class_finalizer.h" 9 #include "vm/class_finalizer.h"
11 #include "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
12 #include "vm/dart_api_message.h" 11 #include "vm/dart_api_message.h"
13 #include "vm/dart_api_state.h" 12 #include "vm/dart_api_state.h"
14 #include "vm/flags.h" 13 #include "vm/flags.h"
15 #include "vm/snapshot.h" 14 #include "vm/snapshot.h"
16 #include "vm/symbols.h" 15 #include "vm/symbols.h"
17 #include "vm/unicode.h" 16 #include "vm/unicode.h"
18 #include "vm/unit_test.h" 17 #include "vm/unit_test.h"
19 18
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 break; 79 break;
81 case Dart_CObject_kBool: 80 case Dart_CObject_kBool:
82 EXPECT_EQ(first->value.as_bool, second->value.as_bool); 81 EXPECT_EQ(first->value.as_bool, second->value.as_bool);
83 break; 82 break;
84 case Dart_CObject_kInt32: 83 case Dart_CObject_kInt32:
85 EXPECT_EQ(first->value.as_int32, second->value.as_int32); 84 EXPECT_EQ(first->value.as_int32, second->value.as_int32);
86 break; 85 break;
87 case Dart_CObject_kInt64: 86 case Dart_CObject_kInt64:
88 EXPECT_EQ(first->value.as_int64, second->value.as_int64); 87 EXPECT_EQ(first->value.as_int64, second->value.as_int64);
89 break; 88 break;
90 case Dart_CObject_kBigint: 89 case Dart_CObject_kBigint: {
91 EXPECT_STREQ(first->value.as_bigint, second->value.as_bigint); 90 char* first_hex_value = TestCase::BigintToHexValue(first);
91 char* second_hex_value = TestCase::BigintToHexValue(second);
92 EXPECT_STREQ(first_hex_value, second_hex_value);
93 free(first_hex_value);
94 free(second_hex_value);
92 break; 95 break;
96 }
93 case Dart_CObject_kDouble: 97 case Dart_CObject_kDouble:
94 EXPECT_EQ(first->value.as_double, second->value.as_double); 98 EXPECT_EQ(first->value.as_double, second->value.as_double);
95 break; 99 break;
96 case Dart_CObject_kString: 100 case Dart_CObject_kString:
97 EXPECT_STREQ(first->value.as_string, second->value.as_string); 101 EXPECT_STREQ(first->value.as_string, second->value.as_string);
98 break; 102 break;
99 case Dart_CObject_kTypedData: 103 case Dart_CObject_kTypedData:
100 EXPECT_EQ(first->value.as_typed_data.length, 104 EXPECT_EQ(first->value.as_typed_data.length,
101 second->value.as_typed_data.length); 105 second->value.as_typed_data.length);
102 for (int i = 0; i < first->value.as_typed_data.length; i++) { 106 for (int i = 0; i < first->value.as_typed_data.length; i++) {
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 bigint ^= Integer::NewCanonical(str); 394 bigint ^= Integer::NewCanonical(str);
391 writer.WriteMessage(bigint); 395 writer.WriteMessage(bigint);
392 intptr_t buffer_len = writer.BytesWritten(); 396 intptr_t buffer_len = writer.BytesWritten();
393 397
394 // Read object back from the snapshot. 398 // Read object back from the snapshot.
395 SnapshotReader reader(buffer, buffer_len, 399 SnapshotReader reader(buffer, buffer_len,
396 Snapshot::kMessage, Isolate::Current()); 400 Snapshot::kMessage, Isolate::Current());
397 Bigint& obj = Bigint::Handle(); 401 Bigint& obj = Bigint::Handle();
398 obj ^= reader.ReadObject(); 402 obj ^= reader.ReadObject();
399 403
400 EXPECT_STREQ(BigintOperations::ToHexCString(bigint, &allocator), 404 EXPECT_STREQ(bigint.ToHexCString(allocator), obj.ToHexCString(allocator));
401 BigintOperations::ToHexCString(obj, &allocator));
402 405
403 // Read object back from the snapshot into a C structure. 406 // Read object back from the snapshot into a C structure.
404 ApiNativeScope scope; 407 ApiNativeScope scope;
405 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 408 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
406 Dart_CObject* root = api_reader.ReadMessage(); 409 Dart_CObject* root = api_reader.ReadMessage();
407 // Bigint not supported.
408 EXPECT_NOTNULL(root); 410 EXPECT_NOTNULL(root);
409 EXPECT_EQ(Dart_CObject_kBigint, root->type); 411 EXPECT_EQ(Dart_CObject_kBigint, root->type);
410 EXPECT_STREQ("270FFFFFFFFFFFFFD8F0", root->value.as_bigint); 412 char* hex_value = TestCase::BigintToHexValue(root);
413 EXPECT_STREQ(cstr, hex_value);
414 free(hex_value);
411 CheckEncodeDecodeMessage(root); 415 CheckEncodeDecodeMessage(root);
412 } 416 }
413 417
414 418
415 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { 419 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
416 // Write snapshot with object content. 420 // Write snapshot with object content.
417 uint8_t* buffer; 421 uint8_t* buffer;
418 MessageWriter writer(&buffer, &zone_allocator); 422 MessageWriter writer(&buffer, &zone_allocator);
419 writer.WriteMessage(bigint); 423 writer.WriteMessage(bigint);
420 intptr_t buffer_len = writer.BytesWritten(); 424 intptr_t buffer_len = writer.BytesWritten();
421 425
422 // Read object back from the snapshot. 426 // Read object back from the snapshot.
423 SnapshotReader reader(buffer, buffer_len, 427 SnapshotReader reader(buffer, buffer_len,
424 Snapshot::kMessage, Isolate::Current()); 428 Snapshot::kMessage, Isolate::Current());
425 Bigint& serialized_bigint = Bigint::Handle(); 429 Bigint& serialized_bigint = Bigint::Handle();
426 serialized_bigint ^= reader.ReadObject(); 430 serialized_bigint ^= reader.ReadObject();
427 const char* str1 = BigintOperations::ToHexCString(bigint, allocator); 431 const char* str1 = bigint.ToHexCString(allocator);
428 const char* str2 = 432 const char* str2 = serialized_bigint.ToHexCString(allocator);
429 BigintOperations::ToHexCString(serialized_bigint, allocator);
430 EXPECT_STREQ(str1, str2); 433 EXPECT_STREQ(str1, str2);
431 free(const_cast<char*>(str1)); 434 free(const_cast<char*>(str1));
432 free(const_cast<char*>(str2)); 435 free(const_cast<char*>(str2));
433 436
434 // Read object back from the snapshot into a C structure. 437 // Read object back from the snapshot into a C structure.
435 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 438 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
436 Dart_CObject* root = api_reader.ReadMessage(); 439 Dart_CObject* root = api_reader.ReadMessage();
437 // Bigint not supported. 440 // Bigint not supported.
438 EXPECT_NOTNULL(root); 441 EXPECT_NOTNULL(root);
439 CheckEncodeDecodeMessage(root); 442 CheckEncodeDecodeMessage(root);
440 return root; 443 return root;
441 } 444 }
442 445
443 446
444 void CheckBigint(const char* bigint_value) { 447 void CheckBigint(const char* bigint_value) {
445 StackZone zone(Isolate::Current()); 448 StackZone zone(Isolate::Current());
446 ApiNativeScope scope; 449 ApiNativeScope scope;
447 450
448 Bigint& bigint = Bigint::Handle(); 451 Bigint& bigint = Bigint::Handle();
449 bigint ^= BigintOperations::NewFromCString(bigint_value); 452 bigint ^= Bigint::NewFromCString(bigint_value);
450 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint); 453 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint);
451 EXPECT_EQ(Dart_CObject_kBigint, bigint_cobject->type); 454 EXPECT_EQ(Dart_CObject_kBigint, bigint_cobject->type);
452 if (bigint_value[0] == '0') { 455 char* hex_value = TestCase::BigintToHexValue(bigint_cobject);
453 EXPECT_STREQ(bigint_value + 2, bigint_cobject->value.as_bigint); 456 EXPECT_STREQ(bigint_value, hex_value);
454 } else { 457 free(hex_value);
455 EXPECT_EQ('-', bigint_value[0]);
456 EXPECT_EQ('-', bigint_cobject->value.as_bigint[0]);
457 EXPECT_STREQ(bigint_value + 3, bigint_cobject->value.as_bigint + 1);
458 }
459 } 458 }
460 459
461 460
462 TEST_CASE(SerializeBigint2) { 461 TEST_CASE(SerializeBigint2) {
463 CheckBigint("0x0"); 462 CheckBigint("0x0");
464 CheckBigint("0x1"); 463 CheckBigint("0x1");
465 CheckBigint("-0x1"); 464 CheckBigint("-0x1");
466 CheckBigint("0x11111111111111111111"); 465 CheckBigint("0x11111111111111111111");
467 CheckBigint("-0x11111111111111111111"); 466 CheckBigint("-0x11111111111111111111");
468 CheckBigint("0x9876543210987654321098765432109876543210"); 467 CheckBigint("0x9876543210987654321098765432109876543210");
(...skipping 1162 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 bigint ^= Api::UnwrapHandle(bigint_result); 1630 bigint ^= Api::UnwrapHandle(bigint_result);
1632 writer.WriteMessage(bigint); 1631 writer.WriteMessage(bigint);
1633 intptr_t buffer_len = writer.BytesWritten(); 1632 intptr_t buffer_len = writer.BytesWritten();
1634 1633
1635 // Read object back from the snapshot into a C structure. 1634 // Read object back from the snapshot into a C structure.
1636 ApiNativeScope scope; 1635 ApiNativeScope scope;
1637 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1636 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1638 Dart_CObject* root = api_reader.ReadMessage(); 1637 Dart_CObject* root = api_reader.ReadMessage();
1639 EXPECT_NOTNULL(root); 1638 EXPECT_NOTNULL(root);
1640 EXPECT_EQ(Dart_CObject_kBigint, root->type); 1639 EXPECT_EQ(Dart_CObject_kBigint, root->type);
1641 EXPECT_STREQ("-424242424242424242424242424242424242", 1640 char* hex_value = TestCase::BigintToHexValue(root);
1642 root->value.as_bigint); 1641 EXPECT_STREQ("-0x424242424242424242424242424242424242", hex_value);
1642 free(hex_value);
1643 CheckEncodeDecodeMessage(root); 1643 CheckEncodeDecodeMessage(root);
1644 } 1644 }
1645 CheckString(ascii_string_result, "Hello, world!"); 1645 CheckString(ascii_string_result, "Hello, world!");
1646 CheckString(non_ascii_string_result, "Blåbærgrød"); 1646 CheckString(non_ascii_string_result, "Blåbærgrød");
1647 CheckString(non_bmp_string_result, 1647 CheckString(non_bmp_string_result,
1648 "\xf0\x90\x80\x80" 1648 "\xf0\x90\x80\x80"
1649 "\xf0\x9f\x98\x81" 1649 "\xf0\x9f\x98\x81"
1650 "\xf0\x9f\x98\xb7" 1650 "\xf0\x9f\x98\xb7"
1651 "\xf0\xa0\x80\x80"); 1651 "\xf0\xa0\x80\x80");
1652 CheckStringInvalid(lead_surrogate_string_result); 1652 CheckStringInvalid(lead_surrogate_string_result);
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
2078 // Generate a list of bigints from Dart code. 2078 // Generate a list of bigints from Dart code.
2079 ApiNativeScope scope; 2079 ApiNativeScope scope;
2080 Dart_CObject* root = GetDeserializedDartMessage(lib, "getBigintList"); 2080 Dart_CObject* root = GetDeserializedDartMessage(lib, "getBigintList");
2081 EXPECT_NOTNULL(root); 2081 EXPECT_NOTNULL(root);
2082 EXPECT_EQ(Dart_CObject_kArray, root->type); 2082 EXPECT_EQ(Dart_CObject_kArray, root->type);
2083 EXPECT_EQ(kArrayLength, root->value.as_array.length); 2083 EXPECT_EQ(kArrayLength, root->value.as_array.length);
2084 for (int i = 0; i < kArrayLength; i++) { 2084 for (int i = 0; i < kArrayLength; i++) {
2085 Dart_CObject* element = root->value.as_array.values[i]; 2085 Dart_CObject* element = root->value.as_array.values[i];
2086 EXPECT_EQ(root->value.as_array.values[0], element); 2086 EXPECT_EQ(root->value.as_array.values[0], element);
2087 EXPECT_EQ(Dart_CObject_kBigint, element->type); 2087 EXPECT_EQ(Dart_CObject_kBigint, element->type);
2088 EXPECT_STREQ("1234567890123456789012345678901234567890", 2088 char* hex_value = TestCase::BigintToHexValue(element);
2089 element->value.as_bigint); 2089 EXPECT_STREQ("0x1234567890123456789012345678901234567890", hex_value);
2090 free(hex_value);
2090 } 2091 }
2091 } 2092 }
2092 { 2093 {
2093 // Generate a list of doubles from Dart code. 2094 // Generate a list of doubles from Dart code.
2094 ApiNativeScope scope; 2095 ApiNativeScope scope;
2095 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList"); 2096 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList");
2096 EXPECT_NOTNULL(root); 2097 EXPECT_NOTNULL(root);
2097 EXPECT_EQ(Dart_CObject_kArray, root->type); 2098 EXPECT_EQ(Dart_CObject_kArray, root->type);
2098 EXPECT_EQ(kArrayLength, root->value.as_array.length); 2099 EXPECT_EQ(kArrayLength, root->value.as_array.length);
2099 Dart_CObject* element = root->value.as_array.values[0]; 2100 Dart_CObject* element = root->value.as_array.values[0];
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
2293 // Generate a list of bigints from Dart code. 2294 // Generate a list of bigints from Dart code.
2294 ApiNativeScope scope; 2295 ApiNativeScope scope;
2295 Dart_CObject* root = GetDeserializedDartMessage(lib, "getBigintList"); 2296 Dart_CObject* root = GetDeserializedDartMessage(lib, "getBigintList");
2296 EXPECT_NOTNULL(root); 2297 EXPECT_NOTNULL(root);
2297 EXPECT_EQ(Dart_CObject_kArray, root->type); 2298 EXPECT_EQ(Dart_CObject_kArray, root->type);
2298 EXPECT_EQ(kArrayLength, root->value.as_array.length); 2299 EXPECT_EQ(kArrayLength, root->value.as_array.length);
2299 for (int i = 0; i < kArrayLength; i++) { 2300 for (int i = 0; i < kArrayLength; i++) {
2300 Dart_CObject* element = root->value.as_array.values[i]; 2301 Dart_CObject* element = root->value.as_array.values[i];
2301 EXPECT_EQ(root->value.as_array.values[0], element); 2302 EXPECT_EQ(root->value.as_array.values[0], element);
2302 EXPECT_EQ(Dart_CObject_kBigint, element->type); 2303 EXPECT_EQ(Dart_CObject_kBigint, element->type);
2303 EXPECT_STREQ("1234567890123456789012345678901234567890", 2304 char* hex_value = TestCase::BigintToHexValue(element);
2304 element->value.as_bigint); 2305 EXPECT_STREQ("0x1234567890123456789012345678901234567890", hex_value);
2306 free(hex_value);
2305 } 2307 }
2306 } 2308 }
2307 { 2309 {
2308 // Generate a list of doubles from Dart code. 2310 // Generate a list of doubles from Dart code.
2309 ApiNativeScope scope; 2311 ApiNativeScope scope;
2310 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList"); 2312 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList");
2311 EXPECT_NOTNULL(root); 2313 EXPECT_NOTNULL(root);
2312 EXPECT_EQ(Dart_CObject_kArray, root->type); 2314 EXPECT_EQ(Dart_CObject_kArray, root->type);
2313 EXPECT_EQ(kArrayLength, root->value.as_array.length); 2315 EXPECT_EQ(kArrayLength, root->value.as_array.length);
2314 Dart_CObject* element = root->value.as_array.values[0]; 2316 Dart_CObject* element = root->value.as_array.values[0];
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
2724 StackZone zone(Isolate::Current()); 2726 StackZone zone(Isolate::Current());
2725 uint8_t* buffer; 2727 uint8_t* buffer;
2726 MessageWriter writer(&buffer, &zone_allocator); 2728 MessageWriter writer(&buffer, &zone_allocator);
2727 writer.WriteInlinedObjectHeader(kOmittedObjectId); 2729 writer.WriteInlinedObjectHeader(kOmittedObjectId);
2728 // For performance, we'd like single-byte headers when ids are omitted. 2730 // For performance, we'd like single-byte headers when ids are omitted.
2729 // If this starts failing, consider renumbering the snapshot ids. 2731 // If this starts failing, consider renumbering the snapshot ids.
2730 EXPECT_EQ(1, writer.BytesWritten()); 2732 EXPECT_EQ(1, writer.BytesWritten());
2731 } 2733 }
2732 2734
2733 } // namespace dart 2735 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698