Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // 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 | |
| 28 | #include <stdlib.h> |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include "src/v8.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 31 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | #include "src/debug/liveedit.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 33 | #include "test/cctest/cctest.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 34 | |
| 35 | |
| 36 | using namespace v8::internal; |
| 37 | |
| 38 | // Anonymous namespace. |
| 39 | namespace { |
| 40 | |
| 41 | class StringCompareInput : public Comparator::Input { |
| 42 | public: |
| 43 | StringCompareInput(const char* s1, const char* s2) : s1_(s1), s2_(s2) { |
| 44 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 45 | int GetLength1() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 46 | return StrLength(s1_); |
| 47 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 48 | int GetLength2() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 49 | return StrLength(s2_); |
| 50 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 51 | bool Equals(int index1, int index2) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 52 | return s1_[index1] == s2_[index2]; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | const char* s1_; |
| 57 | const char* s2_; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | class DiffChunkStruct : public ZoneObject { |
| 62 | public: |
| 63 | DiffChunkStruct(int pos1_param, int pos2_param, |
| 64 | int len1_param, int len2_param) |
| 65 | : pos1(pos1_param), pos2(pos2_param), |
| 66 | len1(len1_param), len2(len2_param), next(NULL) {} |
| 67 | int pos1; |
| 68 | int pos2; |
| 69 | int len1; |
| 70 | int len2; |
| 71 | DiffChunkStruct* next; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | class ListDiffOutputWriter : public Comparator::Output { |
| 76 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 77 | explicit ListDiffOutputWriter(DiffChunkStruct** next_chunk_pointer, |
| 78 | Zone* zone) |
| 79 | : next_chunk_pointer_(next_chunk_pointer), zone_(zone) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 80 | (*next_chunk_pointer_) = NULL; |
| 81 | } |
| 82 | void AddChunk(int pos1, int pos2, int len1, int len2) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | current_chunk_ = new(zone_) DiffChunkStruct(pos1, pos2, len1, len2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 84 | (*next_chunk_pointer_) = current_chunk_; |
| 85 | next_chunk_pointer_ = ¤t_chunk_->next; |
| 86 | } |
| 87 | private: |
| 88 | DiffChunkStruct** next_chunk_pointer_; |
| 89 | DiffChunkStruct* current_chunk_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | Zone* zone_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
| 94 | void CompareStringsOneWay(const char* s1, const char* s2, |
| 95 | int expected_diff_parameter = -1) { |
| 96 | StringCompareInput input(s1, s2); |
| 97 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 98 | v8::base::AccountingAllocator allocator; |
| 99 | Zone zone(&allocator); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 100 | |
| 101 | DiffChunkStruct* first_chunk; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | ListDiffOutputWriter writer(&first_chunk, &zone); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 103 | |
| 104 | Comparator::CalculateDifference(&input, &writer); |
| 105 | |
| 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | CHECK_EQ(diff_pos2, chunk->pos2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 122 | |
| 123 | for (int j = 0; j < similar_part_length; j++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 124 | CHECK(pos1 + j < len1); |
| 125 | CHECK(pos2 + j < len2); |
| 126 | CHECK_EQ(s1[pos1 + j], s2[pos2 + j]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 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; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | CHECK_EQ(similar_part_length, len2 - pos2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 136 | USE(len2); |
| 137 | for (int j = 0; j < similar_part_length; j++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | CHECK(pos1 + j < len1); |
| 139 | CHECK(pos2 + j < len2); |
| 140 | CHECK_EQ(s1[pos1 + j], s2[pos2 + j]); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | if (expected_diff_parameter != -1) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | CHECK_EQ(expected_diff_parameter, diff_parameter); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void 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 | |
| 161 | TEST(LiveEditDiffer) { |
| 162 | CompareStrings("zz1zzz12zz123zzz", "zzzzzzzzzz", 6); |
| 163 | CompareStrings("zz1zzz12zz123zzz", "zz0zzz0zz0zzz", 9); |
| 164 | CompareStrings("123456789", "987654321", 16); |
| 165 | CompareStrings("zzz", "yyy", 6); |
| 166 | CompareStrings("zzz", "zzz12", 2); |
| 167 | CompareStrings("zzz", "21zzz", 2); |
| 168 | CompareStrings("cat", "cut", 2); |
| 169 | CompareStrings("ct", "cut", 1); |
| 170 | CompareStrings("cat", "ct", 1); |
| 171 | CompareStrings("cat", "cat", 0); |
| 172 | CompareStrings("", "", 0); |
| 173 | CompareStrings("cat", "", 3); |
| 174 | CompareStrings("a cat", "a capybara", 7); |
| 175 | CompareStrings("abbabababababaaabbabababababbabbbbbbbababa", |
| 176 | "bbbbabababbbabababbbabababababbabbababa"); |
| 177 | } |