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

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

Issue 119673004: Version 1.1.0-dev.5.2 (Closed) Base URL: http://dart.googlecode.com/svn/trunk/
Patch Set: Created 6 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
« no previous file with comments | « dart/runtime/vm/object.cc ('k') | dart/runtime/vm/pages.cc » ('j') | 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 "vm/assembler.h" 5 #include "vm/assembler.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/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_entry.h" 9 #include "vm/dart_entry.h"
10 #include "vm/debugger.h" 10 #include "vm/debugger.h"
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 EXPECT(abce.CompareTo(monkey_face) < 0); 325 EXPECT(abce.CompareTo(monkey_face) < 0);
326 EXPECT(abcd.CompareTo(domino) < 0); 326 EXPECT(abcd.CompareTo(domino) < 0);
327 EXPECT(abce.CompareTo(domino) < 0); 327 EXPECT(abce.CompareTo(domino) < 0);
328 EXPECT(domino.CompareTo(abcd) > 0); 328 EXPECT(domino.CompareTo(abcd) > 0);
329 EXPECT(domino.CompareTo(abcd) > 0); 329 EXPECT(domino.CompareTo(abcd) > 0);
330 EXPECT(monkey_face.CompareTo(abce) > 0); 330 EXPECT(monkey_face.CompareTo(abce) > 0);
331 EXPECT(monkey_face.CompareTo(abce) > 0); 331 EXPECT(monkey_face.CompareTo(abce) > 0);
332 } 332 }
333 333
334 334
335 TEST_CASE(StringEncodeURI) {
336 const char* kInput =
337 "file:///usr/local/johnmccutchan/workspace/dart-repo/dart/test.dart";
338 const char* kOutput =
339 "file%3A%2F%2F%2Fusr%2Flocal%2Fjohnmccutchan%2Fworkspace%2F"
340 "dart-repo%2Fdart%2Ftest.dart";
341 const String& input = String::Handle(String::New(kInput));
342 const String& output = String::Handle(String::New(kOutput));
343 const String& encoded = String::Handle(String::EncodeURI(input));
344 EXPECT(output.Equals(encoded));
345 }
346
347
348 TEST_CASE(StringDecodeURI) {
349 const char* kOutput =
350 "file:///usr/local/johnmccutchan/workspace/dart-repo/dart/test.dart";
351 const char* kInput =
352 "file%3A%2F%2F%2Fusr%2Flocal%2Fjohnmccutchan%2Fworkspace%2F"
353 "dart-repo%2Fdart%2Ftest.dart";
354 const String& input = String::Handle(String::New(kInput));
355 const String& output = String::Handle(String::New(kOutput));
356 const String& decoded = String::Handle(String::DecodeURI(input));
357 EXPECT(output.Equals(decoded));
358 }
359
360
335 TEST_CASE(Mint) { 361 TEST_CASE(Mint) {
336 // On 64-bit architectures a Smi is stored in a 64 bit word. A Midint cannot 362 // On 64-bit architectures a Smi is stored in a 64 bit word. A Midint cannot
337 // be allocated if it does fit into a Smi. 363 // be allocated if it does fit into a Smi.
338 #if !defined(ARCH_IS_64_BIT) 364 #if !defined(ARCH_IS_64_BIT)
339 { Mint& med = Mint::Handle(); 365 { Mint& med = Mint::Handle();
340 EXPECT(med.IsNull()); 366 EXPECT(med.IsNull());
341 int64_t v = DART_2PART_UINT64_C(1, 0); 367 int64_t v = DART_2PART_UINT64_C(1, 0);
342 med ^= Integer::New(v); 368 med ^= Integer::New(v);
343 EXPECT_EQ(v, med.value()); 369 EXPECT_EQ(v, med.value());
344 const String& smi_str = String::Handle(String::New("1")); 370 const String& smi_str = String::Handle(String::New("1"));
(...skipping 2397 matching lines...) Expand 10 before | Expand all | Expand 10 after
2742 EXPECT(!Field::IsSetterName(getter_f)); 2768 EXPECT(!Field::IsSetterName(getter_f));
2743 EXPECT(!Field::IsGetterName(setter_f)); 2769 EXPECT(!Field::IsGetterName(setter_f));
2744 EXPECT(Field::IsSetterName(setter_f)); 2770 EXPECT(Field::IsSetterName(setter_f));
2745 EXPECT_STREQ(f.ToCString(), 2771 EXPECT_STREQ(f.ToCString(),
2746 String::Handle(Field::NameFromGetter(getter_f)).ToCString()); 2772 String::Handle(Field::NameFromGetter(getter_f)).ToCString());
2747 EXPECT_STREQ(f.ToCString(), 2773 EXPECT_STREQ(f.ToCString(),
2748 String::Handle(Field::NameFromSetter(setter_f)).ToCString()); 2774 String::Handle(Field::NameFromSetter(setter_f)).ToCString());
2749 } 2775 }
2750 2776
2751 2777
2778
2779
2752 // Expose helper function from object.cc for testing. 2780 // Expose helper function from object.cc for testing.
2753 bool EqualsIgnoringPrivate(const String& name, const String& private_name); 2781 bool EqualsIgnoringPrivate(const String& name, const String& private_name);
2754 2782
2755 2783
2756 TEST_CASE(EqualsIgnoringPrivate) { 2784 TEST_CASE(EqualsIgnoringPrivate) {
2757 String& mangled_name = String::Handle(); 2785 String& mangled_name = String::Handle();
2758 String& bare_name = String::Handle(); 2786 String& bare_name = String::Handle();
2759 2787
2760 // Simple matches. 2788 // Simple matches.
2761 mangled_name = OneByteString::New("foo"); 2789 mangled_name = OneByteString::New("foo");
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
3384 3412
3385 3413
3386 static RawClass* GetClass(const Library& lib, const char* name) { 3414 static RawClass* GetClass(const Library& lib, const char* name) {
3387 const Class& cls = Class::Handle( 3415 const Class& cls = Class::Handle(
3388 lib.LookupClass(String::Handle(Symbols::New(name)))); 3416 lib.LookupClass(String::Handle(Symbols::New(name))));
3389 EXPECT(!cls.IsNull()); // No ambiguity error expected. 3417 EXPECT(!cls.IsNull()); // No ambiguity error expected.
3390 return cls.raw(); 3418 return cls.raw();
3391 } 3419 }
3392 3420
3393 3421
3422 TEST_CASE(FindFieldIndex) {
3423 const char* kScriptChars =
3424 "class A {\n"
3425 " var a;\n"
3426 " var b;\n"
3427 "}\n"
3428 "class B {\n"
3429 " var d;\n"
3430 "}\n"
3431 "test() {\n"
3432 " new A();\n"
3433 " new B();\n"
3434 "}";
3435 Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL);
3436 EXPECT_VALID(h_lib);
3437 Dart_Handle result = Dart_Invoke(h_lib, NewString("test"), 0, NULL);
3438 EXPECT_VALID(result);
3439 Library& lib = Library::Handle();
3440 lib ^= Api::UnwrapHandle(h_lib);
3441 EXPECT(!lib.IsNull());
3442 const Class& class_a = Class::Handle(GetClass(lib, "A"));
3443 const Array& class_a_fields = Array::Handle(class_a.fields());
3444 const Class& class_b = Class::Handle(GetClass(lib, "B"));
3445 const Field& field_a = Field::Handle(GetField(class_a, "a"));
3446 const Field& field_b = Field::Handle(GetField(class_a, "b"));
3447 const Field& field_d = Field::Handle(GetField(class_b, "d"));
3448 intptr_t field_a_index = class_a.FindFieldIndex(field_a);
3449 intptr_t field_b_index = class_a.FindFieldIndex(field_b);
3450 intptr_t field_d_index = class_a.FindFieldIndex(field_d);
3451 // Valid index.
3452 EXPECT_GE(field_a_index, 0);
3453 // Valid index.
3454 EXPECT_GE(field_b_index, 0);
3455 // Invalid index.
3456 EXPECT_EQ(field_d_index, -1);
3457 Field& field_a_from_index = Field::Handle();
3458 field_a_from_index ^= class_a_fields.At(field_a_index);
3459 ASSERT(!field_a_from_index.IsNull());
3460 // Same field.
3461 EXPECT_EQ(field_a.raw(), field_a_from_index.raw());
3462 Field& field_b_from_index = Field::Handle();
3463 field_b_from_index ^= class_a_fields.At(field_b_index);
3464 ASSERT(!field_b_from_index.IsNull());
3465 // Same field.
3466 EXPECT_EQ(field_b.raw(), field_b_from_index.raw());
3467 }
3468
3469
3470 TEST_CASE(FindFunctionIndex) {
3471 // Tests both FindFunctionIndex and FindImplicitClosureFunctionIndex.
3472 const char* kScriptChars =
3473 "class A {\n"
3474 " void a() {}\n"
3475 " Function b() { return a; }\n"
3476 "}\n"
3477 "class B {\n"
3478 " dynamic d() {}\n"
3479 "}\n"
3480 "var x;\n"
3481 "test() {\n"
3482 " x = new A().b();\n"
3483 " x();\n"
3484 " new B();\n"
3485 " return x;\n"
3486 "}";
3487 Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL);
3488 EXPECT_VALID(h_lib);
3489 Dart_Handle result = Dart_Invoke(h_lib, NewString("test"), 0, NULL);
3490 EXPECT_VALID(result);
3491 Library& lib = Library::Handle();
3492 lib ^= Api::UnwrapHandle(h_lib);
3493 EXPECT(!lib.IsNull());
3494 const Class& class_a = Class::Handle(GetClass(lib, "A"));
3495 const Class& class_b = Class::Handle(GetClass(lib, "B"));
3496 const Function& func_a = Function::Handle(GetFunction(class_a, "a"));
3497 const Function& func_b = Function::Handle(GetFunction(class_a, "b"));
3498 const Function& func_d = Function::Handle(GetFunction(class_b, "d"));
3499 EXPECT(func_a.HasImplicitClosureFunction());
3500 const Function& func_x = Function::Handle(func_a.ImplicitClosureFunction());
3501 intptr_t func_a_index = class_a.FindFunctionIndex(func_a);
3502 intptr_t func_b_index = class_a.FindFunctionIndex(func_b);
3503 intptr_t func_d_index = class_a.FindFunctionIndex(func_d);
3504 intptr_t func_x_index = class_a.FindImplicitClosureFunctionIndex(func_x);
3505 // Valid index.
3506 EXPECT_GE(func_a_index, 0);
3507 // Valid index.
3508 EXPECT_GE(func_b_index, 0);
3509 // Invalid index.
3510 EXPECT_EQ(func_d_index, -1);
3511 // Valid index.
3512 EXPECT_GE(func_x_index, 0);
3513 Function& func_a_from_index = Function::Handle();
3514 func_a_from_index ^= class_a.FunctionFromIndex(func_a_index);
3515 EXPECT(!func_a_from_index.IsNull());
3516 // Same function.
3517 EXPECT_EQ(func_a.raw(), func_a_from_index.raw());
3518 Function& func_b_from_index = Function::Handle();
3519 func_b_from_index ^= class_a.FunctionFromIndex(func_b_index);
3520 EXPECT(!func_b_from_index.IsNull());
3521 // Same function.
3522 EXPECT_EQ(func_b.raw(), func_b_from_index.raw());
3523 // Retrieve implicit closure function.
3524 Function& func_x_from_index = Function::Handle();
3525 func_x_from_index ^= class_a.ImplicitClosureFunctionFromIndex(func_x_index);
3526 EXPECT_EQ(func_x.raw(), func_x_from_index.raw());
3527 }
3528
3529
3530 TEST_CASE(FindClosureIndex) {
3531 // Allocate the class first.
3532 const String& class_name = String::Handle(Symbols::New("MyClass"));
3533 const Script& script = Script::Handle();
3534 const Class& cls = Class::Handle(CreateDummyClass(class_name, script));
3535 const Array& functions = Array::Handle(Array::New(1));
3536
3537 Function& parent = Function::Handle();
3538 const String& parent_name = String::Handle(Symbols::New("foo_papa"));
3539 parent = Function::New(parent_name, RawFunction::kRegularFunction,
3540 false, false, false, false, false, cls, 0);
3541 functions.SetAt(0, parent);
3542 cls.SetFunctions(functions);
3543
3544 Function& function = Function::Handle();
3545 const String& function_name = String::Handle(Symbols::New("foo"));
3546 function = Function::NewClosureFunction(function_name, parent, 0);
3547 // Add closure function to class.
3548 cls.AddClosureFunction(function);
3549
3550 // The closure should return a valid index.
3551 intptr_t good_closure_index = cls.FindClosureIndex(function);
3552 EXPECT_GE(good_closure_index, 0);
3553 // The parent function should return an invalid index.
3554 intptr_t bad_closure_index = cls.FindClosureIndex(parent);
3555 EXPECT_EQ(bad_closure_index, -1);
3556
3557 // Retrieve closure function via index.
3558 Function& func_from_index = Function::Handle();
3559 func_from_index ^= cls.ClosureFunctionFromIndex(good_closure_index);
3560 // Same closure function.
3561 EXPECT_EQ(func_from_index.raw(), function.raw());
3562 }
3563
3564
3565 TEST_CASE(FindInvocationDispatcherFunctionIndex) {
3566 const String& class_name = String::Handle(Symbols::New("MyClass"));
3567 const Script& script = Script::Handle();
3568 const Class& cls = Class::Handle(CreateDummyClass(class_name, script));
3569 ClassFinalizer::FinalizeTypesInClass(cls);
3570
3571 const Array& functions = Array::Handle(Array::New(1));
3572 Function& parent = Function::Handle();
3573 const String& parent_name = String::Handle(Symbols::New("foo_papa"));
3574 parent = Function::New(parent_name, RawFunction::kRegularFunction,
3575 false, false, false, false, false, cls, 0);
3576 functions.SetAt(0, parent);
3577 cls.SetFunctions(functions);
3578 cls.Finalize();
3579
3580 // Add invocation dispatcher.
3581 const String& invocation_dispatcher_name =
3582 String::Handle(Symbols::New("myMethod"));
3583 const Array& args_desc = Array::Handle(ArgumentsDescriptor::New(1));
3584 Function& invocation_dispatcher = Function::Handle();
3585 invocation_dispatcher ^=
3586 cls.GetInvocationDispatcher(invocation_dispatcher_name, args_desc,
3587 RawFunction::kNoSuchMethodDispatcher);
3588 EXPECT(!invocation_dispatcher.IsNull());
3589 // Get index to function.
3590 intptr_t invocation_dispatcher_index =
3591 cls.FindInvocationDispatcherFunctionIndex(invocation_dispatcher);
3592 // Expect a valid index.
3593 EXPECT_GE(invocation_dispatcher_index, 0);
3594 // Retrieve function through index.
3595 Function& invocation_dispatcher_from_index = Function::Handle();
3596 invocation_dispatcher_from_index ^=
3597 cls.InvocationDispatcherFunctionFromIndex(invocation_dispatcher_index);
3598 // Same function.
3599 EXPECT_EQ(invocation_dispatcher.raw(),
3600 invocation_dispatcher_from_index.raw());
3601 // Test function not found case.
3602 const Function& bad_function = Function::Handle(Function::null());
3603 intptr_t bad_invocation_dispatcher_index =
3604 cls.FindInvocationDispatcherFunctionIndex(bad_function);
3605 EXPECT_EQ(bad_invocation_dispatcher_index, -1);
3606 }
3607
3608
3394 static void PrintMetadata(const char* name, const Object& data) { 3609 static void PrintMetadata(const char* name, const Object& data) {
3395 if (data.IsError()) { 3610 if (data.IsError()) {
3396 OS::Print("Error in metadata evaluation for %s: '%s'\n", 3611 OS::Print("Error in metadata evaluation for %s: '%s'\n",
3397 name, 3612 name,
3398 Error::Cast(data).ToErrorCString()); 3613 Error::Cast(data).ToErrorCString());
3399 } 3614 }
3400 EXPECT(data.IsArray()); 3615 EXPECT(data.IsArray());
3401 const Array& metadata = Array::Cast(data); 3616 const Array& metadata = Array::Cast(data);
3402 OS::Print("Metadata for %s has %" Pd " values:\n", name, metadata.Length()); 3617 OS::Print("Metadata for %s has %" Pd " values:\n", name, metadata.Length());
3403 Object& elem = Object::Handle(); 3618 Object& elem = Object::Handle();
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
3767 // Simple map. 3982 // Simple map.
3768 // 3983 //
3769 // TODO(turnidge): Consider showing something like: {1: 2, 2: 'otter'} 3984 // TODO(turnidge): Consider showing something like: {1: 2, 2: 'otter'}
3770 result = Dart_GetField(lib, NewString("simple_map")); 3985 result = Dart_GetField(lib, NewString("simple_map"));
3771 EXPECT_VALID(result); 3986 EXPECT_VALID(result);
3772 obj ^= Api::UnwrapHandle(result); 3987 obj ^= Api::UnwrapHandle(result);
3773 EXPECT_STREQ("Instance of '_LinkedHashMap'", obj.ToUserCString()); 3988 EXPECT_STREQ("Instance of '_LinkedHashMap'", obj.ToUserCString());
3774 } 3989 }
3775 3990
3776 } // namespace dart 3991 } // namespace dart
OLDNEW
« no previous file with comments | « dart/runtime/vm/object.cc ('k') | dart/runtime/vm/pages.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698