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 "bin/builtin.h" | 5 #include "bin/builtin.h" |
6 #include "include/dart_api.h" | 6 #include "include/dart_api.h" |
7 #include "include/dart_debugger_api.h" | 7 #include "include/dart_debugger_api.h" |
8 #include "include/dart_mirrors_api.h" | 8 #include "include/dart_mirrors_api.h" |
9 #include "include/dart_native_api.h" | 9 #include "include/dart_native_api.h" |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 1267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1278 result = Dart_ListSetAsBytes(list_access_test_obj, 0, native_array, 3); | 1278 result = Dart_ListSetAsBytes(list_access_test_obj, 0, native_array, 3); |
1279 EXPECT(Dart_IsError(result)); | 1279 EXPECT(Dart_IsError(result)); |
1280 EXPECT(Dart_IsUnhandledExceptionError(result)); | 1280 EXPECT(Dart_IsUnhandledExceptionError(result)); |
1281 | 1281 |
1282 result = Dart_ListSetAt(list_access_test_obj, 0, Dart_NewInteger(42)); | 1282 result = Dart_ListSetAt(list_access_test_obj, 0, Dart_NewInteger(42)); |
1283 EXPECT(Dart_IsError(result)); | 1283 EXPECT(Dart_IsError(result)); |
1284 EXPECT(Dart_IsUnhandledExceptionError(result)); | 1284 EXPECT(Dart_IsUnhandledExceptionError(result)); |
1285 } | 1285 } |
1286 | 1286 |
1287 | 1287 |
| 1288 TEST_CASE(MapAccess) { |
| 1289 const char* kScriptChars = |
| 1290 "Map testMain() {" |
| 1291 " return {" |
| 1292 " 'a' : 1," |
| 1293 " 'b' : null," |
| 1294 " };" |
| 1295 "}"; |
| 1296 Dart_Handle result; |
| 1297 |
| 1298 // Create a test library and Load up a test script in it. |
| 1299 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1300 |
| 1301 // Invoke a function which returns an object of type Map. |
| 1302 result = Dart_Invoke(lib, NewString("testMain"), 0, NULL); |
| 1303 EXPECT_VALID(result); |
| 1304 |
| 1305 // First ensure that the returned object is a map. |
| 1306 Dart_Handle map = result; |
| 1307 Dart_Handle a = NewString("a"); |
| 1308 Dart_Handle b = NewString("b"); |
| 1309 Dart_Handle c = NewString("c"); |
| 1310 |
| 1311 EXPECT(Dart_IsMap(map)); |
| 1312 EXPECT(!Dart_IsMap(a)); |
| 1313 |
| 1314 // Access values in the map. |
| 1315 int64_t value; |
| 1316 result = Dart_MapGetAt(map, a); |
| 1317 EXPECT_VALID(result); |
| 1318 result = Dart_IntegerToInt64(result, &value); |
| 1319 EXPECT_VALID(result); |
| 1320 EXPECT_EQ(value, 1); |
| 1321 |
| 1322 result = Dart_MapGetAt(map, b); |
| 1323 EXPECT(Dart_IsNull(result)); |
| 1324 |
| 1325 result = Dart_MapGetAt(map, c); |
| 1326 EXPECT(Dart_IsNull(result)); |
| 1327 |
| 1328 EXPECT(Dart_IsError(Dart_MapGetAt(a, a))); |
| 1329 |
| 1330 // Test for presence of keys. |
| 1331 bool contains = false; |
| 1332 result = Dart_MapContainsKey(map, a); |
| 1333 EXPECT_VALID(result); |
| 1334 result = Dart_BooleanValue(result, &contains); |
| 1335 EXPECT_VALID(result); |
| 1336 EXPECT(contains); |
| 1337 |
| 1338 contains = false; |
| 1339 result = Dart_MapContainsKey(map, NewString("b")); |
| 1340 EXPECT_VALID(result); |
| 1341 result = Dart_BooleanValue(result, &contains); |
| 1342 EXPECT_VALID(result); |
| 1343 EXPECT(contains); |
| 1344 |
| 1345 contains = true; |
| 1346 result = Dart_MapContainsKey(map, NewString("c")); |
| 1347 EXPECT_VALID(result); |
| 1348 result = Dart_BooleanValue(result, &contains); |
| 1349 EXPECT_VALID(result); |
| 1350 EXPECT(!contains); |
| 1351 |
| 1352 EXPECT(Dart_IsError(Dart_MapContainsKey(a, a))); |
| 1353 |
| 1354 // Enumerate keys. (Note literal maps guarantee key order.) |
| 1355 Dart_Handle keys = Dart_MapKeys(map); |
| 1356 EXPECT_VALID(keys); |
| 1357 |
| 1358 intptr_t len = 0; |
| 1359 bool equals; |
| 1360 result = Dart_ListLength(keys, &len); |
| 1361 EXPECT_VALID(result); |
| 1362 EXPECT_EQ(2, len); |
| 1363 |
| 1364 result = Dart_ListGetAt(keys, 0); |
| 1365 EXPECT(Dart_IsString(result)); |
| 1366 equals = false; |
| 1367 EXPECT_VALID(Dart_ObjectEquals(result, a, &equals)); |
| 1368 EXPECT(equals); |
| 1369 |
| 1370 result = Dart_ListGetAt(keys, 1); |
| 1371 EXPECT(Dart_IsString(result)); |
| 1372 equals = false; |
| 1373 EXPECT_VALID(Dart_ObjectEquals(result, b, &equals)); |
| 1374 EXPECT(equals); |
| 1375 |
| 1376 EXPECT(Dart_IsError(Dart_MapKeys(a))); |
| 1377 } |
| 1378 |
| 1379 |
1288 TEST_CASE(TypedDataViewListGetAsBytes) { | 1380 TEST_CASE(TypedDataViewListGetAsBytes) { |
1289 const int kSize = 1000; | 1381 const int kSize = 1000; |
1290 | 1382 |
1291 const char* kScriptChars = | 1383 const char* kScriptChars = |
1292 "import 'dart:typed_data';\n" | 1384 "import 'dart:typed_data';\n" |
1293 "List main(int size) {\n" | 1385 "List main(int size) {\n" |
1294 " var a = new Int8List(size);\n" | 1386 " var a = new Int8List(size);\n" |
1295 " var view = new Int8List.view(a.buffer, 0, size);\n" | 1387 " var view = new Int8List.view(a.buffer, 0, size);\n" |
1296 " return view;\n" | 1388 " return view;\n" |
1297 "}\n"; | 1389 "}\n"; |
(...skipping 7110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8408 NewString("main"), | 8500 NewString("main"), |
8409 1, | 8501 1, |
8410 dart_args); | 8502 dart_args); |
8411 int64_t value = 0; | 8503 int64_t value = 0; |
8412 result = Dart_IntegerToInt64(result, &value); | 8504 result = Dart_IntegerToInt64(result, &value); |
8413 EXPECT_VALID(result); | 8505 EXPECT_VALID(result); |
8414 EXPECT_EQ(6, value); | 8506 EXPECT_EQ(6, value); |
8415 } | 8507 } |
8416 | 8508 |
8417 } // namespace dart | 8509 } // namespace dart |
OLD | NEW |