diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2009-12-05 15:27:56 +0100 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2009-12-05 15:27:56 +0100 |
commit | c8b6ef248e5fc32df62041ec83463923bc8bdc68 (patch) | |
tree | 9012b1d0b0f9c380f6c6aa1b8b43dde44532e19e /deps/v8/test/cctest/test-strings.cc | |
parent | c5d82380f46ed0c992ff56a9e9ddfe6ab2540e62 (diff) | |
download | node-c8b6ef248e5fc32df62041ec83463923bc8bdc68.tar.gz |
upgrade v8 to 2.0.3
Diffstat (limited to 'deps/v8/test/cctest/test-strings.cc')
-rw-r--r-- | deps/v8/test/cctest/test-strings.cc | 117 |
1 files changed, 98 insertions, 19 deletions
diff --git a/deps/v8/test/cctest/test-strings.cc b/deps/v8/test/cctest/test-strings.cc index 0e9bf7a06..59a40af2a 100644 --- a/deps/v8/test/cctest/test-strings.cc +++ b/deps/v8/test/cctest/test-strings.cc @@ -63,6 +63,21 @@ class Resource: public v8::String::ExternalStringResource, }; +class AsciiResource: public v8::String::ExternalAsciiStringResource, + public ZoneObject { + public: + explicit AsciiResource(Vector<const char> string): data_(string.start()) { + length_ = string.length(); + } + virtual const char* data() const { return data_; } + virtual size_t length() const { return length_; } + + private: + const char* data_; + size_t length_; +}; + + static void InitializeBuildingBlocks( Handle<String> building_blocks[NUMBER_OF_BUILDING_BLOCKS]) { // A list of pointers that we don't have any interest in cleaning up. @@ -329,25 +344,89 @@ TEST(Utf8Conversion) { } -class TwoByteResource: public v8::String::ExternalStringResource { - public: - TwoByteResource(const uint16_t* data, size_t length, bool* destructed) - : data_(data), length_(length), destructed_(destructed) { - CHECK_NE(destructed, NULL); - *destructed_ = false; - } +TEST(ExternalShortStringAdd) { + ZoneScope zone(DELETE_ON_EXIT); - virtual ~TwoByteResource() { - CHECK_NE(destructed_, NULL); - CHECK(!*destructed_); - *destructed_ = true; - } + InitializeVM(); + v8::HandleScope handle_scope; - const uint16_t* data() const { return data_; } - size_t length() const { return length_; } + // Make sure we cover all always-flat lengths and at least one above. + static const int kMaxLength = 20; + CHECK_GT(kMaxLength, i::String::kMinNonFlatLength); + + // Allocate two JavaScript arrays for holding short strings. + v8::Handle<v8::Array> ascii_external_strings = + v8::Array::New(kMaxLength + 1); + v8::Handle<v8::Array> non_ascii_external_strings = + v8::Array::New(kMaxLength + 1); + + // Generate short ascii and non-ascii external strings. + for (int i = 0; i <= kMaxLength; i++) { + char* ascii = Zone::NewArray<char>(i + 1); + for (int j = 0; j < i; j++) { + ascii[j] = 'a'; + } + // Terminating '\0' is left out on purpose. It is not required for external + // string data. + AsciiResource* ascii_resource = + new AsciiResource(Vector<const char>(ascii, i)); + v8::Local<v8::String> ascii_external_string = + v8::String::NewExternal(ascii_resource); + + ascii_external_strings->Set(v8::Integer::New(i), ascii_external_string); + uc16* non_ascii = Zone::NewArray<uc16>(i + 1); + for (int j = 0; j < i; j++) { + non_ascii[j] = 0x1234; + } + // Terminating '\0' is left out on purpose. It is not required for external + // string data. + Resource* resource = new Resource(Vector<const uc16>(non_ascii, i)); + v8::Local<v8::String> non_ascii_external_string = + v8::String::NewExternal(resource); + non_ascii_external_strings->Set(v8::Integer::New(i), + non_ascii_external_string); + } - private: - const uint16_t* data_; - size_t length_; - bool* destructed_; -}; + // Add the arrays with the short external strings in the global object. + v8::Handle<v8::Object> global = env->Global(); + global->Set(v8_str("external_ascii"), ascii_external_strings); + global->Set(v8_str("external_non_ascii"), non_ascii_external_strings); + global->Set(v8_str("max_length"), v8::Integer::New(kMaxLength)); + + // Add short external ascii and non-ascii strings checking the result. + static const char* source = + "function test() {" + " var ascii_chars = 'aaaaaaaaaaaaaaaaaaaa';" + " var non_ascii_chars = '\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234\\u1234';" //NOLINT + " if (ascii_chars.length != max_length) return 1;" + " if (non_ascii_chars.length != max_length) return 2;" + " var ascii = Array(max_length + 1);" + " var non_ascii = Array(max_length + 1);" + " for (var i = 0; i <= max_length; i++) {" + " ascii[i] = ascii_chars.substring(0, i);" + " non_ascii[i] = non_ascii_chars.substring(0, i);" + " };" + " for (var i = 0; i <= max_length; i++) {" + " if (ascii[i] != external_ascii[i]) return 3;" + " if (non_ascii[i] != external_non_ascii[i]) return 4;" + " for (var j = 0; j < i; j++) {" + " if (external_ascii[i] !=" + " (external_ascii[j] + external_ascii[i - j])) return 5;" + " if (external_non_ascii[i] !=" + " (external_non_ascii[j] + external_non_ascii[i - j])) return 6;" + " if (non_ascii[i] != (non_ascii[j] + non_ascii[i - j])) return 7;" + " if (ascii[i] != (ascii[j] + ascii[i - j])) return 8;" + " if (ascii[i] != (external_ascii[j] + ascii[i - j])) return 9;" + " if (ascii[i] != (ascii[j] + external_ascii[i - j])) return 10;" + " if (non_ascii[i] !=" + " (external_non_ascii[j] + non_ascii[i - j])) return 11;" + " if (non_ascii[i] !=" + " (non_ascii[j] + external_non_ascii[i - j])) return 12;" + " }" + " }" + " return 0;" + "};" + "test()"; + CHECK_EQ(0, + v8::Script::Compile(v8::String::New(source))->Run()->Int32Value()); +} |