OLD | NEW |
(Empty) | |
| 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 |
| 30 #include "v8.h" |
| 31 |
| 32 #include "liveedit.h" |
| 33 #include "cctest.h" |
| 34 |
| 35 |
| 36 using namespace v8::internal; |
| 37 |
| 38 // Anonymous namespace. |
| 39 namespace { |
| 40 |
| 41 class StringCompareInput : public Compare::Input { |
| 42 public: |
| 43 StringCompareInput(const char* s1, const char* s2) : s1_(s1), s2_(s2) { |
| 44 } |
| 45 int getLength1() { |
| 46 return strlen(s1_); |
| 47 } |
| 48 int getLength2() { |
| 49 return strlen(s2_); |
| 50 } |
| 51 bool equals(int index1, int index2) { |
| 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 Compare::Output { |
| 76 public: |
| 77 explicit ListDiffOutputWriter(DiffChunkStruct** next_chunk_pointer) |
| 78 : next_chunk_pointer_(next_chunk_pointer) { |
| 79 (*next_chunk_pointer_) = NULL; |
| 80 } |
| 81 void AddChunk(int pos1, int pos2, int len1, int len2) { |
| 82 current_chunk_ = new DiffChunkStruct(pos1, pos2, len1, len2); |
| 83 (*next_chunk_pointer_) = current_chunk_; |
| 84 next_chunk_pointer_ = ¤t_chunk_->next; |
| 85 } |
| 86 private: |
| 87 DiffChunkStruct** next_chunk_pointer_; |
| 88 DiffChunkStruct* current_chunk_; |
| 89 }; |
| 90 |
| 91 |
| 92 void CompareStringsOneWay(const char* s1, const char* s2, |
| 93 int expected_diff_parameter = -1) { |
| 94 StringCompareInput input(s1, s2); |
| 95 |
| 96 ZoneScope zone_scope(DELETE_ON_EXIT); |
| 97 |
| 98 DiffChunkStruct* first_chunk; |
| 99 ListDiffOutputWriter writer(&first_chunk); |
| 100 |
| 101 Compare::CalculateDifference(&input, &writer); |
| 102 |
| 103 int len1 = strlen(s1); |
| 104 int len2 = strlen(s2); |
| 105 |
| 106 int pos1 = 0; |
| 107 int pos2 = 0; |
| 108 |
| 109 int diff_parameter = 0; |
| 110 |
| 111 for (DiffChunkStruct* chunk = first_chunk; |
| 112 chunk != NULL; |
| 113 chunk = chunk->next) { |
| 114 int diff_pos1 = chunk->pos1; |
| 115 int similar_part_length = diff_pos1 - pos1; |
| 116 int diff_pos2 = pos2 + similar_part_length; |
| 117 |
| 118 ASSERT_EQ(diff_pos2, chunk->pos2); |
| 119 |
| 120 for (int j = 0; j < similar_part_length; j++) { |
| 121 ASSERT(pos1 + j < len1); |
| 122 ASSERT(pos2 + j < len2); |
| 123 ASSERT_EQ(s1[pos1 + j], s2[pos2 + j]); |
| 124 } |
| 125 diff_parameter += chunk->len1 + chunk->len2; |
| 126 pos1 = diff_pos1 + chunk->len1; |
| 127 pos2 = diff_pos2 + chunk->len2; |
| 128 } |
| 129 { |
| 130 // After last chunk. |
| 131 int similar_part_length = len1 - pos1; |
| 132 ASSERT_EQ(similar_part_length, len2 - pos2); |
| 133 USE(len2); |
| 134 for (int j = 0; j < similar_part_length; j++) { |
| 135 ASSERT(pos1 + j < len1); |
| 136 ASSERT(pos2 + j < len2); |
| 137 ASSERT_EQ(s1[pos1 + j], s2[pos2 + j]); |
| 138 } |
| 139 } |
| 140 |
| 141 if (expected_diff_parameter != -1) { |
| 142 ASSERT_EQ(expected_diff_parameter, diff_parameter); |
| 143 } |
| 144 } |
| 145 |
| 146 |
| 147 void CompareStrings(const char* s1, const char* s2, |
| 148 int expected_diff_parameter = -1) { |
| 149 CompareStringsOneWay(s1, s2, expected_diff_parameter); |
| 150 CompareStringsOneWay(s2, s1, expected_diff_parameter); |
| 151 } |
| 152 |
| 153 } // Anonymous namespace. |
| 154 |
| 155 |
| 156 // --- T h e A c t u a l T e s t s |
| 157 |
| 158 TEST(LiveEditDiffer) { |
| 159 CompareStrings("zz1zzz12zz123zzz", "zzzzzzzzzz", 6); |
| 160 CompareStrings("zz1zzz12zz123zzz", "zz0zzz0zz0zzz", 9); |
| 161 CompareStrings("123456789", "987654321", 16); |
| 162 CompareStrings("zzz", "yyy", 6); |
| 163 CompareStrings("zzz", "zzz12", 2); |
| 164 CompareStrings("zzz", "21zzz", 2); |
| 165 CompareStrings("cat", "cut", 2); |
| 166 CompareStrings("ct", "cut", 1); |
| 167 CompareStrings("cat", "ct", 1); |
| 168 CompareStrings("cat", "cat", 0); |
| 169 CompareStrings("", "", 0); |
| 170 CompareStrings("cat", "", 3); |
| 171 CompareStrings("a cat", "a capybara", 7); |
| 172 CompareStrings("abbabababababaaabbabababababbabbbbbbbababa", |
| 173 "bbbbabababbbabababbbabababababbabbababa"); |
| 174 } |
OLD | NEW |