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

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 8417003: Enhance the array access API to deal with any objct that implements the list interface. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: '' Created 9 years, 1 month 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 6
7 #include "vm/assert.h" 7 #include "vm/assert.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" 9 #include "vm/dart_api_state.h"
10 #include "vm/unit_test.h" 10 #include "vm/unit_test.h"
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 result = Dart_IntegerValue(result, &value); 207 result = Dart_IntegerValue(result, &value);
208 EXPECT(Dart_IsValid(result)); 208 EXPECT(Dart_IsValid(result));
209 EXPECT_EQ(i, value); 209 EXPECT_EQ(i, value);
210 } 210 }
211 211
212 Dart_ExitScope(); // Exit the Dart API scope. 212 Dart_ExitScope(); // Exit the Dart API scope.
213 Dart_ShutdownIsolate(); 213 Dart_ShutdownIsolate();
214 } 214 }
215 215
216 216
217 #if defined(TARGET_ARCH_IA32) // only ia32 can run execution tests.
218
219 UNIT_TEST_CASE(ListAccess) {
220 const char* kScriptChars =
221 "class ListAccessTest {"
222 " ListAccessTest() {}"
223 " static List testMain() {"
224 " List a = new List();"
225 " a.add(10);"
226 " a.add(20);"
227 " a.add(30);"
228 " return a;"
229 " }"
230 "}";
231 Dart_Handle result;
232
233 Dart_CreateIsolate(NULL, NULL);
234 {
235 Dart_EnterScope(); // Start a Dart API scope for invoking API functions.
236
237 // Create a test library and Load up a test script in it.
238 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
239
240 // Invoke a function which returns an object of type InstanceOf..
241 result = Dart_InvokeStatic(lib,
242 Dart_NewString("ListAccessTest"),
243 Dart_NewString("testMain"),
244 0,
245 NULL);
246 EXPECT(Dart_IsValid(result));
247 EXPECT(!Dart_ExceptionOccurred(result));
248
249 // First ensure that the returned object is an array.
250 Dart_Handle ListAccessTestObj = result;
251
252 EXPECT(Dart_IsArray(ListAccessTestObj));
253
254 // Get length of array object.
255 intptr_t len = 0;
256 result = Dart_GetLength(ListAccessTestObj, &len);
257 EXPECT(Dart_IsValid(result));
258 EXPECT_EQ(3, len);
259
260 // Access elements in the array.
261 int64_t value;
262
263 result = Dart_ArrayGetAt(ListAccessTestObj, 0);
264 EXPECT(Dart_IsValid(result));
265 result = Dart_IntegerValue(result, &value);
266 EXPECT(Dart_IsValid(result));
267 EXPECT_EQ(10, value);
268
269 result = Dart_ArrayGetAt(ListAccessTestObj, 1);
270 EXPECT(Dart_IsValid(result));
271 result = Dart_IntegerValue(result, &value);
272 EXPECT(Dart_IsValid(result));
273 EXPECT_EQ(20, value);
274
275 result = Dart_ArrayGetAt(ListAccessTestObj, 2);
276 EXPECT(Dart_IsValid(result));
277 result = Dart_IntegerValue(result, &value);
278 EXPECT(Dart_IsValid(result));
279 EXPECT_EQ(30, value);
280
281 // Set some elements in the array.
282 result = Dart_ArraySetAt(ListAccessTestObj, 0, Dart_NewInteger(0));
283 EXPECT(Dart_IsValid(result));
284 result = Dart_ArraySetAt(ListAccessTestObj, 1, Dart_NewInteger(1));
285 EXPECT(Dart_IsValid(result));
286 result = Dart_ArraySetAt(ListAccessTestObj, 2, Dart_NewInteger(2));
287 EXPECT(Dart_IsValid(result));
288
289 // Get length of array object.
290 result = Dart_GetLength(ListAccessTestObj, &len);
291 EXPECT(Dart_IsValid(result));
292 EXPECT_EQ(3, len);
293
294 // Now try and access these elements in the array.
295 result = Dart_ArrayGetAt(ListAccessTestObj, 0);
296 EXPECT(Dart_IsValid(result));
297 result = Dart_IntegerValue(result, &value);
298 EXPECT(Dart_IsValid(result));
299 EXPECT_EQ(0, value);
300
301 result = Dart_ArrayGetAt(ListAccessTestObj, 1);
302 EXPECT(Dart_IsValid(result));
303 result = Dart_IntegerValue(result, &value);
304 EXPECT(Dart_IsValid(result));
305 EXPECT_EQ(1, value);
306
307 result = Dart_ArrayGetAt(ListAccessTestObj, 2);
308 EXPECT(Dart_IsValid(result));
309 result = Dart_IntegerValue(result, &value);
310 EXPECT(Dart_IsValid(result));
311 EXPECT_EQ(2, value);
312
313 uint8_t native_array[3];
314 result = Dart_ArrayGet(ListAccessTestObj, 0, native_array, 3);
315 EXPECT(Dart_IsValid(result));
316 EXPECT_EQ(0, native_array[0]);
317 EXPECT_EQ(1, native_array[1]);
318 EXPECT_EQ(2, native_array[2]);
319
320 native_array[0] = 10;
321 native_array[1] = 20;
322 native_array[2] = 30;
323 result = Dart_ArraySet(ListAccessTestObj, 0, native_array, 3);
324 EXPECT(Dart_IsValid(result));
325 result = Dart_ArrayGet(ListAccessTestObj, 0, native_array, 3);
326 EXPECT(Dart_IsValid(result));
327 EXPECT_EQ(10, native_array[0]);
328 EXPECT_EQ(20, native_array[1]);
329 EXPECT_EQ(30, native_array[2]);
330 result = Dart_ArrayGetAt(ListAccessTestObj, 2);
331 EXPECT(Dart_IsValid(result));
332 result = Dart_IntegerValue(result, &value);
333 EXPECT(Dart_IsValid(result));
334 EXPECT_EQ(30, value);
335
336 // Check if we get an exception when accessing beyond limit.
337 result = Dart_ArrayGetAt(ListAccessTestObj, 4);
338 EXPECT(!Dart_IsValid(result));
339
340 Dart_ExitScope(); // Exit the Dart API scope.
341 }
342 Dart_ShutdownIsolate();
343 }
344
345 #endif // TARGET_ARCH_IA32.
346
347
217 // Unit test for entering a scope, creating a local handle and exiting 348 // Unit test for entering a scope, creating a local handle and exiting
218 // the scope. 349 // the scope.
219 UNIT_TEST_CASE(EnterExitScope) { 350 UNIT_TEST_CASE(EnterExitScope) {
220 Dart_CreateIsolate(NULL, NULL); 351 Dart_CreateIsolate(NULL, NULL);
221 Isolate* isolate = Isolate::Current(); 352 Isolate* isolate = Isolate::Current();
222 EXPECT(isolate != NULL); 353 EXPECT(isolate != NULL);
223 ApiState* state = isolate->api_state(); 354 ApiState* state = isolate->api_state();
224 EXPECT(state != NULL); 355 EXPECT(state != NULL);
225 ApiLocalScope* scope = state->top_scope(); 356 ApiLocalScope* scope = state->top_scope();
226 Dart_EnterScope(); 357 Dart_EnterScope();
(...skipping 1498 matching lines...) Expand 10 before | Expand all | Expand 10 after
1725 NULL); 1856 NULL);
1726 assert(Dart_IsValid(result)); 1857 assert(Dart_IsValid(result));
1727 1858
1728 Dart_ExitScope(); // Exit the Dart API scope. 1859 Dart_ExitScope(); // Exit the Dart API scope.
1729 } 1860 }
1730 Dart_ShutdownIsolate(); 1861 Dart_ShutdownIsolate();
1731 } 1862 }
1732 #endif // TARGET_ARCH_IA32. 1863 #endif // TARGET_ARCH_IA32.
1733 1864
1734 } // namespace dart 1865 } // namespace dart
OLDNEW
« vm/dart_api_impl.cc ('K') | « vm/dart_api_impl.cc ('k') | vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698