Commit 0a06a1bc authored by Alexey Kozyatinskiy's avatar Alexey Kozyatinskiy Committed by Commit Bot

[liveedit] migrate compare strings tests to native

Extracted from https://chromium-review.googlesource.com/c/v8/v8/+/1105493/

R=yangguo@chromium.org

Bug: v8:7862
Change-Id: I804a444a43047e3303b5f1b0140522b4f609f3ba
Reviewed-on: https://chromium-review.googlesource.com/1107393
Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53891}
parent 4252d53f
This diff is collapsed.
......@@ -33,6 +33,13 @@ namespace internal {
class JavaScriptFrame;
struct SourceChangeRange {
int start_position;
int end_position;
int new_start_position;
int new_end_position;
};
// This class collects some specific information on structure of functions
// in a particular script.
//
......@@ -71,7 +78,6 @@ class LiveEditFunctionTracker
DISALLOW_COPY_AND_ASSIGN(LiveEditFunctionTracker);
};
class LiveEdit : AllStatic {
public:
static void InitializeThreadLocal(Debug* debug);
......@@ -129,6 +135,12 @@ class LiveEdit : AllStatic {
// Return error message or nullptr.
static const char* RestartFrame(JavaScriptFrame* frame);
static void CompareStrings(Isolate* isolate, Handle<String> a,
Handle<String> b,
std::vector<SourceChangeRange>* changes);
static int TranslatePosition(const std::vector<SourceChangeRange>& changed,
int position);
// A copy of this is in liveedit.js.
enum FunctionPatchabilityStatus {
FUNCTION_AVAILABLE_FOR_PATCH = 1,
......@@ -151,40 +163,6 @@ class LiveEdit : AllStatic {
static const bool kFrameDropperSupported;
};
// A general-purpose comparator between 2 arrays.
class Comparator {
public:
// Holds 2 arrays of some elements allowing to compare any pair of
// element from the first array and element from the second array.
class Input {
public:
virtual int GetLength1() = 0;
virtual int GetLength2() = 0;
virtual bool Equals(int index1, int index2) = 0;
protected:
virtual ~Input() {}
};
// Receives compare result as a series of chunks.
class Output {
public:
// Puts another chunk in result list. Note that technically speaking
// only 3 arguments actually needed with 4th being derivable.
virtual void AddChunk(int pos1, int pos2, int len1, int len2) = 0;
protected:
virtual ~Output() {}
};
// Finds the difference between 2 arrays of elements.
static void CalculateDifference(Input* input,
Output* result_writer);
};
// Simple helper class that creates more or less typed structures over
// JSArray object. This is an adhoc method of passing structures from C++
// to JavaScript.
......
......@@ -29,83 +29,22 @@
#include "src/v8.h"
#include "src/api.h"
#include "src/debug/liveedit.h"
#include "src/objects-inl.h"
#include "test/cctest/cctest.h"
namespace v8 {
namespace internal {
// Anonymous namespace.
namespace {
class StringCompareInput : public Comparator::Input {
public:
StringCompareInput(const char* s1, const char* s2) : s1_(s1), s2_(s2) {
}
int GetLength1() {
return StrLength(s1_);
}
int GetLength2() {
return StrLength(s2_);
}
bool Equals(int index1, int index2) {
return s1_[index1] == s2_[index2];
}
private:
const char* s1_;
const char* s2_;
};
class DiffChunkStruct : public ZoneObject {
public:
DiffChunkStruct(int pos1_param, int pos2_param, int len1_param,
int len2_param)
: pos1(pos1_param),
pos2(pos2_param),
len1(len1_param),
len2(len2_param),
next(nullptr) {}
int pos1;
int pos2;
int len1;
int len2;
DiffChunkStruct* next;
};
class ListDiffOutputWriter : public Comparator::Output {
public:
explicit ListDiffOutputWriter(DiffChunkStruct** next_chunk_pointer,
Zone* zone)
: next_chunk_pointer_(next_chunk_pointer), zone_(zone) {
(*next_chunk_pointer_) = nullptr;
}
void AddChunk(int pos1, int pos2, int len1, int len2) {
current_chunk_ = new(zone_) DiffChunkStruct(pos1, pos2, len1, len2);
(*next_chunk_pointer_) = current_chunk_;
next_chunk_pointer_ = &current_chunk_->next;
}
private:
DiffChunkStruct** next_chunk_pointer_;
DiffChunkStruct* current_chunk_;
Zone* zone_;
};
void CompareStringsOneWay(const char* s1, const char* s2,
int expected_diff_parameter = -1) {
StringCompareInput input(s1, s2);
v8::internal::AccountingAllocator allocator;
Zone zone(&allocator, ZONE_NAME);
DiffChunkStruct* first_chunk;
ListDiffOutputWriter writer(&first_chunk, &zone);
Comparator::CalculateDifference(&input, &writer);
int expected_diff_parameter,
std::vector<SourceChangeRange>* changes) {
i::Isolate* isolate = CcTest::i_isolate();
i::Handle<i::String> i_s1 = isolate->factory()->NewStringFromAsciiChecked(s1);
i::Handle<i::String> i_s2 = isolate->factory()->NewStringFromAsciiChecked(s2);
changes->clear();
LiveEdit::CompareStrings(isolate, i_s1, i_s2, changes);
int len1 = StrLength(s1);
int len2 = StrLength(s2);
......@@ -114,23 +53,23 @@ void CompareStringsOneWay(const char* s1, const char* s2,
int pos2 = 0;
int diff_parameter = 0;
for (DiffChunkStruct* chunk = first_chunk; chunk != nullptr;
chunk = chunk->next) {
int diff_pos1 = chunk->pos1;
for (const auto& diff : *changes) {
int diff_pos1 = diff.start_position;
int similar_part_length = diff_pos1 - pos1;
int diff_pos2 = pos2 + similar_part_length;
CHECK_EQ(diff_pos2, chunk->pos2);
CHECK_EQ(diff_pos2, diff.new_start_position);
for (int j = 0; j < similar_part_length; j++) {
CHECK(pos1 + j < len1);
CHECK(pos2 + j < len2);
CHECK_EQ(s1[pos1 + j], s2[pos2 + j]);
}
diff_parameter += chunk->len1 + chunk->len2;
pos1 = diff_pos1 + chunk->len1;
pos2 = diff_pos2 + chunk->len2;
int diff_len1 = diff.end_position - diff.start_position;
int diff_len2 = diff.new_end_position - diff.new_start_position;
diff_parameter += diff_len1 + diff_len2;
pos1 = diff_pos1 + diff_len1;
pos2 = diff_pos2 + diff_len2;
}
{
// After last chunk.
......@@ -149,6 +88,16 @@ void CompareStringsOneWay(const char* s1, const char* s2,
}
}
void CompareStringsOneWay(const char* s1, const char* s2,
int expected_diff_parameter = -1) {
std::vector<SourceChangeRange> changes;
CompareStringsOneWay(s1, s2, expected_diff_parameter, &changes);
}
void CompareStringsOneWay(const char* s1, const char* s2,
std::vector<SourceChangeRange>* changes) {
CompareStringsOneWay(s1, s2, -1, changes);
}
void CompareStrings(const char* s1, const char* s2,
int expected_diff_parameter = -1) {
......@@ -156,12 +105,25 @@ void CompareStrings(const char* s1, const char* s2,
CompareStringsOneWay(s2, s1, expected_diff_parameter);
}
} // Anonymous namespace.
void CompareOneWayPlayWithLF(const char* s1, const char* s2) {
std::string s1_one_line(s1);
std::replace(s1_one_line.begin(), s1_one_line.end(), '\n', ' ');
std::string s2_one_line(s2);
std::replace(s2_one_line.begin(), s2_one_line.end(), '\n', ' ');
CompareStringsOneWay(s1, s2, -1);
CompareStringsOneWay(s1_one_line.c_str(), s2, -1);
CompareStringsOneWay(s1, s2_one_line.c_str(), -1);
CompareStringsOneWay(s1_one_line.c_str(), s2_one_line.c_str(), -1);
}
// --- T h e A c t u a l T e s t s
void CompareStringsPlayWithLF(const char* s1, const char* s2) {
CompareOneWayPlayWithLF(s1, s2);
CompareOneWayPlayWithLF(s2, s1);
}
} // anonymous namespace
TEST(LiveEditDiffer) {
v8::HandleScope handle_scope(CcTest::isolate());
CompareStrings("zz1zzz12zz123zzz", "zzzzzzzzzz", 6);
CompareStrings("zz1zzz12zz123zzz", "zz0zzz0zz0zzz", 9);
CompareStrings("123456789", "987654321", 16);
......@@ -177,7 +139,62 @@ TEST(LiveEditDiffer) {
CompareStrings("a cat", "a capybara", 7);
CompareStrings("abbabababababaaabbabababababbabbbbbbbababa",
"bbbbabababbbabababbbabababababbabbababa");
CompareStringsPlayWithLF("", "");
CompareStringsPlayWithLF("a", "b");
CompareStringsPlayWithLF(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseem\nso\nfar\naway");
CompareStringsPlayWithLF(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"\nall\nmy\ntroubles\nseemed\nso\nfar\naway");
CompareStringsPlayWithLF(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"all\nmy\ntroubles\nseemed\nso\nfar\naway");
CompareStringsPlayWithLF(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway\n");
CompareStringsPlayWithLF(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseemed\nso\n");
}
TEST(LiveEditTranslatePosition) {
v8::HandleScope handle_scope(CcTest::isolate());
std::vector<SourceChangeRange> changes;
CompareStringsOneWay("a", "a", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 1);
CompareStringsOneWay("a", "b", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 1);
CompareStringsOneWay("ababa", "aaa", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 1);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 2), 1);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 3), 2);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 4), 2);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 5), 3);
CompareStringsOneWay("ababa", "acaca", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 1);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 2), 2);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 3), 3);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 4), 4);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 5), 5);
CompareStringsOneWay("aaa", "ababa", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 2);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 2), 4);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 3), 5);
CompareStringsOneWay("aabbaaaa", "aaaabbaa", &changes);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 0), 0);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 1), 1);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 2), 4);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 3), 5);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 4), 6);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 5), 7);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 6), 8);
CHECK_EQ(LiveEdit::TranslatePosition(changes, 8), 8);
}
} // namespace internal
} // namespace v8
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Debug = debug.Debug
function CheckCompareOneWay(s1, s2) {
var diff_array = Debug.LiveEdit.TestApi.CompareStrings(s1, s2);
var pos1 = 0;
var pos2 = 0;
print("Compare:");
print("s1='" + s1 + "'");
print("s2='" + s2 + "'");
print("Diff:");
print("" + diff_array);
for (var i = 0; i < diff_array.length; i += 3) {
var similar_length = diff_array[i] - pos1;
assertEquals(s1.substring(pos1, pos1 + similar_length),
s2.substring(pos2, pos2 + similar_length));
print(s1.substring(pos1, pos1 + similar_length));
pos1 += similar_length;
pos2 += similar_length;
print("<<< " + pos1 + " " + diff_array[i + 1]);
print(s1.substring(pos1, diff_array[i + 1]));
print("===");
print(s2.substring(pos2, diff_array[i + 2]));
print(">>> " + pos2 + " " + diff_array[i + 2]);
pos1 = diff_array[i + 1];
pos2 = diff_array[i + 2];
}
{
// After last change
var similar_length = s1.length - pos1;
assertEquals(similar_length, s2.length - pos2);
assertEquals(s1.substring(pos1, pos1 + similar_length),
s2.substring(pos2, pos2 + similar_length));
print(s1.substring(pos1, pos1 + similar_length));
}
print("");
}
function CheckCompareOneWayPlayWithLF(s1, s2) {
var s1Oneliner = s1.replace(/\n/g, ' ');
var s2Oneliner = s2.replace(/\n/g, ' ');
CheckCompareOneWay(s1, s2);
CheckCompareOneWay(s1Oneliner, s2);
CheckCompareOneWay(s1, s2Oneliner);
CheckCompareOneWay(s1Oneliner, s2Oneliner);
}
function CheckCompare(s1, s2) {
CheckCompareOneWayPlayWithLF(s1, s2);
CheckCompareOneWayPlayWithLF(s2, s1);
}
CheckCompare("", "");
CheckCompare("a", "b");
CheckCompare(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseem\nso\nfar\naway"
);
CheckCompare(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"\nall\nmy\ntroubles\nseemed\nso\nfar\naway"
);
CheckCompare(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"all\nmy\ntroubles\nseemed\nso\nfar\naway"
);
CheckCompare(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway\n"
);
CheckCompare(
"yesterday\nall\nmy\ntroubles\nseemed\nso\nfar\naway",
"yesterday\nall\nmy\ntroubles\nseemed\nso\n"
);
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Debug = debug.Debug
function Return2010() {
return 2010;
}
// Diff it trivial: zero chunks
var NoChunkTranslator = new Debug.LiveEdit.TestApi.PosTranslator([]);
assertEquals(0, NoChunkTranslator.Translate(0));
assertEquals(10, NoChunkTranslator.Translate(10));
// Diff has one chunk
var SingleChunkTranslator = new Debug.LiveEdit.TestApi.PosTranslator([20, 30, 25]);
assertEquals(0, SingleChunkTranslator.Translate(0));
assertEquals(5, SingleChunkTranslator.Translate(5));
assertEquals(10, SingleChunkTranslator.Translate(10));
assertEquals(19, SingleChunkTranslator.Translate(19));
assertEquals(2010, SingleChunkTranslator.Translate(20, Return2010));
assertEquals(25, SingleChunkTranslator.Translate(30));
assertEquals(26, SingleChunkTranslator.Translate(31));
assertEquals(2010, SingleChunkTranslator.Translate(26, Return2010));
try {
SingleChunkTranslator.Translate(21);
assertTrue(false);
} catch (ignore) {
}
try {
SingleChunkTranslator.Translate(24);
assertTrue(false);
} catch (ignore) {
}
// Diff has several chunk (3). See the table below.
/*
chunks: (new <- old)
10 10
15 20
35 40
50 40
70 60
70 70
*/
var MultiChunkTranslator = new Debug.LiveEdit.TestApi.PosTranslator([10, 20, 15, 40, 40, 50, 60, 70, 70 ]);
assertEquals(5, MultiChunkTranslator.Translate(5));
assertEquals(9, MultiChunkTranslator.Translate(9));
assertEquals(2010, MultiChunkTranslator.Translate(10, Return2010));
assertEquals(15, MultiChunkTranslator.Translate(20));
assertEquals(20, MultiChunkTranslator.Translate(25));
assertEquals(34, MultiChunkTranslator.Translate(39));
assertEquals(50, MultiChunkTranslator.Translate(40, Return2010));
assertEquals(55, MultiChunkTranslator.Translate(45));
assertEquals(69, MultiChunkTranslator.Translate(59));
assertEquals(2010, MultiChunkTranslator.Translate(60, Return2010));
assertEquals(70, MultiChunkTranslator.Translate(70));
assertEquals(75, MultiChunkTranslator.Translate(75));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment