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

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

Issue 11368138: Add some support for the code-point code-unit distinction. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: New version integrates feedback, adds less to standard String class. Created 8 years, 1 month 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/assert.h" 5 #include "platform/assert.h"
6 #include "vm/assembler.h" 6 #include "vm/assembler.h"
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/isolate.h" 8 #include "vm/isolate.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/object_store.h" 10 #include "vm/object_store.h"
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 "10000000000000000000")); 266 "10000000000000000000"));
267 Bigint& big2 = Bigint::Handle(BigintOperations::NewFromCString( 267 Bigint& big2 = Bigint::Handle(BigintOperations::NewFromCString(
268 "-10000000000000000000")); 268 "-10000000000000000000"));
269 EXPECT_EQ(-1, a.CompareWith(big1)); 269 EXPECT_EQ(-1, a.CompareWith(big1));
270 EXPECT_EQ(1, a.CompareWith(big2)); 270 EXPECT_EQ(1, a.CompareWith(big2));
271 EXPECT_EQ(-1, c.CompareWith(big1)); 271 EXPECT_EQ(-1, c.CompareWith(big1));
272 EXPECT_EQ(1, c.CompareWith(big2)); 272 EXPECT_EQ(1, c.CompareWith(big2));
273 } 273 }
274 274
275 275
276 TEST_CASE(StringCompareTo) {
277 const String& abcd = String::Handle(String::New("abcd"));
278 const String& abce = String::Handle(String::New("abce"));
279 EXPECT_EQ(0, abcd.CompareTo(abcd));
280 EXPECT_EQ(0, abce.CompareTo(abce));
281 EXPECT(abcd.CompareTo(abce) < 0);
282 EXPECT(abce.CompareTo(abcd) > 0);
283
284 const int kMonkeyLen = 4;
285 const uint8_t monkey_utf8[kMonkeyLen] = { 0xf0, 0x9f, 0x90, 0xb5 };
286 const String& monkey_face =
287 String::Handle(String::New(monkey_utf8, kMonkeyLen));
288 const int kDogLen = 4;
289 // 0x1f436 DOG FACE.
290 const uint8_t dog_utf8[kDogLen] = { 0xf0, 0x9f, 0x90, 0xb6 };
291 const String& dog_face = String::Handle(String::New(dog_utf8, kDogLen));
292 EXPECT_EQ(0, monkey_face.CompareTo(monkey_face));
293 EXPECT_EQ(0, dog_face.CompareTo(dog_face));
294 EXPECT(monkey_face.CompareTo(dog_face) < 0);
295 EXPECT(dog_face.CompareTo(monkey_face) > 0);
296
297 const int kDominoLen = 4;
298 // 0x1f036 DOMINO TILE HORIZONTAL-00-05.
299 const uint8_t domino_utf8[kDominoLen] = { 0xf0, 0x9f, 0x80, 0xb6 };
300 const String& domino = String::Handle(String::New(domino_utf8, kDominoLen));
301 EXPECT_EQ(0, domino.CompareTo(domino));
302 EXPECT(domino.CompareTo(dog_face) < 0);
303 EXPECT(domino.CompareTo(monkey_face) < 0);
304 EXPECT(dog_face.CompareTo(domino) > 0);
305 EXPECT(monkey_face.CompareTo(domino) > 0);
306
307 EXPECT(abcd.CompareTo(monkey_face) < 0);
308 EXPECT(abce.CompareTo(monkey_face) < 0);
309 EXPECT(abcd.CompareTo(domino) < 0);
310 EXPECT(abce.CompareTo(domino) < 0);
311 EXPECT(domino.CompareTo(abcd) > 0);
312 EXPECT(domino.CompareTo(abcd) > 0);
313 EXPECT(monkey_face.CompareTo(abce) > 0);
314 EXPECT(monkey_face.CompareTo(abce) > 0);
315 }
316
317
276 TEST_CASE(Mint) { 318 TEST_CASE(Mint) {
277 // On 64-bit architectures a Smi is stored in a 64 bit word. A Midint cannot 319 // On 64-bit architectures a Smi is stored in a 64 bit word. A Midint cannot
278 // be allocated if it does fit into a Smi. 320 // be allocated if it does fit into a Smi.
279 #if !defined(ARCH_IS_64_BIT) 321 #if !defined(ARCH_IS_64_BIT)
280 { Mint& med = Mint::Handle(); 322 { Mint& med = Mint::Handle();
281 EXPECT(med.IsNull()); 323 EXPECT(med.IsNull());
282 int64_t v = DART_2PART_UINT64_C(1, 0); 324 int64_t v = DART_2PART_UINT64_C(1, 0);
283 med = Mint::New(v); 325 med = Mint::New(v);
284 EXPECT_EQ(v, med.value()); 326 EXPECT_EQ(v, med.value());
285 const String& smi_str = String::Handle(String::New("1")); 327 const String& smi_str = String::Handle(String::New("1"));
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 EXPECT(i.IsSmi()); 497 EXPECT(i.IsSmi());
456 i = Integer::New(String::Handle(String::New("0"))); 498 i = Integer::New(String::Handle(String::New("0")));
457 EXPECT(i.IsSmi()); 499 EXPECT(i.IsSmi());
458 i = Integer::New(String::Handle(String::New("12345678901234567890"))); 500 i = Integer::New(String::Handle(String::New("12345678901234567890")));
459 EXPECT(i.IsBigint()); 501 EXPECT(i.IsBigint());
460 i = Integer::New(String::Handle(String::New("-12345678901234567890111222"))); 502 i = Integer::New(String::Handle(String::New("-12345678901234567890111222")));
461 EXPECT(i.IsBigint()); 503 EXPECT(i.IsBigint());
462 } 504 }
463 505
464 506
507 static unsigned char ToUChar(char x) { return static_cast<unsigned char>(x); }
508
509
465 TEST_CASE(String) { 510 TEST_CASE(String) {
466 const char* kHello = "Hello World!"; 511 const char* kHello = "Hello World!";
467 int32_t hello_len = strlen(kHello); 512 int32_t hello_len = strlen(kHello);
468 const String& str = String::Handle(String::New(kHello)); 513 const String& str = String::Handle(String::New(kHello));
469 EXPECT(str.IsInstance()); 514 EXPECT(str.IsInstance());
470 EXPECT(str.IsString()); 515 EXPECT(str.IsString());
471 EXPECT(str.IsOneByteString()); 516 EXPECT(str.IsOneByteString());
472 EXPECT(!str.IsTwoByteString()); 517 EXPECT(!str.IsTwoByteString());
473 EXPECT(!str.IsNumber()); 518 EXPECT(!str.IsNumber());
474 EXPECT_EQ(hello_len, str.Length()); 519 EXPECT_EQ(hello_len, str.Length());
475 EXPECT_EQ('H', str.CharAt(0)); 520 EXPECT_EQ(ToUChar('H'), str.CodeUnitAt(0));
476 EXPECT_EQ('e', str.CharAt(1)); 521 EXPECT_EQ(ToUChar('e'), str.CodeUnitAt(1));
477 EXPECT_EQ('l', str.CharAt(2)); 522 EXPECT_EQ(ToUChar('l'), str.CodeUnitAt(2));
478 EXPECT_EQ('l', str.CharAt(3)); 523 EXPECT_EQ(ToUChar('l'), str.CodeUnitAt(3));
479 EXPECT_EQ('o', str.CharAt(4)); 524 EXPECT_EQ(ToUChar('o'), str.CodeUnitAt(4));
480 EXPECT_EQ(' ', str.CharAt(5)); 525 EXPECT_EQ(ToUChar(' '), str.CodeUnitAt(5));
481 EXPECT_EQ('W', str.CharAt(6)); 526 EXPECT_EQ(ToUChar('W'), str.CodeUnitAt(6));
482 EXPECT_EQ('o', str.CharAt(7)); 527 EXPECT_EQ(ToUChar('o'), str.CodeUnitAt(7));
483 EXPECT_EQ('r', str.CharAt(8)); 528 EXPECT_EQ(ToUChar('r'), str.CodeUnitAt(8));
484 EXPECT_EQ('l', str.CharAt(9)); 529 EXPECT_EQ(ToUChar('l'), str.CodeUnitAt(9));
485 EXPECT_EQ('d', str.CharAt(10)); 530 EXPECT_EQ(ToUChar('d'), str.CodeUnitAt(10));
486 EXPECT_EQ('!', str.CharAt(11)); 531 EXPECT_EQ(ToUChar('!'), str.CodeUnitAt(11));
487 532
488 const uint8_t* motto = 533 const uint8_t* motto =
489 reinterpret_cast<const uint8_t*>("Dart's bescht wos je hets gits"); 534 reinterpret_cast<const uint8_t*>("Dart's bescht wos je hets gits");
490 const String& str2 = String::Handle(String::New(motto+7, 4)); 535 const String& str2 = String::Handle(String::New(motto+7, 4));
491 EXPECT_EQ(4, str2.Length()); 536 EXPECT_EQ(4, str2.Length());
492 EXPECT_EQ('b', str2.CharAt(0)); 537 EXPECT_EQ(ToUChar('b'), str2.CodeUnitAt(0));
493 EXPECT_EQ('e', str2.CharAt(1)); 538 EXPECT_EQ(ToUChar('e'), str2.CodeUnitAt(1));
494 EXPECT_EQ('s', str2.CharAt(2)); 539 EXPECT_EQ(ToUChar('s'), str2.CodeUnitAt(2));
495 EXPECT_EQ('c', str2.CharAt(3)); 540 EXPECT_EQ(ToUChar('c'), str2.CodeUnitAt(3));
496 541
497 const String& str3 = String::Handle(String::New(kHello)); 542 const String& str3 = String::Handle(String::New(kHello));
498 EXPECT(str.Equals(str)); 543 EXPECT(str.Equals(str));
499 EXPECT_EQ(str.Hash(), str.Hash()); 544 EXPECT_EQ(str.Hash(), str.Hash());
500 EXPECT(!str.Equals(str2)); 545 EXPECT(!str.Equals(str2));
501 EXPECT(str.Equals(str3)); 546 EXPECT(str.Equals(str3));
502 EXPECT_EQ(str.Hash(), str3.Hash()); 547 EXPECT_EQ(str.Hash(), str3.Hash());
503 EXPECT(str3.Equals(str)); 548 EXPECT(str3.Equals(str));
504 549
505 const String& str4 = String::Handle(String::New("foo")); 550 const String& str4 = String::Handle(String::New("foo"));
506 const String& str5 = String::Handle(String::New("bar")); 551 const String& str5 = String::Handle(String::New("bar"));
507 const String& str6 = String::Handle(String::Concat(str4, str5)); 552 const String& str6 = String::Handle(String::Concat(str4, str5));
508 const String& str7 = String::Handle(String::New("foobar")); 553 const String& str7 = String::Handle(String::New("foobar"));
509 EXPECT(str6.Equals(str7)); 554 EXPECT(str6.Equals(str7));
510 EXPECT(!str6.Equals(Smi::Handle(Smi::New(4)))); 555 EXPECT(!str6.Equals(Smi::Handle(Smi::New(4))));
511 556
512 const String& empty1 = String::Handle(String::New("")); 557 const String& empty1 = String::Handle(String::New(""));
513 const String& empty2 = String::Handle(String::New("")); 558 const String& empty2 = String::Handle(String::New(""));
514 EXPECT(empty1.Equals(empty2, 0, 0)); 559 EXPECT(empty1.Equals(empty2, 0, 0));
515 560
516 const intptr_t kCharsLen = 8; 561 const intptr_t kCharsLen = 8;
517 const uint8_t chars[kCharsLen] = { 1, 2, 127, 64, 92, 0, 55, 55 }; 562 const uint8_t chars[kCharsLen] = { 1, 2, 127, 64, 92, 0, 55, 55 };
518 const String& str8 = String::Handle(String::New(chars, kCharsLen)); 563 const String& str8 = String::Handle(String::New(chars, kCharsLen));
519 EXPECT_EQ(kCharsLen, str8.Length()); 564 EXPECT_EQ(kCharsLen, str8.Length());
520 EXPECT_EQ(1, str8.CharAt(0)); 565 EXPECT_EQ(1u, str8.CodeUnitAt(0));
521 EXPECT_EQ(127, str8.CharAt(2)); 566 EXPECT_EQ(127u, str8.CodeUnitAt(2));
522 EXPECT_EQ(64, str8.CharAt(3)); 567 EXPECT_EQ(64u, str8.CodeUnitAt(3));
523 EXPECT_EQ(0, str8.CharAt(5)); 568 EXPECT_EQ(0u, str8.CodeUnitAt(5));
524 EXPECT_EQ(55, str8.CharAt(6)); 569 EXPECT_EQ(55u, str8.CodeUnitAt(6));
525 EXPECT_EQ(55, str8.CharAt(7)); 570 EXPECT_EQ(55u, str8.CodeUnitAt(7));
526 const intptr_t kCharsIndex = 3; 571 const intptr_t kCharsIndex = 3;
527 const String& sub1 = String::Handle(String::SubString(str8, kCharsIndex)); 572 const String& sub1 = String::Handle(String::SubString(str8, kCharsIndex));
528 EXPECT_EQ((kCharsLen - kCharsIndex), sub1.Length()); 573 EXPECT_EQ((kCharsLen - kCharsIndex), sub1.Length());
529 EXPECT_EQ(64, sub1.CharAt(0)); 574 EXPECT_EQ(64u, sub1.CodeUnitAt(0));
530 EXPECT_EQ(92, sub1.CharAt(1)); 575 EXPECT_EQ(92u, sub1.CodeUnitAt(1));
531 EXPECT_EQ(0, sub1.CharAt(2)); 576 EXPECT_EQ(0u, sub1.CodeUnitAt(2));
532 EXPECT_EQ(55, sub1.CharAt(3)); 577 EXPECT_EQ(55u, sub1.CodeUnitAt(3));
533 EXPECT_EQ(55, sub1.CharAt(4)); 578 EXPECT_EQ(55u, sub1.CodeUnitAt(4));
534 579
535 const intptr_t kWideCharsLen = 7; 580 const intptr_t kWideCharsLen = 7;
536 uint16_t wide_chars[kWideCharsLen] = { 'H', 'e', 'l', 'l', 'o', 256, '!' }; 581 uint16_t wide_chars[kWideCharsLen] = { 'H', 'e', 'l', 'l', 'o', 256, '!' };
537 const String& two_str = String::Handle(String::New(wide_chars, 582 const String& two_str = String::Handle(String::New(wide_chars,
538 kWideCharsLen)); 583 kWideCharsLen));
539 EXPECT(two_str.IsInstance()); 584 EXPECT(two_str.IsInstance());
540 EXPECT(two_str.IsString()); 585 EXPECT(two_str.IsString());
541 EXPECT(two_str.IsTwoByteString()); 586 EXPECT(two_str.IsTwoByteString());
542 EXPECT(!two_str.IsOneByteString()); 587 EXPECT(!two_str.IsOneByteString());
543 EXPECT_EQ(kWideCharsLen, two_str.Length()); 588 EXPECT_EQ(kWideCharsLen, two_str.Length());
544 EXPECT_EQ('H', two_str.CharAt(0)); 589 EXPECT_EQ(ToUChar('H'), two_str.CodeUnitAt(0));
545 EXPECT_EQ(256, two_str.CharAt(5)); 590 EXPECT_EQ(256u, two_str.CodeUnitAt(5));
546 const intptr_t kWideCharsIndex = 3; 591 const intptr_t kWideCharsIndex = 3;
547 const String& sub2 = String::Handle(String::SubString(two_str, kCharsIndex)); 592 const String& sub2 = String::Handle(String::SubString(two_str, kCharsIndex));
548 EXPECT_EQ((kWideCharsLen - kWideCharsIndex), sub2.Length()); 593 EXPECT_EQ((kWideCharsLen - kWideCharsIndex), sub2.Length());
549 EXPECT_EQ('l', sub2.CharAt(0)); 594 EXPECT_EQ(ToUChar('l'), sub2.CodeUnitAt(0));
550 EXPECT_EQ('o', sub2.CharAt(1)); 595 EXPECT_EQ(ToUChar('o'), sub2.CodeUnitAt(1));
551 EXPECT_EQ(256, sub2.CharAt(2)); 596 EXPECT_EQ(256u, sub2.CodeUnitAt(2));
552 EXPECT_EQ('!', sub2.CharAt(3)); 597 EXPECT_EQ(ToUChar('!'), sub2.CodeUnitAt(3));
553 598
554 { 599 {
555 const String& str1 = String::Handle(String::New("My.create")); 600 const String& str1 = String::Handle(String::New("My.create"));
556 const String& str2 = String::Handle(String::New("My")); 601 const String& str2 = String::Handle(String::New("My"));
557 const String& str3 = String::Handle(String::New("create")); 602 const String& str3 = String::Handle(String::New("create"));
558 EXPECT_EQ(true, str1.StartsWith(str2)); 603 EXPECT_EQ(true, str1.StartsWith(str2));
559 EXPECT_EQ(false, str1.StartsWith(str3)); 604 EXPECT_EQ(false, str1.StartsWith(str3));
560 } 605 }
561 606
562 const uint32_t four_chars[] = { 'C', 0xFF, 'h', 0xFFFF, 'a', 0x10FFFF, 'r' }; 607 const uint32_t four_chars[] = { 'C', 0xFF, 'h', 0xFFFF, 'a', 0x10FFFF, 'r' };
563 const String& four_str = String::Handle(String::New(four_chars, 7)); 608 const String& four_str = String::Handle(String::New(four_chars, 7));
564 EXPECT_EQ(four_str.Hash(), four_str.Hash()); 609 EXPECT_EQ(four_str.Hash(), four_str.Hash());
565 EXPECT(four_str.IsTwoByteString()); 610 EXPECT(four_str.IsTwoByteString());
566 EXPECT(!four_str.IsOneByteString()); 611 EXPECT(!four_str.IsOneByteString());
567 EXPECT_EQ(8, four_str.Length()); 612 EXPECT_EQ(8, four_str.Length());
568 EXPECT_EQ('C', four_str.CharAt(0)); 613 EXPECT_EQ(ToUChar('C'), four_str.CodeUnitAt(0));
569 EXPECT_EQ(0xFF, four_str.CharAt(1)); 614 EXPECT_EQ(0xFFu, four_str.CodeUnitAt(1));
570 EXPECT_EQ('h', four_str.CharAt(2)); 615 EXPECT_EQ(ToUChar('h'), four_str.CodeUnitAt(2));
571 EXPECT_EQ(0xFFFF, four_str.CharAt(3)); 616 EXPECT_EQ(0xFFFFu, four_str.CodeUnitAt(3));
572 EXPECT_EQ('a', four_str.CharAt(4)); 617 EXPECT_EQ(ToUChar('a'), four_str.CodeUnitAt(4));
573 EXPECT_EQ(0xDBFF, four_str.CharAt(5)); 618 EXPECT_EQ(0xDBFFu, four_str.CodeUnitAt(5));
574 EXPECT_EQ(0xDFFF, four_str.CharAt(6)); 619 EXPECT_EQ(0xDFFFu, four_str.CodeUnitAt(6));
575 EXPECT_EQ('r', four_str.CharAt(7)); 620 EXPECT_EQ(ToUChar('r'), four_str.CodeUnitAt(7));
576 621
577 // Create a 1-byte string from an array of 2-byte elements. 622 // Create a 1-byte string from an array of 2-byte elements.
578 { 623 {
579 const uint16_t char16[] = { 0x00, 0x1F, 0x7F }; 624 const uint16_t char16[] = { 0x00, 0x1F, 0x7F };
580 const String& str8 = String::Handle(String::New(char16, 3)); 625 const String& str8 = String::Handle(String::New(char16, 3));
581 EXPECT(str8.IsOneByteString()); 626 EXPECT(str8.IsOneByteString());
582 EXPECT(!str8.IsTwoByteString()); 627 EXPECT(!str8.IsTwoByteString());
583 EXPECT_EQ(0x00, str8.CharAt(0)); 628 EXPECT_EQ(0x00u, str8.CodeUnitAt(0));
584 EXPECT_EQ(0x1F, str8.CharAt(1)); 629 EXPECT_EQ(0x1Fu, str8.CodeUnitAt(1));
585 EXPECT_EQ(0x7F, str8.CharAt(2)); 630 EXPECT_EQ(0x7Fu, str8.CodeUnitAt(2));
586 } 631 }
587 632
588 // Create a 1-byte string from an array of 4-byte elements. 633 // Create a 1-byte string from an array of 4-byte elements.
589 { 634 {
590 const uint32_t char32[] = { 0x00, 0x1F, 0x7F }; 635 const uint32_t char32[] = { 0x00, 0x1F, 0x7F };
591 const String& str8 = String::Handle(String::New(char32, 3)); 636 const String& str8 = String::Handle(String::New(char32, 3));
592 EXPECT(str8.IsOneByteString()); 637 EXPECT(str8.IsOneByteString());
593 EXPECT(!str8.IsTwoByteString()); 638 EXPECT(!str8.IsTwoByteString());
594 EXPECT_EQ(0x00, str8.CharAt(0)); 639 EXPECT_EQ(0x00u, str8.CodeUnitAt(0));
595 EXPECT_EQ(0x1F, str8.CharAt(1)); 640 EXPECT_EQ(0x1Fu, str8.CodeUnitAt(1));
596 EXPECT_EQ(0x7F, str8.CharAt(2)); 641 EXPECT_EQ(0x7Fu, str8.CodeUnitAt(2));
597 } 642 }
598 643
599 // Create a 2-byte string from an array of 4-byte elements. 644 // Create a 2-byte string from an array of 4-byte elements.
600 { 645 {
601 const uint32_t char32[] = { 0, 0x7FFF, 0xFFFF }; 646 const uint32_t char32[] = { 0, 0x7FFF, 0xFFFF };
602 const String& str16 = String::Handle(String::New(char32, 3)); 647 const String& str16 = String::Handle(String::New(char32, 3));
603 EXPECT(!str16.IsOneByteString()); 648 EXPECT(!str16.IsOneByteString());
604 EXPECT(str16.IsTwoByteString()); 649 EXPECT(str16.IsTwoByteString());
605 EXPECT_EQ(0x0000, str16.CharAt(0)); 650 EXPECT_EQ(0x0000u, str16.CodeUnitAt(0));
606 EXPECT_EQ(0x7FFF, str16.CharAt(1)); 651 EXPECT_EQ(0x7FFFu, str16.CodeUnitAt(1));
607 EXPECT_EQ(0xFFFF, str16.CharAt(2)); 652 EXPECT_EQ(0xFFFFu, str16.CodeUnitAt(2));
608 } 653 }
609 } 654 }
610 655
611 656
612 TEST_CASE(StringFormat) { 657 TEST_CASE(StringFormat) {
613 const char* hello_str = "Hello World!"; 658 const char* hello_str = "Hello World!";
614 const String& str = 659 const String& str =
615 String::Handle(String::NewFormatted("Hello %s!", "World")); 660 String::Handle(String::NewFormatted("Hello %s!", "World"));
616 EXPECT(str.IsInstance()); 661 EXPECT(str.IsInstance());
617 EXPECT(str.IsString()); 662 EXPECT(str.IsString());
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after
1258 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 1303 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7,
1259 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 1304 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
1260 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 1305 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
1261 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 1306 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
1262 }; 1307 };
1263 const String& str = String::Handle(String::New(src)); 1308 const String& str = String::Handle(String::New(src));
1264 EXPECT(str.IsTwoByteString()); 1309 EXPECT(str.IsTwoByteString());
1265 intptr_t expected_length = sizeof(expected); 1310 intptr_t expected_length = sizeof(expected);
1266 EXPECT_EQ(expected_length, str.Length()); 1311 EXPECT_EQ(expected_length, str.Length());
1267 for (int i = 0; i < str.Length(); ++i) { 1312 for (int i = 0; i < str.Length(); ++i) {
1268 EXPECT_EQ(expected[i], str.CharAt(i)); 1313 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1269 } 1314 }
1270 } 1315 }
1271 1316
1272 // Create a 2-byte string from a UTF-8 encoded string literal. 1317 // Create a 2-byte string from a UTF-8 encoded string literal.
1273 { 1318 {
1274 const char* src = 1319 const char* src =
1275 "\xD7\x92\xD7\x9C\xD7\xA2\xD7\x93" 1320 "\xD7\x92\xD7\x9C\xD7\xA2\xD7\x93"
1276 "\xD7\x91\xD7\xA8\xD7\x9B\xD7\x94"; 1321 "\xD7\x91\xD7\xA8\xD7\x9B\xD7\x94";
1277 const uint16_t expected[] = { 1322 const uint16_t expected[] = {
1278 0x5D2, 0x5DC, 0x5E2, 0x5D3, 1323 0x5D2, 0x5DC, 0x5E2, 0x5D3,
1279 0x5D1, 0x5E8, 0x5DB, 0x5D4 1324 0x5D1, 0x5E8, 0x5DB, 0x5D4
1280 }; 1325 };
1281 const String& str = String::Handle(String::New(src)); 1326 const String& str = String::Handle(String::New(src));
1282 EXPECT(str.IsTwoByteString()); 1327 EXPECT(str.IsTwoByteString());
1283 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]); 1328 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]);
1284 EXPECT_EQ(expected_size, str.Length()); 1329 EXPECT_EQ(expected_size, str.Length());
1285 for (int i = 0; i < str.Length(); ++i) { 1330 for (int i = 0; i < str.Length(); ++i) {
1286 EXPECT_EQ(expected[i], str.CharAt(i)); 1331 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1287 } 1332 }
1288 } 1333 }
1289 1334
1290 // Create a BMP 2-byte string from UTF-8 encoded 1- and 2-byte 1335 // Create a BMP 2-byte string from UTF-8 encoded 1- and 2-byte
1291 // characters. 1336 // characters.
1292 { 1337 {
1293 const char* src = 1338 const char* src =
1294 "\x0A\x0B\x0D\x0C\x0E\x0F\xC2\xA0" 1339 "\x0A\x0B\x0D\x0C\x0E\x0F\xC2\xA0"
1295 "\xC2\xB0\xC3\x80\xC3\x90\xC3\xA0" 1340 "\xC2\xB0\xC3\x80\xC3\x90\xC3\xA0"
1296 "\xC3\xB0\xE0\xA8\x80\xE0\xAC\x80" 1341 "\xC3\xB0\xE0\xA8\x80\xE0\xAC\x80"
1297 "\xE0\xB0\x80\xE0\xB4\x80\xE0\xB8" 1342 "\xE0\xB0\x80\xE0\xB4\x80\xE0\xB8"
1298 "\x80\xE0\xBC\x80\xEA\x80\x80\xEB" 1343 "\x80\xE0\xBC\x80\xEA\x80\x80\xEB"
1299 "\x80\x80\xEC\x80\x80\xED\x80\x80" 1344 "\x80\x80\xEC\x80\x80\xED\x80\x80"
1300 "\xEE\x80\x80\xEF\x80\x80"; 1345 "\xEE\x80\x80\xEF\x80\x80";
1301 const intptr_t expected[] = { 1346 const uint32_t expected[] = {
1302 0x000A, 0x000B, 0x000D, 0x000C, 0x000E, 0x000F, 0x00A0, 0x00B0, 1347 0x000A, 0x000B, 0x000D, 0x000C, 0x000E, 0x000F, 0x00A0, 0x00B0,
1303 0x00C0, 0x00D0, 0x00E0, 0x00F0, 0x0A00, 0x0B00, 0x0C00, 0x0D00, 1348 0x00C0, 0x00D0, 0x00E0, 0x00F0, 0x0A00, 0x0B00, 0x0C00, 0x0D00,
1304 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000 1349 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000
1305 }; 1350 };
1306 const String& str = String::Handle(String::New(src)); 1351 const String& str = String::Handle(String::New(src));
1307 EXPECT(str.IsTwoByteString()); 1352 EXPECT(str.IsTwoByteString());
1308 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]); 1353 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]);
1309 EXPECT_EQ(expected_size, str.Length()); 1354 EXPECT_EQ(expected_size, str.Length());
1310 for (int i = 0; i < str.Length(); ++i) { 1355 for (int i = 0; i < str.Length(); ++i) {
1311 EXPECT_EQ(expected[i], str.CharAt(i)); 1356 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1312 } 1357 }
1313 } 1358 }
1314 1359
1315 // Create a SMP 2-byte string from a UTF-8 string literal. 1360 // Create a SMP 2-byte string from a UTF-8 string literal.
1316 { 1361 {
1317 const char* src = 1362 const char* src =
1318 "\xF0\x9D\x91\xA0\xF0\x9D\x91\xA1" 1363 "\xF0\x9D\x91\xA0\xF0\x9D\x91\xA1"
1319 "\xF0\x9D\x91\xA2\xF0\x9D\x91\xA3"; 1364 "\xF0\x9D\x91\xA2\xF0\x9D\x91\xA3";
1320 const intptr_t expected[] = { 0xd835, 0xdc60, 0xd835, 0xdc61, 1365 const uint32_t expected[] = { 0xd835, 0xdc60, 0xd835, 0xdc61,
1321 0xd835, 0xdc62, 0xd835, 0xdc63 }; 1366 0xd835, 0xdc62, 0xd835, 0xdc63 };
1322 const String& str = String::Handle(String::New(src)); 1367 const String& str = String::Handle(String::New(src));
1323 EXPECT(str.IsTwoByteString()); 1368 EXPECT(str.IsTwoByteString());
1324 intptr_t expected_size = (sizeof(expected) / sizeof(expected[0])); 1369 intptr_t expected_size = (sizeof(expected) / sizeof(expected[0]));
1325 EXPECT_EQ(expected_size, str.Length()); 1370 EXPECT_EQ(expected_size, str.Length());
1326 for (int i = 0; i < str.Length(); ++i) { 1371 for (int i = 0; i < str.Length(); ++i) {
1327 EXPECT_EQ(expected[i], str.CharAt(i)); 1372 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1328 } 1373 }
1329 } 1374 }
1330 1375
1331 // Create a 2-byte string from UTF-8 encoded 2- and 4-byte 1376 // Create a 2-byte string from UTF-8 encoded 2- and 4-byte
1332 // characters. 1377 // characters.
1333 { 1378 {
1334 const char* src = 1379 const char* src =
1335 "\xE0\xA8\x80\xE0\xAC\x80\xE0\xB0" 1380 "\xE0\xA8\x80\xE0\xAC\x80\xE0\xB0"
1336 "\x80\xE0\xB4\x80\xE0\xB8\x80\xE0" 1381 "\x80\xE0\xB4\x80\xE0\xB8\x80\xE0"
1337 "\xBC\x80\xEA\x80\x80\xEB\x80\x80" 1382 "\xBC\x80\xEA\x80\x80\xEB\x80\x80"
1338 "\xEC\x80\x80\xED\x80\x80\xEE\x80" 1383 "\xEC\x80\x80\xED\x80\x80\xEE\x80"
1339 "\x80\xEF\x80\x80\xF0\x9A\x80\x80" 1384 "\x80\xEF\x80\x80\xF0\x9A\x80\x80"
1340 "\xF0\x9B\x80\x80\xF0\x9D\x80\x80" 1385 "\xF0\x9B\x80\x80\xF0\x9D\x80\x80"
1341 "\xF0\x9E\x80\x80\xF0\x9F\x80\x80"; 1386 "\xF0\x9E\x80\x80\xF0\x9F\x80\x80";
1342 const intptr_t expected[] = { 1387 const uint32_t expected[] = {
1343 0x0A00, 0x0B00, 0x0C00, 0x0D00, 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000, 1388 0x0A00, 0x0B00, 0x0C00, 0x0D00, 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000,
1344 0xD000, 0xE000, 0xF000, 0xD828, 0xDC00, 0xD82c, 0xDC00, 0xD834, 0xDC00, 1389 0xD000, 0xE000, 0xF000, 0xD828, 0xDC00, 0xD82c, 0xDC00, 0xD834, 0xDC00,
1345 0xD838, 0xDC00, 0xD83c, 0xDC00, 1390 0xD838, 0xDC00, 0xD83c, 0xDC00,
1346 }; 1391 };
1347 const String& str = String::Handle(String::New(src)); 1392 const String& str = String::Handle(String::New(src));
1348 EXPECT(str.IsTwoByteString()); 1393 EXPECT(str.IsTwoByteString());
1349 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]); 1394 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]);
1350 EXPECT_EQ(expected_size, str.Length()); 1395 EXPECT_EQ(expected_size, str.Length());
1351 for (int i = 0; i < str.Length(); ++i) { 1396 for (int i = 0; i < str.Length(); ++i) {
1352 EXPECT_EQ(expected[i], str.CharAt(i)); 1397 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1353 } 1398 }
1354 } 1399 }
1355 1400
1356 // Create a 2-byte string from UTF-8 encoded 1-, 2- and 4-byte 1401 // Create a 2-byte string from UTF-8 encoded 1-, 2- and 4-byte
1357 // characters. 1402 // characters.
1358 { 1403 {
1359 const char* src = 1404 const char* src =
1360 "\x0A\x0B\x0D\x0C\x0E\x0F\xC2\xA0" 1405 "\x0A\x0B\x0D\x0C\x0E\x0F\xC2\xA0"
1361 "\xC2\xB0\xC3\x80\xC3\x90\xC3\xA0" 1406 "\xC2\xB0\xC3\x80\xC3\x90\xC3\xA0"
1362 "\xC3\xB0\xE0\xA8\x80\xE0\xAC\x80" 1407 "\xC3\xB0\xE0\xA8\x80\xE0\xAC\x80"
1363 "\xE0\xB0\x80\xE0\xB4\x80\xE0\xB8" 1408 "\xE0\xB0\x80\xE0\xB4\x80\xE0\xB8"
1364 "\x80\xE0\xBC\x80\xEA\x80\x80\xEB" 1409 "\x80\xE0\xBC\x80\xEA\x80\x80\xEB"
1365 "\x80\x80\xEC\x80\x80\xED\x80\x80" 1410 "\x80\x80\xEC\x80\x80\xED\x80\x80"
1366 "\xEE\x80\x80\xEF\x80\x80\xF0\x9A" 1411 "\xEE\x80\x80\xEF\x80\x80\xF0\x9A"
1367 "\x80\x80\xF0\x9B\x80\x80\xF0\x9D" 1412 "\x80\x80\xF0\x9B\x80\x80\xF0\x9D"
1368 "\x80\x80\xF0\x9E\x80\x80\xF0\x9F" 1413 "\x80\x80\xF0\x9E\x80\x80\xF0\x9F"
1369 "\x80\x80"; 1414 "\x80\x80";
1370 const intptr_t expected[] = { 1415 const uint32_t expected[] = {
1371 0x000A, 0x000B, 0x000D, 0x000C, 0x000E, 0x000F, 0x00A0, 0x00B0, 1416 0x000A, 0x000B, 0x000D, 0x000C, 0x000E, 0x000F, 0x00A0, 0x00B0,
1372 0x00C0, 0x00D0, 0x00E0, 0x00F0, 0x0A00, 0x0B00, 0x0C00, 0x0D00, 1417 0x00C0, 0x00D0, 0x00E0, 0x00F0, 0x0A00, 0x0B00, 0x0C00, 0x0D00,
1373 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000, 1418 0x0E00, 0x0F00, 0xA000, 0xB000, 0xC000, 0xD000, 0xE000, 0xF000,
1374 0xD828, 0xDC00, 0xD82c, 0xDC00, 0xD834, 0xDC00, 0xD838, 0xDC00, 1419 0xD828, 0xDC00, 0xD82c, 0xDC00, 0xD834, 0xDC00, 0xD838, 0xDC00,
1375 0xD83c, 0xDC00, 1420 0xD83c, 0xDC00,
1376 }; 1421 };
1377 const String& str = String::Handle(String::New(src)); 1422 const String& str = String::Handle(String::New(src));
1378 EXPECT(str.IsTwoByteString()); 1423 EXPECT(str.IsTwoByteString());
1379 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]); 1424 intptr_t expected_size = sizeof(expected) / sizeof(expected[0]);
1380 EXPECT_EQ(expected_size, str.Length()); 1425 EXPECT_EQ(expected_size, str.Length());
1381 for (int i = 0; i < str.Length(); ++i) { 1426 for (int i = 0; i < str.Length(); ++i) {
1382 EXPECT_EQ(expected[i], str.CharAt(i)); 1427 EXPECT_EQ(expected[i], str.CodeUnitAt(i));
1383 } 1428 }
1384 } 1429 }
1385 } 1430 }
1386 1431
1387 1432
1388 TEST_CASE(ExternalOneByteString) { 1433 TEST_CASE(ExternalOneByteString) {
1389 uint8_t characters[] = { 0xF6, 0xF1, 0xE9 }; 1434 uint8_t characters[] = { 0xF6, 0xF1, 0xE9 };
1390 intptr_t len = ARRAY_SIZE(characters); 1435 intptr_t len = ARRAY_SIZE(characters);
1391 1436
1392 const String& str = 1437 const String& str =
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 const char* source_chars = "This will not compile."; 2018 const char* source_chars = "This will not compile.";
1974 const String& url = String::Handle(String::New(url_chars)); 2019 const String& url = String::Handle(String::New(url_chars));
1975 const String& source = String::Handle(String::New(source_chars)); 2020 const String& source = String::Handle(String::New(source_chars));
1976 const Script& script = Script::Handle(Script::New(url, 2021 const Script& script = Script::Handle(Script::New(url,
1977 source, 2022 source,
1978 RawScript::kSourceTag)); 2023 RawScript::kSourceTag));
1979 EXPECT(!script.IsNull()); 2024 EXPECT(!script.IsNull());
1980 EXPECT(script.IsScript()); 2025 EXPECT(script.IsScript());
1981 String& str = String::Handle(script.url()); 2026 String& str = String::Handle(script.url());
1982 EXPECT_EQ(17, str.Length()); 2027 EXPECT_EQ(17, str.Length());
1983 EXPECT_EQ('b', str.CharAt(0)); 2028 EXPECT_EQ(ToUChar('b'), str.CodeUnitAt(0));
1984 EXPECT_EQ(':', str.CharAt(7)); 2029 EXPECT_EQ(ToUChar(':'), str.CodeUnitAt(7));
1985 EXPECT_EQ('e', str.CharAt(16)); 2030 EXPECT_EQ(ToUChar('e'), str.CodeUnitAt(16));
1986 str = script.Source(); 2031 str = script.Source();
1987 EXPECT_EQ(22, str.Length()); 2032 EXPECT_EQ(22, str.Length());
1988 EXPECT_EQ('T', str.CharAt(0)); 2033 EXPECT_EQ(ToUChar('T'), str.CodeUnitAt(0));
1989 EXPECT_EQ('n', str.CharAt(10)); 2034 EXPECT_EQ(ToUChar('n'), str.CodeUnitAt(10));
1990 EXPECT_EQ('.', str.CharAt(21)); 2035 EXPECT_EQ(ToUChar('.'), str.CodeUnitAt(21));
1991 } 2036 }
1992 2037
1993 2038
1994 TEST_CASE(Context) { 2039 TEST_CASE(Context) {
1995 const int kNumVariables = 5; 2040 const int kNumVariables = 5;
1996 const Context& parent_context = Context::Handle(Context::New(0)); 2041 const Context& parent_context = Context::Handle(Context::New(0));
1997 EXPECT_EQ(parent_context.isolate(), Isolate::Current()); 2042 EXPECT_EQ(parent_context.isolate(), Isolate::Current());
1998 const Context& context = Context::Handle(Context::New(kNumVariables)); 2043 const Context& context = Context::Handle(Context::New(kNumVariables));
1999 context.set_parent(parent_context); 2044 context.set_parent(parent_context);
2000 const Context& check_parent_context = Context::Handle(context.parent()); 2045 const Context& check_parent_context = Context::Handle(context.parent());
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
2220 Code& code = Code::Handle(Code::FinalizeCode( 2265 Code& code = Code::Handle(Code::FinalizeCode(
2221 *CreateFunction("Test_EmbedStringInCode"), &_assembler_)); 2266 *CreateFunction("Test_EmbedStringInCode"), &_assembler_));
2222 Instructions& instructions = Instructions::Handle(code.instructions()); 2267 Instructions& instructions = Instructions::Handle(code.instructions());
2223 typedef uword (*EmbedStringCode)(); 2268 typedef uword (*EmbedStringCode)();
2224 uword retval = reinterpret_cast<EmbedStringCode>(instructions.EntryPoint())(); 2269 uword retval = reinterpret_cast<EmbedStringCode>(instructions.EntryPoint())();
2225 EXPECT((retval & kSmiTagMask) == kHeapObjectTag); 2270 EXPECT((retval & kSmiTagMask) == kHeapObjectTag);
2226 String& string_object = String::Handle(); 2271 String& string_object = String::Handle();
2227 string_object ^= reinterpret_cast<RawInstructions*>(retval); 2272 string_object ^= reinterpret_cast<RawInstructions*>(retval);
2228 EXPECT(string_object.Length() == expected_length); 2273 EXPECT(string_object.Length() == expected_length);
2229 for (int i = 0; i < expected_length; i ++) { 2274 for (int i = 0; i < expected_length; i ++) {
2230 EXPECT(string_object.CharAt(i) == kHello[i]); 2275 EXPECT(string_object.CodeUnitAt(i) == static_cast<uint32_t>(kHello[i]));
2231 } 2276 }
2232 } 2277 }
2233 2278
2234 2279
2235 // Test for Embedded Smi object in the instructions. 2280 // Test for Embedded Smi object in the instructions.
2236 TEST_CASE(EmbedSmiInCode) { 2281 TEST_CASE(EmbedSmiInCode) {
2237 extern void GenerateEmbedSmiInCode(Assembler* assembler, intptr_t value); 2282 extern void GenerateEmbedSmiInCode(Assembler* assembler, intptr_t value);
2238 const intptr_t kSmiTestValue = 5; 2283 const intptr_t kSmiTestValue = 5;
2239 Assembler _assembler_; 2284 Assembler _assembler_;
2240 GenerateEmbedSmiInCode(&_assembler_, kSmiTestValue); 2285 GenerateEmbedSmiInCode(&_assembler_, kSmiTestValue);
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after
3031 isolate->heap()->CollectAllGarbage(); 3076 isolate->heap()->CollectAllGarbage();
3032 EXPECT(weak1.key() == Object::null()); 3077 EXPECT(weak1.key() == Object::null());
3033 EXPECT(weak1.value() == Object::null()); 3078 EXPECT(weak1.value() == Object::null());
3034 EXPECT(weak2.key() == Object::null()); 3079 EXPECT(weak2.key() == Object::null());
3035 EXPECT(weak2.value() == Object::null()); 3080 EXPECT(weak2.value() == Object::null());
3036 } 3081 }
3037 3082
3038 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 3083 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
3039 3084
3040 } // namespace dart 3085 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698