Chromium Code Reviews| 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 "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 7144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 7155 EXPECT_EQ(2, isolate->heap()->PeerCount()); | 7155 EXPECT_EQ(2, isolate->heap()->PeerCount()); |
| 7156 o2 = &o2; | 7156 o2 = &o2; |
| 7157 EXPECT_VALID(Dart_GetPeer(s2, &o2)); | 7157 EXPECT_VALID(Dart_GetPeer(s2, &o2)); |
| 7158 EXPECT(o2 == reinterpret_cast<void*>(&p2)); | 7158 EXPECT(o2 == reinterpret_cast<void*>(&p2)); |
| 7159 } | 7159 } |
| 7160 Dart_ExitScope(); | 7160 Dart_ExitScope(); |
| 7161 isolate->heap()->CollectGarbage(Heap::kOld); | 7161 isolate->heap()->CollectGarbage(Heap::kOld); |
| 7162 EXPECT_EQ(0, isolate->heap()->PeerCount()); | 7162 EXPECT_EQ(0, isolate->heap()->PeerCount()); |
| 7163 } | 7163 } |
| 7164 | 7164 |
| 7165 | |
| 7166 // Test API call to make strings external. | |
| 7167 static void MakeExternalCback(void* peer) { | |
| 7168 *static_cast<int*>(peer) *= 2; | |
| 7169 } | |
| 7170 | |
| 7171 | |
| 7172 TEST_CASE(MakeExternalString) { | |
| 7173 int peer8 = 40; | |
| 7174 int peer16 = 41; | |
| 7175 intptr_t length = 0; | |
| 7176 intptr_t expected_length = 0; | |
| 7177 { | |
| 7178 Dart_EnterScope(); | |
| 7179 | |
| 7180 // First test some negative conditions. | |
| 7181 uint8_t data8[] = { 'h', 'e', 'l', 'l', 'o' }; | |
| 7182 const char* err = "string"; | |
| 7183 Dart_Handle err_str = NewString(err); | |
| 7184 Dart_Handle ext_err_str = Dart_NewExternalUTF8String( | |
| 7185 data8, ARRAY_SIZE(data8), NULL, NULL); | |
| 7186 Dart_Handle result = Dart_MakeExternalString(Dart_Null(), | |
| 7187 data8, | |
| 7188 ARRAY_SIZE(data8), | |
| 7189 NULL, | |
| 7190 NULL); | |
| 7191 EXPECT(Dart_IsError(result)); // Null string object passed in. | |
| 7192 result = Dart_MakeExternalString(err_str, | |
| 7193 NULL, | |
| 7194 ARRAY_SIZE(data8), | |
| 7195 NULL, | |
| 7196 NULL); | |
| 7197 EXPECT(Dart_IsError(result)); // Null array pointer passed in. | |
| 7198 result = Dart_MakeExternalString(err_str, | |
| 7199 data8, | |
| 7200 1, | |
| 7201 NULL, | |
| 7202 NULL); | |
| 7203 EXPECT(Dart_IsError(result)); // Invalid length passed in. | |
| 7204 result = Dart_MakeExternalString(ext_err_str, | |
| 7205 data8, | |
| 7206 ARRAY_SIZE(data8), | |
| 7207 NULL, | |
| 7208 NULL); | |
| 7209 EXPECT(Dart_IsError(result)); // External string passed in. | |
| 7210 | |
| 7211 // Now test with a one byte ascii string. | |
| 7212 const char* ascii = "string"; | |
| 7213 expected_length = strlen(ascii); | |
| 7214 Dart_Handle ascii_str = NewString(ascii); | |
| 7215 EXPECT_VALID(ascii_str); | |
| 7216 EXPECT(Dart_IsString(ascii_str)); | |
| 7217 EXPECT(Dart_IsAsciiString(ascii_str)); | |
| 7218 EXPECT(!Dart_IsExternalString(ascii_str)); | |
| 7219 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); | |
| 7220 EXPECT_EQ(expected_length, length); | |
| 7221 | |
| 7222 const intptr_t kLength = 10; | |
| 7223 uint8_t ext_ascii_str[kLength]; | |
| 7224 Dart_Handle str = Dart_MakeExternalString(ascii_str, | |
| 7225 ext_ascii_str, | |
| 7226 kLength, | |
| 7227 &peer8, | |
| 7228 MakeExternalCback); | |
| 7229 EXPECT(Dart_IsString(str)); | |
| 7230 EXPECT(Dart_IsString(ascii_str)); | |
| 7231 EXPECT(Dart_IsAsciiString(str)); | |
| 7232 EXPECT(Dart_IsAsciiString(ascii_str)); | |
| 7233 EXPECT(Dart_IsExternalString(str)); | |
| 7234 EXPECT(Dart_IsExternalString(ascii_str)); | |
| 7235 EXPECT_VALID(Dart_StringLength(str, &length)); | |
| 7236 EXPECT_EQ(expected_length, length); | |
| 7237 EXPECT_VALID(Dart_StringLength(ascii_str, &length)); | |
| 7238 EXPECT_EQ(expected_length, length); | |
| 7239 for (intptr_t i = 0; i < length; i++) { | |
| 7240 EXPECT_EQ(ascii[i], ext_ascii_str[i]); | |
| 7241 } | |
| 7242 | |
| 7243 uint8_t data[] = { 0xE4, 0xBA, 0x8c }; // U+4E8C. | |
| 7244 expected_length = 1; | |
| 7245 Dart_Handle utf16_str = Dart_NewStringFromUTF8(data, ARRAY_SIZE(data)); | |
| 7246 EXPECT_VALID(utf16_str); | |
| 7247 EXPECT(Dart_IsString(utf16_str)); | |
| 7248 EXPECT(!Dart_IsAsciiString(utf16_str)); | |
| 7249 EXPECT(!Dart_IsExternalString(utf16_str)); | |
| 7250 EXPECT_VALID(Dart_StringLength(utf16_str, &length)); | |
| 7251 EXPECT_EQ(expected_length, length); | |
| 7252 | |
| 7253 // Now test with a two byte string. | |
| 7254 uint16_t ext_utf16_str[kLength]; | |
| 7255 str = Dart_MakeExternalString(utf16_str, | |
| 7256 ext_utf16_str, | |
| 7257 kLength, | |
| 7258 &peer16, | |
| 7259 MakeExternalCback); | |
| 7260 EXPECT(Dart_IsString(str)); | |
| 7261 EXPECT(Dart_IsString(utf16_str)); | |
| 7262 EXPECT(!Dart_IsAsciiString(str)); | |
| 7263 EXPECT(!Dart_IsAsciiString(utf16_str)); | |
| 7264 EXPECT(Dart_IsExternalString(str)); | |
| 7265 EXPECT(Dart_IsExternalString(utf16_str)); | |
| 7266 EXPECT_VALID(Dart_StringLength(str, &length)); | |
| 7267 EXPECT_EQ(expected_length, length); | |
| 7268 EXPECT_VALID(Dart_StringLength(utf16_str, &length)); | |
| 7269 EXPECT_EQ(expected_length, length); | |
|
Anton Muhin
2012/11/07 10:05:46
may you add a test that it's the same object?
siva
2012/11/07 21:43:27
Done.
| |
| 7270 for (intptr_t i = 0; i < length; i++) { | |
| 7271 EXPECT_EQ(0x4e8c, ext_utf16_str[i]); | |
| 7272 } | |
| 7273 | |
| 7274 Dart_ExitScope(); | |
| 7275 } | |
| 7276 EXPECT_EQ(40, peer8); | |
| 7277 EXPECT_EQ(41, peer16); | |
| 7278 Isolate::Current()->heap()->CollectGarbage(Heap::kNew); | |
| 7279 EXPECT_EQ(80, peer8); | |
| 7280 EXPECT_EQ(82, peer16); | |
| 7281 } | |
| 7282 | |
| 7283 | |
| 7165 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 7284 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 7166 | 7285 |
| 7167 } // namespace dart | 7286 } // namespace dart |
| OLD | NEW |