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/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/dart.h" | 6 #include "vm/dart.h" |
7 #include "vm/isolate.h" | 7 #include "vm/isolate.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 #include "vm/zone.h" | 9 #include "vm/zone.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 DECLARE_DEBUG_FLAG(bool, trace_zones); | 13 DECLARE_DEBUG_FLAG(bool, trace_zones); |
14 | 14 |
15 UNIT_TEST_CASE(AllocateZone) { | 15 UNIT_TEST_CASE(AllocateZone) { |
16 #if defined(DEBUG) | 16 #if defined(DEBUG) |
17 FLAG_trace_zones = true; | 17 FLAG_trace_zones = true; |
18 #endif | 18 #endif |
19 Isolate::Flags vm_flags; | 19 Dart_CreateIsolate( |
20 Dart_IsolateFlags api_flags; | 20 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
21 vm_flags.CopyTo(&api_flags); | 21 Isolate* isolate = Isolate::Current(); |
22 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
23 EXPECT(Isolate::Current() == isolate); | |
24 EXPECT(isolate->current_zone() == NULL); | 22 EXPECT(isolate->current_zone() == NULL); |
25 { | 23 { |
26 StackZone stack_zone(isolate); | 24 StackZone stack_zone(isolate); |
27 EXPECT(isolate->current_zone() != NULL); | 25 EXPECT(isolate->current_zone() != NULL); |
28 Zone* zone = stack_zone.GetZone(); | 26 Zone* zone = stack_zone.GetZone(); |
29 intptr_t allocated_size = 0; | 27 intptr_t allocated_size = 0; |
30 | 28 |
31 // The loop is to make sure we overflow one segment and go on | 29 // The loop is to make sure we overflow one segment and go on |
32 // to the next segment. | 30 // to the next segment. |
33 for (int i = 0; i < 1000; i++) { | 31 for (int i = 0; i < 1000; i++) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 63 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
66 | 64 |
67 buffer = reinterpret_cast<uint8_t*>( | 65 buffer = reinterpret_cast<uint8_t*>( |
68 zone->AllocUnsafe(kSegmentSize + kWordSize)); | 66 zone->AllocUnsafe(kSegmentSize + kWordSize)); |
69 EXPECT(buffer != NULL); | 67 EXPECT(buffer != NULL); |
70 buffer[(kSegmentSize + kWordSize) - 1] = 0; | 68 buffer[(kSegmentSize + kWordSize) - 1] = 0; |
71 allocated_size += (kSegmentSize + kWordSize); | 69 allocated_size += (kSegmentSize + kWordSize); |
72 EXPECT_LE(allocated_size, zone->SizeInBytes()); | 70 EXPECT_LE(allocated_size, zone->SizeInBytes()); |
73 } | 71 } |
74 EXPECT(isolate->current_zone() == NULL); | 72 EXPECT(isolate->current_zone() == NULL); |
75 isolate->Shutdown(); | 73 Dart_ShutdownIsolate(); |
76 delete isolate; | |
77 } | 74 } |
78 | 75 |
79 | 76 |
80 UNIT_TEST_CASE(AllocGeneric_Success) { | 77 UNIT_TEST_CASE(AllocGeneric_Success) { |
81 #if defined(DEBUG) | 78 #if defined(DEBUG) |
82 FLAG_trace_zones = true; | 79 FLAG_trace_zones = true; |
83 #endif | 80 #endif |
84 Isolate::Flags vm_flags; | 81 Dart_CreateIsolate( |
85 Dart_IsolateFlags api_flags; | 82 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
86 vm_flags.CopyTo(&api_flags); | 83 Isolate* isolate = Isolate::Current(); |
87 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
88 EXPECT(Isolate::Current() == isolate); | |
89 EXPECT(isolate->current_zone() == NULL); | 84 EXPECT(isolate->current_zone() == NULL); |
90 { | 85 { |
91 StackZone zone(isolate); | 86 StackZone zone(isolate); |
92 EXPECT(isolate->current_zone() != NULL); | 87 EXPECT(isolate->current_zone() != NULL); |
93 intptr_t allocated_size = 0; | 88 intptr_t allocated_size = 0; |
94 | 89 |
95 const intptr_t kNumElements = 1000; | 90 const intptr_t kNumElements = 1000; |
96 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 91 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
97 allocated_size += sizeof(uint32_t) * kNumElements; | 92 allocated_size += sizeof(uint32_t) * kNumElements; |
98 EXPECT_LE(allocated_size, zone.SizeInBytes()); | 93 EXPECT_LE(allocated_size, zone.SizeInBytes()); |
99 } | 94 } |
100 EXPECT(isolate->current_zone() == NULL); | 95 EXPECT(isolate->current_zone() == NULL); |
101 isolate->Shutdown(); | 96 Dart_ShutdownIsolate(); |
102 delete isolate; | |
103 } | 97 } |
104 | 98 |
105 | 99 |
106 // This test is expected to crash. | 100 // This test is expected to crash. |
107 UNIT_TEST_CASE(AllocGeneric_Overflow) { | 101 UNIT_TEST_CASE(AllocGeneric_Overflow) { |
108 #if defined(DEBUG) | 102 #if defined(DEBUG) |
109 FLAG_trace_zones = true; | 103 FLAG_trace_zones = true; |
110 #endif | 104 #endif |
111 Isolate::Flags vm_flags; | 105 Dart_CreateIsolate( |
112 Dart_IsolateFlags api_flags; | 106 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
113 vm_flags.CopyTo(&api_flags); | 107 Isolate* isolate = Isolate::Current(); |
114 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
115 EXPECT(Isolate::Current() == isolate); | |
116 EXPECT(isolate->current_zone() == NULL); | 108 EXPECT(isolate->current_zone() == NULL); |
117 { | 109 { |
118 StackZone zone(isolate); | 110 StackZone zone(isolate); |
119 EXPECT(isolate->current_zone() != NULL); | 111 EXPECT(isolate->current_zone() != NULL); |
120 | 112 |
121 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; | 113 const intptr_t kNumElements = (kIntptrMax / sizeof(uint32_t)) + 1; |
122 zone.GetZone()->Alloc<uint32_t>(kNumElements); | 114 zone.GetZone()->Alloc<uint32_t>(kNumElements); |
123 } | 115 } |
124 isolate->Shutdown(); | 116 Dart_ShutdownIsolate(); |
125 delete isolate; | |
126 } | 117 } |
127 | 118 |
128 | 119 |
129 UNIT_TEST_CASE(ZoneAllocated) { | 120 UNIT_TEST_CASE(ZoneAllocated) { |
130 #if defined(DEBUG) | 121 #if defined(DEBUG) |
131 FLAG_trace_zones = true; | 122 FLAG_trace_zones = true; |
132 #endif | 123 #endif |
133 Isolate::Flags vm_flags; | 124 Dart_CreateIsolate( |
134 Dart_IsolateFlags api_flags; | 125 NULL, NULL, bin::isolate_snapshot_buffer, NULL, NULL, NULL); |
135 vm_flags.CopyTo(&api_flags); | 126 Isolate* isolate = Isolate::Current(); |
136 Isolate* isolate = Isolate::Init(NULL, api_flags); | |
137 EXPECT(Isolate::Current() == isolate); | |
138 EXPECT(isolate->current_zone() == NULL); | 127 EXPECT(isolate->current_zone() == NULL); |
139 static int marker; | 128 static int marker; |
140 | 129 |
141 class SimpleZoneObject : public ZoneAllocated { | 130 class SimpleZoneObject : public ZoneAllocated { |
142 public: | 131 public: |
143 SimpleZoneObject() : slot(marker++) { } | 132 SimpleZoneObject() : slot(marker++) { } |
144 virtual ~SimpleZoneObject() { } | 133 virtual ~SimpleZoneObject() { } |
145 virtual int GetSlot() { return slot; } | 134 virtual int GetSlot() { return slot; } |
146 int slot; | 135 int slot; |
147 }; | 136 }; |
(...skipping 17 matching lines...) Expand all Loading... |
165 EXPECT_EQ(0, first->slot); | 154 EXPECT_EQ(0, first->slot); |
166 EXPECT_EQ(1, second->slot); | 155 EXPECT_EQ(1, second->slot); |
167 | 156 |
168 // Make sure we can write to the members of the zone objects. | 157 // Make sure we can write to the members of the zone objects. |
169 first->slot = 42; | 158 first->slot = 42; |
170 second->slot = 87; | 159 second->slot = 87; |
171 EXPECT_EQ(42, first->slot); | 160 EXPECT_EQ(42, first->slot); |
172 EXPECT_EQ(87, second->slot); | 161 EXPECT_EQ(87, second->slot); |
173 } | 162 } |
174 EXPECT(isolate->current_zone() == NULL); | 163 EXPECT(isolate->current_zone() == NULL); |
175 isolate->Shutdown(); | 164 Dart_ShutdownIsolate(); |
176 delete isolate; | |
177 } | 165 } |
178 | 166 |
179 | 167 |
180 TEST_CASE(PrintToString) { | 168 TEST_CASE(PrintToString) { |
181 StackZone zone(Isolate::Current()); | 169 StackZone zone(Isolate::Current()); |
182 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); | 170 const char* result = zone.GetZone()->PrintToString("Hello %s!", "World"); |
183 EXPECT_STREQ("Hello World!", result); | 171 EXPECT_STREQ("Hello World!", result); |
184 } | 172 } |
185 | 173 |
186 } // namespace dart | 174 } // namespace dart |
OLD | NEW |