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

Side by Side Diff: test/cctest/test-api.cc

Issue 1157353002: Version 4.4.63.4 (cherry-pick) (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@4.4
Patch Set: Created 5 years, 6 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
« no previous file with comments | « src/unicode.cc ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 7187 matching lines...) Expand 10 before | Expand all | Expand 10 after
7198 Local<v8::String> string = 7198 Local<v8::String> string =
7199 Local<v8::String>::Cast(a->Get(i)); 7199 Local<v8::String>::Cast(a->Get(i));
7200 Local<v8::Number> expected_len = 7200 Local<v8::Number> expected_len =
7201 Local<v8::Number>::Cast(alens->Get(i)); 7201 Local<v8::Number>::Cast(alens->Get(i));
7202 int length = GetUtf8Length(string); 7202 int length = GetUtf8Length(string);
7203 CHECK_EQ(static_cast<int>(expected_len->Value()), length); 7203 CHECK_EQ(static_cast<int>(expected_len->Value()), length);
7204 } 7204 }
7205 } 7205 }
7206 7206
7207 7207
7208 static uint16_t StringGet(Handle<String> str, int index) {
7209 i::Handle<i::String> istring =
7210 v8::Utils::OpenHandle(String::Cast(*str));
7211 return istring->Get(index);
7212 }
7213
7214
7215 static void WriteUtf8Helper(
7216 LocalContext& context, // NOLINT
7217 const char* name,
7218 const char* lengths_name,
7219 int len) {
7220 Local<v8::Array> b =
7221 Local<v8::Array>::Cast(context->Global()->Get(v8_str(name)));
7222 Local<v8::Array> alens =
7223 Local<v8::Array>::Cast(context->Global()->Get(v8_str(lengths_name)));
7224 char buffer[1000];
7225 char buffer2[1000];
7226 for (int i = 0; i < len; i++) {
7227 Local<v8::String> string =
7228 Local<v8::String>::Cast(b->Get(i));
7229 Local<v8::Number> expected_len =
7230 Local<v8::Number>::Cast(alens->Get(i));
7231 int utf8_length = static_cast<int>(expected_len->Value());
7232 for (int j = utf8_length + 1; j >= 0; j--) {
7233 memset(reinterpret_cast<void*>(&buffer), 42, sizeof(buffer));
7234 memset(reinterpret_cast<void*>(&buffer2), 42, sizeof(buffer2));
7235 int nchars;
7236 int utf8_written =
7237 string->WriteUtf8(buffer, j, &nchars, String::NO_OPTIONS);
7238 int utf8_written2 =
7239 string->WriteUtf8(buffer2, j, &nchars, String::NO_NULL_TERMINATION);
7240 CHECK_GE(utf8_length + 1, utf8_written);
7241 CHECK_GE(utf8_length, utf8_written2);
7242 for (int k = 0; k < utf8_written2; k++) {
7243 CHECK_EQ(buffer[k], buffer2[k]);
7244 }
7245 CHECK(nchars * 3 >= utf8_written - 1);
7246 CHECK(nchars <= utf8_written);
7247 if (j == utf8_length + 1) {
7248 CHECK_EQ(utf8_written2, utf8_length);
7249 CHECK_EQ(utf8_written2 + 1, utf8_written);
7250 }
7251 CHECK_EQ(buffer[utf8_written], 42);
7252 if (j > utf8_length) {
7253 if (utf8_written != 0) CHECK_EQ(buffer[utf8_written - 1], 0);
7254 if (utf8_written > 1) CHECK_NE(buffer[utf8_written - 2], 42);
7255 Handle<String> roundtrip = v8_str(buffer);
7256 CHECK(roundtrip->Equals(string));
7257 } else {
7258 if (utf8_written != 0) CHECK_NE(buffer[utf8_written - 1], 42);
7259 }
7260 if (utf8_written2 != 0) CHECK_NE(buffer[utf8_written - 1], 42);
7261 if (nchars >= 2) {
7262 uint16_t trail = StringGet(string, nchars - 1);
7263 uint16_t lead = StringGet(string, nchars - 2);
7264 if (((lead & 0xfc00) == 0xd800) &&
7265 ((trail & 0xfc00) == 0xdc00)) {
7266 unsigned u1 = buffer2[utf8_written2 - 4];
7267 unsigned u2 = buffer2[utf8_written2 - 3];
7268 unsigned u3 = buffer2[utf8_written2 - 2];
7269 unsigned u4 = buffer2[utf8_written2 - 1];
7270 CHECK_EQ((u1 & 0xf8), 0xf0u);
7271 CHECK_EQ((u2 & 0xc0), 0x80u);
7272 CHECK_EQ((u3 & 0xc0), 0x80u);
7273 CHECK_EQ((u4 & 0xc0), 0x80u);
7274 uint32_t c = 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
7275 CHECK_EQ((u4 & 0x3f), (c & 0x3f));
7276 CHECK_EQ((u3 & 0x3f), ((c >> 6) & 0x3f));
7277 CHECK_EQ((u2 & 0x3f), ((c >> 12) & 0x3f));
7278 CHECK_EQ((u1 & 0x3), c >> 18);
7279 }
7280 }
7281 }
7282 }
7283 }
7284
7285
7286 THREADED_TEST(Utf16) { 7208 THREADED_TEST(Utf16) {
7287 LocalContext context; 7209 LocalContext context;
7288 v8::HandleScope scope(context->GetIsolate()); 7210 v8::HandleScope scope(context->GetIsolate());
7289 CompileRun( 7211 CompileRun(
7290 "var pad = '01234567890123456789';" 7212 "var pad = '01234567890123456789';"
7291 "var p = [];" 7213 "var p = [];"
7292 "var plens = [20, 3, 3];" 7214 "var plens = [20, 3, 3];"
7293 "p.push('01234567890123456789');" 7215 "p.push('01234567890123456789');"
7294 "var lead = 0xd800;" 7216 "var lead = 0xd800;"
7295 "var trail = 0xdc00;" 7217 "var trail = 0xdc00;"
(...skipping 26 matching lines...) Expand all
7322 " c2.push(newc.substring(1, newc.length - 1));" 7244 " c2.push(newc.substring(1, newc.length - 1));"
7323 " var utf = alens[m] + alens[n];" // And here. 7245 " var utf = alens[m] + alens[n];" // And here.
7324 // The 'n's that start with 0xdc.. are 6-8 7246 // The 'n's that start with 0xdc.. are 6-8
7325 // The 'm's that end with 0xd8.. are 1, 4 and 7 7247 // The 'm's that end with 0xd8.. are 1, 4 and 7
7326 " if ((m % 3) == 1 && n >= 6) utf -= 2;" 7248 " if ((m % 3) == 1 && n >= 6) utf -= 2;"
7327 " a2lens.push(utf);" 7249 " a2lens.push(utf);"
7328 " }" 7250 " }"
7329 "}"); 7251 "}");
7330 Utf16Helper(context, "a", "alens", 9); 7252 Utf16Helper(context, "a", "alens", 9);
7331 Utf16Helper(context, "a2", "a2lens", 81); 7253 Utf16Helper(context, "a2", "a2lens", 81);
7332 WriteUtf8Helper(context, "b", "alens", 9);
7333 WriteUtf8Helper(context, "b2", "a2lens", 81);
7334 WriteUtf8Helper(context, "c2", "a2lens", 81);
7335 } 7254 }
7336 7255
7337 7256
7338 static bool SameSymbol(Handle<String> s1, Handle<String> s2) { 7257 static bool SameSymbol(Handle<String> s1, Handle<String> s2) {
7339 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1)); 7258 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1));
7340 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2)); 7259 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2));
7341 return *is1 == *is2; 7260 return *is1 == *is2;
7342 } 7261 }
7343 7262
7344 static void SameSymbolHelper(v8::Isolate* isolate, const char* a,
7345 const char* b) {
7346 Handle<String> symbol1 =
7347 v8::String::NewFromUtf8(isolate, a, v8::String::kInternalizedString);
7348 Handle<String> symbol2 =
7349 v8::String::NewFromUtf8(isolate, b, v8::String::kInternalizedString);
7350 CHECK(SameSymbol(symbol1, symbol2));
7351 }
7352
7353 7263
7354 THREADED_TEST(Utf16Symbol) { 7264 THREADED_TEST(Utf16Symbol) {
7355 LocalContext context; 7265 LocalContext context;
7356 v8::HandleScope scope(context->GetIsolate()); 7266 v8::HandleScope scope(context->GetIsolate());
7357 7267
7358 Handle<String> symbol1 = v8::String::NewFromUtf8( 7268 Handle<String> symbol1 = v8::String::NewFromUtf8(
7359 context->GetIsolate(), "abc", v8::String::kInternalizedString); 7269 context->GetIsolate(), "abc", v8::String::kInternalizedString);
7360 Handle<String> symbol2 = v8::String::NewFromUtf8( 7270 Handle<String> symbol2 = v8::String::NewFromUtf8(
7361 context->GetIsolate(), "abc", v8::String::kInternalizedString); 7271 context->GetIsolate(), "abc", v8::String::kInternalizedString);
7362 CHECK(SameSymbol(symbol1, symbol2)); 7272 CHECK(SameSymbol(symbol1, symbol2));
7363 7273
7364 SameSymbolHelper(context->GetIsolate(),
7365 "\360\220\220\205", // 4 byte encoding.
7366 "\355\240\201\355\260\205"); // 2 3-byte surrogates.
7367 SameSymbolHelper(context->GetIsolate(),
7368 "\355\240\201\355\260\206", // 2 3-byte surrogates.
7369 "\360\220\220\206"); // 4 byte encoding.
7370 SameSymbolHelper(context->GetIsolate(),
7371 "x\360\220\220\205", // 4 byte encoding.
7372 "x\355\240\201\355\260\205"); // 2 3-byte surrogates.
7373 SameSymbolHelper(context->GetIsolate(),
7374 "x\355\240\201\355\260\206", // 2 3-byte surrogates.
7375 "x\360\220\220\206"); // 4 byte encoding.
7376 CompileRun( 7274 CompileRun(
7377 "var sym0 = 'benedictus';" 7275 "var sym0 = 'benedictus';"
7378 "var sym0b = 'S\303\270ren';" 7276 "var sym0b = 'S\303\270ren';"
7379 "var sym1 = '\355\240\201\355\260\207';" 7277 "var sym1 = '\355\240\201\355\260\207';"
7380 "var sym2 = '\360\220\220\210';" 7278 "var sym2 = '\360\220\220\210';"
7381 "var sym3 = 'x\355\240\201\355\260\207';" 7279 "var sym3 = 'x\355\240\201\355\260\207';"
7382 "var sym4 = 'x\360\220\220\210';" 7280 "var sym4 = 'x\360\220\220\210';"
7383 "if (sym1.length != 2) throw sym1;" 7281 "if (sym1.length != 2) throw sym1;"
7384 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);" 7282 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);"
7385 "if (sym2.length != 2) throw sym2;" 7283 "if (sym2.length != 2) throw sym2;"
(...skipping 13636 matching lines...) Expand 10 before | Expand all | Expand 10 after
21022 20920
21023 { 20921 {
21024 v8::HandleScope handle_scope(isolate); 20922 v8::HandleScope handle_scope(isolate);
21025 20923
21026 // Should work 20924 // Should work
21027 v8::Local<v8::Object> obj = v8::Object::New(isolate); 20925 v8::Local<v8::Object> obj = v8::Object::New(isolate);
21028 20926
21029 USE(obj); 20927 USE(obj);
21030 } 20928 }
21031 } 20929 }
OLDNEW
« no previous file with comments | « src/unicode.cc ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698