Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: runtime/vm/guard_field_test.cc

Issue 22915008: Tests for GuardField length check along with bug fixes (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/flow_graph_builder.cc ('k') | runtime/vm/intermediate_language_arm.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "vm/dart_api_impl.h"
6 #include "vm/dart_api_state.h"
7 #include "vm/intermediate_language.h"
8 #include "vm/object.h"
9 #include "vm/unit_test.h"
10
11 namespace dart {
12
13 RawField* LookupField(Dart_Handle library, const char* class_name,
14 const char* field_name) {
15 RawLibrary* raw_library = Library::RawCast(Api::UnwrapHandle(library));
16 Library& lib = Library::ZoneHandle(raw_library);
17 const String& classname = String::Handle(Symbols::New(class_name));
18 String& ambiguity_error_msg = String::Handle();
19 Class& cls = Class::Handle(lib.LookupClass(classname, &ambiguity_error_msg));
20 EXPECT(!cls.IsNull()); // No ambiguity error expected.
21
22 String& fieldname = String::Handle(String::New(field_name));
23 Field& field = Field::ZoneHandle(cls.LookupInstanceField(fieldname));
24 EXPECT(!field.IsNull());
25 return field.raw();
26 }
27
28
29 TEST_CASE(GuardFieldSimpleTest) {
30 const char* script_chars =
31 "class A {\n"
32 " var f1 = 3.0;\n"
33 " var f2 = 3;\n"
34 " var f3 = new List(4);\n"
35 " foo() {\n"
36 " f1 = f1 + f1;\n"
37 " }\n"
38 " bar() {\n"
39 " f2 = null;\n"
40 " f2 = 3.0;\n"
41 " }\n"
42 "}\n"
43 "\n"
44 "runFoo() {\n"
45 " var a = new A();\n"
46 " for (int i = 0; i < 2000; i++) {\n"
47 " a.foo();\n"
48 " }\n"
49 "}\n"
50 "\n"
51 "runBar() {\n"
52 " var a = new A();\n"
53 " for (int i = 0; i < 2000; i++) {\n"
54 " a.bar();\n"
55 " }\n"
56 "}\n"
57 "main() {\n"
58 " runFoo();\n"
59 " runBar();\n"
60 "}\n";
61 Dart_Handle lib = TestCase::LoadTestScript(script_chars, NULL);
62 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
63 EXPECT_VALID(result);
64 Field& f1 = Field::ZoneHandle(LookupField(lib, "A", "f1"));
65 Field& f2 = Field::ZoneHandle(LookupField(lib, "A", "f2"));
66 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3"));
67 const intptr_t no_length = Field::kNoFixedLength;
68 EXPECT_EQ(no_length, f1.guarded_list_length());
69 EXPECT_EQ(kDoubleCid, f1.guarded_cid());
70 EXPECT_EQ(false, f1.is_nullable());
71 EXPECT_EQ(no_length, f2.guarded_list_length());
72 EXPECT_EQ(kDynamicCid, f2.guarded_cid());
73 EXPECT_EQ(true, f2.is_nullable());
74 EXPECT_EQ(no_length, f3.guarded_list_length());
75 }
76
77
78 TEST_CASE(GuardFieldFinalListTest) {
79 const char* script_chars =
80 "class A {\n"
81 " var f1 = 3.0;\n"
82 " var f2 = 3;\n"
83 " final f3 = new List(4);\n"
84 " foo() {\n"
85 " f1 = f1 + f1;\n"
86 " }\n"
87 " bar() {\n"
88 " f2 = null;\n"
89 " f2 = 3.0;\n"
90 " }\n"
91 "}\n"
92 "\n"
93 "runFoo() {\n"
94 " var a = new A();\n"
95 " for (int i = 0; i < 2000; i++) {\n"
96 " a.foo();\n"
97 " }\n"
98 "}\n"
99 "\n"
100 "runBar() {\n"
101 " var a = new A();\n"
102 " for (int i = 0; i < 2000; i++) {\n"
103 " a.bar();\n"
104 " }\n"
105 "}\n"
106 "main() {\n"
107 " runFoo();\n"
108 " runBar();\n"
109 "}\n";
110 Dart_Handle lib = TestCase::LoadTestScript(script_chars, NULL);
111 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
112 EXPECT_VALID(result);
113 Field& f1 = Field::ZoneHandle(LookupField(lib, "A", "f1"));
114 Field& f2 = Field::ZoneHandle(LookupField(lib, "A", "f2"));
115 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3"));
116 const intptr_t no_length = Field::kNoFixedLength;
117 EXPECT_EQ(no_length, f1.guarded_list_length());
118 EXPECT_EQ(kDoubleCid, f1.guarded_cid());
119 EXPECT_EQ(false, f1.is_nullable());
120 EXPECT_EQ(no_length, f2.guarded_list_length());
121 EXPECT_EQ(kDynamicCid, f2.guarded_cid());
122 EXPECT_EQ(true, f2.is_nullable());
123 EXPECT_EQ(4, f3.guarded_list_length());
124 EXPECT_EQ(kArrayCid, f3.guarded_cid());
125 EXPECT_EQ(false, f3.is_nullable());
126 }
127
128
129 TEST_CASE(GuardFieldFinalVariableLengthListTest) {
130 const char* script_chars =
131 "class A {\n"
132 " var f1 = 3.0;\n"
133 " var f2 = 3;\n"
134 " final f3 = new List();\n"
135 " foo() {\n"
136 " f1 = f1 + f1;\n"
137 " }\n"
138 " bar() {\n"
139 " f2 = null;\n"
140 " f2 = 3.0;\n"
141 " }\n"
142 "}\n"
143 "\n"
144 "runFoo() {\n"
145 " var a = new A();\n"
146 " for (int i = 0; i < 2000; i++) {\n"
147 " a.foo();\n"
148 " }\n"
149 "}\n"
150 "\n"
151 "runBar() {\n"
152 " var a = new A();\n"
153 " for (int i = 0; i < 2000; i++) {\n"
154 " a.bar();\n"
155 " }\n"
156 "}\n"
157 "main() {\n"
158 " runFoo();\n"
159 " runBar();\n"
160 "}\n";
161 Dart_Handle lib = TestCase::LoadTestScript(script_chars, NULL);
162 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
163 EXPECT_VALID(result);
164 Field& f1 = Field::ZoneHandle(LookupField(lib, "A", "f1"));
165 Field& f2 = Field::ZoneHandle(LookupField(lib, "A", "f2"));
166 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3"));
167 const intptr_t no_length = Field::kNoFixedLength;
168 EXPECT_EQ(no_length, f1.guarded_list_length());
169 EXPECT_EQ(kDoubleCid, f1.guarded_cid());
170 EXPECT_EQ(false, f1.is_nullable());
171 EXPECT_EQ(no_length, f2.guarded_list_length());
172 EXPECT_EQ(kDynamicCid, f2.guarded_cid());
173 EXPECT_EQ(true, f2.is_nullable());
174 EXPECT_EQ(no_length, f3.guarded_list_length());
175 EXPECT_EQ(kGrowableObjectArrayCid, f3.guarded_cid());
176 EXPECT_EQ(false, f3.is_nullable());
177 }
178
179
180 TEST_CASE(GuardFieldConstructorTest) {
181 const char* script_chars =
182 "import 'dart:typed_data';\n"
183 "class A {\n"
184 " var f1 = 3.0;\n"
185 " var f2 = 3;\n"
186 " final f3;\n"
187 " A(x) : f3 = x;\n"
188 " foo() {\n"
189 " f1 = f1 + f1;\n"
190 " }\n"
191 " bar() {\n"
192 " f2 = null;\n"
193 " f2 = 3.0;\n"
194 " }\n"
195 "}\n"
196 "\n"
197 "runFoo() {\n"
198 " var l = new Float32List(5);\n"
199 " for (int i = 0; i < 2000; i++) {\n"
200 " var a = new A(l);\n"
201 " a.foo();\n"
202 " }\n"
203 "}\n"
204 "\n"
205 "runBar() {\n"
206 " var l = new Float32List(5);\n"
207 " var a = new A(l);\n"
208 " for (int i = 0; i < 2000; i++) {\n"
209 " a.bar();\n"
210 " }\n"
211 "}\n"
212 "main() {\n"
213 " runFoo();\n"
214 " runBar();\n"
215 "}\n";
216 Dart_Handle lib = TestCase::LoadTestScript(script_chars, NULL);
217 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
218 EXPECT_VALID(result);
219 Field& f1 = Field::ZoneHandle(LookupField(lib, "A", "f1"));
220 Field& f2 = Field::ZoneHandle(LookupField(lib, "A", "f2"));
221 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3"));
222 const intptr_t no_length = Field::kNoFixedLength;
223 EXPECT_EQ(no_length, f1.guarded_list_length());
224 EXPECT_EQ(kDoubleCid, f1.guarded_cid());
225 EXPECT_EQ(false, f1.is_nullable());
226 EXPECT_EQ(no_length, f2.guarded_list_length());
227 EXPECT_EQ(kDynamicCid, f2.guarded_cid());
228 EXPECT_EQ(true, f2.is_nullable());
229 const intptr_t length = 5;
230 EXPECT_EQ(length, f3.guarded_list_length());
231 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid());
232 EXPECT_EQ(false, f3.is_nullable());
233 }
234
235
236 TEST_CASE(GuardFieldConstructor2Test) {
237 const char* script_chars =
238 "import 'dart:typed_data';\n"
239 "class A {\n"
240 " final f3;\n"
241 " A(x) : f3 = x;\n"
242 " foo() {\n"
243 " }\n"
244 " bar() {\n"
245 " }\n"
246 "}\n"
247 "\n"
248 "runFoo() {\n"
249 " var l = new Float32List(5);\n"
250 " for (int i = 0; i < 2000; i++) {\n"
251 " var a = new A(l);\n"
252 " }\n"
253 "}\n"
254 "\n"
255 "runBar() {\n"
256 " var l = new Float32List(99);\n"
257 " var a = new A(l);\n"
258 "}\n"
259 "main() {\n"
260 " runFoo();\n"
261 " runBar();\n"
262 "}\n";
263 Dart_Handle lib = TestCase::LoadTestScript(script_chars, NULL);
264 Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
265 EXPECT_VALID(result);
266 Field& f3 = Field::ZoneHandle(LookupField(lib, "A", "f3"));
267 const intptr_t no_length = Field::kNoFixedLength;
268 EXPECT_EQ(no_length, f3.guarded_list_length());
269 EXPECT_EQ(kTypedDataFloat32ArrayCid, f3.guarded_cid());
270 EXPECT_EQ(false, f3.is_nullable());
271 }
272
273 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/flow_graph_builder.cc ('k') | runtime/vm/intermediate_language_arm.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698