OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #include "platform/assert.h" |
| 6 #include "vm/object_graph.h" |
| 7 #include "vm/unit_test.h" |
| 8 |
| 9 namespace dart { |
| 10 |
| 11 class Counter : public ObjectGraph::Visitor { |
| 12 public: |
| 13 // Records the number of objects and total size visited, excluding 'skip' |
| 14 // and any objects only reachable through 'skip'. |
| 15 Counter(RawObject* skip, RawObject* expected_parent) |
| 16 : count_(0), size_(0), skip_(skip), expected_parent_(expected_parent) { } |
| 17 |
| 18 virtual Direction VisitObject(ObjectGraph::StackIterator* it) { |
| 19 RawObject* obj = it->Get(); |
| 20 if (obj == skip_) { |
| 21 EXPECT(it->MoveToParent()); |
| 22 EXPECT_EQ(expected_parent_, it->Get()); |
| 23 return kBacktrack; |
| 24 } |
| 25 ++count_; |
| 26 size_ += obj->Size(); |
| 27 return kProceed; |
| 28 } |
| 29 |
| 30 int count() const { return count_; } |
| 31 int size() const { return size_; } |
| 32 |
| 33 private: |
| 34 int count_; |
| 35 intptr_t size_; |
| 36 RawObject* skip_; |
| 37 RawObject* expected_parent_; |
| 38 }; |
| 39 |
| 40 |
| 41 TEST_CASE(ObjectGraph) { |
| 42 Isolate* isolate = Isolate::Current(); |
| 43 // Create a simple object graph with objects a, b, c, d: |
| 44 // a+->b+->c |
| 45 // + + |
| 46 // | v |
| 47 // +-->d |
| 48 Array& a = Array::Handle(); |
| 49 RawArray* a_raw; |
| 50 a = a_raw = Array::New(2, Heap::kNew); // Test cross-generation pointers. |
| 51 RawArray* b_raw; |
| 52 RawArray* c_raw; |
| 53 { |
| 54 HANDLESCOPE(isolate); |
| 55 Array& b = Array::Handle(); |
| 56 b = b_raw = Array::New(2, Heap::kOld); |
| 57 a.SetAt(0, b); |
| 58 { |
| 59 HANDLESCOPE(isolate); |
| 60 Array& c = Array::Handle(); |
| 61 c = c_raw = Array::New(0, Heap::kOld); |
| 62 b.SetAt(0, c); |
| 63 { |
| 64 HANDLESCOPE(isolate); |
| 65 Array& d = Array::Handle(); |
| 66 d = Array::New(0, Heap::kOld); |
| 67 b.SetAt(1, d); |
| 68 a.SetAt(1, d); |
| 69 } |
| 70 } |
| 71 } |
| 72 ObjectGraph graph(isolate); |
| 73 { |
| 74 // Compare count and size when 'b' is/isn't skipped. |
| 75 Counter with(Object::null(), Object::null()); |
| 76 graph.IterateObjectsFrom(a_raw, &with); |
| 77 Counter without(b_raw, a_raw); |
| 78 graph.IterateObjectsFrom(a_raw, &without); |
| 79 // Only 'b' and 'c' were cut off. |
| 80 EXPECT_EQ(2, with.count() - without.count()); |
| 81 EXPECT_EQ(b_raw->Size() + c_raw->Size(), |
| 82 with.size() - without.size()); |
| 83 } |
| 84 { |
| 85 // Like above, but iterate over the entire isolate. The counts and sizes |
| 86 // are thus larger, but the difference should still be just 'b' and 'c'. |
| 87 Counter with(Object::null(), Object::null()); |
| 88 graph.IterateObjects(&with); |
| 89 Counter without(b_raw, a_raw); |
| 90 graph.IterateObjects(&without); |
| 91 EXPECT_EQ(2, with.count() - without.count()); |
| 92 EXPECT_EQ(b_raw->Size() + c_raw->Size(), |
| 93 with.size() - without.size()); |
| 94 } |
| 95 } |
| 96 |
| 97 } // namespace dart |
OLD | NEW |