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

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 11360114: - Add functionality to morph a string into an external string (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 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) 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/json.h" 7 #include "platform/json.h"
8 #include "platform/utils.h" 8 #include "platform/utils.h"
9 #include "vm/class_finalizer.h" 9 #include "vm/class_finalizer.h"
10 #include "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
(...skipping 7182 matching lines...) Expand 10 before | Expand all | Expand 10 after
7193 o2 = &o2; 7193 o2 = &o2;
7194 EXPECT_VALID(Dart_GetPeer(s2, &o2)); 7194 EXPECT_VALID(Dart_GetPeer(s2, &o2));
7195 EXPECT(o2 == reinterpret_cast<void*>(&p2)); 7195 EXPECT(o2 == reinterpret_cast<void*>(&p2));
7196 } 7196 }
7197 Dart_ExitScope(); 7197 Dart_ExitScope();
7198 isolate->heap()->CollectGarbage(Heap::kOld); 7198 isolate->heap()->CollectGarbage(Heap::kOld);
7199 EXPECT_EQ(0, isolate->heap()->PeerCount()); 7199 EXPECT_EQ(0, isolate->heap()->PeerCount());
7200 } 7200 }
7201 7201
7202 7202
7203 // Test API call to make strings external.
7204 static void MakeExternalCback(void* peer) {
7205 *static_cast<int*>(peer) *= 2;
7206 }
7207
7208
7209 TEST_CASE(MakeExternalString) {
7210 int peer8 = 40;
7211 int peer16 = 41;
7212 intptr_t length = 0;
7213 intptr_t expected_length = 0;
7214 {
7215 Dart_EnterScope();
7216
7217 // First test some negative conditions.
7218 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' };
7219 const char* err = "string";
7220 Dart_Handle err_str = NewString(err);
7221 Dart_Handle ext_err_str = Dart_NewExternalUTF8String(
7222 data8, ARRAY_SIZE(data8), NULL, NULL);
7223 Dart_Handle result = Dart_MakeExternalString(Dart_Null(),
7224 data8,
7225 ARRAY_SIZE(data8),
7226 NULL,
7227 NULL);
7228 EXPECT(Dart_IsError(result)); // Null string object passed in.
7229 result = Dart_MakeExternalString(err_str,
7230 NULL,
7231 ARRAY_SIZE(data8),
7232 NULL,
7233 NULL);
7234 EXPECT(Dart_IsError(result)); // Null array pointer passed in.
7235 result = Dart_MakeExternalString(err_str,
7236 data8,
7237 1,
7238 NULL,
7239 NULL);
7240 EXPECT(Dart_IsError(result)); // Invalid length passed in.
7241
7242 const intptr_t kLength = 10;
7243 intptr_t size = 0;
7244
7245 // Test with an external string.
7246 result = Dart_MakeExternalString(ext_err_str,
7247 data8,
7248 ARRAY_SIZE(data8),
7249 NULL,
7250 NULL);
7251 EXPECT(Dart_IsString(result));
7252 EXPECT(Dart_IsExternalString(result));
7253
7254 // Test with an empty string.
7255 Dart_Handle empty_str = NewString("");
7256 EXPECT(Dart_IsString(empty_str));
7257 EXPECT(!Dart_IsExternalString(empty_str));
7258 uint8_t ext_empty_str[kLength];
7259 Dart_Handle str = Dart_MakeExternalString(empty_str,
7260 ext_empty_str,
7261 kLength,
7262 NULL,
7263 NULL);
7264 EXPECT(Dart_IsString(str));
7265 EXPECT(Dart_IsString(empty_str));
7266 EXPECT(Dart_IsAsciiString(str));
7267 EXPECT(Dart_IsAsciiString(empty_str));
7268 EXPECT(Dart_IsExternalString(str));
7269 EXPECT(Dart_IsExternalString(empty_str));
7270 EXPECT_VALID(Dart_StringLength(str, &length));
7271 EXPECT_EQ(0, length);
7272
7273 // Test with a one byte ascii string.
7274 const char* ascii = "string";
7275 expected_length = strlen(ascii);
7276 Dart_Handle ascii_str = NewString(ascii);
7277 EXPECT_VALID(ascii_str);
7278 EXPECT(Dart_IsString(ascii_str));
7279 EXPECT(Dart_IsAsciiString(ascii_str));
7280 EXPECT(!Dart_IsExternalString(ascii_str));
7281 EXPECT_VALID(Dart_StringLength(ascii_str, &length));
7282 EXPECT_EQ(expected_length, length);
7283
7284 uint8_t ext_ascii_str[kLength];
7285 EXPECT_VALID(Dart_StringStorageSize(ascii_str, &size));
7286 str = Dart_MakeExternalString(ascii_str,
7287 ext_ascii_str,
7288 size,
7289 &peer8,
7290 MakeExternalCback);
7291 EXPECT(Dart_IsString(str));
7292 EXPECT(Dart_IsString(ascii_str));
7293 EXPECT(Dart_IsAsciiString(str));
7294 EXPECT(Dart_IsAsciiString(ascii_str));
7295 EXPECT(Dart_IsExternalString(str));
7296 EXPECT(Dart_IsExternalString(ascii_str));
7297 EXPECT_VALID(Dart_StringLength(str, &length));
7298 EXPECT_EQ(expected_length, length);
7299 EXPECT_VALID(Dart_StringLength(ascii_str, &length));
7300 EXPECT_EQ(expected_length, length);
7301 EXPECT(Dart_IdentityEquals(str, ascii_str));
7302 for (intptr_t i = 0; i < length; i++) {
7303 EXPECT_EQ(ascii[i], ext_ascii_str[i]);
7304 }
7305
7306 uint8_t data[] = { 0xE4, 0xBA, 0x8c }; // U+4E8C.
7307 expected_length = 1;
7308 Dart_Handle utf16_str = Dart_NewStringFromUTF8(data, ARRAY_SIZE(data));
7309 EXPECT_VALID(utf16_str);
7310 EXPECT(Dart_IsString(utf16_str));
7311 EXPECT(!Dart_IsAsciiString(utf16_str));
7312 EXPECT(!Dart_IsExternalString(utf16_str));
7313 EXPECT_VALID(Dart_StringLength(utf16_str, &length));
7314 EXPECT_EQ(expected_length, length);
7315
7316 // Test with a two byte string.
7317 uint16_t ext_utf16_str[kLength];
7318 EXPECT_VALID(Dart_StringStorageSize(utf16_str, &size));
7319 str = Dart_MakeExternalString(utf16_str,
7320 ext_utf16_str,
7321 size,
7322 &peer16,
7323 MakeExternalCback);
7324 EXPECT(Dart_IsString(str));
7325 EXPECT(Dart_IsString(utf16_str));
7326 EXPECT(!Dart_IsAsciiString(str));
7327 EXPECT(!Dart_IsAsciiString(utf16_str));
7328 EXPECT(Dart_IsExternalString(str));
7329 EXPECT(Dart_IsExternalString(utf16_str));
7330 EXPECT_VALID(Dart_StringLength(str, &length));
7331 EXPECT_EQ(expected_length, length);
7332 EXPECT_VALID(Dart_StringLength(utf16_str, &length));
7333 EXPECT_EQ(expected_length, length);
7334 EXPECT(Dart_IdentityEquals(str, utf16_str));
7335 for (intptr_t i = 0; i < length; i++) {
7336 EXPECT_EQ(0x4e8c, ext_utf16_str[i]);
7337 }
7338
7339 Dart_ExitScope();
7340 }
7341 EXPECT_EQ(40, peer8);
7342 EXPECT_EQ(41, peer16);
7343 Isolate::Current()->heap()->CollectGarbage(Heap::kNew);
7344 EXPECT_EQ(80, peer8);
7345 EXPECT_EQ(82, peer16);
7346 }
7347
7348
7203 TEST_CASE(LazyLoadDeoptimizes) { 7349 TEST_CASE(LazyLoadDeoptimizes) {
7204 const char* kLoadFirst = 7350 const char* kLoadFirst =
7205 "start(a) {\n" 7351 "start(a) {\n"
7206 " var obj = (a == 1) ? createB() : new A();\n" 7352 " var obj = (a == 1) ? createB() : new A();\n"
7207 " for (int i = 0; i < 4000; i++) {\n" 7353 " for (int i = 0; i < 4000; i++) {\n"
7208 " var res = obj.foo();\n" 7354 " var res = obj.foo();\n"
7209 " if (a == 1) { if (res != 1) throw 'Error'; }\n" 7355 " if (a == 1) { if (res != 1) throw 'Error'; }\n"
7210 " else if (res != 2) throw 'Error'; \n" 7356 " else if (res != 2) throw 'Error'; \n"
7211 " }\n" 7357 " }\n"
7212 "}\n" 7358 "}\n"
(...skipping 21 matching lines...) Expand all
7234 Dart_LoadSource(TestCase::lib(), url, source); 7380 Dart_LoadSource(TestCase::lib(), url, source);
7235 7381
7236 dart_args[0] = Dart_NewInteger(1); 7382 dart_args[0] = Dart_NewInteger(1);
7237 result = Dart_Invoke(lib1, NewString("start"), 1, dart_args); 7383 result = Dart_Invoke(lib1, NewString("start"), 1, dart_args);
7238 EXPECT_VALID(result); 7384 EXPECT_VALID(result);
7239 } 7385 }
7240 7386
7241 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 7387 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
7242 7388
7243 } // namespace dart 7389 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698