test-liveedit.cc 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Copyright 2007-2008 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.

#include <stdlib.h>

30
#include "src/v8.h"
31

32
#include "src/debug/liveedit.h"
33
#include "src/objects-inl.h"
34
#include "test/cctest/cctest.h"
35

36 37
namespace v8 {
namespace internal {
38 39 40 41

// Anonymous namespace.
namespace {

42
class StringCompareInput : public Comparator::Input {
43 44 45
 public:
  StringCompareInput(const char* s1, const char* s2) : s1_(s1), s2_(s2) {
  }
46
  int GetLength1() {
47
    return StrLength(s1_);
48
  }
49
  int GetLength2() {
50
    return StrLength(s2_);
51
  }
52
  bool Equals(int index1, int index2) {
53 54 55 56 57 58 59 60 61 62 63
    return s1_[index1] == s2_[index2];
  }

 private:
  const char* s1_;
  const char* s2_;
};


class DiffChunkStruct : public ZoneObject {
 public:
64 65 66 67 68 69 70
  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) {}
71 72 73 74 75 76 77 78
  int pos1;
  int pos2;
  int len1;
  int len2;
  DiffChunkStruct* next;
};


79
class ListDiffOutputWriter : public Comparator::Output {
80
 public:
81 82 83
  explicit ListDiffOutputWriter(DiffChunkStruct** next_chunk_pointer,
                                Zone* zone)
      : next_chunk_pointer_(next_chunk_pointer), zone_(zone) {
84
    (*next_chunk_pointer_) = nullptr;
85 86
  }
  void AddChunk(int pos1, int pos2, int len1, int len2) {
87
    current_chunk_ = new(zone_) DiffChunkStruct(pos1, pos2, len1, len2);
88 89 90 91 92 93
    (*next_chunk_pointer_) = current_chunk_;
    next_chunk_pointer_ = &current_chunk_->next;
  }
 private:
  DiffChunkStruct** next_chunk_pointer_;
  DiffChunkStruct* current_chunk_;
94
  Zone* zone_;
95 96 97 98 99 100 101
};


void CompareStringsOneWay(const char* s1, const char* s2,
                          int expected_diff_parameter = -1) {
  StringCompareInput input(s1, s2);

102
  v8::internal::AccountingAllocator allocator;
103
  Zone zone(&allocator, ZONE_NAME);
104 105

  DiffChunkStruct* first_chunk;
106
  ListDiffOutputWriter writer(&first_chunk, &zone);
107

108
  Comparator::CalculateDifference(&input, &writer);
109

110 111
  int len1 = StrLength(s1);
  int len2 = StrLength(s2);
112 113 114 115 116 117

  int pos1 = 0;
  int pos2 = 0;

  int diff_parameter = 0;

118 119
  for (DiffChunkStruct* chunk = first_chunk; chunk != nullptr;
       chunk = chunk->next) {
120 121 122 123
    int diff_pos1 = chunk->pos1;
    int similar_part_length = diff_pos1 - pos1;
    int diff_pos2 = pos2 + similar_part_length;

124
    CHECK_EQ(diff_pos2, chunk->pos2);
125 126

    for (int j = 0; j < similar_part_length; j++) {
127 128 129
      CHECK(pos1 + j < len1);
      CHECK(pos2 + j < len2);
      CHECK_EQ(s1[pos1 + j], s2[pos2 + j]);
130 131 132 133 134 135 136 137
    }
    diff_parameter += chunk->len1 + chunk->len2;
    pos1 = diff_pos1 + chunk->len1;
    pos2 = diff_pos2 + chunk->len2;
  }
  {
    // After last chunk.
    int similar_part_length = len1 - pos1;
138
    CHECK_EQ(similar_part_length, len2 - pos2);
139 140
    USE(len2);
    for (int j = 0; j < similar_part_length; j++) {
141 142 143
      CHECK(pos1 + j < len1);
      CHECK(pos2 + j < len2);
      CHECK_EQ(s1[pos1 + j], s2[pos2 + j]);
144 145 146 147
    }
  }

  if (expected_diff_parameter != -1) {
148
    CHECK_EQ(expected_diff_parameter, diff_parameter);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  }
}


void CompareStrings(const char* s1, const char* s2,
                    int expected_diff_parameter = -1) {
  CompareStringsOneWay(s1, s2, expected_diff_parameter);
  CompareStringsOneWay(s2, s1, expected_diff_parameter);
}

}  // Anonymous namespace.


// --- T h e   A c t u a l   T e s t s

TEST(LiveEditDiffer) {
  CompareStrings("zz1zzz12zz123zzz", "zzzzzzzzzz", 6);
  CompareStrings("zz1zzz12zz123zzz", "zz0zzz0zz0zzz", 9);
  CompareStrings("123456789", "987654321", 16);
  CompareStrings("zzz", "yyy", 6);
  CompareStrings("zzz", "zzz12", 2);
  CompareStrings("zzz", "21zzz", 2);
  CompareStrings("cat", "cut", 2);
  CompareStrings("ct", "cut", 1);
  CompareStrings("cat", "ct", 1);
  CompareStrings("cat", "cat", 0);
  CompareStrings("", "", 0);
  CompareStrings("cat", "", 3);
  CompareStrings("a cat", "a capybara", 7);
  CompareStrings("abbabababababaaabbabababababbabbbbbbbababa",
                 "bbbbabababbbabababbbabababababbabbababa");
}
181 182 183

}  // namespace internal
}  // namespace v8