| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/object_id_ring.h" |
| 5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 6 #include "vm/globals.h" | |
| 7 #include "vm/object_id_ring.h" | |
| 8 #include "vm/unit_test.h" | |
| 9 #include "vm/dart_api_impl.h" | 7 #include "vm/dart_api_impl.h" |
| 10 #include "vm/dart_api_state.h" | 8 #include "vm/dart_api_state.h" |
| 9 #include "vm/globals.h" |
| 10 #include "vm/unit_test.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 #ifndef PRODUCT | 14 #ifndef PRODUCT |
| 15 | 15 |
| 16 class ObjectIdRingTestHelper { | 16 class ObjectIdRingTestHelper { |
| 17 public: | 17 public: |
| 18 static void SetCapacityAndMaxSerial(ObjectIdRing* ring, | 18 static void SetCapacityAndMaxSerial(ObjectIdRing* ring, |
| 19 int32_t capacity, | 19 int32_t capacity, |
| 20 int32_t max_serial) { | 20 int32_t max_serial) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 41 return Symbols::New(Thread::Current(), s); | 41 return Symbols::New(Thread::Current(), s); |
| 42 } | 42 } |
| 43 | 43 |
| 44 static void ExpectString(RawObject* obj, const char* s) { | 44 static void ExpectString(RawObject* obj, const char* s) { |
| 45 String& str = String::Handle(); | 45 String& str = String::Handle(); |
| 46 str ^= obj; | 46 str ^= obj; |
| 47 EXPECT(str.Equals(s)); | 47 EXPECT(str.Equals(s)); |
| 48 } | 48 } |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 | |
| 52 // Test that serial number wrapping works. | 51 // Test that serial number wrapping works. |
| 53 ISOLATE_UNIT_TEST_CASE(ObjectIdRingSerialWrapTest) { | 52 ISOLATE_UNIT_TEST_CASE(ObjectIdRingSerialWrapTest) { |
| 54 Isolate* isolate = Isolate::Current(); | 53 Isolate* isolate = Isolate::Current(); |
| 55 ObjectIdRing* ring = isolate->object_id_ring(); | 54 ObjectIdRing* ring = isolate->object_id_ring(); |
| 56 ObjectIdRingTestHelper::SetCapacityAndMaxSerial(ring, 2, 4); | 55 ObjectIdRingTestHelper::SetCapacityAndMaxSerial(ring, 2, 4); |
| 57 intptr_t id; | 56 intptr_t id; |
| 58 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; | 57 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; |
| 59 id = ring->GetIdForObject(ObjectIdRingTestHelper::MakeString("0")); | 58 id = ring->GetIdForObject(ObjectIdRingTestHelper::MakeString("0")); |
| 60 EXPECT_EQ(0, id); | 59 EXPECT_EQ(0, id); |
| 61 ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0); | 60 ObjectIdRingTestHelper::ExpectIndexId(ring, 0, 0); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // Index 1 has id 1. | 112 // Index 1 has id 1. |
| 114 ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1); | 113 ObjectIdRingTestHelper::ExpectIndexId(ring, 1, 1); |
| 115 ObjectIdRingTestHelper::ExpectString(ring->GetObjectForId(id, &kind), "5"); | 114 ObjectIdRingTestHelper::ExpectString(ring->GetObjectForId(id, &kind), "5"); |
| 116 EXPECT_EQ(ObjectIdRing::kValid, kind); | 115 EXPECT_EQ(ObjectIdRing::kValid, kind); |
| 117 ObjectIdRingTestHelper::ExpectIdIsValid(ring, 0); | 116 ObjectIdRingTestHelper::ExpectIdIsValid(ring, 0); |
| 118 ObjectIdRingTestHelper::ExpectIdIsValid(ring, 1); | 117 ObjectIdRingTestHelper::ExpectIdIsValid(ring, 1); |
| 119 ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 2); | 118 ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 2); |
| 120 ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 3); | 119 ObjectIdRingTestHelper::ExpectIdIsInvalid(ring, 3); |
| 121 } | 120 } |
| 122 | 121 |
| 123 | |
| 124 // Test that the ring table is updated when the scavenger moves an object. | 122 // Test that the ring table is updated when the scavenger moves an object. |
| 125 TEST_CASE(ObjectIdRingScavengeMoveTest) { | 123 TEST_CASE(ObjectIdRingScavengeMoveTest) { |
| 126 const char* kScriptChars = | 124 const char* kScriptChars = |
| 127 "main() {\n" | 125 "main() {\n" |
| 128 " return [1, 2, 3];\n" | 126 " return [1, 2, 3];\n" |
| 129 "}\n"; | 127 "}\n"; |
| 130 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 128 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 131 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL); | 129 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL); |
| 132 intptr_t list_length = 0; | 130 intptr_t list_length = 0; |
| 133 EXPECT_VALID(result); | 131 EXPECT_VALID(result); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 EXPECT_VALID(moved_handle); | 179 EXPECT_VALID(moved_handle); |
| 182 EXPECT(!Dart_IsNull(moved_handle)); | 180 EXPECT(!Dart_IsNull(moved_handle)); |
| 183 EXPECT(Dart_IsList(moved_handle)); | 181 EXPECT(Dart_IsList(moved_handle)); |
| 184 EXPECT_VALID(Dart_ListLength(moved_handle, &list_length)); | 182 EXPECT_VALID(Dart_ListLength(moved_handle, &list_length)); |
| 185 EXPECT_EQ(3, list_length); | 183 EXPECT_EQ(3, list_length); |
| 186 // Test id reuse. | 184 // Test id reuse. |
| 187 EXPECT_EQ(raw_obj_id1, | 185 EXPECT_EQ(raw_obj_id1, |
| 188 ring->GetIdForObject(raw_object_moved1, ObjectIdRing::kReuseId)); | 186 ring->GetIdForObject(raw_object_moved1, ObjectIdRing::kReuseId)); |
| 189 } | 187 } |
| 190 | 188 |
| 191 | |
| 192 // Test that the ring table is updated with nulls when the old GC collects. | 189 // Test that the ring table is updated with nulls when the old GC collects. |
| 193 ISOLATE_UNIT_TEST_CASE(ObjectIdRingOldGCTest) { | 190 ISOLATE_UNIT_TEST_CASE(ObjectIdRingOldGCTest) { |
| 194 Isolate* isolate = thread->isolate(); | 191 Isolate* isolate = thread->isolate(); |
| 195 Heap* heap = isolate->heap(); | 192 Heap* heap = isolate->heap(); |
| 196 ObjectIdRing* ring = isolate->object_id_ring(); | 193 ObjectIdRing* ring = isolate->object_id_ring(); |
| 197 | 194 |
| 198 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; | 195 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; |
| 199 intptr_t raw_obj_id1 = -1; | 196 intptr_t raw_obj_id1 = -1; |
| 200 intptr_t raw_obj_id2 = -1; | 197 intptr_t raw_obj_id2 = -1; |
| 201 { | 198 { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // those ids. | 231 // those ids. |
| 235 heap->CollectGarbage(Heap::kOld); | 232 heap->CollectGarbage(Heap::kOld); |
| 236 RawObject* raw_object_moved1 = ring->GetObjectForId(raw_obj_id1, &kind); | 233 RawObject* raw_object_moved1 = ring->GetObjectForId(raw_obj_id1, &kind); |
| 237 EXPECT_EQ(ObjectIdRing::kCollected, kind); | 234 EXPECT_EQ(ObjectIdRing::kCollected, kind); |
| 238 EXPECT_EQ(Object::null(), raw_object_moved1); | 235 EXPECT_EQ(Object::null(), raw_object_moved1); |
| 239 RawObject* raw_object_moved2 = ring->GetObjectForId(raw_obj_id2, &kind); | 236 RawObject* raw_object_moved2 = ring->GetObjectForId(raw_obj_id2, &kind); |
| 240 EXPECT_EQ(ObjectIdRing::kCollected, kind); | 237 EXPECT_EQ(ObjectIdRing::kCollected, kind); |
| 241 EXPECT_EQ(Object::null(), raw_object_moved2); | 238 EXPECT_EQ(Object::null(), raw_object_moved2); |
| 242 } | 239 } |
| 243 | 240 |
| 244 | |
| 245 // Test that the ring table correctly reports an entry as expired when it is | 241 // Test that the ring table correctly reports an entry as expired when it is |
| 246 // overridden by new entries. | 242 // overridden by new entries. |
| 247 ISOLATE_UNIT_TEST_CASE(ObjectIdRingExpiredEntryTest) { | 243 ISOLATE_UNIT_TEST_CASE(ObjectIdRingExpiredEntryTest) { |
| 248 Isolate* isolate = Isolate::Current(); | 244 Isolate* isolate = Isolate::Current(); |
| 249 ObjectIdRing* ring = isolate->object_id_ring(); | 245 ObjectIdRing* ring = isolate->object_id_ring(); |
| 250 | 246 |
| 251 // Insert an object and check we can look it up. | 247 // Insert an object and check we can look it up. |
| 252 String& obj = String::Handle(String::New("I will expire")); | 248 String& obj = String::Handle(String::New("I will expire")); |
| 253 intptr_t obj_id = ring->GetIdForObject(obj.raw()); | 249 intptr_t obj_id = ring->GetIdForObject(obj.raw()); |
| 254 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; | 250 ObjectIdRing::LookupResult kind = ObjectIdRing::kInvalid; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 270 // Check our first entry reports it has expired. | 266 // Check our first entry reports it has expired. |
| 271 obj_lookup = ring->GetObjectForId(obj_id, &kind); | 267 obj_lookup = ring->GetObjectForId(obj_id, &kind); |
| 272 EXPECT_EQ(ObjectIdRing::kExpired, kind); | 268 EXPECT_EQ(ObjectIdRing::kExpired, kind); |
| 273 EXPECT_NE(obj.raw(), obj_lookup); | 269 EXPECT_NE(obj.raw(), obj_lookup); |
| 274 EXPECT_EQ(Object::null(), obj_lookup); | 270 EXPECT_EQ(Object::null(), obj_lookup); |
| 275 } | 271 } |
| 276 | 272 |
| 277 #endif // !PRODUCT | 273 #endif // !PRODUCT |
| 278 | 274 |
| 279 } // namespace dart | 275 } // namespace dart |
| OLD | NEW |