summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/heap/test-iterators.cc
blob: f2c1f171873c884f28ccb1efa802b64c79a52e86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "include/v8.h"
#include "src/api/api-inl.h"
#include "src/execution/isolate.h"
#include "src/heap/combined-heap.h"
#include "src/heap/heap.h"
#include "src/heap/read-only-heap.h"
#include "src/objects/heap-object.h"
#include "src/objects/objects.h"
#include "src/roots/roots-inl.h"
#include "test/cctest/cctest.h"

namespace v8 {
namespace internal {
namespace heap {

TEST(HeapObjectIteratorNullPastEnd) {
  HeapObjectIterator iterator(CcTest::heap());
  while (!iterator.Next().is_null()) {
  }
  for (int i = 0; i < 20; i++) {
    CHECK(iterator.Next().is_null());
  }
}

TEST(ReadOnlyHeapObjectIteratorNullPastEnd) {
  ReadOnlyHeapObjectIterator iterator(CcTest::read_only_heap());
  while (!iterator.Next().is_null()) {
  }
  for (int i = 0; i < 20; i++) {
    CHECK(iterator.Next().is_null());
  }
}

TEST(CombinedHeapObjectIteratorNullPastEnd) {
  CombinedHeapObjectIterator iterator(CcTest::heap());
  while (!iterator.Next().is_null()) {
  }
  for (int i = 0; i < 20; i++) {
    CHECK(iterator.Next().is_null());
  }
}

namespace {
// An arbitrary object guaranteed to live on the non-read-only heap.
Object CreateWritableObject() {
  return *v8::Utils::OpenHandle(*v8::Object::New(CcTest::isolate()));
}
}  // namespace

TEST(ReadOnlyHeapObjectIterator) {
  CcTest::InitializeVM();
  HandleScope handle_scope(CcTest::i_isolate());
  const Object sample_object = CreateWritableObject();
  ReadOnlyHeapObjectIterator iterator(CcTest::read_only_heap());

  for (HeapObject obj = iterator.Next(); !obj.is_null();
       obj = iterator.Next()) {
    CHECK(ReadOnlyHeap::Contains(obj));
    CHECK(!CcTest::heap()->Contains(obj));
    CHECK_NE(sample_object, obj);
  }
}

TEST(HeapObjectIterator) {
  CcTest::InitializeVM();
  HandleScope handle_scope(CcTest::i_isolate());
  const Object sample_object = CreateWritableObject();
  HeapObjectIterator iterator(CcTest::heap());
  bool seen_sample_object = false;

  for (HeapObject obj = iterator.Next(); !obj.is_null();
       obj = iterator.Next()) {
    CHECK(!ReadOnlyHeap::Contains(obj));
    CHECK(CcTest::heap()->Contains(obj));
    if (sample_object == obj) seen_sample_object = true;
  }
  CHECK(seen_sample_object);
}

TEST(CombinedHeapObjectIterator) {
  CcTest::InitializeVM();
  HandleScope handle_scope(CcTest::i_isolate());
  const Object sample_object = CreateWritableObject();
  CombinedHeapObjectIterator iterator(CcTest::heap());
  bool seen_sample_object = false;

  for (HeapObject obj = iterator.Next(); !obj.is_null();
       obj = iterator.Next()) {
    CHECK(IsValidHeapObject(CcTest::heap(), obj));
    if (sample_object == obj) seen_sample_object = true;
  }
  CHECK(seen_sample_object);
}

TEST(PagedSpaceIterator) {
  Heap* const heap = CcTest::heap();
  PagedSpaceIterator iterator(heap);
  CHECK_EQ(iterator.Next(), reinterpret_cast<PagedSpace*>(heap->old_space()));
  CHECK_EQ(iterator.Next(), reinterpret_cast<PagedSpace*>(heap->code_space()));
  CHECK_EQ(iterator.Next(), reinterpret_cast<PagedSpace*>(heap->map_space()));
  for (int i = 0; i < 20; i++) {
    CHECK_NULL(iterator.Next());
  }
}

TEST(SpaceIterator) {
  auto* const read_only_space = CcTest::read_only_heap()->read_only_space();
  for (SpaceIterator it(CcTest::heap()); it.HasNext();) {
    CHECK_NE(it.Next(), reinterpret_cast<Space*>(read_only_space));
  }
}

}  // namespace heap
}  // namespace internal
}  // namespace v8