| OLD | NEW |
| 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 "vm/assembler.h" | 7 #include "vm/assembler.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_entry.h" | 10 #include "vm/dart_entry.h" |
| (...skipping 4450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4461 EXPECT_STREQ( | 4461 EXPECT_STREQ( |
| 4462 "{\"type\":\"@List\",\"_vmType\":\"@GrowableObjectArray\"," | 4462 "{\"type\":\"@List\",\"_vmType\":\"@GrowableObjectArray\"," |
| 4463 "\"class\":{\"type\":\"@Class\",\"fixedId\":true,\"id\":\"\"," | 4463 "\"class\":{\"type\":\"@Class\",\"fixedId\":true,\"id\":\"\"," |
| 4464 "\"name\":\"_GrowableList\"," | 4464 "\"name\":\"_GrowableList\"," |
| 4465 "\"_vmName\":\"\"},\"id\":\"\",\"length\":0}", | 4465 "\"_vmName\":\"\"},\"id\":\"\",\"length\":0}", |
| 4466 buffer); | 4466 buffer); |
| 4467 } | 4467 } |
| 4468 // LinkedHashMap reference | 4468 // LinkedHashMap reference |
| 4469 { | 4469 { |
| 4470 JSONStream js; | 4470 JSONStream js; |
| 4471 const LinkedHashMap& array = LinkedHashMap::Handle(LinkedHashMap::New()); | 4471 const LinkedHashMap& array = |
| 4472 LinkedHashMap::Handle(LinkedHashMap::NewDefault()); |
| 4472 array.PrintJSON(&js, true); | 4473 array.PrintJSON(&js, true); |
| 4473 elideSubstring("classes", js.ToCString(), buffer); | 4474 elideSubstring("classes", js.ToCString(), buffer); |
| 4474 elideSubstring("objects", buffer, buffer); | 4475 elideSubstring("objects", buffer, buffer); |
| 4475 elideSubstring("_InternalLinkedHashMap@", buffer, buffer); | 4476 elideSubstring("_InternalLinkedHashMap@", buffer, buffer); |
| 4476 EXPECT_STREQ( | 4477 EXPECT_STREQ( |
| 4477 "{\"type\":\"@Instance\",\"_vmType\":\"@LinkedHashMap\"," | 4478 "{\"type\":\"@Instance\",\"_vmType\":\"@LinkedHashMap\"," |
| 4478 "\"class\":{\"type\":\"@Class\",\"fixedId\":true,\"id\":\"\"," | 4479 "\"class\":{\"type\":\"@Class\",\"fixedId\":true,\"id\":\"\"," |
| 4479 "\"name\":\"_InternalLinkedHashMap\",\"_vmName\":\"\"},\"id\":\"\"}", | 4480 "\"name\":\"_InternalLinkedHashMap\",\"_vmName\":\"\"},\"id\":\"\"}", |
| 4480 buffer); | 4481 buffer); |
| 4481 } | 4482 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4600 Dart_Handle h_result = Dart_Invoke(h_lib, NewString("foo"), 0, NULL); | 4601 Dart_Handle h_result = Dart_Invoke(h_lib, NewString("foo"), 0, NULL); |
| 4601 EXPECT_VALID(h_result); | 4602 EXPECT_VALID(h_result); |
| 4602 Integer& result = Integer::Handle(); | 4603 Integer& result = Integer::Handle(); |
| 4603 result ^= Api::UnwrapHandle(h_result); | 4604 result ^= Api::UnwrapHandle(h_result); |
| 4604 String& foo = String::Handle(String::New("foo")); | 4605 String& foo = String::Handle(String::New("foo")); |
| 4605 Integer& expected = Integer::Handle(); | 4606 Integer& expected = Integer::Handle(); |
| 4606 expected ^= foo.HashCode(); | 4607 expected ^= foo.HashCode(); |
| 4607 EXPECT(result.IsIdenticalTo(expected)); | 4608 EXPECT(result.IsIdenticalTo(expected)); |
| 4608 } | 4609 } |
| 4609 | 4610 |
| 4611 |
| 4612 static void CheckIdenticalHashStructure(const Instance& a, const Instance& b) { |
| 4613 const char* kScript = |
| 4614 "(a, b) {\n" |
| 4615 " if (a._usedData != b._usedData ||\n" |
| 4616 " a._deletedKeys != b._deletedKeys ||\n" |
| 4617 " a._hashMask != b._hashMask ||\n" |
| 4618 " a._index.length != b._index.length ||\n" |
| 4619 " a._data.length != b._data.length) {\n" |
| 4620 " return false;\n" |
| 4621 " }\n" |
| 4622 " for (var i = 0; i < a._index.length; ++i) {\n" |
| 4623 " if (a._index[i] != b._index[i]) {\n" |
| 4624 " return false;\n" |
| 4625 " }\n" |
| 4626 " }\n" |
| 4627 " for (var i = 0; i < a._data.length; ++i) {\n" |
| 4628 " var ad = a._data[i];\n" |
| 4629 " var bd = b._data[i];\n" |
| 4630 " if (!identical(ad, bd) && !(ad == a && bd == b)) {\n" |
| 4631 " return false;\n" |
| 4632 " }\n" |
| 4633 " }\n" |
| 4634 " return true;\n" |
| 4635 "}(a, b)"; |
| 4636 String& name = String::Handle(); |
| 4637 Array& param_names = Array::Handle(Array::New(2)); |
| 4638 name = String::New("a"); |
| 4639 param_names.SetAt(0, name); |
| 4640 name = String::New("b"); |
| 4641 param_names.SetAt(1, name); |
| 4642 Array& param_values = Array::Handle(Array::New(2)); |
| 4643 param_values.SetAt(0, a); |
| 4644 param_values.SetAt(1, b); |
| 4645 name = String::New(kScript); |
| 4646 Library& lib = Library::Handle(Library::CollectionLibrary()); |
| 4647 EXPECT(lib.Evaluate(name, param_names, param_values) == Bool::True().raw()); |
| 4648 } |
| 4649 |
| 4650 |
| 4651 TEST_CASE(LinkedHashMap) { |
| 4652 // Check that initial index size and hash mask match in Dart vs. C++. |
| 4653 // 1. Create an empty custom linked hash map in Dart. |
| 4654 const char* kScript = |
| 4655 "import 'dart:collection';\n" |
| 4656 "makeMap() {\n" |
| 4657 " Function eq = (a, b) => true;\n" |
| 4658 " Function hc = (a) => 42;\n" |
| 4659 " return new LinkedHashMap(equals: eq, hashCode: hc);\n" |
| 4660 "}"; |
| 4661 Dart_Handle h_lib = TestCase::LoadTestScript(kScript, NULL); |
| 4662 EXPECT_VALID(h_lib); |
| 4663 Library& lib = Library::Handle(); |
| 4664 lib ^= Api::UnwrapHandle(h_lib); |
| 4665 EXPECT(!lib.IsNull()); |
| 4666 Dart_Handle h_result = Dart_Invoke(h_lib, NewString("makeMap"), 0, NULL); |
| 4667 EXPECT_VALID(h_result); |
| 4668 |
| 4669 // 2. Create an empty internalized LinkedHashMap in C++. |
| 4670 Instance& dart_map = Instance::Handle(); |
| 4671 dart_map ^= Api::UnwrapHandle(h_result); |
| 4672 LinkedHashMap& cc_map = LinkedHashMap::Handle(LinkedHashMap::NewDefault()); |
| 4673 |
| 4674 // 3. Expect them to have identical structure. |
| 4675 CheckIdenticalHashStructure(dart_map, cc_map); |
| 4676 } |
| 4677 |
| 4610 } // namespace dart | 4678 } // namespace dart |
| OLD | NEW |