blob: 2c89a387dc900468d8d05d019d9f1f229e949aea [file] [log] [blame]
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001// Copyright 2007-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000028#ifdef ENABLE_DEBUGGER_SUPPORT
29
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000030#include <stdlib.h>
31
32#include "v8.h"
33
34#include "liveedit.h"
35#include "cctest.h"
36
37
38using namespace v8::internal;
39
40// Anonymous namespace.
41namespace {
42
lrn@chromium.orgc34f5802010-04-28 12:53:43 +000043class StringCompareInput : public Comparator::Input {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000044 public:
45 StringCompareInput(const char* s1, const char* s2) : s1_(s1), s2_(s2) {
46 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000047 int GetLength1() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000048 return StrLength(s1_);
49 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000050 int GetLength2() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000051 return StrLength(s2_);
52 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000053 bool Equals(int index1, int index2) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000054 return s1_[index1] == s2_[index2];
55 }
56
57 private:
58 const char* s1_;
59 const char* s2_;
60};
61
62
63class DiffChunkStruct : public ZoneObject {
64 public:
65 DiffChunkStruct(int pos1_param, int pos2_param,
66 int len1_param, int len2_param)
67 : pos1(pos1_param), pos2(pos2_param),
68 len1(len1_param), len2(len2_param), next(NULL) {}
69 int pos1;
70 int pos2;
71 int len1;
72 int len2;
73 DiffChunkStruct* next;
74};
75
76
lrn@chromium.orgc34f5802010-04-28 12:53:43 +000077class ListDiffOutputWriter : public Comparator::Output {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000078 public:
79 explicit ListDiffOutputWriter(DiffChunkStruct** next_chunk_pointer)
80 : next_chunk_pointer_(next_chunk_pointer) {
81 (*next_chunk_pointer_) = NULL;
82 }
83 void AddChunk(int pos1, int pos2, int len1, int len2) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000084 current_chunk_ = new(Isolate::Current()->runtime_zone()) DiffChunkStruct(
85 pos1, pos2, len1, len2);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000086 (*next_chunk_pointer_) = current_chunk_;
87 next_chunk_pointer_ = &current_chunk_->next;
88 }
89 private:
90 DiffChunkStruct** next_chunk_pointer_;
91 DiffChunkStruct* current_chunk_;
92};
93
94
95void CompareStringsOneWay(const char* s1, const char* s2,
96 int expected_diff_parameter = -1) {
97 StringCompareInput input(s1, s2);
98
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000099 ZoneScope zone_scope(Isolate::Current()->runtime_zone(), DELETE_ON_EXIT);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000100
101 DiffChunkStruct* first_chunk;
102 ListDiffOutputWriter writer(&first_chunk);
103
lrn@chromium.orgc34f5802010-04-28 12:53:43 +0000104 Comparator::CalculateDifference(&input, &writer);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000105
106 int len1 = StrLength(s1);
107 int len2 = StrLength(s2);
108
109 int pos1 = 0;
110 int pos2 = 0;
111
112 int diff_parameter = 0;
113
114 for (DiffChunkStruct* chunk = first_chunk;
115 chunk != NULL;
116 chunk = chunk->next) {
117 int diff_pos1 = chunk->pos1;
118 int similar_part_length = diff_pos1 - pos1;
119 int diff_pos2 = pos2 + similar_part_length;
120
121 ASSERT_EQ(diff_pos2, chunk->pos2);
122
123 for (int j = 0; j < similar_part_length; j++) {
124 ASSERT(pos1 + j < len1);
125 ASSERT(pos2 + j < len2);
126 ASSERT_EQ(s1[pos1 + j], s2[pos2 + j]);
127 }
128 diff_parameter += chunk->len1 + chunk->len2;
129 pos1 = diff_pos1 + chunk->len1;
130 pos2 = diff_pos2 + chunk->len2;
131 }
132 {
133 // After last chunk.
134 int similar_part_length = len1 - pos1;
135 ASSERT_EQ(similar_part_length, len2 - pos2);
136 USE(len2);
137 for (int j = 0; j < similar_part_length; j++) {
138 ASSERT(pos1 + j < len1);
139 ASSERT(pos2 + j < len2);
140 ASSERT_EQ(s1[pos1 + j], s2[pos2 + j]);
141 }
142 }
143
144 if (expected_diff_parameter != -1) {
145 ASSERT_EQ(expected_diff_parameter, diff_parameter);
146 }
147}
148
149
150void CompareStrings(const char* s1, const char* s2,
151 int expected_diff_parameter = -1) {
152 CompareStringsOneWay(s1, s2, expected_diff_parameter);
153 CompareStringsOneWay(s2, s1, expected_diff_parameter);
154}
155
156} // Anonymous namespace.
157
158
159// --- T h e A c t u a l T e s t s
160
161TEST(LiveEditDiffer) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 v8::internal::V8::Initialize(NULL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000163 CompareStrings("zz1zzz12zz123zzz", "zzzzzzzzzz", 6);
164 CompareStrings("zz1zzz12zz123zzz", "zz0zzz0zz0zzz", 9);
165 CompareStrings("123456789", "987654321", 16);
166 CompareStrings("zzz", "yyy", 6);
167 CompareStrings("zzz", "zzz12", 2);
168 CompareStrings("zzz", "21zzz", 2);
169 CompareStrings("cat", "cut", 2);
170 CompareStrings("ct", "cut", 1);
171 CompareStrings("cat", "ct", 1);
172 CompareStrings("cat", "cat", 0);
173 CompareStrings("", "", 0);
174 CompareStrings("cat", "", 3);
175 CompareStrings("a cat", "a capybara", 7);
176 CompareStrings("abbabababababaaabbabababababbabbbbbbbababa",
177 "bbbbabababbbabababbbabababababbabbababa");
178}
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000179
180#endif // ENABLE_DEBUGGER_SUPPORT