-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathallocation_test.cc
159 lines (134 loc) · 3.73 KB
/
allocation_test.cc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/allocation.h"
#include "platform/assert.h"
#include "vm/longjump.h"
#include "vm/unit_test.h"
namespace dart {
class TestValueObject : public ValueObject {
public:
explicit TestValueObject(int* ptr) : ptr_(ptr) {
EXPECT_EQ(1, *ptr_);
*ptr_ = 2;
}
virtual ~TestValueObject() {
EXPECT_EQ(3, *ptr_);
*ptr_ = 4;
}
int value() const { return *ptr_; }
virtual int GetId() const { return 3; }
private:
int* ptr_;
};
class TestStackResource : public StackResource {
public:
explicit TestStackResource(int* ptr)
: StackResource(Thread::Current()), ptr_(ptr) {
EXPECT_EQ(1, *ptr_);
*ptr_ = 2;
}
~TestStackResource() {
EXPECT_EQ(6, *ptr_);
*ptr_ = 7;
}
int value() const { return *ptr_; }
virtual int GetId() const { return 3; }
private:
int* ptr_;
};
class TestStackedStackResource : public StackResource {
public:
explicit TestStackedStackResource(int* ptr)
: StackResource(Thread::Current()), ptr_(ptr) {
EXPECT_EQ(3, *ptr_);
*ptr_ = 4;
}
~TestStackedStackResource() {
EXPECT_EQ(5, *ptr_);
*ptr_ = 6;
}
int value() const { return *ptr_; }
private:
int* ptr_;
};
static void StackAllocatedDestructionHelper(int* ptr) {
TestValueObject stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
}
ISOLATE_UNIT_TEST_CASE(StackAllocatedDestruction) {
int data = 1;
StackAllocatedDestructionHelper(&data);
EXPECT_EQ(4, data);
}
static void StackAllocatedLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestValueObject stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
const Error& error = Error::Handle(LanguageError::New(
String::Handle(String::New("StackAllocatedLongJump"))));
jump->Jump(1, error);
UNREACHABLE();
}
ISOLATE_UNIT_TEST_CASE(StackAllocatedLongJump) {
LongJumpScope jump;
int data = 1;
if (DART_SETJMP(*jump.Set()) == 0) {
StackAllocatedLongJumpHelper(&data, &jump);
UNREACHABLE();
} else {
ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
}
EXPECT_EQ(3, data);
}
static void StackedStackResourceDestructionHelper(int* ptr) {
TestStackedStackResource stacked(ptr);
EXPECT_EQ(4, *ptr);
*ptr = 5;
}
static void StackResourceDestructionHelper(int* ptr) {
TestStackResource stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
StackedStackResourceDestructionHelper(ptr);
EXPECT_EQ(6, *ptr);
// Do not set data because the LongJump version does not return control here.
}
ISOLATE_UNIT_TEST_CASE(StackResourceDestruction) {
int data = 1;
StackResourceDestructionHelper(&data);
EXPECT_EQ(7, data);
}
static void StackedStackResourceLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestStackedStackResource stacked(ptr);
EXPECT_EQ(4, *ptr);
*ptr = 5;
const Error& error = Error::Handle(LanguageError::New(
String::Handle(String::New("StackedStackResourceLongJump"))));
jump->Jump(1, error);
UNREACHABLE();
}
static void StackResourceLongJumpHelper(int* ptr, LongJumpScope* jump) {
TestStackResource stacked(ptr);
EXPECT_EQ(2, *ptr);
*ptr = 3;
StackedStackResourceLongJumpHelper(ptr, jump);
UNREACHABLE();
}
ISOLATE_UNIT_TEST_CASE(StackResourceLongJump) {
LongJumpScope* base = Thread::Current()->long_jump_base();
{
LongJumpScope jump;
int data = 1;
if (DART_SETJMP(*jump.Set()) == 0) {
StackResourceLongJumpHelper(&data, &jump);
UNREACHABLE();
} else {
ASSERT(Error::Handle(thread->StealStickyError()).IsLanguageError());
}
EXPECT_EQ(7, data);
}
ASSERT(base == Thread::Current()->long_jump_base());
}
} // namespace dart